@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700;800&display=swap');

:root {
    --color-primary: #3D4A34;
    --color-primary-light: #5A6B4D;
    --color-secondary: #8B6F47;
    --color-accent: #C48A3E;
    --color-bg: #F7F5F0;
    --color-surface: #FFFFFF;
    --color-border: #E3DED2;
    --color-text: #2A2A24;
    --color-text-muted: #6B6558;
    --color-success: #4C7A3A;
    --color-success-bg: #EAF2E4;
    --color-warning: #B9842B;
    --color-warning-bg: #FBF0DC;
    --color-error: #A4392B;
    --color-error-bg: #FBE7E3;
    --radius: 10px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: 'Cairo', sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    direction: rtl;
}

a { color: inherit; text-decoration: none; }

.app-shell { display: flex; min-height: 100vh; }

/* ----- Sidebar ----- */
.sidebar, aside.sidebar {
    width: 240px;
    flex-shrink: 0;
    background: var(--color-primary);
    color: #F2F0E9;
    padding: 20px 0;
    min-height: 100vh;
}
.sidebar-brand {
    padding: 0 20px 20px;
    border-bottom: 1px solid rgba(255,255,255,0.15);
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.sidebar-brand strong { font-size: 1.1rem; }
.sidebar-brand span { font-size: 0.78rem; color: #CFCBBB; }
.sidebar nav { display: flex; flex-direction: column; }
.sidebar nav a {
    padding: 12px 20px;
    color: #DCD8C9;
    font-size: 0.95rem;
    border-right: 3px solid transparent;
    transition: background .15s;
}
.sidebar nav a:hover { background: rgba(255,255,255,0.08); }
.sidebar nav a.active {
    background: rgba(255,255,255,0.12);
    border-right-color: var(--color-accent);
    color: #fff;
    font-weight: 600;
}
.sidebar-footer {
    margin-top: auto;
    padding: 16px 20px;
    font-size: 0.82rem;
    color: #CFCBBB;
    border-top: 1px solid rgba(255,255,255,0.15);
}

/* ----- Main area / topbar ----- */
.main-area { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--color-surface);
    padding: 14px 24px;
    border-bottom: 1px solid var(--color-border);
}
.topbar-user { display: flex; align-items: center; gap: 12px; font-size: 0.9rem; color: var(--color-text-muted); }

.content-area { padding: 24px; }

/* ----- KPI cards ----- */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.kpi-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 18px;
}
.kpi-card .kpi-value { font-size: 1.8rem; font-weight: 800; color: var(--color-primary); }
.kpi-card .kpi-label { font-size: 0.85rem; color: var(--color-text-muted); margin-top: 4px; }

/* ----- Panels ----- */
.panel {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    margin-bottom: 20px;
    overflow: hidden;
}
.panel-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--color-border);
    font-weight: 700;
    color: var(--color-primary);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.panel-body { padding: 18px; }

/* ----- Tables ----- */
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--color-border); }
th { background: #F1EEE5; color: var(--color-text-muted); font-weight: 600; }
tr.pen-detail-row td { background: #FAF9F4; padding: 0; }
tr.pen-detail-row .pen-detail-inner { padding: 14px 20px; }
tr.clickable-pen { cursor: pointer; }
tr.clickable-pen:hover { background: #F4F1E8; }

/* ----- Badges ----- */
.badge { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 0.78rem; font-weight: 600; }
.badge-success { background: var(--color-success-bg); color: var(--color-success); }
.badge-warning { background: var(--color-warning-bg); color: var(--color-warning); }
.badge-error { background: var(--color-error-bg); color: var(--color-error); }
.badge-muted { background: #EEEBE2; color: var(--color-text-muted); }

/* ----- Buttons ----- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 9px 18px;
    border-radius: 8px;
    border: none;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-light); }
.btn-outline { background: transparent; border: 1px solid var(--color-border); color: var(--color-text); }
.btn-outline:hover { background: #F1EEE5; }
.btn-danger { background: var(--color-error); color: #fff; }
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 5px 12px; font-size: 0.8rem; }
.btn-icon { padding: 6px 9px; font-size: 0.95rem; }

/* ----- Forms ----- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
.form-field { display: flex; flex-direction: column; gap: 6px; }
.form-field label { font-size: 0.85rem; font-weight: 600; color: var(--color-text-muted); }
.form-field input, .form-field select, .form-field textarea {
    padding: 10px 12px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.92rem;
    background: var(--color-surface);
    color: var(--color-text);
}
.form-field input:focus, .form-field select:focus, .form-field textarea:focus {
    outline: none;
    border-color: var(--color-primary);
}
.form-field input[readonly] { background: #F1EEE5; color: var(--color-text-muted); }
.form-actions { margin-top: 20px; display: flex; gap: 10px; }

/* ----- Alerts ----- */
.alert { padding: 12px 24px; font-size: 0.9rem; display: flex; align-items: center; gap: 10px; }
.alert a { text-decoration: underline; font-weight: 600; }
.alert-success { background: var(--color-success-bg); color: var(--color-success); }
.alert-warning { background: var(--color-warning-bg); color: var(--color-warning); }
.alert-error { background: var(--color-error-bg); color: var(--color-error); }

/* ----- Login ----- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--color-bg); }
.login-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 36px;
    width: 100%;
    max-width: 360px;
    text-align: center;
}
.login-card h1 { font-size: 1.3rem; color: var(--color-primary); margin-bottom: 4px; }
.login-card p.sub { color: var(--color-text-muted); font-size: 0.85rem; margin-bottom: 24px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; text-align: right; }
.login-card .btn { justify-content: center; margin-top: 10px; }

/* ----- Empty state ----- */
.empty-state { text-align: center; padding: 40px 20px; color: var(--color-text-muted); }

/* ----- Notifications box ----- */
.notif-list { display: flex; flex-direction: column; gap: 10px; }
.notif-item { background: var(--color-warning-bg); color: var(--color-warning); padding: 10px 14px; border-radius: 8px; font-size: 0.9rem; }

@media (max-width: 860px) {
    .sidebar, aside.sidebar { display: none; }
    .content-area { padding: 14px; }
    .form-grid { grid-template-columns: 1fr; }
}

/* مفتاح النوع العام */
.species-switch { display:inline-flex; border:1px solid #cfc8b4; border-radius:8px; overflow:hidden; }
.species-switch a { padding:6px 14px; text-decoration:none; color:inherit; font-size:0.9rem; background:#fff; border-inline-start:1px solid #cfc8b4; }
.species-switch a:first-child { border-inline-start:none; }
.species-switch a.active { background:var(--color-primary); color:#fff; font-weight:600; }
.species-banner { background:#eef0e2; border:1px solid #c9d1a6; color:#3f4a1e; padding:8px 14px; margin:0 0 10px; border-radius:8px; font-size:0.88rem; }

/* ===== تخطيط الأعمدة (يملأ الشاشات العريضة، ويرجع لعمود واحد على الجوال) ===== */
.app-shell { width: 100%; }
.cols { display: grid; gap: 20px; align-items: start; grid-template-columns: 1fr; }
.cols > .panel, .cols > div > .panel { margin-bottom: 20px; }
.cols > div { min-width: 0; }
@media (min-width: 1000px) {
    .cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (min-width: 700px) and (max-width: 999px) {
    .cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.cols table { font-size: 0.85rem; }
.cols th, .cols td { padding: 8px 8px; }

/* بيانات على شكل شبكة: عنوان + قيمة */
.kv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.kv-grid .kv span { display: block; font-size: 0.78rem; color: var(--color-text-muted); }
.kv-grid .kv strong { font-size: 0.98rem; }
.stack-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.stack-form .full { grid-column: 1 / -1; }

/* ===== صفحة إدارة الحيوانات: ملخص + بطاقات الحظائر ===== */
.summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.summary-card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius); padding: 14px 16px; text-decoration: none; color: inherit; display: block; }
.summary-card .v { font-size: 1.6rem; font-weight: 800; color: var(--color-primary); }
.summary-card .l { font-size: 0.82rem; color: var(--color-text-muted); margin-top: 2px; }
.summary-card.warn .v { color: var(--color-warning); }
.summary-card.bad .v { color: var(--color-error); }
a.summary-card:hover { background: #F4F1E8; }

.pen-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; margin-bottom: 18px; }
.pen-card { background: var(--color-surface); border: 2px solid var(--color-border); border-radius: var(--radius); padding: 14px; cursor: pointer; transition: border-color .15s, background .15s; }
.pen-card:hover { background: #FAF9F4; }
.pen-card.selected { border-color: var(--color-primary); background: #F4F1E8; }
.pen-card .pc-title { display: flex; justify-content: space-between; align-items: center; font-weight: 700; color: var(--color-primary); margin-bottom: 6px; }
.pen-card .pc-counts { font-size: 0.88rem; color: var(--color-text-muted); margin-bottom: 8px; }
.cap-bar { height: 7px; background: #EEEBE2; border-radius: 6px; overflow: hidden; margin: 6px 0; }
.cap-bar > i { display: block; height: 100%; background: var(--color-primary-light); }
.cap-bar.full > i { background: var(--color-error); }
.cap-bar.high > i { background: var(--color-accent); }
.pen-card .pc-badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.pen-panel { display: none; }
.pen-panel.open { display: block; }

.row-actions { display: flex; gap: 4px; flex-wrap: nowrap; }
.animal-badges { display: flex; flex-wrap: wrap; gap: 3px; }
.live-search { width: 100%; max-width: 380px; padding: 10px 12px; border: 1px solid var(--color-border); border-radius: 8px; font-family: inherit; font-size: 0.95rem; }

/* جداول الحيوانات على الشاشات الضيقة تتحول إلى بطاقات */
@media (max-width: 700px) {
    table.table-cards thead { display: none; }
    table.table-cards, table.table-cards tbody, table.table-cards tr, table.table-cards td { display: block; width: 100%; }
    table.table-cards tr { border: 1px solid var(--color-border); border-radius: 10px; margin-bottom: 10px; padding: 6px 4px; background: var(--color-surface); }
    table.table-cards td { border: none; padding: 5px 10px; display: flex; justify-content: space-between; gap: 10px; }
    table.table-cards td::before { content: attr(data-label); color: var(--color-text-muted); font-size: 0.8rem; flex-shrink: 0; }
    .kv-grid { grid-template-columns: 1fr 1fr; }
}

.stack-form .form-field { min-width: 0; }
.stack-form input, .stack-form select { width: 100%; box-sizing: border-box; min-width: 0; }
@media (min-width: 1000px) and (max-width: 1399px) { .cols-3 .stack-form { grid-template-columns: 1fr; } }

/* قائمة الجوال */
.menu-toggle { display: none; background: transparent; border: 1px solid var(--color-border); border-radius: 8px; font-size: 1.2rem; padding: 4px 10px; cursor: pointer; margin-inline-end: 10px; }
@media (max-width: 860px) {
    .menu-toggle { display: inline-block; }
    aside.sidebar.open { display: block !important; position: fixed; top: 0; bottom: 0; right: 0; width: 270px; z-index: 100; overflow-y: auto; box-shadow: -4px 0 20px rgba(0,0,0,.3); }
    .menu-backdrop.open { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 99; }
}
.menu-backdrop { display: none; }
@media (max-width: 700px) { .topbar { padding: 10px 12px; } .topbar-user span { display: none; } }
