/* نظام إدارة مزارع الدواجن — RTL بلا أي مكتبات خارجية */
:root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --ink: #0f172a;
    --muted: #64748b;
    --line: #e2e8f0;
    --brand: #0f766e;
    --brand-2: #115e59;
    --brand-soft: #ccfbf1;
    --warn: #b45309;
    --warn-soft: #fef3c7;
    --bad: #b91c1c;
    --bad-soft: #fee2e2;
    --good: #15803d;
    --good-soft: #dcfce7;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .05);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: "Segoe UI", Tahoma, "Noto Naskh Arabic", Arial, sans-serif;
    font-size: 15px;
    line-height: 1.65;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 .4rem; line-height: 1.35; font-weight: 700; }
h1 { font-size: 1.25rem; }
h3 { font-size: 1.02rem; }
p { margin: .35rem 0; }
code { background: #f1f5f9; padding: .05rem .3rem; border-radius: 6px; font-size: .85em; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.ok { color: var(--good); }
.bad { color: var(--bad); }
.inline { display: inline-flex; gap: .4rem; align-items: center; margin: 0; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row.space { justify-content: space-between; }
.num { text-align: left; font-variant-numeric: tabular-nums; direction: ltr; }

/* ---------- الهيكل ---------- */
.app { display: flex; min-height: 100vh; }

.sidebar {
    width: 250px;
    flex: 0 0 250px;
    background: #0b3b39;
    color: #e6fffb;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}

.brand { display: flex; gap: .6rem; align-items: center; padding: 1rem .9rem; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-mark { font-size: 1.5rem; }
.brand strong { display: block; font-size: .98rem; }
.brand small { display: block; font-size: .72rem; opacity: .75; }

.nav { padding: .5rem .4rem 1rem; display: flex; flex-direction: column; gap: 1px; }
.nav-section { font-size: .7rem; letter-spacing: .03em; opacity: .55; padding: .7rem .7rem .25rem; }
.nav-link {
    display: flex; align-items: center; gap: .55rem;
    padding: .5rem .7rem; border-radius: 9px;
    color: #d7fbf6; font-size: .88rem; text-decoration: none;
}
.nav-link:hover { background: rgba(255,255,255,.08); text-decoration: none; }
.nav-link.active { background: #ffffff; color: var(--brand-2); font-weight: 700; }
.sidebar-foot { margin-top: auto; padding: .8rem; font-size: .74rem; opacity: .7; border-top: 1px solid rgba(255,255,255,.08); }
.sidebar-foot .small { font-size: .7rem; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
    display: flex; align-items: center; gap: .75rem;
    padding: .7rem 1.1rem; background: var(--panel);
    border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
.topbar-title h1 { margin: 0; font-size: 1.05rem; }
.topbar-actions { margin-inline-start: auto; display: flex; align-items: center; gap: .6rem; }
.user-chip { display: flex; flex-direction: column; font-size: .82rem; line-height: 1.2; }
.user-chip small { color: var(--muted); font-size: .7rem; }
.sync-pill { background: var(--warn-soft); color: var(--warn); border-radius: 999px; padding: .15rem .55rem; font-size: .72rem; }
.icon-btn { display: none; background: none; border: 1px solid var(--line); border-radius: 9px; padding: .25rem .5rem; font-size: 1.1rem; cursor: pointer; }

.content { padding: 1rem 1.1rem 2rem; display: flex; flex-direction: column; gap: .85rem; max-width: 1500px; width: 100%; }

/* ---------- البطاقات والمؤشرات ---------- */
.card {
    background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
    padding: .9rem 1rem; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .6rem; margin-bottom: .55rem; flex-wrap: wrap; }
.card-head h3 { margin: 0; }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(165px, 1fr)); gap: .7rem; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem .8rem; box-shadow: var(--shadow); }
.stat-label { font-size: .74rem; color: var(--muted); }
.stat-value { font-size: 1.15rem; font-weight: 800; font-variant-numeric: tabular-nums; direction: ltr; text-align: right; }
.stat-hint { font-size: .7rem; color: var(--muted); }
.stat.good .stat-value { color: var(--good); }
.stat.bad .stat-value { color: var(--bad); }
.stat.warn .stat-value { color: var(--warn); }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: .85rem; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: .6rem; }
.grid-4 { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .6rem; }
.aging-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .5rem; }
.meta > div { display: flex; flex-direction: column; font-size: .85rem; }
.meta span { font-size: .72rem; color: var(--muted); }
.memo { background: #f8fafc; border-radius: 10px; padding: .45rem .6rem; }

.spark { width: 100%; height: 80px; display: block; margin-top: .4rem; }
.spark-labels { display: flex; justify-content: space-between; font-size: .68rem; color: var(--muted); }
.section-title { font-size: .9rem; color: var(--brand-2); border-bottom: 2px solid var(--brand-soft); padding-bottom: .2rem; margin-bottom: .4rem; }
.bullets { margin: .3rem 0 0; padding-inline-start: 1.1rem; font-size: .88rem; }
.bullets li { margin-bottom: .25rem; }

/* ---------- الجداول ---------- */
.table-wrap { overflow-x: auto; border-radius: 10px; }
.tbl { width: 100%; border-collapse: collapse; font-size: .86rem; background: var(--panel); }
.tbl th, .tbl td { padding: .45rem .55rem; border-bottom: 1px solid var(--line); text-align: right; vertical-align: middle; }
.tbl thead th { background: #f1f5f9; font-size: .78rem; color: #334155; font-weight: 700; white-space: nowrap; }
.tbl tbody tr:hover { background: #f8fafc; }
.tbl tfoot th { background: #f8fafc; }
.tbl .num { text-align: left; }
.tbl input, .tbl select { min-width: 90px; }
.empty { color: var(--muted); text-align: center; padding: 1rem; background: #f8fafc; border-radius: 10px; font-size: .88rem; }
.totals { display: flex; gap: 1rem; flex-wrap: wrap; padding-top: .55rem; border-top: 1px dashed var(--line); font-size: .86rem; }
.pager { display: flex; gap: .25rem; margin-top: .6rem; flex-wrap: wrap; }
.page { padding: .2rem .55rem; border: 1px solid var(--line); border-radius: 8px; font-size: .82rem; }
.page.active { background: var(--brand); color: #fff; border-color: var(--brand); }

/* ---------- النماذج ---------- */
.field { display: flex; flex-direction: column; gap: .2rem; font-size: .82rem; min-width: 0; }
.field > span { color: #334155; font-weight: 600; font-size: .78rem; }
.field small { color: var(--muted); font-size: .7rem; }
input, select, textarea {
    font: inherit; padding: .42rem .55rem; border: 1px solid #cbd5e1; border-radius: 9px;
    background: #fff; color: var(--ink); width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
input.narrow { width: 6.5rem; }
.stack { display: flex; flex-direction: column; gap: .55rem; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: .6rem; }
.filters .field { min-width: 140px; }
.check { display: inline-flex; gap: .35rem; align-items: center; font-size: .82rem; }
.check input { width: auto; }

/* ---------- الأزرار والشارات ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
    padding: .42rem .8rem; border-radius: 10px; border: 1px solid #cbd5e1;
    background: #fff; color: var(--ink); font-size: .84rem; cursor: pointer; text-decoration: none;
}
.btn:hover { background: #f8fafc; text-decoration: none; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 700; }
.btn-primary:hover { background: var(--brand-2); }
.btn-ghost { background: transparent; }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-sm { padding: .25rem .55rem; font-size: .78rem; }
.btn-block { width: 100%; }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .72rem; font-weight: 700; }
.badge-good { background: var(--good-soft); color: var(--good); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-bad { background: var(--bad-soft); color: var(--bad); }
.badge-muted { background: #e2e8f0; color: #475569; }

.alert { padding: .55rem .8rem; border-radius: 10px; font-size: .87rem; border: 1px solid transparent; }
.alert-success { background: var(--good-soft); border-color: #86efac; color: #14532d; }
.alert-error { background: var(--bad-soft); border-color: #fca5a5; color: #7f1d1d; }
.alert-info { background: #e0f2fe; border-color: #bae6fd; color: #075985; }

.result-box { margin-top: .6rem; padding: .7rem .9rem; border-radius: 12px; background: #f1f5f9; display: flex; flex-direction: column; gap: .1rem; }
.result-box b { font-size: 1.3rem; direction: ltr; text-align: right; }
.result-box.good { background: var(--good-soft); }
.result-box.bad { background: var(--bad-soft); }

.tabs { display: flex; gap: .35rem; flex-wrap: wrap; margin-bottom: .7rem; }
.tab { padding: .3rem .75rem; border: 1px solid var(--line); border-radius: 999px; font-size: .82rem; background: #fff; color: var(--ink); }
.tab:hover { text-decoration: none; background: #f1f5f9; }
.tab.active { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 700; }

/* ---------- ميزان المراجعة الثلاثي ---------- */
.tb-3 thead th { text-align: center; vertical-align: middle; }
.tb-3 thead tr:first-child th[colspan] { background: #dbeafe; color: #1e3a8a; }
.tb-3 tbody td:first-child, .tb-3 tbody td:nth-child(2) { text-align: right; white-space: nowrap; }
.tb-3 tfoot th { background: #e2e8f0; text-align: center; }
.tb-3 tfoot th.num { text-align: left; }

/* ---------- فواصل مجموعات الجداول: خط ثقيل بين الفئات وخط خفيف بين التقسيمات ---------- */
.tbl--grouped thead tr:first-child th {
    background: #e8eef7; color: #1e3a8a; text-align: center; vertical-align: middle;
    border-bottom: 2px solid #94a3b8;
}
.tbl--grouped thead tr:first-child th.grp-start { border-inline-start: 2px solid #64748b; }
.tbl--grouped thead tr:nth-child(2) th {
    background: #f8fafc; color: #475569; font-size: .74rem; font-weight: 600; text-align: center;
}
.tbl--grouped thead tr:nth-child(2) th.grp-sub-start { border-inline-start: 2px solid #64748b; }
.tbl--grouped thead tr:nth-child(2) th.sub { border-inline-start: 1px dashed #cbd5e1; }
.tbl--grouped tbody td.grp-start,
.tbl--grouped tfoot th.grp-start { border-inline-start: 2px solid #94a3b8; }
.tbl--grouped tbody td.sub,
.tbl--grouped tfoot th.sub { border-inline-start: 1px dashed #cbd5e1; }
.tbl--grouped tbody tr:first-child td { border-top: 1px solid var(--line); }
.tbl--grouped tfoot th { background: #e2e8f0; font-weight: 700; border-top: 2px solid #64748b; }
@media print {
    .tbl--grouped thead tr:first-child th { background: #e8eef7 !important; }
}

/* ---------- المصادقة ---------- */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 1.2rem; background: linear-gradient(160deg, #0b3b39, #0f766e 55%, #115e59); }
.auth-card { background: #fff; border-radius: 18px; padding: 1.5rem; width: 100%; max-width: 470px; box-shadow: 0 20px 50px rgba(2, 44, 42, .35); }
.auth-card.center { text-align: center; }
.auth-brand { display: flex; gap: .6rem; align-items: center; margin-bottom: 1rem; }
.auth-brand small { display: block; color: var(--muted); font-size: .72rem; }
.auth-foot { margin-top: 1rem; font-size: .72rem; color: var(--muted); text-align: center; }
.checks { list-style: none; padding: 0; margin: .5rem 0; font-size: .84rem; }
.checks li { padding: .25rem .1rem; }
.checks li.ok { color: var(--good); }
.checks li.bad { color: var(--bad); }
.error-code { font-size: 3rem; font-weight: 800; color: var(--bad); }

/* ---------- التصدير والفروع وشبكة الإنتاج ---------- */
.export-bar { display: flex; gap: .35rem; align-items: center; flex-wrap: wrap; margin: .35rem 0 .7rem; }
/* شريط الفرز */
.branch-switch { margin: 0; }
.branch-switch select { min-width: 150px; padding: .3rem .5rem; font-size: .8rem; }
.branch-switch select:focus { outline: 2px solid var(--brand-soft); }

/* البحث الشامل ومبدّل اللغة وأدوات الفرز */
.topbar-search { margin: 0; }
.topbar-search input {
    width: 180px; padding: .38rem .75rem; border: 1px solid var(--line); border-radius: 999px;
    background: #fff; font-size: .8rem; font-family: inherit;
}
.topbar-search input:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.lang-switch { display: flex; gap: .1rem; padding: .15rem; border: 1px solid var(--line); border-radius: 999px; background: #fff; }
.lang-opt { padding: .18rem .55rem; border-radius: 999px; font-size: .72rem; color: var(--muted); line-height: 1.6; }
.lang-opt:hover { text-decoration: none; background: #f1f5f9; }
.lang-opt.active { background: var(--brand); color: #fff; font-weight: 700; }
.sort-row { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; margin: 0 0 .6rem; }
.sort-row .sort-label { font-size: .78rem; color: var(--muted); }
.sort-row .tab { display: inline-flex; align-items: center; gap: .25rem; }
.search-results { display: grid; gap: 1rem; }
.search-group { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: .9rem 1rem; }
.search-group h3 { margin: 0 0 .6rem; font-size: .95rem; display: flex; align-items: center; gap: .4rem; }
.search-group h3 .count { font-size: .75rem; color: var(--muted); font-weight: 400; }
.search-hit { display: flex; align-items: center; justify-content: space-between; gap: .8rem; padding: .45rem 0; border-top: 1px dashed var(--line); }
.search-hit:first-of-type { border-top: 0; }
.search-hit .meta { color: var(--muted); font-size: .78rem; }
.integrity-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .8rem; margin-bottom: 1rem; }
.integrity-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; box-shadow: var(--shadow); }
.integrity-card b { display: block; font-size: 1.35rem; }
.integrity-card.bad b { color: var(--bad); }
.integrity-card.good b { color: var(--good); }
@media (max-width: 900px) {
    .topbar-search input { width: 120px; }
}
.branch-edit { border: 1px solid var(--line); border-radius: 10px; padding: .45rem .7rem; margin-bottom: .5rem; background: #f8fafc; }
.branch-edit summary { cursor: pointer; font-size: .9rem; }
.branch-edit[open] { background: #fff; }
.grid-table { font-size: .74rem; white-space: nowrap; }
.grid-table th, .grid-table td { padding: .28rem .4rem; text-align: center; }
.table-wrap.wide { overflow-x: auto; }
.table-wrap.wide .grid-table { min-width: 1500px; }
@media print {
    .grid-table { font-size: 6.8pt; white-space: normal; }
    .table-wrap.wide .grid-table { min-width: none; }
    @page { size: A3 landscape; margin: 6mm; }
}

/* ---------- الفاتورة ---------- */
.toolbar { display: flex; gap: .5rem; align-items: center; margin-bottom: .6rem; }
.invoice-print { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 1.2rem; box-shadow: var(--shadow); max-width: 900px; }
.inv-head { display: flex; justify-content: space-between; gap: 1rem; border-bottom: 2px solid var(--brand); padding-bottom: .7rem; margin-bottom: .7rem; }
.inv-head h2 { color: var(--brand-2); }
.inv-meta { text-align: left; font-size: .85rem; }
.inv-title { font-weight: 800; color: var(--brand); font-size: 1.05rem; }
.inv-parties { display: flex; gap: 2rem; margin-bottom: .7rem; font-size: .88rem; }
.inv-parties > div { display: flex; flex-direction: column; }
.inv-lines { margin-bottom: .8rem; }
.inv-totals { margin-inline-start: auto; width: min(340px, 100%); display: flex; flex-direction: column; gap: .2rem; font-size: .88rem; }
.inv-totals > div { display: flex; justify-content: space-between; border-bottom: 1px dotted var(--line); padding: .15rem 0; }
.inv-totals .grand { font-weight: 800; font-size: 1.05rem; color: var(--brand-2); border-bottom: 2px solid var(--brand); }
.inv-foot { margin-top: 1.2rem; border-top: 1px dashed var(--line); padding-top: .6rem; }
.signatures { display: flex; justify-content: space-between; margin-top: 1.6rem; font-size: .82rem; color: #334155; }

/* ---------- استجابة الشاشات ---------- */
@media (max-width: 980px) {
    .sidebar { position: fixed; inset-inline-start: 0; top: 0; z-index: 50; transform: translateX(100%); transition: transform .2s ease; }
    .sidebar.open { transform: translateX(0); }
    .icon-btn { display: inline-flex; }
    .content { padding: .8rem .7rem 2rem; }
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
    .topbar { padding: .6rem .7rem; }
    .inv-head { flex-direction: column; }
    .inv-meta { text-align: right; }
}

@media (max-width: 520px) {
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .user-chip { display: none; }
}

/* ---------- الطباعة ---------- */
@media print {
    body { background: #fff; }
    .sidebar, .topbar, .no-print, .toolbar, .alert { display: none !important; }
    .main { display: block; }
    .content { padding: 0; max-width: none; }
    .card, .invoice-print { box-shadow: none; border: none; padding: 0; }
    .invoice-print { max-width: none; }
    .tbl thead th { background: #f1f5f9 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    a { color: #000; text-decoration: none; }
    @page { margin: 12mm; }
}


/* ---------- تعليمات وتلميحات ---------- */
.help {
    display: inline-flex; align-items: center; justify-content: center;
    width: 1.05rem; height: 1.05rem; border-radius: 50%;
    background: var(--brand-soft); color: var(--brand-2);
    font-size: .72rem; font-weight: 800; cursor: help; position: relative;
    vertical-align: middle; margin-inline-start: .3rem; user-select: none;
}
.help::after {
    content: attr(data-tip);
    position: absolute; left: 50%; bottom: 140%; transform: translateX(-50%);
    background: #0f172a; color: #f8fafc; padding: .45rem .6rem; border-radius: 8px;
    font-size: .74rem; font-weight: 400; line-height: 1.55; text-align: right;
    width: max-content; max-width: 280px; white-space: normal;
    box-shadow: 0 10px 26px rgba(2, 6, 23, .35);
    opacity: 0; visibility: hidden; transition: opacity .15s ease; z-index: 80; pointer-events: none;
}
.help::before {
    content: ''; position: absolute; left: 50%; bottom: 125%; transform: translateX(-50%);
    border: 6px solid transparent; border-top-color: #0f172a;
    opacity: 0; visibility: hidden; transition: opacity .15s ease; z-index: 81; pointer-events: none;
}
.help:hover::after, .help:focus::after, .help:hover::before, .help:focus::before {
    opacity: 1; visibility: visible;
}

/* ---------- الوضع الداكن ---------- */
.theme-toggle { display: inline-flex; align-items: center; justify-content: center; }
html[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0b1220; --panel: #111a2b; --ink: #e2e8f0; --muted: #94a3b8; --line: #22304a;
    --brand: #2dd4bf; --brand-2: #5eead4; --brand-soft: #134e4a;
    --warn: #fbbf24; --warn-soft: #3f3013; --bad: #f87171; --bad-soft: #40191b;
    --good: #4ade80; --good-soft: #0f3524;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 10px 30px rgba(0, 0, 0, .35);
}
html[data-theme="dark"] body { background: var(--bg); color: var(--ink); }
html[data-theme="dark"] .sidebar { background: #071b1a; }
html[data-theme="dark"] .nav-link { color: #c7f5ee; }
html[data-theme="dark"] .nav-link.active { background: #0f2a28; color: #5eead4; }
html[data-theme="dark"] .theme-toggle { border-color: #334155; color: #e2e8f0; background: #16233a; }
html[data-theme="dark"] code,
html[data-theme="dark"] .memo,
html[data-theme="dark"] .empty,
html[data-theme="dark"] .result-box { background: #16233a; }
html[data-theme="dark"] .tbl thead th { background: #16233a; color: #cbd5e1; }
html[data-theme="dark"] .tbl tbody tr:hover { background: #152035; }
html[data-theme="dark"] .tbl tfoot th { background: #16233a; }
html[data-theme="dark"] .tbl--grouped thead tr:first-child th,
html[data-theme="dark"] .tb-3 thead tr:first-child th[colspan] { background: #1b2b47; color: #93c5fd; }
html[data-theme="dark"] .tbl--grouped thead tr:nth-child(2) th { background: #152035; color: #cbd5e1; }
html[data-theme="dark"] .tbl--grouped tfoot th,
html[data-theme="dark"] .tb-3 tfoot th { background: #1b2b47; }
html[data-theme="dark"] .tbl--grouped tbody td.grp-start,
html[data-theme="dark"] .tbl--grouped thead tr:first-child th.grp-start,
html[data-theme="dark"] .tbl--grouped thead tr:nth-child(2) th.grp-sub-start { border-inline-start-color: #475569; }
html[data-theme="dark"] .tbl--grouped tbody td.sub,
html[data-theme="dark"] .tbl--grouped thead tr:nth-child(2) th.sub { border-inline-start-color: #334155; }
html[data-theme="dark"] input, html[data-theme="dark"] select, html[data-theme="dark"] textarea {
    background: #0e1a2c; color: var(--ink); border-color: #2a3a55;
}
html[data-theme="dark"] .btn { background: #16233a; color: var(--ink); border-color: #2a3a55; }
html[data-theme="dark"] .btn:hover { background: #1c2c46; }
html[data-theme="dark"] .btn-primary { background: var(--brand); border-color: var(--brand); color: #042f2e; }
html[data-theme="dark"] .btn-danger { background: var(--bad); border-color: var(--bad); color: #2a0a0a; }
html[data-theme="dark"] .tab,
html[data-theme="dark"] .lang-switch,
html[data-theme="dark"] .page { background: #16233a; color: var(--ink); border-color: #2a3a55; }
html[data-theme="dark"] .tab:hover,
html[data-theme="dark"] .lang-opt:hover { background: #1c2c46; }
html[data-theme="dark"] .branch-edit[open],
html[data-theme="dark"] .invoice-print,
html[data-theme="dark"] .auth-card,
html[data-theme="dark"] .topbar-search input,
html[data-theme="dark"] .branch-switch select { background: #111a2b; color: var(--ink); }
html[data-theme="dark"] .badge-muted { background: #25324a; color: #cbd5e1; }
html[data-theme="dark"] .sync-pill { background: #3f3013; color: #fbbf24; }
html[data-theme="dark"] .section-title { color: #5eead4; border-bottom-color: #134e4a; }
html[data-theme="dark"] .auth-body { background: linear-gradient(160deg, #04100f, #07211f 55%, #062b28); }
html[data-theme="dark"] .help { background: #134e4a; color: #5eead4; }
@media print {
    html[data-theme="dark"] body,
    html[data-theme="dark"] .card,
    html[data-theme="dark"] .invoice-print { background: #fff !important; color: #000 !important; }
}



/* سجل العمليات: شارات ونوع الحذف وفرق الحقول */
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.nowrap { white-space: nowrap; }
.audit-op {
    display: inline-block; padding: .12rem .55rem; border-radius: 999px;
    font-size: .72rem; font-weight: 800; background: var(--surface-2, #e2e8f0); color: #334155;
}
.audit-op.op-create { background: #dcfce7; color: #166534; }
.audit-op.op-update, .audit-op.op-post { background: #dbeafe; color: #1d4ed8; }
.audit-op.op-delete { background: #fee2e2; color: #b91c1c; }
.audit-op.op-close { background: #fef3c7; color: #b45309; }
.audit-op.op-export, .audit-op.op-import { background: #ede9fe; color: #5b21b6; }
.audit-op.op-auth, .audit-op.op-sync, .audit-op.op-system { background: #e2e8f0; color: #334155; }
.audit-filters { margin-bottom: .8rem; }
.audit-meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .6rem 1rem; margin: .4rem 0 1rem; }
.audit-meta > div {
    display: flex; flex-direction: column; gap: .15rem;
    background: var(--surface-2, #f8fafc); border: 1px solid var(--line, #e2e8f0);
    border-radius: 10px; padding: .5rem .65rem;
}
.diff-table .diff-old { background: #fef2f2; color: #7f1d1d; }
.diff-table .diff-new { background: #ecfdf5; color: #065f46; }
.audit-json-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: .8rem; margin-top: .8rem; }
.json-box {
    background: #0f172a; color: #e2e8f0; padding: .7rem; border-radius: 10px;
    overflow: auto; max-height: 320px; font-size: .75rem; text-align: left; direction: ltr;
}
html[data-theme="dark"] .audit-meta > div { background: #1b2436; border-color: #2a3550; }
html[data-theme="dark"] .audit-op { filter: brightness(.92); }
html[data-theme="dark"] .diff-table .diff-old { background: #3b1d1d; color: #fecaca; }
html[data-theme="dark"] .diff-table .diff-new { background: #123327; color: #bbf7d0; }

/* جدول نصوص الواجهة القابلة للتعديل */
.translations-table td { vertical-align: middle; }
.translations-table input {
    width: 100%; min-width: 130px; padding: .32rem .45rem;
    font-size: .82rem; border-radius: 8px;
}
.translations-table td:first-child { max-width: 260px; overflow-wrap: anywhere; }
.translations-table .badge { font-size: .68rem; }

/* ================= استجابة كاملة لكل أحجام الشاشات ================= */
html { -webkit-text-size-adjust: 100%; }
img, svg { max-width: 100%; }
body.nav-open { overflow: hidden; }
button, .btn, .tab, .page, .nav-link { touch-action: manipulation; }

/* أهداف لمس مريحة على الأجهزة اللمسية */
@media (pointer: coarse) {
    .btn, .tab, .page, .nav-link { min-height: 42px; }
    .icon-btn { min-width: 42px; min-height: 42px; }
}

/* درج القائمة والطبقة المعتمة خلفه */
.nav-backdrop {
    position: fixed; inset: 0; z-index: 45;
    background: rgba(2, 6, 23, .48);
    -webkit-backdrop-filter: blur(1px); backdrop-filter: blur(1px);
}
.sidebar-close {
    display: none; position: absolute; inset-inline-end: .55rem; top: .55rem;
    width: 38px; height: 38px; border-radius: 10px; cursor: pointer;
    border: 1px solid rgba(255, 255, 255, .25);
    background: rgba(255, 255, 255, .08); color: #e6fffb; font-size: 1rem;
    align-items: center; justify-content: center;
}
.sidebar-close:hover { background: rgba(255, 255, 255, .18); }

/* شاشات متوسطة (لابتوب صغير / تابلت أفقي) */
@media (max-width: 1200px) {
    .content { padding: .85rem .85rem 1.8rem; }
    .card { padding: .85rem .9rem; }
    .stats-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
}

/* شاشات كبيرة جداً: توسيط المحتوى وزيادة كثافة المؤشرات */
@media (min-width: 1600px) {
    .content { max-width: 1560px; margin-inline: auto; }
    .sidebar { width: 268px; flex: 0 0 268px; }
    .stats-grid { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
}

/* تابلت عمودي وموبايل كبير: قائمة منزلقة */
@media (max-width: 980px) {
    .sidebar {
        position: fixed; inset-inline-start: 0; top: 0; bottom: 0;
        width: min(86vw, 320px); flex-basis: auto;
        height: 100vh; height: 100dvh;
        transform: translateX(100%); transition: transform .22s ease;
        z-index: 50; box-shadow: 0 0 40px rgba(2, 6, 23, .35); overflow-y: auto;
    }
    [dir="ltr"] .sidebar { transform: translateX(-100%); }
    .sidebar.open, [dir="ltr"] .sidebar.open { transform: translateX(0); }
    .sidebar-close { display: inline-flex; }
    .brand { padding-inline-end: 3.2rem; }
    .icon-btn { display: inline-flex; align-items: center; justify-content: center; }
    .content { padding: .8rem .7rem 2rem; }
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
    .topbar { padding: .6rem .7rem; }
    .inv-head { flex-direction: column; }
    .inv-meta { text-align: right; }
    .user-chip { display: none; }
    .nav-link { padding: .58rem .7rem; }
}

/* موبايل: شريط علوي من صفّين + جداول تمريرية + نماذج كاملة العرض */
@media (max-width: 760px) {
    .topbar { flex-wrap: wrap; row-gap: .45rem; }
    .topbar-title { order: 1; flex: 1 1 40%; min-width: 0; }
    .topbar-title h1 { font-size: .98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .topbar-title > .muted { display: none; }
    #menuToggle { order: 0; }
    .topbar-actions { order: 2; flex: 1 1 100%; margin-inline-start: 0; gap: .4rem; flex-wrap: wrap; }
    .topbar-search { order: -1; flex: 1 1 100%; }
    .topbar-search input { width: 100%; }
    .branch-switch { flex: 1 1 100%; display: flex; order: 1; }
    .branch-switch select { width: 100%; min-width: 0; }
    .lang-switch { flex: 0 0 auto; order: 2; }
    .topbar-actions > .btn, .topbar-actions > .icon-btn { order: 0; }
    .topbar-actions .inline { order: 3; margin-inline-start: auto; }

    .content { padding: .7rem .6rem calc(1.8rem + env(safe-area-inset-bottom)); gap: .7rem; }
    .topbar { padding-top: calc(.6rem + env(safe-area-inset-top)); }
    .card { padding: .75rem .7rem; border-radius: 12px; }
    .card-head { gap: .4rem; }
    .card-head h3 { font-size: .98rem; }

    .stats-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
    .aging-grid { grid-template-columns: repeat(2, 1fr); }
    .integrity-summary { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }

    .filters { gap: .45rem; }
    .filters .field { flex: 1 1 100%; min-width: 0; }
    .filters .btn { flex: 1 1 auto; }
    input, select, textarea { font-size: 16px; }
    .btn { padding: .5rem .75rem; }
    .btn-sm { padding: .42rem .6rem; }
    .export-bar .btn, .toolbar .btn, .sort-row .tab { flex: 1 1 auto; justify-content: center; }
    .toolbar { flex-wrap: wrap; }

    .table-wrap { -webkit-overflow-scrolling: touch; }
    .tbl { font-size: .82rem; }
    .tbl th, .tbl td { padding: .4rem .45rem; }
    .tbl thead th { font-size: .74rem; }
    .tbl input, .tbl select { min-width: 76px; }
    .tbl td { overflow-wrap: anywhere; }
    .grid-table { font-size: .7rem; }
    .grid-table th, .grid-table td { padding: .25rem .3rem; }

    .inv-parties { flex-direction: column; gap: .5rem; }
    .inv-totals { width: 100%; }
    .signatures { flex-wrap: wrap; gap: 1rem; }
    .invoice-print { padding: .8rem; border-radius: 12px; }

    .audit-meta { grid-template-columns: 1fr 1fr; gap: .45rem; }
    .audit-json-grid { grid-template-columns: 1fr; }
    .audit-op { font-size: .68rem; }
    .pager { overflow-x: auto; }
    .search-hit { flex-direction: column; align-items: flex-start; gap: .2rem; }
}

/* هواتف صغيرة */
@media (max-width: 480px) {
    body { font-size: 14px; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
    .stat-value { font-size: 1.02rem; overflow-wrap: anywhere; }
    .card-head { flex-wrap: wrap; }
    .audit-meta { grid-template-columns: 1fr; }
    .topbar-title h1 { font-size: .92rem; }
    .topbar-actions .btn { padding: .38rem .5rem; font-size: .78rem; }
    .lang-opt { padding: .15rem .45rem; }
    .help::after { left: auto; right: 0; transform: none; max-width: 76vw; }
    .help::before { left: auto; right: .3rem; transform: none; }
    .inv-head h2 { font-size: 1rem; }
    .inv-totals .grand { overflow-wrap: anywhere; }
    .signatures { font-size: .76rem; }
}

/* جوال أفقي (ارتفاع قصير) */
@media (max-height: 520px) and (orientation: landscape) {
    .sidebar { width: min(70vw, 300px); }
    .nav-link { padding-block: .45rem; }
    .nav-section { padding-top: .45rem; }
}

/* الطباعة: إخفاء عناصر التنقل مهما كان حجم الشاشة */
@media print {
    .nav-backdrop, .sidebar-close { display: none !important; }
    .table-wrap { overflow: visible; }
    .tbl th, .tbl td { padding: 2mm 1.5mm; }
}


/* ---------- نوافذ التعديل المنبثقة ---------- */
dialog.modal {
    width: min(940px, 96vw);
    max-width: min(940px, 96vw);
    max-height: 92dvh;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--ink);
    margin: auto;
    overflow: visible;
}

dialog.modal::backdrop {
    background: rgba(15, 23, 42, .55);
    backdrop-filter: blur(2px);
}

.modal-box {
    display: flex;
    flex-direction: column;
    max-height: 92dvh;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: 0 25px 60px rgba(2, 6, 23, .35);
    overflow: hidden;
    animation: modal-in .16s ease-out;
}

@keyframes modal-in {
    from { opacity: 0; transform: translateY(8px) scale(.99); }
    to { opacity: 1; transform: none; }
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .75rem;
    padding: .8rem 1rem;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
    position: sticky;
    top: 0;
    z-index: 2;
}

.modal-head h3 { margin: 0; }

.modal-close {
    flex: 0 0 auto;
    width: 2rem;
    height: 2rem;
    border-radius: 9px;
    border: 1px solid var(--line);
    background: transparent;
    color: var(--muted);
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
}

.modal-close:hover { background: var(--bg); color: var(--ink); }

.modal-body {
    padding: 1rem;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.modal-body > .card {
    border: 0;
    box-shadow: none;
    padding: 0;
    border-radius: 0;
    background: transparent;
}

.modal-body > form.card { padding: 0; }

.modal-body .card-head a.btn-ghost[href]:not([data-modal]) { display: none; }

@media (max-width: 640px) {
    dialog.modal {
        width: 100vw;
        max-width: 100vw;
        max-height: 95dvh;
        margin: auto 0 0;
    }
    .modal-box {
        max-height: 95dvh;
        border-radius: 16px 16px 0 0;
    }
    .modal-body { padding: .8rem; }
}

@media print {
    dialog.modal { display: none !important; }
}

