/* ============================================================
   سرعة اليقين — Analytics
   Visual direction: chemiluminescence. The C5000 measures light
   emitted from a dark reaction well, so the dark theme is an
   instrument panel where meaning arrives as emitted light.
   The light theme keeps the same logic in reflected light: a
   clinical paper sheet where the same accents carry the signal.
   Only the palette changes between themes; every rule below
   reads from variables, never from hard-coded colour.
   ============================================================ */

:root, [data-theme="dark"] {
  --ink:        #080D16;
  --ground:     #0D1421;
  --surface:    #131C2C;
  --surface-2:  #18233696;
  --line:       #22304A;
  --line-soft:  #1A2438;

  --glow:       #3FE3C6;   /* emitted light — the accent */
  --glow-dim:   #1E6E63;
  --glow-soft:  rgba(63,227,198,.07);
  --amber:      #F2A93B;
  --amber-soft: rgba(242,169,59,.07);
  --amber-line: #5A4420;
  --red:        #FF6B6B;
  --red-soft:   rgba(255,107,107,.07);
  --red-line:   #4B2230;
  --violet:     #8B7BF0;

  --text:       #E7EEF9;
  --text-2:     #9DAFC8;
  --muted:      #67799A;

  --on-accent:  #04241E;
  --head-bg:    var(--ground);
  --total-bg:   var(--ground);
  --scheme:     dark;
  --shadow:     none;
  --head-text:  var(--text-2);
}

[data-theme="light"] {
  --ink:        #F4F6FA;
  --ground:     #FFFFFF;
  --surface:    #FFFFFF;
  --surface-2:  #F0F4FA;
  --line:       #D3DCE9;
  --line-soft:  #E4EAF3;

  --glow:       #0E9C86;
  --glow-dim:   #0E9C86;
  --glow-soft:  rgba(14,156,134,.10);
  --amber:      #B4700B;
  --amber-soft: rgba(180,112,11,.10);
  --amber-line: #E4C089;
  --red:        #C42B2B;
  --red-soft:   rgba(196,43,43,.09);
  --red-line:   #EBB4B4;
  --violet:     #5B4BC4;

  --text:       #10151F;
  --text-2:     #3F4C60;
  --muted:      #6B7A90;

  --on-accent:  #FFFFFF;
  --head-bg:    #1F3864;
  --total-bg:   #E8EEF7;
  --scheme:     light;
  --shadow:     0 1px 2px rgba(16,21,31,.06);
  --head-text:  #E8EEF7;
}

/* Tokens that do not change between themes. */
:root {
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;

  --ar: 'IBM Plex Sans Arabic', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--ink);
  color: var(--text);
  font-family: var(--ar);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

/* Numerals are data: always tabular, always latin glyphs. */
.num, td.num, th.num, .kpi-value, .mono {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  direction: ltr;
  unicode-bidi: embed;
}

button, input, select { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--glow); outline-offset: 2px; border-radius: 3px; }

/* ---------- signature: the assay strip ---------- */
.assay-strip {
  position: fixed; inset-block-start: 0; inset-inline: 0;
  height: 2px; z-index: 60;
  background: var(--line);
  transition: background 600ms ease, box-shadow 600ms ease;
}
.assay-strip.is-clean {
  background: linear-gradient(90deg, transparent, var(--glow) 20%, var(--glow) 80%, transparent);
  box-shadow: 0 0 14px 0 var(--glow);
}
.assay-strip.is-warn {
  background: linear-gradient(90deg, transparent, var(--amber) 20%, var(--amber) 80%, transparent);
  box-shadow: 0 0 12px 0 var(--amber);
}
.assay-strip.is-error {
  background: linear-gradient(90deg, transparent, var(--red) 15%, var(--red) 85%, transparent);
  box-shadow: 0 0 12px 0 var(--red);
}

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.rail {
  background: var(--ground);
  border-inline-start: 1px solid var(--line-soft);
  display: flex; flex-direction: column;
  padding: 20px 14px;
  position: sticky; top: 0; height: 100vh;
}

.brand { display: flex; align-items: center; gap: 11px; padding: 4px 6px 22px; }
.brand-mark { display: flex; gap: 3px; align-items: flex-end; height: 22px; }
.brand-mark span {
  width: 4px; border-radius: 2px; background: var(--glow-dim);
  animation: emit 2.6s ease-in-out infinite;
}
.brand-mark span:nth-child(1) { height: 10px; animation-delay: 0s; }
.brand-mark span:nth-child(2) { height: 20px; animation-delay: .35s; }
.brand-mark span:nth-child(3) { height: 14px; animation-delay: .7s; }
@keyframes emit {
  0%, 100% { background: var(--glow-dim); box-shadow: none; }
  50% { background: var(--glow); box-shadow: 0 0 8px var(--glow); }
}
@media (prefers-reduced-motion: reduce) { .brand-mark span { animation: none; } }

.brand-text strong { display: block; font-size: 15px; font-weight: 600; letter-spacing: -.2px; }
.brand-text small { display: block; color: var(--muted); font-size: 11.5px; }

#nav { display: flex; flex-direction: column; gap: 1px; overflow-y: auto; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  background: none; border: 0; color: var(--text-2);
  padding: 9px 11px; border-radius: var(--r-sm);
  text-align: start; font-size: 13.5px; width: 100%;
  transition: background 140ms, color 140ms;
}
.nav-item i { font-style: normal; color: var(--muted); font-size: 12px; width: 14px; }
.nav-item:hover { background: var(--surface); color: var(--text); }
.nav-item.is-active { background: var(--surface); color: var(--text); box-shadow: inset 2px 0 0 var(--glow); }
.nav-item.is-active i { color: var(--glow); }
.nav-sep {
  color: var(--muted); font-size: 10.5px; letter-spacing: .12em;
  padding: 18px 11px 7px; text-transform: uppercase;
}
.badge {
  margin-inline-start: auto; background: var(--red); color: #fff;
  border-radius: 20px; padding: 1px 7px; font-size: 11px; font-family: var(--mono);
}
.rail-foot { padding-top: 14px; border-top: 1px solid var(--line-soft); }

.main { min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 16px;
  padding: 14px 24px; border-bottom: 1px solid var(--line-soft);
  background: var(--ground); position: sticky; top: 0; z-index: 20; flex-wrap: wrap;
}
.range { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.range-modes { display: flex; background: var(--surface); border-radius: var(--r-sm); padding: 2px; }
.mode {
  background: none; border: 0; color: var(--text-2);
  padding: 6px 12px; border-radius: 5px; font-size: 12.5px;
}
.mode.is-active { background: var(--glow-dim); color: var(--on-accent); }
/* `display: flex` on a class beats the browser's default [hidden] rule, so an
   element with the hidden attribute stays visible. Restore the expected
   behaviour globally rather than per-component. */
[hidden] { display: none !important; }

.range-fields { display: flex; gap: 10px; align-items: center; }
.range-fields label { color: var(--muted); font-size: 12.5px; display: flex; gap: 6px; align-items: center; }
input[type=date], input[type=month], input[type=text], input[type=password], select {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-sm); padding: 6px 9px; font-size: 13px;
  color: var(--text);
  color-scheme: var(--scheme);
}
input::placeholder { color: var(--muted); opacity: 1; }

/* Browsers repaint autofilled fields with an opaque white background and their
   own text colour, which renders dark-theme text nearly invisible. The colour
   cannot be set directly, but a long inset shadow covers the background and
   -webkit-text-fill-color overrides the text. */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
  -webkit-text-fill-color: var(--text) !important;
  -webkit-box-shadow: 0 0 0 1000px var(--surface) inset !important;
  box-shadow: 0 0 0 1000px var(--surface) inset !important;
  caret-color: var(--text);
  transition: background-color 100000s ease-in-out 0s;
}
input:autofill {
  -webkit-text-fill-color: var(--text);
  box-shadow: 0 0 0 1000px var(--surface) inset;
}
.hint { color: var(--muted); font-size: 12.5px; font-family: var(--mono); }
.topbar-actions { display: flex; gap: 8px; }

.primary-btn {
  background: var(--glow); color: var(--on-accent); border: 0;
  border-radius: var(--r-sm); padding: 7px 15px; font-weight: 600; font-size: 13px;
  transition: filter 140ms;
}
.primary-btn:hover { filter: brightness(1.12); }
.ghost-btn {
  background: transparent; color: var(--text-2);
  border: 1px solid var(--line); border-radius: var(--r-sm);
  padding: 7px 14px; font-size: 13px; transition: border-color 140ms, color 140ms;
}
.ghost-btn:hover { border-color: var(--glow-dim); color: var(--text); }
.danger-btn { background: transparent; border: 1px solid var(--red-line); color: var(--red);
  border-radius: var(--r-sm); padding: 5px 11px; font-size: 12.5px; }

.view { padding: 22px 24px 60px; }
.loading { color: var(--muted); padding: 40px 0; text-align: center; }

.flash {
  margin: 14px 24px 0; padding: 11px 14px; border-radius: var(--r-md);
  border: 1px solid var(--line); background: var(--surface); font-size: 13.5px;
}

/* Data freshness. States when the underlying Root export was taken, so a
   figure is never read as "today" when it is three weeks old. */
.freshness {
  margin: 12px 24px 0; padding: 9px 14px; border-radius: var(--r-md);
  border: 1px solid var(--line-soft); background: var(--surface);
  font-size: 12.5px; color: var(--text-2);
  display: flex; align-items: center; gap: 9px; flex-wrap: wrap;
}
.freshness .dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--glow); flex: 0 0 auto;
}
.freshness.stale { border-color: var(--amber-line); background: var(--amber-soft); }
.freshness.stale .dot { background: var(--amber); }
.freshness.old { border-color: var(--red-line); background: var(--red-soft); }
.freshness.old .dot { background: var(--red); }
.freshness b { color: var(--text); font-weight: 600; }
.freshness .sep { color: var(--muted); }
@media (max-width: 900px) { .freshness, .flash { margin-inline: 12px; } }

#theme-toggle { font-size: 15px; line-height: 1; padding-inline: 11px; }
.flash.ok { border-color: var(--glow-dim); color: var(--glow); background: var(--glow-soft); }
.flash.err { border-color: var(--red-line); color: var(--red); background: var(--red-soft); }

/* ---------- headings ---------- */
.view-head { margin-bottom: 18px; }
.view-head h1 { font-size: 21px; font-weight: 600; margin: 0 0 3px; letter-spacing: -.3px; }
.view-head p { margin: 0; color: var(--muted); font-size: 13px; }

.section-title {
  font-size: 12px; letter-spacing: .1em; color: var(--muted);
  margin: 26px 0 10px; text-transform: uppercase;
}

/* ---------- KPI ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(178px, 1fr)); gap: 10px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line-soft);
  border-radius: var(--r-md); padding: 14px 15px;
  position: relative; overflow: hidden;
}
.kpi::after {
  content: ''; position: absolute; inset-block-start: 0; inset-inline: 0;
  height: 1px; background: var(--line);
}
.kpi.is-pos::after { background: linear-gradient(90deg, transparent, var(--glow)); }
.kpi.is-neg::after { background: linear-gradient(90deg, transparent, var(--red)); }
.kpi-label { color: var(--muted); font-size: 11.5px; margin-bottom: 6px; }
.kpi-value { font-size: 20px; font-weight: 600; letter-spacing: -.4px; }
.kpi-sub { color: var(--muted); font-size: 11px; margin-top: 3px; }
.kpi-value.pos { color: var(--glow); }
.kpi-value.neg { color: var(--red); }

/* ---------- cards & tables ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line-soft);
  border-radius: var(--r-md); padding: 16px; margin-bottom: 14px;
}
.card > h3 { margin: 0 0 12px; font-size: 14px; font-weight: 600; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 1080px) { .grid-2 { grid-template-columns: 1fr; } }

.table-wrap { overflow: auto; border: 1px solid var(--line-soft); border-radius: var(--r-md); }
table { width: 100%; border-collapse: collapse; font-size: 13px; background: var(--surface); }
thead th {
  background: var(--head-bg); color: var(--head-text, var(--text-2));
  font-weight: 500; font-size: 11.5px; letter-spacing: .02em;
  padding: 10px 11px; text-align: center; white-space: nowrap;
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2;
}
thead th:first-child { text-align: start; }
thead th.sortable { cursor: pointer; user-select: none; position: relative; }
thead th.sortable:hover { color: var(--glow); }
thead th.sortable::after {
  content: '⇅'; font-size: 9px; opacity: .35; margin-inline-start: 5px;
}
thead th.sorted[data-sort="asc"]::after { content: '▲'; opacity: 1; color: var(--glow); }
thead th.sorted[data-sort="desc"]::after { content: '▼'; opacity: 1; color: var(--glow); }

tbody td { padding: 9px 11px; text-align: center; border-bottom: 1px solid var(--line-soft); }
tbody td:first-child { text-align: start; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tfoot td {
  padding: 10px 11px; text-align: center; font-weight: 600;
  background: var(--total-bg); border-top: 1px solid var(--line);
}
tfoot td:first-child { text-align: start; }

/* value signalling */
.v-good { color: var(--glow); }
.v-warn { color: var(--amber); }
.v-bad  { color: var(--red); }
.v-none { color: var(--muted); font-style: italic; }

.pill {
  display: inline-block; padding: 2px 9px; border-radius: 20px;
  font-size: 11px; border: 1px solid var(--line);
}
.pill.good { color: var(--glow); border-color: var(--glow-dim); background: var(--glow-soft); }
.pill.warn { color: var(--amber); border-color: var(--amber-line); background: var(--amber-soft); }
.pill.bad  { color: var(--red); border-color: var(--red-line); background: var(--red-soft); }
.pill.mute { color: var(--muted); }

/* ---------- bar chart ---------- */
.bars { display: flex; align-items: flex-end; gap: 10px; height: 168px; padding-top: 10px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 0; }
.bar-stack { width: 100%; display: flex; flex-direction: column-reverse; justify-content: flex-start;
  height: 130px; align-items: stretch; }
.bar {
  width: 100%; border-radius: 3px 3px 0 0; min-height: 2px;
  background: var(--glow-dim); transition: filter 140ms;
}
.bar.b-sales { background: linear-gradient(180deg, var(--glow), var(--glow-dim)); }
.bar.b-exp { background: var(--amber); opacity: .8; }
.bar-col:hover .bar { filter: brightness(1.25); }
.bar-label { font-size: 10.5px; color: var(--muted); white-space: nowrap; }
.legend { display: flex; gap: 14px; font-size: 11.5px; color: var(--muted); margin-top: 10px; }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-inline-end: 5px; }

/* ---------- meters ---------- */
.meter { height: 6px; background: var(--line-soft); border-radius: 4px; overflow: hidden; min-width: 70px; }
.meter > span { display: block; height: 100%; border-radius: 4px; background: var(--glow); }
.meter.warn > span { background: var(--amber); }
.meter.bad > span { background: var(--red); }

/* ---------- issues ---------- */
.issue {
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: start;
  padding: 12px 14px; border: 1px solid var(--line-soft);
  border-radius: var(--r-md); margin-bottom: 8px; background: var(--surface);
}
.issue.sev-error { border-inline-start: 3px solid var(--red); }
.issue.sev-warning { border-inline-start: 3px solid var(--amber); }
.issue.sev-info { border-inline-start: 3px solid var(--glow-dim); }
.issue code { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.issue p { margin: 4px 0 0; font-size: 13px; color: var(--text-2); line-height: 1.6; }

/* ---------- dropzone ---------- */
.drop {
  border: 1.5px dashed var(--line); border-radius: var(--r-lg);
  padding: 34px 20px; text-align: center; color: var(--text-2);
  background: var(--surface); transition: border-color 160ms, background 160ms;
}
.drop.is-over { border-color: var(--glow); background: var(--glow-soft); }
.drop strong { display: block; font-size: 15px; color: var(--text); margin-bottom: 5px; }
.drop small { color: var(--muted); }

.file-row {
  display: flex; align-items: center; gap: 12px; justify-content: space-between;
  padding: 10px 13px; border: 1px solid var(--line-soft); border-radius: var(--r-sm);
  margin-bottom: 6px; font-size: 13px; background: var(--surface);
}

/* ---------- forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 11px; }
.form-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.form-grid input, .form-grid select { width: 100%; }
.row-actions { display: flex; gap: 6px; justify-content: center; }

.empty { color: var(--muted); text-align: center; padding: 30px; font-size: 13.5px; }

/* ============================================================
   Responsive
   Desktop is the primary target, but the phone must be usable:
   the rail becomes an off-canvas drawer, tables scroll inside
   their own frame, and controls grow to touch size.
   ============================================================ */

.rail-toggle { display: none; }
.scrim { display: none; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }

  .rail-toggle {
    display: inline-flex; align-items: center; justify-content: center;
    width: 38px; height: 38px; flex: 0 0 auto;
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--r-sm); color: var(--text); font-size: 17px; line-height: 1;
  }

  /*
   * Off-canvas drawer.
   * Physical `right`/`translateX` are used deliberately. Logical properties
   * flip under dir="rtl": `inset-inline-end` resolves to the LEFT edge, while
   * `translateX(100%)` still moves right — so the pair pushed the drawer INTO
   * the viewport instead of out of it.
   */
  .rail {
    position: fixed; top: 0; bottom: 0; right: 0; left: auto;
    width: min(80vw, 260px); z-index: 50; height: 100dvh;
    transform: translateX(100%);
    transition: transform 220ms ease;
    border-left: 1px solid var(--line);
    border-right: 0;
    box-shadow: -8px 0 24px rgba(0,0,0,.35);
    overflow-y: auto;
  }
  body.nav-open .rail { transform: translateX(0); }

  .scrim {
    display: block; position: fixed; inset: 0; z-index: 40;
    background: rgba(0,0,0,.5); opacity: 0; pointer-events: none;
    transition: opacity 220ms ease;
  }
  body.nav-open .scrim { opacity: 1; pointer-events: auto; }

  .topbar { padding: 10px 12px; gap: 10px; align-items: stretch; }
  .topbar-row { display: flex; align-items: center; gap: 10px; width: 100%; }
  .range { width: 100%; gap: 8px; }
  .range-modes { width: 100%; }
  .mode { flex: 1; padding: 9px 6px; font-size: 12px; }
  .range-fields { width: 100%; flex-wrap: wrap; }
  .range-fields label { flex: 1 1 46%; }
  .range-fields input { width: 100%; }
  #apply-range { flex: 1 1 100%; padding: 10px; }
  .topbar-actions { width: 100%; }
  .topbar-actions button { flex: 1; padding: 10px 8px; }

  .view { padding: 14px 12px 60px; }
  .view-head h1 { font-size: 18px; }

  .kpis { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
  .kpi { padding: 12px; }
  .kpi-value { font-size: 17px; }

  /* Horizontal scroll is the honest answer for wide tables on a phone:
     shrinking columns to fit would make the numbers unreadable. */
  .table-wrap { max-width: 100%; -webkit-overflow-scrolling: touch; }
  table { font-size: 12px; }
  thead th, tbody td { padding: 8px 9px; }
  tbody td:first-child { position: sticky; inset-inline-start: 0; background: var(--surface); z-index: 1; }
  thead th:first-child { position: sticky; inset-inline-start: 0; z-index: 3; }

  .grid-2 { grid-template-columns: 1fr; }
  .bars { height: 150px; gap: 5px; }
  .bar-label { font-size: 9px; }
  .form-grid { grid-template-columns: 1fr 1fr; }
  .issue { grid-template-columns: auto 1fr; }
  .issue > button { grid-column: 2; justify-self: start; }
  .file-row { flex-wrap: wrap; }
}

@media (max-width: 460px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .range-fields label { flex: 1 1 100%; }
}

/* Touch devices: larger hit targets regardless of screen width. */
@media (hover: none) {
  .nav-item { padding: 12px 11px; }
  .ghost-btn, .primary-btn, .danger-btn { padding: 9px 14px; }
  input, select { padding: 9px 10px; font-size: 16px; } /* 16px prevents iOS zoom-on-focus */
}

@media print {
  .rail, .topbar, .scrim, .assay-strip { display: none !important; }
  .shell { grid-template-columns: 1fr; }
  body { background: #fff; color: #000; }
}

/* Sortable column headers. The affordance is deliberately quiet: a caret that
   only appears on hover or when the column is the active sort key. */
thead th.sortable { cursor: pointer; user-select: none; position: relative; }
thead th.sortable::after {
  content: '⇅'; font-size: 9px; opacity: 0; margin-inline-start: 5px;
  transition: opacity 120ms;
}
thead th.sortable:hover::after { opacity: .5; }
thead th.sortable[data-sort="desc"]::after { content: '▼'; opacity: 1; }
thead th.sortable[data-sort="asc"]::after { content: '▲'; opacity: 1; }
thead th.is-sorted { color: var(--glow); }
