/* ==========================================================================
   نظام مستودعات المدارس — التصميم الكامل
   RTL • الوضع الفاتح والداكن (CSS Variables) • IBM Plex Sans Arabic
   قوالب الطباعة: فاتورة A4، فاتورة حرارية 80mm، محضر الجرد، التقارير
   ========================================================================== */

:root {
  --font: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, system-ui, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;

  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --surface-3: #eef2f4;
  --border: #e1e6ea;
  --border-strong: #c9d1d8;
  --text: #14212b;
  --text-2: #4a5a67;
  --muted: #7a8894;

  --primary: #0d7466;
  --primary-hover: #0a5f54;
  --primary-soft: #e2f3f0;
  --on-primary: #ffffff;
  --focus: rgba(13, 116, 102, .28);

  --success: #15803d; --success-soft: #dcfce7;
  --warning: #a16207; --warning-soft: #fef3c7;
  --danger:  #b91c1c; --danger-soft:  #fee2e2;
  --info:    #1d4ed8; --info-soft:    #dbeafe;
  --neutral-soft: #eceff2;

  --chart-bar: #0d7466;
  --chart-bar-hover: #0a5f54;
  --chart-grid: #e6eaed;
  --chart-axis: #8996a1;

  --sidebar-bg: #0f2a2f;
  --sidebar-text: #c9d8da;
  --sidebar-active: #ffffff;
  --sidebar-active-bg: rgba(255, 255, 255, .1);

  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(16, 32, 40, .05), 0 2px 8px rgba(16, 32, 40, .04);
  --shadow-lg: 0 20px 50px rgba(16, 32, 40, .18);
  --sidebar-w: 248px;
  --topbar-h: 60px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1418;
    --surface: #151d22;
    --surface-2: #1a242a;
    --surface-3: #212d34;
    --border: #273540;
    --border-strong: #35464f;
    --text: #e6edf0;
    --text-2: #a9b8c1;
    --muted: #7d8d97;
    --primary: #2bb3a0;
    --primary-hover: #3fc6b2;
    --primary-soft: rgba(43, 179, 160, .14);
    --on-primary: #06201c;
    --focus: rgba(43, 179, 160, .35);
    --success: #4ade80; --success-soft: rgba(74, 222, 128, .12);
    --warning: #fbbf24; --warning-soft: rgba(251, 191, 36, .12);
    --danger:  #f87171; --danger-soft:  rgba(248, 113, 113, .13);
    --info:    #60a5fa; --info-soft:    rgba(96, 165, 250, .13);
    --neutral-soft: #222e35;
    --chart-bar: #2bb3a0;
    --chart-bar-hover: #52d1bd;
    --chart-grid: #24323a;
    --chart-axis: #7d8d97;
    --sidebar-bg: #0b1114;
    --sidebar-text: #9fb1b8;
    --sidebar-active-bg: rgba(43, 179, 160, .14);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    --shadow-lg: 0 20px 50px rgba(0, 0, 0, .5);
  }
}

:root[data-theme="dark"] {
  --bg: #0e1418;
  --surface: #151d22;
  --surface-2: #1a242a;
  --surface-3: #212d34;
  --border: #273540;
  --border-strong: #35464f;
  --text: #e6edf0;
  --text-2: #a9b8c1;
  --muted: #7d8d97;
  --primary: #2bb3a0;
  --primary-hover: #3fc6b2;
  --primary-soft: rgba(43, 179, 160, .14);
  --on-primary: #06201c;
  --focus: rgba(43, 179, 160, .35);
  --success: #4ade80; --success-soft: rgba(74, 222, 128, .12);
  --warning: #fbbf24; --warning-soft: rgba(251, 191, 36, .12);
  --danger:  #f87171; --danger-soft:  rgba(248, 113, 113, .13);
  --info:    #60a5fa; --info-soft:    rgba(96, 165, 250, .13);
  --neutral-soft: #222e35;
  --chart-bar: #2bb3a0;
  --chart-bar-hover: #52d1bd;
  --chart-grid: #24323a;
  --chart-axis: #7d8d97;
  --sidebar-bg: #0b1114;
  --sidebar-text: #9fb1b8;
  --sidebar-active-bg: rgba(43, 179, 160, .14);
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, .5);
}

/* ---------- الأساسيات ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0; line-height: 1.35; font-weight: 600; }
h1 { font-size: 22px; } h2 { font-size: 18px; } h3 { font-size: 15px; } h4 { font-size: 14px; }
p { margin: 0 0 .6em; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: 12.5px; direction: ltr; unicode-bidi: embed; }
small, .small { font-size: 12px; }
.muted { color: var(--muted); }
.num { text-align: left; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ltr { direction: ltr; unicode-bidi: embed; }
.spacer { flex: 1; }
.grow { flex: 1; }
[hidden] { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.icon-lg { width: 34px; height: 34px; stroke-width: 1.5; }
.flip { transform: scaleX(-1); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- الأزرار ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s, box-shadow .15s;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger-ghost { color: var(--danger); border-color: transparent; background: transparent; }
.btn-danger-ghost:hover { background: var(--danger-soft); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn-lg { height: 46px; font-size: 15px; }
.btn-block { width: 100%; }
.btn .icon { width: 16px; height: 16px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; color: var(--text-2); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.sm { width: 30px; height: 30px; }
.icon-btn.sm .icon { width: 16px; height: 16px; }
.icon-btn.danger:hover { color: var(--danger); background: var(--danger-soft); }
.link { font-size: 13px; }

/* ---------- الحقول ---------- */
input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  height: 36px; padding: 0 10px; min-width: 0;
  transition: border-color .15s, box-shadow .15s;
}
textarea { height: auto; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus); }
input[type="checkbox"], input[type="radio"] { height: auto; width: 16px; height: 16px; accent-color: var(--primary); margin: 0; }
input[type="number"] { font-variant-numeric: tabular-nums; }
input[readonly] { background: var(--surface-2); }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12.5px; font-weight: 500; color: var(--text-2); }
.field input, .field select, .field textarea { width: 100%; }
.field.invalid input { border-color: var(--danger); }
.check, .radio { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.inline-field { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); font-size: 13px; }
.inline-field input { height: 34px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; align-content: start; }
.form-grid.compact { gap: 8px 10px; }
.form-grid h2, .form-grid h3, .form-grid h4 { margin-top: 6px; }
.span-2 { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; }
.form-error { color: var(--danger); background: var(--danger-soft); padding: 8px 10px; border-radius: var(--radius-sm); font-size: 13px; }

.search { position: relative; display: flex; align-items: center; min-width: 220px; }
.search .icon { position: absolute; inset-inline-start: 10px; color: var(--muted); pointer-events: none; }
.search input { width: 100%; padding-inline-start: 34px; }
.search.scan input { height: 42px; font-size: 15px; }

/* ---------- البطاقات والتنبيهات ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; min-width: 0; }
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.head-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.alert { padding: 10px 12px; border-radius: var(--radius-sm); margin: 0 0 12px; font-size: 13.5px; border: 1px solid transparent; }
.alert-success { background: var(--success-soft); color: var(--success); }
.alert-warning { background: var(--warning-soft); color: var(--warning); }
.alert-danger { background: var(--danger-soft); color: var(--danger); }
.alert-info { background: var(--info-soft); color: var(--info); }
.empty { text-align: center; color: var(--muted); padding: 36px 16px; }
.empty .icon { width: 40px; height: 40px; stroke-width: 1.3; margin-bottom: 8px; opacity: .7; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 8px;
  border-radius: 999px; font-size: 11.5px; font-weight: 600; white-space: nowrap;
  background: var(--neutral-soft); color: var(--text-2);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.plain::before { display: none; }
.badge.success { background: var(--success-soft); color: var(--success); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.primary { background: var(--primary-soft); color: var(--primary); }

/* ---------- الإقلاع وتسجيل الدخول ---------- */
.boot { position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; background: var(--bg); color: var(--muted); z-index: 50; }
.spinner { width: 34px; height: 34px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(1200px 600px at 85% -10%, var(--primary-soft), transparent 60%), var(--bg); }
.login-card { width: min(400px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg); padding: 28px; display: grid; gap: 14px; }
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.login-brand h1 { font-size: 20px; }
.login-brand p { margin: 0; font-size: 12.5px; }
.login-foot { font-size: 11.5px; text-align: center; margin: 0; }
.brand-mark { width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; background: var(--primary); color: var(--on-primary); font-weight: 700; font-size: 20px; flex-shrink: 0; }

/* ---------- الهيكل العام ---------- */
.shell { display: flex; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; width: var(--sidebar-w); flex-shrink: 0;
  background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; overflow-y: auto; z-index: 30;
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 16px; border-bottom: 1px solid rgba(255, 255, 255, .07); }
.sidebar-brand strong { color: #fff; display: block; font-size: 15px; }
.sidebar-brand small { display: block; font-size: 11.5px; color: var(--sidebar-text); opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px; }
.nav { display: flex; flex-direction: column; gap: 2px; padding: 12px 10px; flex: 1; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px;
  color: var(--sidebar-text); font-weight: 500; text-decoration: none; position: relative;
}
.nav a:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav a.active { background: var(--sidebar-active-bg); color: var(--sidebar-active); }
.nav a.active::before { content: ""; position: absolute; inset-inline-start: -10px; top: 8px; bottom: 8px; width: 3px; border-radius: 3px; background: var(--primary); }
.nav-sep { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; opacity: .55; padding: 14px 12px 4px; }
.nav-badge { margin-inline-start: auto; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--warning); color: #1f1500; font-size: 11px; display: grid; place-items: center; }
.sidebar-foot { padding: 12px 16px; font-size: 11.5px; color: var(--sidebar-text); opacity: .6; }
.nav-scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px; padding: 0 20px;
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.page-title { font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-tools { margin-inline-start: auto; display: flex; align-items: center; gap: 8px; }
.branch-picker { display: flex; align-items: center; gap: 6px; padding-inline-start: 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: var(--surface); color: var(--text-2); }
.branch-picker select { border: 0; box-shadow: none; background: transparent; max-width: 260px; padding-inline-start: 2px; }
.branch-picker.attention { animation: pulse 1s 2; border-color: var(--warning); }
@keyframes pulse { 50% { box-shadow: 0 0 0 4px var(--warning-soft); } }
.theme-light-icon { display: none; }
:root[data-theme="dark"] .theme-light-icon { display: block; }
:root[data-theme="dark"] .theme-dark-icon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-light-icon { display: block; }
  :root:not([data-theme="light"]) .theme-dark-icon { display: none; }
}
.user-menu { position: relative; }
.user-chip { display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 6px 0 10px; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text); font: inherit; cursor: pointer; }
.user-chip:hover { background: var(--surface-3); }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-weight: 700; }
.user-meta { display: flex; flex-direction: column; text-align: start; line-height: 1.25; }
.user-meta strong { font-size: 13px; font-weight: 600; }
.user-meta small { font-size: 11.5px; }
.menu { position: absolute; inset-inline-end: 0; top: calc(100% + 6px); min-width: 210px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 6px; z-index: 40; }
.menu button { display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 10px; border: 0; background: transparent; color: var(--text); font: inherit; border-radius: 6px; cursor: pointer; text-align: start; }
.menu button:hover { background: var(--surface-3); }

.view { padding: 20px; display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.page { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 16px; }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.summary-bar { display: flex; gap: 18px; flex-wrap: wrap; color: var(--text-2); font-size: 13px; }
.summary-bar b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- مؤشرات الأداء ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.kpi .label { color: var(--text-2); font-size: 12.5px; display: flex; align-items: center; gap: 6px; }
.kpi .label .icon { width: 15px; height: 15px; color: var(--muted); }
.kpi .value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .value small { font-size: 12px; font-weight: 500; color: var(--muted); }
.kpi .sub { font-size: 12px; color: var(--muted); }
.kpi.warn .value { color: var(--warning); }
.kpi.bad .value { color: var(--danger); }
.kpi.good .value { color: var(--success); }
.kpis-6 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis-compact .kpi { padding: 10px 14px; }
.kpis-compact .kpi .value { font-size: 18px; }

/* ---------- الرسم البياني ---------- */
.chart { position: relative; width: 100%; min-height: 230px; direction: ltr; }
.chart svg { display: block; width: 100%; height: 240px; overflow: visible; }
.chart .grid line { stroke: var(--chart-grid); stroke-width: 1; }
.chart .grid line.zero { stroke: var(--chart-axis); }
.chart .axis text { fill: var(--chart-axis); font-size: 11px; font-family: var(--font); }
.chart .bar { fill: var(--chart-bar); transition: fill .12s; }
.chart .col:hover .bar, .chart .col.active .bar { fill: var(--chart-bar-hover); }
.chart .hit { fill: transparent; cursor: default; }
.chart .col:hover .hit { fill: var(--surface-3); opacity: .5; }
.chart-tip {
  position: absolute; pointer-events: none; transform: translate(-50%, calc(-100% - 8px));
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow-lg);
  padding: 6px 10px; font-size: 12px; white-space: nowrap; z-index: 2;
}
.chart-tip strong { display: block; font-size: 13.5px; font-variant-numeric: tabular-nums; }

/* ---------- الجداول ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th, .table td { padding: 9px 12px; text-align: start; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { background: var(--surface-2); color: var(--text-2); font-weight: 600; font-size: 12.5px; white-space: nowrap; position: sticky; top: 0; z-index: 1; }
.table th.num, .table td.num { text-align: left; }
.table td.num { direction: ltr; }
.table th[data-sort] { cursor: pointer; }
.table th[data-sort]:hover { color: var(--text); }
.table th.sorted::after { content: " ▾"; font-size: 10px; }
.table th.sorted.asc::after { content: " ▴"; }
.table tbody tr:hover td { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: 0; }
.table tfoot td { font-weight: 600; background: var(--surface-2); }
.table .col-img { width: 52px; }
.table .col-actions { width: 1%; white-space: nowrap; text-align: end; }
.table td.actions { white-space: nowrap; text-align: end; }
.table td .sub { display: block; font-size: 12px; color: var(--muted); }
.table tr.clickable { cursor: pointer; }
.table .qty-low { color: var(--danger); font-weight: 600; }
.thumb { width: 38px; height: 38px; border-radius: 8px; object-fit: cover; background: var(--surface-3); display: grid; place-items: center; color: var(--muted); border: 1px solid var(--border); }
.thumb .icon { width: 18px; height: 18px; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; color: var(--text-2); font-size: 13px; flex-wrap: wrap; }
.pager .pages { display: flex; gap: 6px; }

/* ---------- عناصر التبويب المجزأ ---------- */
.segmented { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-3); border-radius: 9px; flex-wrap: wrap; }
.segmented button { height: 30px; padding: 0 12px; border: 0; border-radius: 7px; background: transparent; color: var(--text-2); font: inherit; font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap; }
.segmented button:hover { color: var(--text); }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented-block { display: flex; }
.segmented-block button { flex: 1; }

/* ---------- الاستيراد ---------- */
.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; text-align: center;
  border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 28px 16px; cursor: pointer;
  color: var(--text-2); background: var(--surface-2); transition: border-color .15s, background .15s; position: relative;
}
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.dropzone.drag, .dropzone:hover { border-color: var(--primary); background: var(--primary-soft); }
.import-options { display: grid; gap: 8px; margin-top: 14px; }
.mapping { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px; }
.mapping .field select { height: 34px; }
.row-error td { background: var(--danger-soft) !important; }
.row-error .err { color: var(--danger); font-size: 12px; }

/* ---------- الجرد ---------- */
.count-title { display: flex; flex-direction: column; line-height: 1.3; }
.count-table input[name="counted"] { width: 92px; height: 32px; text-align: center; font-variant-numeric: tabular-nums; }
.count-table select, .count-table .note { height: 32px; font-size: 13px; }
.count-table .note { width: 150px; }
.count-table tr.diff-neg td { background: color-mix(in srgb, var(--danger-soft) 60%, transparent); }
.count-table tr.diff-pos td { background: color-mix(in srgb, var(--info-soft) 60%, transparent); }
.count-table tr.flash td { animation: flash 0.9s; }
@keyframes flash { from { background: var(--primary-soft); } }
.diff-neg-text { color: var(--danger); font-weight: 600; }
.diff-pos-text { color: var(--info); font-weight: 600; }

/* ---------- نقطة البيع ---------- */
.pos { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 16px; align-items: start; }
.pos-catalog { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.pos-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.product {
  display: flex; flex-direction: column; gap: 6px; text-align: start; padding: 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer;
  font: inherit; color: inherit; transition: border-color .12s, transform .12s, box-shadow .12s; min-width: 0;
}
.product:hover { border-color: var(--primary); box-shadow: var(--shadow); }
.product:active { transform: scale(.98); }
.product:disabled { opacity: .45; cursor: not-allowed; }
.product .ph { aspect-ratio: 4 / 3; border-radius: 8px; background: var(--surface-3); display: grid; place-items: center; color: var(--muted); overflow: hidden; }
.product .ph img { width: 100%; height: 100%; object-fit: cover; }
.product .name { font-weight: 600; font-size: 13.5px; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.product .meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.product .row { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.product .price { font-weight: 700; color: var(--primary); font-variant-numeric: tabular-nums; }
.product .stock { font-size: 11.5px; color: var(--muted); }
.product .stock.low { color: var(--danger); font-weight: 600; }
.pos-cart { position: sticky; top: calc(var(--topbar-h) + 16px); display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - var(--topbar-h) - 32px); overflow-y: auto; }
.cart-lines { display: flex; flex-direction: column; gap: 6px; min-height: 80px; }
.cart-line { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 8px; align-items: center; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.cart-line .t { font-weight: 600; font-size: 13px; line-height: 1.3; }
.cart-line .d { font-size: 11.5px; color: var(--muted); }
.cart-line .lt { font-weight: 600; font-variant-numeric: tabular-nums; font-size: 13px; }
.qty { display: inline-flex; align-items: center; border: 1px solid var(--border-strong); border-radius: 7px; overflow: hidden; background: var(--surface); }
.qty button { width: 28px; height: 28px; border: 0; background: transparent; color: var(--text); cursor: pointer; display: grid; place-items: center; }
.qty button:hover { background: var(--surface-3); }
.qty button .icon { width: 14px; height: 14px; }
.qty input { width: 44px; height: 28px; border: 0; border-radius: 0; text-align: center; padding: 0; box-shadow: none !important; }
.buyer-box { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.buyer-box summary { cursor: pointer; font-weight: 600; font-size: 13px; }
.buyer-box[open] summary { margin-bottom: 10px; }
.pos.kind-simplified .b2b { display: none; }
.totals { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin: 0; font-size: 13.5px; }
.totals dt { color: var(--text-2); }
.totals dd { margin: 0; text-align: left; font-variant-numeric: tabular-nums; font-weight: 500; }
.totals .grand { font-size: 18px; font-weight: 700; color: var(--text); padding-top: 6px; border-top: 1px solid var(--border); }
.totals.inline { margin-top: 12px; max-width: 360px; margin-inline-start: auto; }
.cart-actions { display: flex; gap: 8px; }

/* ---------- الإعدادات ---------- */
.settings { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 16px; align-items: start; }
.settings-main { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.branch-list { display: flex; flex-direction: column; gap: 6px; }
.branch-item { display: flex; flex-direction: column; gap: 4px; text-align: start; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); cursor: pointer; font: inherit; color: inherit; }
.branch-item:hover { border-color: var(--border-strong); background: var(--surface-2); }
.branch-item.active { border-color: var(--primary); background: var(--primary-soft); }
.branch-item strong { font-size: 13.5px; }
.branch-item .row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.steps { display: grid; gap: 10px; counter-reset: step; }
.step { display: grid; grid-template-columns: 32px minmax(0, 1fr); gap: 12px; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); }
.step::before { counter-increment: step; content: counter(step); width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; background: var(--surface-3); color: var(--text-2); font-weight: 700; }
.step.done::before { content: "✓"; background: var(--success-soft); color: var(--success); }
.step.current { border-color: var(--primary); }
.step .step-body { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.step .row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 14px; font-size: 13px; margin: 0; }
.kv dt { color: var(--text-2); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.code-box { width: 100%; font-family: var(--mono); font-size: 11.5px; direction: ltr; background: var(--surface-2); }
details.manual { border: 1px dashed var(--border-strong); border-radius: var(--radius); padding: 10px 12px; }
details.manual summary { cursor: pointer; font-weight: 600; }

/* ---------- النوافذ ---------- */
.dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text);
  box-shadow: var(--shadow-lg); width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 32px);
}
.dialog[open] { display: flex; flex-direction: column; }
.dialog > form { display: flex; flex-direction: column; min-height: 0; max-height: calc(100vh - 34px); }
.dialog::backdrop { background: rgba(8, 16, 20, .5); backdrop-filter: blur(2px); }
.dialog-sm { width: min(400px, calc(100vw - 24px)); }
.dialog-lg { width: min(860px, calc(100vw - 24px)); }
.dialog-xl { width: min(1060px, calc(100vw - 24px)); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.dialog-body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 14px 14px; }
.confirm-body { flex-direction: row; align-items: flex-start; gap: 12px; }
.confirm-body .icon { color: var(--warning); width: 28px; height: 28px; }
.item-form { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 18px; }
.image-picker { display: flex; flex-direction: column; gap: 8px; align-items: stretch; }
.image-preview { aspect-ratio: 1; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface-2); display: grid; place-items: center; color: var(--muted); overflow: hidden; }
.image-preview img { width: 100%; height: 100%; object-fit: cover; }
.item-form.type-book .uniform-only { display: none; }
.item-form.vat-S .exempt-only { display: none; }
.price-hint { margin: 0; font-size: 12.5px; color: var(--text-2); background: var(--surface-2); padding: 8px 10px; border-radius: 7px; }
.scanner-video { width: 100%; border-radius: var(--radius); background: #000; aspect-ratio: 4 / 3; object-fit: cover; }
.invoice-preview-wrap { background: var(--surface-3); align-items: center; }
.paper { background: #fff; color: #111; box-shadow: var(--shadow-lg); border-radius: 4px; max-width: 100%; overflow-x: auto; }

/* ---------- الإشعارات المنبثقة ---------- */
.toasts { position: fixed; bottom: 18px; inset-inline-start: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: min(420px, calc(100vw - 36px)); }
.toast { display: flex; align-items: flex-start; gap: 10px; padding: 11px 14px; border-radius: 10px; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-inline-start: 4px solid var(--info); box-shadow: var(--shadow-lg); font-size: 13.5px; animation: toast-in .2s ease-out; }
.toast.success { border-inline-start-color: var(--success); }
.toast.error { border-inline-start-color: var(--danger); }
.toast.warning { border-inline-start-color: var(--warning); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- صفحة التثبيت ---------- */
.install-body { background: var(--bg); }
.install { width: min(860px, 100%); margin: 0 auto; padding: 24px 16px 48px; display: flex; flex-direction: column; gap: 16px; }
.install-head { display: flex; align-items: center; gap: 14px; }
.install-head p { margin: 2px 0 0; }
.install .card h2 { font-size: 16px; margin-bottom: 4px; }
.checklist { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 6px; }
.checklist li { display: flex; align-items: center; gap: 8px; }
.checklist .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); flex-shrink: 0; }
.checklist .ok .dot { background: var(--success); }

/* ==========================================================================
   مستندات الطباعة (الفواتير ومحضر الجرد والتقارير) — ألوان ثابتة للطباعة
   ========================================================================== */
.doc { font-family: var(--font); color: #111; background: #fff; direction: rtl; }
.doc table { width: 100%; border-collapse: collapse; }
.doc .en { direction: ltr; font-size: .85em; color: #555; }
.doc .n { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: embed; white-space: nowrap; }

/* A4 */
.doc-a4 { width: 210mm; max-width: 100%; min-height: 200mm; padding: 14mm 14mm 12mm; font-size: 12px; line-height: 1.55; }
.doc-a4 .doc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; padding-bottom: 10px; border-bottom: 2px solid #0d7466; }
.doc-a4 .seller h2 { font-size: 17px; margin-bottom: 4px; color: #0d7466; }
.doc-a4 .seller div { font-size: 11.5px; color: #333; }
.doc-a4 .qr { width: 34mm; height: 34mm; flex-shrink: 0; }
.doc-a4 .qr svg { width: 100%; height: 100%; }
.doc-a4 .doc-title { text-align: center; margin: 12px 0 10px; }
.doc-a4 .doc-title h1 { font-size: 19px; color: #111; }
.doc-a4 .doc-title .en { font-size: 12px; letter-spacing: .03em; }
.doc-a4 .meta { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.doc-a4 .box { border: 1px solid #cfd6da; border-radius: 6px; padding: 8px 10px; }
.doc-a4 .box h4 { font-size: 12px; color: #0d7466; margin-bottom: 4px; border-bottom: 1px solid #e3e7ea; padding-bottom: 3px; }
.doc-a4 .box table td { padding: 2px 0; vertical-align: top; font-size: 11.5px; }
.doc-a4 .box table td:first-child { color: #555; width: 42%; }
.doc-a4 .lines th { background: #0d7466; color: #fff; font-weight: 600; font-size: 11px; padding: 6px; text-align: center; }
.doc-a4 .lines th .en { color: #d8efeb; display: block; font-size: 9.5px; }
.doc-a4 .lines td { border-bottom: 1px solid #e3e7ea; padding: 6px; font-size: 11.5px; text-align: center; }
.doc-a4 .lines td.t { text-align: start; }
.doc-a4 .lines tr:nth-child(even) td { background: #f6f8f9; }
.doc-a4 .bottom { display: grid; grid-template-columns: 1fr 78mm; gap: 12px; margin-top: 12px; align-items: start; }
.doc-a4 .sum td { padding: 5px 8px; border-bottom: 1px solid #e3e7ea; font-size: 12px; }
.doc-a4 .sum td:last-child { text-align: left; }
.doc-a4 .sum tr.grand td { background: #0d7466; color: #fff; font-weight: 700; font-size: 13px; }
.doc-a4 .vat-break td, .doc-a4 .vat-break th { font-size: 10.5px; padding: 4px 6px; border: 1px solid #e3e7ea; text-align: center; }
.doc-a4 .vat-break th { background: #f1f4f5; }
.doc-a4 .foot { margin-top: 14px; padding-top: 8px; border-top: 1px solid #cfd6da; font-size: 10.5px; color: #555; display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.doc-a4 .stamp { display: inline-block; padding: 2px 8px; border: 1px solid #0d7466; color: #0d7466; border-radius: 4px; font-weight: 600; font-size: 10.5px; }
.doc-a4 .stamp.sim { border-color: #a16207; color: #a16207; }
.doc-a4 .note-ref { margin-bottom: 10px; padding: 7px 10px; background: #fff8e6; border: 1px solid #f1d79a; border-radius: 6px; font-size: 11.5px; }
.doc .uuid { font-family: var(--mono); font-size: 9.5px; direction: ltr; unicode-bidi: embed; word-break: break-all; }

/* حراري 80mm */
.doc-thermal { width: 72mm; padding: 3mm 2mm; font-size: 11px; line-height: 1.45; margin: 0 auto; }
.doc-thermal .c { text-align: center; }
.doc-thermal h2 { font-size: 13px; margin: 0; }
.doc-thermal h3 { font-size: 12px; margin: 4px 0 0; }
.doc-thermal .sep { border-top: 1px dashed #333; margin: 5px 0; }
.doc-thermal table td { padding: 1px 0; vertical-align: top; font-size: 10.5px; }
.doc-thermal .ln .nm { font-weight: 600; }
.doc-thermal .ln td.n { text-align: left; }
.doc-thermal .tot td { font-size: 11px; }
.doc-thermal .tot td:last-child { text-align: left; }
.doc-thermal .tot tr.grand td { font-size: 13px; font-weight: 700; border-top: 1px solid #111; padding-top: 3px; }
.doc-thermal .qr { width: 40mm; height: 40mm; margin: 6px auto 2px; }
.doc-thermal .qr svg { width: 100%; height: 100%; }
.doc-thermal .small { font-size: 9.5px; color: #333; }

/* محضر الجرد والتقارير */
.doc-report { width: 210mm; max-width: 100%; padding: 12mm; font-size: 11.5px; }
.doc-report h1 { font-size: 18px; text-align: center; margin-bottom: 2px; }
.doc-report .sub { text-align: center; color: #555; margin-bottom: 10px; }
.doc-report .grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 10px; }
.doc-report .grid div { border: 1px solid #cfd6da; border-radius: 5px; padding: 6px 8px; }
.doc-report .grid b { display: block; font-size: 13px; }
.doc-report table.t th, .doc-report table.t td { border: 1px solid #cfd6da; padding: 4px 6px; font-size: 10.5px; text-align: center; }
.doc-report table.t th { background: #eef2f3; }
.doc-report table.t td.s { text-align: start; }
.doc-report .neg { color: #b91c1c; font-weight: 600; }
.doc-report .pos { color: #1d4ed8; font-weight: 600; }
.doc-report .signs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-top: 28px; text-align: center; }
.doc-report .signs div { border-top: 1px solid #333; padding-top: 6px; }
.doc-report h3 { margin: 12px 0 6px; font-size: 13px; }

/* التقارير على الشاشة */
.report { display: flex; flex-direction: column; gap: 16px; }
.report .table td.neg { color: var(--danger); }

/* ---------- الطباعة ---------- */
.print-root { display: none; }
@media print {
  html, body { background: #fff !important; color: #000 !important; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block !important; }
  #print-root .doc { box-shadow: none; margin: 0; }
  #print-root .doc-a4, #print-root .doc-report { width: auto; min-height: 0; padding: 0; }
  #print-root .doc-thermal { width: 72mm; padding: 0; }
  #print-root table { page-break-inside: auto; }
  #print-root tr { page-break-inside: avoid; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---------- الاستجابة للشاشات ---------- */
.only-mobile { display: none; }
@media (max-width: 1200px) {
  .pos { grid-template-columns: minmax(0, 1fr) 360px; }
  .col-branch { display: none; }
}
@media (max-width: 1024px) {
  .kpis-6 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2, .settings { grid-template-columns: minmax(0, 1fr); }
  .pos { grid-template-columns: minmax(0, 1fr); }
  .pos-cart { position: static; max-height: none; }
}
@media (max-width: 860px) {
  .only-mobile { display: inline-grid; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; transform: translateX(100%); transition: transform .2s ease; box-shadow: var(--shadow-lg); }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .nav-scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 25; }
  .topbar { padding: 0 12px; gap: 6px; }
  .user-meta { display: none; }
  .branch-picker select { max-width: 150px; }
  .view { padding: 16px; }
  .item-form { grid-template-columns: minmax(0, 1fr); }
  .image-picker { flex-direction: row; align-items: center; }
  .image-preview { width: 90px; }
}
@media (max-width: 600px) {
  .form-grid, .mapping { grid-template-columns: minmax(0, 1fr); }
  .search { min-width: 0; flex: 1 1 100%; }
  .page-title { font-size: 15px; }
  .kpis, .kpis-6 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .kpi { padding: 10px 12px; }
  .kpi .value { font-size: 17px; }
  .kpi .sub { font-size: 11px; }
  .table th, .table td { padding: 8px; }
  .dialog-head .head-actions .btn { padding: 0 8px; }
  .doc-a4 .meta, .doc-a4 .bottom { grid-template-columns: 1fr; }
}
