/* ==========================================================================
   MERLIN INTELLIGENCE v3 — DESIGN TOKENS
   "Instrument grade, editorial."

   Rules (enforced by review + static/app/_dev/contrast.mjs):
   - Components consume ONLY the semantic tokens below. No raw hex in kit,
     shell or view CSS. If you need a colour that is not here, you are
     probably encoding meaning that should be a token: ask in _handoff/.
   - Every text token passes WCAG AA (4.5:1) on every surface it may sit on,
     in every theme. Run `node static/app/_dev/contrast.mjs` after edits.
   - Cyan (--live) means LIVE / data-link only. Never decoration.
   - Gold (--accent) is the single brand accent: primary action, active
     navigation, editorial rules. Use it sparingly; it is loud.
   - Status colours never carry meaning alone: pair with an icon or a word.

   Themes: Carrier (dark, default) · Arctic (light) · Night (OLED black).
   "Auto" is resolved by core/theme.js to carrier|arctic and written to
   <html data-theme>. No attribute = Carrier.
   ========================================================================== */

@layer tokens {
  :root {
    color-scheme: dark;

    /* ---- Type families -------------------------------------------------- */
    --font-serif: "Source Serif 4", "Source Serif Pro", Georgia, Cambria, "Times New Roman", serif;
    --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

    /* ---- Type ramp: 8 steps. 1-5 fixed (density surfaces must not drift),
            6-8 fluid between 390px and 1440px viewports. ------------------- */
    --fs-1: 0.6875rem;  /* 11px  uppercase micro-labels ONLY, with --track-micro */
    --fs-2: 0.75rem;    /* 12px  captions, meta, axis ticks (hard floor for mixed case) */
    --fs-3: 0.8125rem;  /* 13px  dense data rows, table cells, chips */
    --fs-4: 0.875rem;   /* 14px  default UI text */
    --fs-5: 1rem;       /* 16px  lead UI text, small prose */
    --fs-6: clamp(1.125rem, 1.0786rem + 0.1905vw, 1.25rem);   /* 18-20  card titles (serif), prose */
    --fs-7: clamp(1.375rem, 1.2357rem + 0.5714vw, 1.75rem);   /* 22-28  section heads, KPI numerals */
    --fs-8: clamp(1.875rem, 1.457rem + 1.7143vw, 3rem);       /* 30-48  view titles, hero numerals */

    --lh-tight: 1.12;   /* display + numerals */
    --lh-snug: 1.3;     /* headings, single-line chrome */
    --lh-ui: 1.45;      /* UI text */
    --lh-prose: 1.62;   /* serif reading text */

    --track-micro: 0.08em;   /* uppercase 11px labels */
    --track-tight: -0.012em; /* serif display */
    --track-num: -0.01em;    /* mono numerals at display size */

    --fw-regular: 400;
    --fw-medium: 500;
    --fw-semibold: 600;

    /* ---- Spacing (4px base; Tailwind-style index = n x 4px) ------------- */
    --s-px: 1px;
    --s-0_5: 0.125rem;  /* 2 */
    --s-1: 0.25rem;     /* 4 */
    --s-1_5: 0.375rem;  /* 6 */
    --s-2: 0.5rem;      /* 8 */
    --s-2_5: 0.625rem;  /* 10 */
    --s-3: 0.75rem;     /* 12 */
    --s-4: 1rem;        /* 16 */
    --s-5: 1.25rem;     /* 20 */
    --s-6: 1.5rem;      /* 24 */
    --s-8: 2rem;        /* 32 */
    --s-10: 2.5rem;     /* 40 */
    --s-12: 3rem;       /* 48 */
    --s-16: 4rem;       /* 64 */
    --s-20: 5rem;       /* 80 */

    /* Component-intent aliases (density mode retargets these, not 40 rules) */
    --pad-card: var(--s-5);
    --pad-cell-y: var(--s-2);
    --pad-cell-x: var(--s-3);
    --gap-grid: var(--s-6);
    --gap-stack: var(--s-4);
    --control-h: 2.25rem;      /* 36  default control */
    --control-h-sm: 1.75rem;   /* 28  dense control (desktop only) */
    --control-h-lg: 2.75rem;   /* 44  touch / primary CTA */
    --hit: 2.75rem;            /* 44  minimum touch target */

    /* ---- Radii ---------------------------------------------------------- */
    --r-xs: 2px;     /* pips, ticks, bars */
    --r-sm: 4px;     /* chips, inputs, buttons */
    --r-md: 6px;     /* cards, panels */
    --r-lg: 10px;    /* popovers, menus, dialogs, palette */
    --r-xl: 14px;    /* sheet top corners */
    --r-pill: 999px;

    /* ---- Layout --------------------------------------------------------- */
    --content-max: 1680px;
    --prose-max: 68ch;
    --gutter: var(--s-6);
    --rail-w: 72px;
    --rail-w-open: 232px;
    --strip-h: 52px;
    --tabbar-h: 60px;
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-t: env(safe-area-inset-top, 0px);

    /* ---- Motion (150 / 220 / 320 scale; nothing interactive > 320 except sheet) */
    --dur-0: 70ms;    /* hover-in, press */
    --dur-1: 150ms;   /* tooltips, popovers, toggles */
    --dur-2: 220ms;   /* panels, drawers, tab content */
    --dur-3: 320ms;   /* view transitions, large panes */
    --dur-sheet: 460ms;
    --dur-flash: 600ms;   /* number-change flash */
    --dur-pulse: 1600ms;  /* the single LIVE pip */

    --ease-out: cubic-bezier(0.215, 0.61, 0.355, 1);
    --ease-out-strong: cubic-bezier(0.16, 1, 0.3, 1);
    --ease-in-out: cubic-bezier(0.45, 0, 0.25, 1);
    --ease-exit: cubic-bezier(0.4, 0, 1, 1);
    --ease-sheet: cubic-bezier(0.32, 0.72, 0, 1);
    --ease-spring: cubic-bezier(0.22, 1, 0.36, 1);   /* fallback for linear() spring */

    /* ---- Z-index scale (never invent numbers) ---------------------------- */
    --z-base: 0;
    --z-raised: 10;
    --z-sticky: 100;     /* status strip, sticky table heads */
    --z-rail: 200;
    --z-tabbar: 210;
    --z-popover: 400;    /* tooltip, popover, menu, select list */
    --z-drawer: 500;     /* right inspector drawer */
    --z-sheet: 600;      /* mobile bottom sheet */
    --z-dialog: 700;
    --z-palette: 800;
    --z-toast: 900;
    --z-skip: 1000;

    /* ---- Focus ---------------------------------------------------------- */
    --focus-w: 2px;
    --focus-offset: 2px;
  }

  @supports (transition-timing-function: linear(0, 1)) {
    :root {
      /* ~7% overshoot spring (open-props spring-2 class). Sheets + palette only. */
      --ease-spring: linear(0, 0.007, 0.029 2.2%, 0.118 4.7%, 0.625 14.4%, 0.826 19%,
        0.902, 0.962, 1.008 26.1%, 1.041 28.7%, 1.064 32.1%, 1.07 36%, 1.061 40.5%,
        1.015 53.4%, 0.999 61.6%, 0.995 71.2%, 1);
    }
  }

  /* ========================================================================
     CARRIER — dark graphite (default)
     ======================================================================== */
  :root,
  :root[data-theme="carrier"] {
    color-scheme: dark;

    --bg: #0a0c10;              /* app canvas */
    --surface-1: #0f1217;       /* cards, panels, rail */
    --surface-2: #151920;       /* raised rows, inputs, card headers, hover */
    --surface-3: #1b2029;       /* floating: popover, menu, sheet, dialog */
    --surface-sunken: #07090c;  /* wells, chart plots, code */

    --line-1: rgba(255, 255, 255, 0.07);   /* hairline: card borders, dividers */
    --line-2: rgba(255, 255, 255, 0.115);  /* control borders, emphasised dividers */
    --line-3: rgba(255, 255, 255, 0.2);    /* hover borders, active outlines */

    --ink-1: #edf0f4;   /* titles, values, primary text */
    --ink-2: #bec5cf;   /* body text */
    --ink-3: #959dab;   /* secondary labels, meta */
    --ink-4: #7f8896;   /* tertiary: timestamps, axis ticks (still AA) */

    --accent: #f3b84a;                       /* gold: text + icons */
    --accent-fill: #f5a524;                  /* gold: solid fills (primary button) */
    --accent-fill-hover: #ffb73d;
    --accent-fill-edge: #f5a524;             /* button edge; >= 3:1 vs surfaces (matters in Arctic) */
    --accent-ink: #171000;                   /* text on --accent-fill */
    --accent-soft: rgba(245, 165, 36, 0.12); /* tinted backgrounds */
    --accent-line: rgba(245, 165, 36, 0.45);

    --live: #54e0e4;                         /* LIVE / data-link only */
    --live-soft: rgba(84, 224, 228, 0.12);
    --live-line: rgba(84, 224, 228, 0.42);

    --up: #4fd394;
    --up-soft: rgba(79, 211, 148, 0.12);
    --down: #ff7a7a;
    --down-soft: rgba(255, 122, 122, 0.12);
    --warn: #ff9d4d;
    --warn-soft: rgba(255, 157, 77, 0.12);
    --model: #cda4ff;                        /* MODEL values (scenario, never forecast) */
    --model-soft: rgba(205, 164, 255, 0.12);

    --focus: #f3b84a;
    --selection: rgba(245, 165, 36, 0.3);
    --scrim: rgba(3, 4, 6, 0.66);

    /* chart marks (>= 3:1 vs surface-1 for non-text graphics) */
    --chart-grid: rgba(255, 255, 255, 0.06);
    --chart-axis: rgba(255, 255, 255, 0.16);
    --cat-1: #f3b84a;  /* financing */
    --cat-2: #6fb6ff;  /* government */
    --cat-3: #cda4ff;  /* listing / index */
    --cat-4: #4fd394;  /* certification */
    --cat-5: #ff9d7a;  /* facilities */
    --cat-6: #9fb0c4;  /* earnings / other */

    --heat-0: #151920;
    --heat-1: #173a3d;
    --heat-2: #1f5c60;
    --heat-3: #2f8d92;
    --heat-4: #54e0e4;

    --shadow-color: 220deg 40% 2%;
    --rim: inset 0 1px 0 rgba(255, 255, 255, 0.045);
    --shadow-1: 0 1px 2px hsl(var(--shadow-color) / 0.35);
    --shadow-2: 0 1px 2px hsl(var(--shadow-color) / 0.4), 0 4px 10px -2px hsl(var(--shadow-color) / 0.45),
      0 12px 28px -8px hsl(var(--shadow-color) / 0.55);
    --shadow-3: 0 2px 4px hsl(var(--shadow-color) / 0.4), 0 12px 24px -6px hsl(var(--shadow-color) / 0.55),
      0 32px 72px -16px hsl(var(--shadow-color) / 0.7);

    --skeleton: rgba(255, 255, 255, 0.055);
    --skeleton-hi: rgba(255, 255, 255, 0.09);
  }

  /* ========================================================================
     ARCTIC — light, AA-tuned (paper + graphite ink)
     ======================================================================== */
  :root[data-theme="arctic"] {
    color-scheme: light;

    --bg: #f2f3f5;
    --surface-1: #ffffff;
    --surface-2: #f6f7f9;
    --surface-3: #ffffff;
    --surface-sunken: #eceef1;

    --line-1: rgba(14, 18, 26, 0.085);
    --line-2: rgba(14, 18, 26, 0.14);
    --line-3: rgba(14, 18, 26, 0.24);

    --ink-1: #0d1117;
    --ink-2: #353c47;
    --ink-3: #525a66;
    --ink-4: #636c79;

    --accent: #8a5300;
    --accent-fill: #f5a524;
    --accent-fill-hover: #f0981a;
    --accent-fill-edge: #a86a00;
    --accent-ink: #1a1000;
    --accent-soft: rgba(245, 165, 36, 0.16);
    --accent-line: rgba(176, 108, 0, 0.5);

    --live: #00717a;
    --live-soft: rgba(0, 150, 160, 0.1);
    --live-line: rgba(0, 113, 122, 0.45);

    --up: #0b7141;
    --up-soft: rgba(11, 113, 65, 0.09);
    --down: #bd2229;
    --down-soft: rgba(189, 34, 41, 0.08);
    --warn: #9c4a00;
    --warn-soft: rgba(214, 110, 0, 0.11);
    --model: #7a35ad;
    --model-soft: rgba(122, 53, 173, 0.08);

    --focus: #8a5300;
    --selection: rgba(245, 165, 36, 0.32);
    --scrim: rgba(13, 17, 23, 0.4);

    --chart-grid: rgba(14, 18, 26, 0.07);
    --chart-axis: rgba(14, 18, 26, 0.2);
    --cat-1: #b06c00;
    --cat-2: #1f63c4;
    --cat-3: #7a35ad;
    --cat-4: #0b7a47;
    --cat-5: #c0471c;
    --cat-6: #56657a;

    --heat-0: #eceef1;
    --heat-1: #c6e9eb;
    --heat-2: #86cdd1;
    --heat-3: #2fa3aa;
    --heat-4: #00717a;

    --shadow-color: 220deg 25% 40%;
    --rim: inset 0 1px 0 rgba(255, 255, 255, 0.8);
    --shadow-1: 0 1px 2px hsl(var(--shadow-color) / 0.08);
    --shadow-2: 0 1px 2px hsl(var(--shadow-color) / 0.1), 0 4px 12px -2px hsl(var(--shadow-color) / 0.12),
      0 12px 28px -8px hsl(var(--shadow-color) / 0.16);
    --shadow-3: 0 2px 4px hsl(var(--shadow-color) / 0.1), 0 12px 24px -6px hsl(var(--shadow-color) / 0.16),
      0 32px 72px -16px hsl(var(--shadow-color) / 0.24);

    --skeleton: rgba(14, 18, 26, 0.06);
    --skeleton-hi: rgba(14, 18, 26, 0.1);
  }

  /* ========================================================================
     NIGHT — true black for OLED. Same ink as Carrier, slightly dimmed ink-1
     to limit bloom; surfaces step up from #000.
     ======================================================================== */
  :root[data-theme="night"] {
    color-scheme: dark;

    --bg: #000000;
    --surface-1: #08090b;
    --surface-2: #0e1014;
    --surface-3: #15181e;
    --surface-sunken: #000000;

    --line-1: rgba(255, 255, 255, 0.08);
    --line-2: rgba(255, 255, 255, 0.125);
    --line-3: rgba(255, 255, 255, 0.21);

    --ink-1: #e7eaef;
    --ink-2: #b9c0ca;
    --ink-3: #9098a5;
    --ink-4: #7b8491;

    --accent: #f0b448;
    --accent-fill: #f5a524;
    --accent-fill-hover: #ffb73d;
    --accent-fill-edge: #f5a524;
    --accent-ink: #171000;
    --accent-soft: rgba(245, 165, 36, 0.11);
    --accent-line: rgba(245, 165, 36, 0.42);

    --live: #4fd9dd;
    --live-soft: rgba(79, 217, 221, 0.11);
    --live-line: rgba(79, 217, 221, 0.4);

    --up: #4acb8d;
    --up-soft: rgba(74, 203, 141, 0.11);
    --down: #ff7676;
    --down-soft: rgba(255, 118, 118, 0.11);
    --warn: #ff9a48;
    --warn-soft: rgba(255, 154, 72, 0.11);
    --model: #c9a0ff;
    --model-soft: rgba(201, 160, 255, 0.11);

    --focus: #f0b448;
    --selection: rgba(245, 165, 36, 0.28);
    --scrim: rgba(0, 0, 0, 0.72);

    --chart-grid: rgba(255, 255, 255, 0.06);
    --chart-axis: rgba(255, 255, 255, 0.16);
    --cat-1: #f0b448;
    --cat-2: #6aaef5;
    --cat-3: #c9a0ff;
    --cat-4: #4acb8d;
    --cat-5: #ff9a78;
    --cat-6: #98a8bb;

    --heat-0: #0e1014;
    --heat-1: #12302f;
    --heat-2: #1a5254;
    --heat-3: #2a8589;
    --heat-4: #4fd9dd;

    --shadow-color: 0deg 0% 0%;
    --rim: inset 0 1px 0 rgba(255, 255, 255, 0.05);
    --shadow-1: none;
    --shadow-2: 0 0 0 1px rgba(255, 255, 255, 0.04), 0 12px 28px -8px rgba(0, 0, 0, 0.9);
    --shadow-3: 0 0 0 1px rgba(255, 255, 255, 0.05), 0 32px 72px -16px rgba(0, 0, 0, 0.95);

    --skeleton: rgba(255, 255, 255, 0.06);
    --skeleton-hi: rgba(255, 255, 255, 0.1);
  }

  /* Density: compact (desktop power users). Retargets aliases only. */
  :root[data-density="compact"] {
    --pad-card: var(--s-4);
    --pad-cell-y: var(--s-1_5);
    --pad-cell-x: var(--s-2);
    --gap-grid: var(--s-4);
    --gap-stack: var(--s-3);
    --control-h: 2rem;
  }

  @media (max-width: 719px) {
    :root {
      --gutter: var(--s-4);
      --pad-card: var(--s-4);
      --gap-grid: var(--s-4);
      --strip-h: 52px;
    }
  }

  @media (pointer: coarse), (max-width: 799px) {
    :root {
      --control-h: 2.75rem;
      --control-h-sm: 2.75rem;
    }
  }
}
