/* =============================================================================
   Walform design tokens — the single source of truth for BOTH themes.

   Load order:  sf-pro.css → (bootstrap) → app.css → tokens.css → wf-<module>.css → walform.css
   tokens.css comes after app.css on purpose: its selectors (:root and
   :root[data-theme="dark"]) out-rank the older html[data-theme="dark"] blocks
   in app.css, so every colour in the product derives from the values below.

   Theme is chosen in this order: saved choice (localStorage "lk.theme") →
   operating-system setting (prefers-color-scheme) → light.  See js/theme.js.

   Contrast (WCAG 2.2 AA) — measured with scripts in /tools/contrast-check.py:
     body text on canvas / surface / surface-2 / surface-3 ≥ 4.5:1 in both modes
     accent as text on every surface ≥ 4.5:1;  --on-accent on --accent ≥ 4.5:1
     input borders / focus ring (non-text) ≥ 3:1
   ========================================================================== */

/* ---------- LIGHT (default) ---------- */
:root {
  color-scheme: light;

  /* surfaces */
  --canvas:     #F3F5F7;
  --surface:    #FFFFFF;
  --surface-2:  #F8FAFB;
  --surface-3:  #E8ECEF;
  --bs-body-bg: var(--canvas);

  /* text */
  --text:   #0F1B26;
  --text-2: #2E3C48;
  --muted:  #55626E;
  --faint:  #55626E;           /* kept for older rules; text must never go lighter than --muted */
  --ink: var(--text);  --ink-2: var(--text);  --strong: var(--text);
  --bs-body-color: var(--text);
  --bs-secondary-color: var(--muted);

  /* lines */
  --line:         rgba(15,27,38,.14);
  --line-2:       rgba(15,27,38,.08);
  --line-strong:  #7A8793;     /* 3.6:1 on white — form-control borders */
  --input-border: #7A8793;
  --bs-border-color: rgba(15,27,38,.14);

  /* accent: blue, one family for both themes */
  --accent:        #1E5A80;    /* fills (buttons). white on it 7.4:1 */
  --accent-hover:  #17496A;
  --accent-press:  #123B55;
  --accent-hi:     #3F7FA6;
  --accent-text:   #1E5A80;    /* links / icons on surfaces, ≥4.6:1 on all four */
  --accent-deep:   #17496A;
  --accent-ink:    #123B55;
  --accent-soft:   #E6EFF5;
  --accent-line:   #B2CCDC;
  --on-accent:     #FFFFFF;
  --bs-primary:        var(--accent);
  --bs-primary-rgb:    30,90,128;
  --bs-link-color:     var(--accent-text);
  --bs-link-hover-color: var(--accent-hover);
  --brand-50:  #E6EFF5;  --brand-100: #D2E2EC;  --brand-200: #B2CCDC;
  --brand-500: var(--accent);  --brand-600: var(--accent);
  --brand-700: var(--accent-hover);  --brand-800: var(--accent-press);  --brand-900: var(--text);
  --cta: var(--accent);  --cta-hi: var(--accent-hover);  --cta-deep: var(--accent-press);  --cta-edge: var(--accent);
  --on-cta: var(--on-accent);  --on-lime: var(--on-accent);

  /* status — text colours ≥ 4.5:1 on their own tinted background and on surfaces */
  --ok:   #1E7A34;   --ok-base:   #34C759;  --ok-bg:   #E6F6EA;  --ok-line:   #A9E4B9;  --ok-ink:   var(--text);
  --warn: #8F4A00;   --warn-fill: #FFF1DB;  --warn-bg: #FFF1DB;  --warn-line: #FFD08A;  --warn-ink: var(--text);
  --bad:  #C4111F;   --bad-solid: #C4111F;  --bad-base:  #FF3B30;  --bad-bg:  #FFE9E7;  --bad-line:  #FFBDB8;  --bad-ink:  var(--text);
  --info: var(--accent-text);                --info-bg: #E6EFF5;  --info-line: #B2CCDC;  --info-ink: var(--text);
  --ic-ink: var(--accent-text);  --ic-ink-bg: #E6EFF5;
  --ic-ok:  var(--ok);   --ic-ok-bg:   var(--ok-bg);
  --ic-warn:var(--warn); --ic-warn-bg: var(--warn-bg);
  --ic-bad: var(--bad);  --ic-bad-bg:  var(--bad-bg);
  --ic-neutral: var(--muted);  --ic-neutral-bg: #E8ECEF;

  /* KPI / dashboard */
  --kpi-card: var(--surface);  --kpi-mix: var(--surface);  --kpi-line: var(--line);
  --kpi-label: var(--text);    --kpi-cap: var(--muted);
  --kpi-up: var(--ok);  --kpi-down: var(--bad);
  --kpi-1: var(--accent-text);  --kpi-2: var(--ok);  --kpi-3: var(--bad);  --kpi-4: var(--warn);  --kpi-5: var(--muted);

  /* elevation */
  --sh-xs: 0 1px 2px rgba(15,27,38,.06);
  --sh-sm: 0 1px 3px rgba(15,27,38,.10), 0 1px 2px rgba(15,27,38,.06);
  --sh-md: 0 12px 28px -12px rgba(15,27,38,.22), 0 3px 8px -3px rgba(15,27,38,.10);
  --sh-lg: 0 30px 60px -20px rgba(15,27,38,.30);

  /* focus (3:1 against every surface) */
  --focus: #1E5A80;
  --ring:  0 0 0 3px rgba(30,90,128,.35);

  /* misc component tokens that older layers hard-coded */
  --tb-panel: var(--surface);  --tb-acc: var(--accent-text);
  --uk-blue: var(--accent);  --uk-blue-hi: var(--accent-hover);  --uk-blue-lo: var(--accent-press);
  --icon-blue: var(--accent);
  --sg-text: var(--muted);

  /* browser UI colour (also read by theme.js) */
  --theme-color: #F3F5F7;
}

/* ---------- DARK ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;

  /* soft charcoal ramp, not pure black: each step is visibly distinct, nothing glares */
  --canvas:     #0C141B;
  --surface:    #121C25;
  --surface-2:  #18252F;
  --surface-3:  #20303C;
  --bs-body-bg: var(--canvas);

  --text:   #EAF0F4;
  --text-2: #CAD6DF;
  --muted:  #9AABB8;
  --faint:  #9AABB8;
  --ink: var(--text);  --ink-2: var(--text);  --strong: var(--text);
  --bs-body-color: var(--text);
  --bs-secondary-color: var(--muted);

  --line:         rgba(234,240,244,.12);
  --line-2:       rgba(234,240,244,.07);
  --line-strong:  #6F8190;     /* 3.9:1 on --surface */
  --input-border: #6F8190;
  --bs-border-color: rgba(234,240,244,.12);

  --accent:        #2B6A94;    /* white on it 5.8:1 */
  --accent-hover:  #245B7F;
  --accent-press:  #1D4C6B;
  --accent-hi:     #4A8DB8;
  --accent-text:   #86BFE3;    /* ≥6.7:1 on all four surfaces */
  --accent-deep:   #B6D8EE;
  --accent-ink:    #EAF0F4;
  --accent-soft:   rgba(74,141,184,.18);
  --accent-line:   rgba(134,191,227,.45);
  --on-accent:     #FFFFFF;
  --bs-primary-rgb:    43,106,148;
  --bs-link-color:     var(--accent-text);
  --bs-link-hover-color: var(--accent-deep);
  --brand-50:  #10283A;  --brand-100: #173A52;  --brand-200: #235273;
  --brand-600: var(--accent-text);
  --brand-700: var(--text);  --brand-800: var(--text);  --brand-900: var(--text);

  --ok:   #4CD27A;   --ok-base:   #30D158;  --ok-bg:   rgba(48,209,88,.14);   --ok-line:   rgba(48,209,88,.45);
  --warn: #FFB04A;   --warn-fill: rgba(255,159,10,.14);  --warn-bg: rgba(255,159,10,.14);  --warn-line: rgba(255,159,10,.50);
  --bad:  #FF7A70;   --bad-solid: #D92D20;  --bad-base:  #FF453A;  --bad-bg:  rgba(255,69,58,.15);   --bad-line:  rgba(255,69,58,.50);
  --info-bg: rgba(74,141,184,.18);  --info-line: rgba(134,191,227,.45);
  --ic-ink-bg: rgba(74,141,184,.18);
  --ic-neutral-bg: rgba(154,171,184,.16);

  --sh-xs: 0 1px 2px rgba(0,0,0,.40);
  --sh-sm: 0 1px 3px rgba(0,0,0,.50);
  --sh-md: 0 12px 28px -10px rgba(0,0,0,.65), 0 3px 8px -3px rgba(0,0,0,.45);
  --sh-lg: 0 30px 60px -16px rgba(0,0,0,.75);

  --focus: #86BFE3;
  --ring:  0 0 0 3px rgba(134,191,227,.45);

  --theme-color: #0C141B;
}

/* No-JS / pre-script fallback: follow the OS until data-theme is set */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --canvas:#0C141B; --surface:#121C25; --surface-2:#18252F; --surface-3:#20303C;
    --text:#EAF0F4; --text-2:#CAD6DF; --muted:#9AABB8; --faint:#9AABB8;
    --line:rgba(234,240,244,.12); --line-strong:#6F8190;
    --accent:#2B6A94; --accent-text:#86BFE3; --on-accent:#FFFFFF;
  }
}

/* ---------- Component surfaces that older layers hard-coded as near-black ---------- */
html[data-theme="dark"] body .lk-sidebar {
  --sb-bg: var(--surface);
  --sb-text: var(--text);  --sb-strong: var(--text);  --sb-muted: var(--muted);
  --sb-line: var(--line);  --sb-card-line: var(--line);
  --sb-marker: var(--accent-text);  --sb-focus: var(--accent-text);
}
html:not([data-theme="dark"]) body .lk-sidebar { --sb-muted: var(--muted); }

/* ---------- Form controls & native widgets follow the theme ---------- */
:root { accent-color: var(--accent); }
::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); }
:where(input, select, textarea):not([type="checkbox"]):not([type="radio"]):not([type="range"]) {
  border-color: var(--input-border);
}
:where(input, textarea)::placeholder { color: var(--muted); opacity: 1; }
:where(a, button, input, select, textarea, [tabindex]):focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
:where(input, select, textarea):disabled { color: var(--muted); }

/* Solid status chips: white text must reach 4.5:1 */
.tr-badge, .badge.bg-danger { background-color: var(--bad-solid) !important; color: #fff !important; }

/* Dark top bar / preview well: use the soft canvas, not pure black */
html[data-theme="dark"] body .lk-topbar { background: color-mix(in srgb, var(--canvas) 84%, transparent); border-bottom-color: var(--line); }
html[data-theme="dark"] #lkPreviewModal .lkpv-body { background: var(--canvas); }
