/* QNAD.KB dashboard design system.
   One layer on top of the existing markup: tokens, shared components, light and dark themes.
   Loaded after the inline base styles in _head.html, so it wins on equal specificity.
   Rules are prefixed with html:root so they also beat the older html.dark overrides.
   Rollback: remove the <link> to ds.css and the shell.js/icons.js tags in _head.html. */

/* ---------- 1. Tokens ---------- */
:root {
  /* type: system stack only, no web font to block rendering */
  --ds-font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", "Helvetica Neue", Arial, sans-serif;
  --ds-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --ds-fs-xs: .75rem; --ds-fs-sm: .875rem; --ds-fs-md: 1rem; --ds-fs-lg: 1.125rem; --ds-fs-xl: 1.375rem; --ds-fs-2xl: 1.75rem;
  /* 4/8 spacing scale */
  --ds-s1: 4px; --ds-s2: 8px; --ds-s3: 12px; --ds-s4: 16px; --ds-s5: 24px; --ds-s6: 32px; --ds-s7: 48px;
  --ds-r-sm: 8px; --ds-r: 12px; --ds-r-lg: 16px;
  /* surfaces (light) */
  --ds-bg: #f5f6fa; --ds-surface: #ffffff; --ds-surface-2: #f1f3f9; --ds-hover: #eef1f8;
  --ds-border: #dde2ed; --ds-border-strong: #c3cbdc;
  --ds-text: #0e1630; --ds-text-2: #3d4963; --ds-text-3: #566179;       /* all >= 4.5:1 on surface and bg */
  --ds-brand: #4338ca; --ds-brand-hover: #372fa8; --ds-brand-soft: #eceefe; --ds-brand-text: #3730a3; --ds-on-brand: #ffffff;
  --ds-ok: #0b7a55; --ds-ok-soft: #e3f6ee; --ds-warn: #92560a; --ds-warn-soft: #fdf1d9; --ds-danger: #b4232f; --ds-danger-soft: #fdecee; --ds-info: #0b6aa2; --ds-info-soft: #e2f1fb;
  --ds-focus: #4f46e5;
  /* sidebar stays dark in both themes (brand anchor) */
  --ds-side-bg: #0c1330; --ds-side-text: #b8c3de; --ds-side-dim: #8d9bbf; --ds-side-hover: rgba(255,255,255,.07); --ds-side-active: rgba(255,255,255,.12);
  --ds-shadow-1: 0 1px 2px rgba(14,22,48,.06);
  --ds-shadow-2: 0 4px 14px rgba(14,22,48,.09), 0 1px 3px rgba(14,22,48,.06);
  --ds-shadow-3: 0 18px 48px rgba(14,22,48,.22);
  --ds-scrim: rgba(8,12,28,.5);
  /* motion */
  --ds-ease: cubic-bezier(.22,1,.36,1); --ds-t-fast: 120ms; --ds-t: 200ms;
  /* z-index scale */
  --ds-z-sticky: 20; --ds-z-header: 30; --ds-z-dropdown: 40; --ds-z-modal: 50; --ds-z-toast: 60; --ds-z-top: 80;
  color-scheme: light;
}
html.dark {
  --ds-bg: #0a0f20; --ds-surface: #121a31; --ds-surface-2: #182140; --ds-hover: #1a2445;
  --ds-border: #27335a; --ds-border-strong: #3a4878;
  --ds-text: #e8edf8; --ds-text-2: #bac5dc; --ds-text-3: #94a2bf;
  --ds-brand: #7c83f5; --ds-brand-hover: #9aa0ff; --ds-brand-soft: rgba(124,131,245,.16); --ds-brand-text: #b3b8ff; --ds-on-brand: #0a0f20;
  --ds-ok: #52d6a0; --ds-ok-soft: rgba(82,214,160,.14); --ds-warn: #f2bd5a; --ds-warn-soft: rgba(242,189,90,.14); --ds-danger: #ff8d97; --ds-danger-soft: rgba(255,141,151,.14); --ds-info: #6cc3f5; --ds-info-soft: rgba(108,195,245,.14);
  --ds-focus: #a5abff;
  --ds-side-bg: #070b1a;
  --ds-shadow-1: 0 1px 2px rgba(0,0,0,.4); --ds-shadow-2: 0 6px 18px rgba(0,0,0,.45); --ds-shadow-3: 0 20px 56px rgba(0,0,0,.6); --ds-scrim: rgba(0,0,0,.62);
  color-scheme: dark;
}

/* ---------- 2. Base ---------- */
html:root, html:root body { background: var(--ds-bg); color: var(--ds-text); font-family: var(--ds-font); }
html:root body { font-size: 15px; line-height: 1.5; -webkit-text-size-adjust: 100%; text-rendering: optimizeLegibility; }
html:root .font-display, html:root h1, html:root h2, html:root h3 { font-family: var(--ds-font); letter-spacing: -.012em; }
html:root h1.font-display, html:root main h1 { font-weight: 700; text-wrap: balance; }
html:root h1, html:root h2, html:root h3 { color: var(--ds-text); }
html:root .bg-slate-50 { background: var(--ds-bg); }
html:root .bg-white { background: var(--ds-surface); }
html:root .tabular, html:root table td, html:root table th, html:root .font-display.font-bold { font-variant-numeric: tabular-nums; }
html:root td { unicode-bidi: plaintext; }
::selection { background: var(--ds-brand-soft); color: var(--ds-text); }
/* contrast lift for the lightest utility greys used across older templates */
html:root .text-slate-400, html:root .placeholder-slate-400::placeholder { color: var(--ds-text-3); }
html:root .text-slate-500 { color: var(--ds-text-3); }
html:root .text-slate-600, html:root .text-slate-700 { color: var(--ds-text-2); }
html:root .text-slate-800, html:root .text-slate-900 { color: var(--ds-text); }
html:root ::placeholder { color: var(--ds-text-3); opacity: 1; }
html:root .text-brand-600, html:root .text-brand-700 { color: var(--ds-brand-text); }
html:root .border-slate-100, html:root .border-slate-200, html:root .border, html:root .border-b, html:root .border-t, html:root .border-y, html:root .divide-slate-100 > * + * { border-color: var(--ds-border); }
html:root .divide-y > * + * { border-color: var(--ds-border); }
html:root .hover\:bg-slate-50:hover, html:root .hover\:bg-slate-100:hover { background: var(--ds-hover); }
html:root .bg-slate-100, html:root .bg-slate-50\/70 { background: var(--ds-surface-2); }
html:root .bg-brand-50 { background: var(--ds-brand-soft); }

/* ---------- 3. Focus, motion, touch ---------- */
:focus-visible { outline: 2px solid var(--ds-focus); outline-offset: 2px; border-radius: 6px; }
.inp:focus-visible { outline: none; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
  main { animation: none !important; }
}
@media (pointer: coarse) {
  html:root .btn, html:root .inp, html:root select.inp { min-height: 44px; }
  html:root .tap { min-width: 44px; min-height: 44px; justify-content: center; }
}
.sr-only-focusable:focus { position: fixed; z-index: var(--ds-z-top); }

/* ---------- 4. Components ---------- */
html:root .card { background: var(--ds-surface); border: 1px solid var(--ds-border); border-radius: var(--ds-r); box-shadow: var(--ds-shadow-1); }
html:root a.card:hover, html:root .card.is-link:hover { border-color: var(--ds-border-strong); box-shadow: var(--ds-shadow-2); }

html:root .btn { border-radius: 10px; font-weight: 600; line-height: 1.25; justify-content: center;
  transition: background-color var(--ds-t-fast) var(--ds-ease), border-color var(--ds-t-fast) var(--ds-ease), box-shadow var(--ds-t-fast) var(--ds-ease), transform var(--ds-t-fast) var(--ds-ease); }
/* zero specificity so utility classes such as !px-2.5 or py-1 still win */
:where(.btn) { min-height: 36px; padding: 6px 14px; }
html:root .btn:active { transform: translateY(1px); }
html:root .btn[disabled], html:root .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; transform: none; }
html:root .btn-primary { background: var(--ds-brand); color: var(--ds-on-brand); box-shadow: 0 1px 2px rgba(67,56,202,.35); }
html:root .btn-primary:hover { background: var(--ds-brand-hover); }
html:root .btn-ghost { background: var(--ds-surface); color: var(--ds-text); border: 1px solid var(--ds-border-strong); }
html:root .btn-ghost:hover { background: var(--ds-hover); }
html:root .btn-danger { background: var(--ds-danger-soft); color: var(--ds-danger); border: 1px solid color-mix(in srgb, var(--ds-danger) 35%, transparent); }
html:root .btn-danger:hover { background: color-mix(in srgb, var(--ds-danger) 18%, var(--ds-surface)); }
html:root .btn.is-busy { position: relative; color: transparent !important; pointer-events: none; }
html:root .btn.is-busy::after { content: ""; position: absolute; inset: 0; margin: auto; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--ds-on-brand); border-right-color: transparent; animation: ds-spin .7s linear infinite; }
html:root .btn-ghost.is-busy::after { border-color: var(--ds-brand); border-right-color: transparent; }
@keyframes ds-spin { to { transform: rotate(360deg); } }

html:root .inp { background: var(--ds-surface); color: var(--ds-text); border: 1px solid var(--ds-border-strong); border-radius: 10px; min-height: 38px; }
html:root .inp:hover { border-color: var(--ds-text-3); }
html:root .inp:focus { border-color: var(--ds-focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ds-focus) 25%, transparent); }
html:root .lbl { color: var(--ds-text-2); }

html:root .badge { border-radius: 999px; padding: 2px 10px; font-size: var(--ds-fs-xs); font-weight: 600; line-height: 1.5; background: var(--ds-surface-2); color: var(--ds-text-2); }
.badge-ok { background: var(--ds-ok-soft) !important; color: var(--ds-ok) !important; }
.badge-warn { background: var(--ds-warn-soft) !important; color: var(--ds-warn) !important; }
.badge-danger { background: var(--ds-danger-soft) !important; color: var(--ds-danger) !important; }
.badge-info { background: var(--ds-info-soft) !important; color: var(--ds-info) !important; }
.badge-brand { background: var(--ds-brand-soft) !important; color: var(--ds-brand-text) !important; }
/* older tinted utility badges stay readable in both themes */
html:root .bg-emerald-50 { background: var(--ds-ok-soft); } html:root .text-emerald-700, html:root .text-emerald-600 { color: var(--ds-ok); }
html:root .bg-amber-50 { background: var(--ds-warn-soft); } html:root .text-amber-700, html:root .text-amber-600 { color: var(--ds-warn); }
html:root .bg-rose-50 { background: var(--ds-danger-soft); } html:root .text-rose-700, html:root .text-rose-600 { color: var(--ds-danger); }
html:root .bg-sky-50 { background: var(--ds-info-soft); } html:root .text-sky-700, html:root .text-sky-600 { color: var(--ds-info); }

kbd { font-family: var(--ds-mono); font-size: 11px; color: var(--ds-text-2); background: var(--ds-surface-2); border: 1px solid var(--ds-border); border-bottom-width: 2px; border-radius: 6px; padding: 0 6px; }

/* tables */
html:root table.rtable thead tr, html:root table thead tr.bg-slate-50\/70 { background: var(--ds-surface-2); }
html:root table.rtable thead th { color: var(--ds-text-3); font-weight: 600; letter-spacing: .04em; white-space: nowrap; }
html:root table.rtable tbody tr { transition: background-color var(--ds-t-fast) var(--ds-ease); }
html:root table.rtable tbody tr:hover { background: var(--ds-hover); }
html:root table.rtable td { vertical-align: middle; }
html[data-density="compact"] table td, html[data-density="compact"] table th { padding-top: 4px !important; padding-bottom: 4px !important; }
html[data-density="compact"] .card.p-5 { padding: 16px; }
html[data-density="compact"] .btn { min-height: 32px; padding: 4px 12px; }
html[data-density="compact"] .inp { min-height: 34px; padding-top: 4px; padding-bottom: 4px; }

/* sticky filters bar (add class ds-sticky to any filters container) */
.ds-sticky { border-bottom: 1px solid var(--ds-border); }
@media (min-width: 1024px) { .ds-sticky { position: sticky; top: 64px; z-index: var(--ds-z-sticky); background: color-mix(in srgb, var(--ds-surface) 94%, transparent); backdrop-filter: blur(8px); } }

/* page head, toolbar, pager, empty state, stat tile */
.ds-page-head { display: flex; flex-wrap: wrap; align-items: center; gap: var(--ds-s3); margin-bottom: var(--ds-s5); }
.ds-pager { display: flex; align-items: center; justify-content: space-between; gap: var(--ds-s3); padding: var(--ds-s3) var(--ds-s4); border-top: 1px solid var(--ds-border); font-size: var(--ds-fs-sm); color: var(--ds-text-2); }
.ds-empty { text-align: center; padding: var(--ds-s7) var(--ds-s5); color: var(--ds-text-2); }
.ds-stat { display: block; border: 1px solid var(--ds-border); border-radius: var(--ds-r); padding: var(--ds-s3) var(--ds-s4); background: var(--ds-surface); transition: border-color var(--ds-t-fast) var(--ds-ease), box-shadow var(--ds-t-fast) var(--ds-ease), transform var(--ds-t-fast) var(--ds-ease); }
a.ds-stat:hover { border-color: var(--ds-brand); box-shadow: var(--ds-shadow-2); transform: translateY(-1px); }
.ds-stat-label { display: flex; align-items: center; gap: 8px; font-size: var(--ds-fs-xs); font-weight: 600; color: var(--ds-text-3); }
.ds-stat-label svg { flex: none; }
.ds-stat-num { margin-top: 4px; font-size: var(--ds-fs-2xl); line-height: 1.15; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; color: var(--ds-text); }
.ds-stat-num.is-alert { color: var(--ds-danger); }
.ds-seg { display: inline-flex; border: 1px solid var(--ds-border-strong); border-radius: 10px; overflow: hidden; background: var(--ds-surface); }
.ds-seg > * { padding: 6px 12px; font-size: var(--ds-fs-sm); font-weight: 600; color: var(--ds-text-2); }
.ds-seg > [aria-pressed="true"], .ds-seg > .is-on { background: var(--ds-brand-soft); color: var(--ds-brand-text); }

html:root main:focus { outline: none; }
/* chart placeholders: shimmer until Chart.js sets inline canvas styles */
.ds-chartbox { border-radius: var(--ds-r-sm); background: linear-gradient(90deg, var(--ds-surface-2) 25%, var(--ds-hover) 37%, var(--ds-surface-2) 63%); background-size: 600px 100%; animation: ds-shimmer 1.3s infinite linear; }
.ds-chartbox:has(canvas[style*="display"]) { background: none; animation: none; }

/* skeletons */
@keyframes ds-shimmer { 0% { background-position: -300px 0; } 100% { background-position: 300px 0; } }
html:root .skel, .ds-skel { display: block; height: 14px; border-radius: 8px; background: linear-gradient(90deg, var(--ds-surface-2) 25%, var(--ds-hover) 37%, var(--ds-surface-2) 63%); background-size: 600px 100%; animation: ds-shimmer 1.3s infinite linear; }
.ds-skel-block { height: 96px; border-radius: var(--ds-r); }
[data-skel] { position: relative; }
html.ds-nav-busy main { opacity: .6; transition: opacity var(--ds-t) var(--ds-ease) 90ms; pointer-events: none; }

/* top progress bar (driven by shell.js) */
#ds-progress { position: fixed; inset: 0 0 auto 0; height: 2px; z-index: var(--ds-z-top); pointer-events: none; opacity: 0; transition: opacity var(--ds-t) var(--ds-ease); }
#ds-progress i { display: block; height: 100%; width: 100%; background: var(--ds-brand); transform: scaleX(0); transform-origin: left; transition: transform 400ms var(--ds-ease); }
#ds-progress.on { opacity: 1; }
[dir="rtl"] #ds-progress i { transform-origin: right; }

/* ---------- 5. Shell ---------- */
html:root aside.bg-ink-900 { background: var(--ds-side-bg); color: var(--ds-side-text); border-inline-end: 1px solid rgba(255,255,255,.05); }
html:root aside .text-slate-300 { color: var(--ds-side-text); }
html:root aside .text-slate-400, html:root aside .text-slate-500, html:root .drawer-panel .text-slate-400, html:root .drawer-panel .text-slate-500 { color: var(--ds-side-dim); }
html:root aside nav a, html:root .drawer-panel nav a { border-radius: 10px; min-height: 36px; transition: background-color var(--ds-t-fast) var(--ds-ease), color var(--ds-t-fast) var(--ds-ease); }
html:root aside nav a:hover, html:root .drawer-panel nav a:hover, html:root aside nav button:hover { background: var(--ds-side-hover); color: #fff; }
html:root aside nav a[aria-current="page"], html:root .drawer-panel nav a[aria-current="page"] { background: var(--ds-side-active); color: #fff; font-weight: 600; box-shadow: inset 0 0 0 1px rgba(255,255,255,.06); }
html:root aside nav a[aria-current="page"] svg { color: #ffc861; }
html:root aside :focus-visible, html:root .drawer-panel :focus-visible { outline-color: #c3c8ff; }
html:root aside nav [class*="uppercase"] { letter-spacing: .08em; }
html:root aside, html:root .drawer-panel { scrollbar-width: thin; scrollbar-color: rgba(255,255,255,.18) transparent; }
html:root header.sticky { background: color-mix(in srgb, var(--ds-surface) 88%, transparent); border-color: var(--ds-border); backdrop-filter: saturate(1.4) blur(10px); }
html:root header.sticky .btn-ghost { border-color: transparent; background: transparent; }
html:root header.sticky .btn-ghost:hover { background: var(--ds-hover); }
html:root header.sticky .btn-primary { border-color: transparent; }
html:root main { max-width: 1480px; }
@media (min-width: 1024px) { html:root main { padding: 28px 32px; } }
@media (max-width: 640px) { html:root main { padding: 16px; } }

/* modals and menus share one elevation + scrim */
html:root [role="dialog"].fixed.inset-0, html:root .fixed.inset-0.bg-black\/40, html:root .fixed.inset-0.bg-black\/50 { background: var(--ds-scrim); }
html:root .card.shadow-xl, html:root .card.shadow-lg, html:root .card.shadow-2xl { box-shadow: var(--ds-shadow-3); }
html:root [x-cloak] { display: none !important; }

/* cross-document view transition: the sidebar stays put and the page crossfades.
   Only the sidebar is named: a named element becomes a containing block for fixed descendants,
   and <main> and the top bar hold fixed modals and menus. */
@supports (view-transition-name: x) {
  @view-transition { navigation: auto; }
  aside.bg-ink-900 { view-transition-name: ds-side; }
  ::view-transition-group(ds-side) { animation-duration: 0s; }
  ::view-transition-old(root) { animation: ds-vt-out 80ms ease-out both; }
  ::view-transition-new(root) { animation: ds-vt-in 150ms var(--ds-ease) both; }
  @keyframes ds-vt-out { to { opacity: 0; } } @keyframes ds-vt-in { from { opacity: 0; } }
  main { animation: none; }
}
@media (prefers-reduced-motion: reduce) { ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; } }

/* ---------- 6. Print ---------- */
@media print { aside, header.sticky, #ds-progress { display: none !important; } main { padding: 0 !important; max-width: none !important; } }
