/* theme-variables.css — GENIE.AI Design System tokens
 *
 * Token hierarchy:
 *   1. :root defines all DS tokens (OKLch, light mode)
 *   2. [data-theme="dark"] overrides for dark mode
 *   3. @media (prefers-color-scheme: dark) for system preference
 *   4. Runtime JS (main.js) sets --brand from config; all accent
 *      variants are derived in CSS using relative OKLch syntax
 */

/* ================================================================
   LIGHT MODE — Default tokens
   ================================================================ */
:root {
  /* --- Brand (set by JS from config, fallback for dev) --- */
  --brand: oklch(47% 0.14 265);

  /* --- Core palette (OKLch) --- */
  --bg: var(--config-bg, oklch(98% 0.005 250));
  --surface: oklch(100% 0 0);
  --fg: var(--config-fg, oklch(22% 0.02 240));
  --muted: oklch(50% 0.018 240);
  --muted-soft: oklch(65% 0.01 240);
  --border: oklch(82% 0.012 240);
  --border-light: oklch(88% 0.008 240);

  /* --- Accent (derived from --brand) --- */
  --accent: var(--brand);
  --accent-hover: oklch(from var(--brand) calc(l - 0.04) c h);
  --accent-muted: oklch(from var(--brand) l c h / 0.12);
  --accent-fg: oklch(from var(--brand) min(calc(l + 0.56), 0.98) calc(c * 0.1) h);
  /* Same pairing, lifted past the 4.5:1 AA floor for small text
     (initials on the accent circle). */
  --accent-fg-strong: color-mix(in oklch, var(--accent-fg) 60%, white);
  /* One step above the panel ground — the answer cards read by fill,
     not only by shadow. The same relative lift works in both modes:
     each theme's surface starts where its own ramp does. */
  --surface-raised: oklch(from var(--surface) min(calc(l + 0.025), 0.995) c h);
  /* The message timestamps float over the wave band; plain muted ink
     drowns there, so they carry a stronger mix of the body ink. */
  --time-ink: color-mix(in oklch, var(--fg) 55%, var(--muted));
  --accent-secondary: oklch(from var(--brand) max(calc(l - 0.07), 0.05) c calc(h + 0));

  /* --- Navbar (light: a pale wash of the brand, overridable via config) ---
     The bar is a tinted plane, not a brand-coloured slab: its lightness
     and chroma are the light surface's, its hue is the deployment's, so a
     flavour keeps its own colour. The ink is the brand darkened, the same
     expression the dark block uses, which is what keeps the bar's controls
     readable at both ends of the theme. */
  --navbar-bg: oklch(from var(--brand) 0.94 0.04 h);
  --navbar-fg: oklch(from var(--brand) max(calc(l - 0.32), 0.1) c h);

  /* --- Semantic status --- */
  --success: #10b981;
  --warning: #f59e0b;
  /* The badge token cannot carry body text on a light ground — the ink is
     the token darkened, derived here so components never re-derive it. */
  --warning-ink: color-mix(in oklch, var(--warning), black 55%);
  --danger: #ef4444;

  /* --- Typography --- */
  --font-scale: 1;
  --font-display: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', system-ui, sans-serif;
  --font-body: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'IBM Plex Mono', ui-monospace, Menlo, monospace;

  /* --- Type scale (multiplied by --font-scale) --- */
  --text-xs: calc(0.7rem * var(--font-scale));
  --text-sm: calc(0.75rem * var(--font-scale));
  --text-base: calc(0.875rem * var(--font-scale));
  --text-md: calc(1rem * var(--font-scale));
  --text-lg: calc(1.25rem * var(--font-scale));
  --text-xl: calc(1.5rem * var(--font-scale));
  --text-2xl: calc(2rem * var(--font-scale));
  --text-3xl: calc(2.5rem * var(--font-scale));

  /* --- Spacing scale (4px base) --- */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;

  /* --- Breakpoints --- */
  --bp-sm: 480px;
  --bp-md: 768px;
  --bp-lg: 1024px;
  --bp-xl: 1280px;

  /* --- Border radius --- */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;

  /* --- Shadows (floating elements only) --- */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);

  /* --- Overlay --- */
  --overlay-bg: rgba(0, 0, 0, 0.5);

  /* --- Tooltip --- */
  --tooltip-bg: oklch(22% 0.02 240);
  --tooltip-fg: oklch(98% 0.005 250);

  /* --- Semantic badge backgrounds --- */
  --success-bg: oklch(72% 0.17 162 / 0.1);
  --warning-bg: oklch(80% 0.16 75 / 0.1);
  --danger-bg: oklch(62% 0.22 25 / 0.1);
  --info-bg: oklch(65% 0.15 240 / 0.1);

  /* --- Info (semantic) --- */
  --info: oklch(65% 0.15 240);

  /* --- Semantic surface tokens ---
     --bg-sidebar is the page surface, not a shade below it. It
     carried its own value (93 % lightness against --bg's 98 %),
     which put a visible tonal seam down the edge of every sidebar:
     measured light-theme rgb(228,232,237) against the centre's
     rgb(246,249,252), and oklch(0.16 …) against oklch(0.14 …) in
     dark. The layout already separates panels with a 1 px --border,
     so the third surface tone was doing nothing but cutting the app
     into pieces. It stays a distinct token — twelve rules across six
     components read it, and an alias is cheaper for them than
     --bg spelled out — but it resolves to --bg so every one of them
     moves together. */
  --bg-tertiary: oklch(91% 0.008 250);
  --bg-sidebar: var(--bg);

  /* --- Form elements --- */
  --slider-track: oklch(92% 0.005 250);
  --slider-thumb: var(--accent);
  --switch-track-off: oklch(88% 0.005 250);
  --switch-track-on: var(--accent);
  --switch-thumb: oklch(100% 0 0);

  /* --- Button tokens --- */
  --btn-secondary-bg: oklch(88% 0.005 250);
  --btn-secondary-hover: oklch(82% 0.008 250);
  --btn-secondary-fg: var(--fg);
}

/* ================================================================
   DARK MODE
   ================================================================ */
html[data-theme='dark'],
body[data-theme='dark'],
[data-theme='dark'] {
  /* --- Core palette (brand-tinted dark) --- */
  --bg: oklch(from var(--brand) 0.14 calc(c * 0.25) h);
  --surface: oklch(from var(--brand) 0.22 calc(c * 0.18) h);
  --fg: oklch(95% 0.005 250);
  --muted: oklch(from var(--brand) 0.58 calc(c * 0.15) h);
  --muted-soft: oklch(from var(--brand) 0.45 calc(c * 0.12) h);
  --border: oklch(from var(--brand) 0.3 calc(c * 0.2) h);
  --border-light: oklch(from var(--brand) 0.24 calc(c * 0.18) h);

  /* On a dark ground the badge token itself is the readable ink. */
  --warning-ink: var(--warning);

  /* --- Accent (lighter for dark bg, derived from --brand) --- */
  --accent: oklch(from var(--brand) calc(l + 0.2) calc(c * 0.85) h);
  --accent-hover: oklch(from var(--brand) calc(l + 0.14) calc(c * 0.9) h);
  --accent-muted: oklch(from var(--brand) calc(l + 0.2) c h / 0.15);
  --accent-fg: oklch(from var(--brand) max(calc(l - 0.32), 0.1) c h);
  --accent-fg-strong: var(--accent-fg);
  --surface-raised: oklch(from var(--surface) min(calc(l + 0.025), 0.995) c h);
  --time-ink: color-mix(in oklch, var(--fg) 65%, var(--muted));
  --accent-secondary: oklch(from var(--brand) max(calc(l + 0.1), 0.05) c calc(h + 0));

  /* --- Navbar (dark mode) ---
     The dark band carries the brand's hue at low lightness and low
     chroma — the same idea the light block uses at the pale end of
     the same range. Both keep the deployment's hue through
     `oklch(from var(--brand) ... h)`, and both reuse the same chroma
     slope (0.04-0.05) so the swap never turns a deep navy into a
     saturated blue. The ink stays the brand lightened — the light
     block's darkened expression mirrored — so the controls stay
     readable against the deep band without restating the chroma. */
  --navbar-bg: oklch(from var(--brand) 0.22 0.05 h);
  --navbar-fg: oklch(from var(--brand) min(calc(l + 0.56), 0.98) calc(c * 0.1) h);

  /* --- Semantic surfaces (brand-tinted) --- */
  --bg-tertiary: oklch(from var(--brand) 0.19 calc(c * 0.2) h);
  --bg-sidebar: var(--bg);

  /* --- Shadows (stronger for dark) --- */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.4);

  /* --- Overlay (stronger for dark) --- */
  --overlay-bg: rgba(0, 0, 0, 0.7);

  /* --- Tooltip (dark) --- */
  --tooltip-bg: oklch(85% 0.005 250);
  --tooltip-fg: oklch(22% 0.02 240);

  /* --- Semantic badge backgrounds (dark) --- */
  --success-bg: oklch(72% 0.17 162 / 0.15);
  --warning-bg: oklch(80% 0.16 75 / 0.15);
  --danger-bg: oklch(62% 0.22 25 / 0.15);
  --info-bg: oklch(65% 0.15 240 / 0.15);

  /* --- Info (semantic) --- */
  --info: oklch(70% 0.14 240);

  /* --- Form elements --- */
  --slider-track: oklch(from var(--brand) 0.25 calc(c * 0.15) h);
  --switch-track-off: oklch(from var(--brand) 0.3 calc(c * 0.15) h);

  /* --- Button tokens --- */
  --btn-secondary-bg: oklch(from var(--brand) 0.32 calc(c * 0.2) h);
  --btn-secondary-hover: oklch(from var(--brand) 0.38 calc(c * 0.22) h);
  --btn-secondary-fg: oklch(88% 0.005 250);
}

/* ================================================================
   SYSTEM PREFERENCE — dark when data-theme="system"
   ================================================================ */
@media (prefers-color-scheme: dark) {
  html[data-theme='system'],
  body[data-theme='system'],
  [data-theme='system'] {
    --bg: oklch(from var(--brand) 0.14 calc(c * 0.25) h);
    --surface: oklch(from var(--brand) 0.22 calc(c * 0.18) h);
    --fg: oklch(95% 0.005 250);
    --muted: oklch(from var(--brand) 0.58 calc(c * 0.15) h);
    --muted-soft: oklch(from var(--brand) 0.45 calc(c * 0.12) h);
    --border: oklch(from var(--brand) 0.3 calc(c * 0.2) h);
    --border-light: oklch(from var(--brand) 0.24 calc(c * 0.18) h);

    --accent: oklch(from var(--brand) calc(l + 0.2) calc(c * 0.85) h);
    --accent-hover: oklch(from var(--brand) calc(l + 0.14) calc(c * 0.9) h);
    --accent-muted: oklch(from var(--brand) calc(l + 0.2) c h / 0.15);
    --accent-fg: oklch(from var(--brand) max(calc(l - 0.32), 0.1) c h);
  --accent-fg-strong: var(--accent-fg);
  --surface-raised: oklch(from var(--surface) min(calc(l + 0.025), 0.995) c h);
  --time-ink: color-mix(in oklch, var(--fg) 65%, var(--muted));
    --accent-secondary: oklch(from var(--brand) max(calc(l + 0.1), 0.05) c calc(h + 0));

    --navbar-bg: var(--brand);
    --navbar-fg: oklch(from var(--brand) min(calc(l + 0.56), 0.98) calc(c * 0.1) h);

    --bg-tertiary: oklch(from var(--brand) 0.19 calc(c * 0.2) h);
    --bg-sidebar: var(--bg);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.4);

    --overlay-bg: rgba(0, 0, 0, 0.7);

    --tooltip-bg: oklch(85% 0.005 250);
    --tooltip-fg: oklch(22% 0.02 240);

    --success-bg: oklch(72% 0.17 162 / 0.15);
    --warning-bg: oklch(80% 0.16 75 / 0.15);
    --danger-bg: oklch(62% 0.22 25 / 0.15);
    --info-bg: oklch(65% 0.15 240 / 0.15);

    --info: oklch(70% 0.14 240);

    --slider-track: oklch(from var(--brand) 0.25 calc(c * 0.15) h);
    --switch-track-off: oklch(from var(--brand) 0.3 calc(c * 0.15) h);

    --btn-secondary-bg: oklch(from var(--brand) 0.32 calc(c * 0.2) h);
    --btn-secondary-hover: oklch(from var(--brand) 0.38 calc(c * 0.22) h);
    --btn-secondary-fg: oklch(85% 0.005 250);
  }
}
