/*
 * Cerulean UI — Design Tokens
 *
 * Canonical values for color, typography, spacing, radii, shadows, focus,
 * and motion. Any component styling that isn't a token is a bug.
 *
 * Theme model:
 *   - `:root`                       → dark mode (Cerulean's brand default)
 *   - `:root[data-theme="light"]`   → explicit light mode override
 *
 * Cerulean is intentionally dark-first; light mode is only applied when a
 * user explicitly opts in via `data-theme="light"` on the document root.
 * Platform's `workspace.css` defaults to light + follows `prefers-color-scheme`,
 * so a single theme toggle on the workspace flips both layers in lockstep.
 *
 * Applied to :root. For scoped theming, re-declare on a parent element.
 */

:root {
  /* ========================================================================
   * Surface colors (dark — default)
   * ====================================================================== */
  --cer-color-stage:           #0c0c0e;
  --cer-color-surface:         #131316;
  --cer-color-surface-hover:   #17171a;
  --cer-color-surface-raised:  #1a1a1e;
  --cer-color-surface-deep:    #0f0f12;

  /* ========================================================================
   * Border colors (dark)
   * ====================================================================== */
  --cer-color-border:          #24242a;
  --cer-color-border-muted:    #1e1e22;
  --cer-color-border-strong:   #2a2a2e;
  --cer-color-border-sep:      #3a3a40;

  /* ========================================================================
   * Text colors (dark)
   * ====================================================================== */
  --cer-color-text:            #e8e8ea;
  --cer-color-text-strong:     #ffffff;
  --cer-color-text-secondary:  #c8c8ce;
  --cer-color-text-tertiary:   #8e8e93;
  --cer-color-text-muted:      #5a5a60;
  --cer-color-text-label:      #7a7a80;

  /* ========================================================================
   * Semantic colors (foreground — same hue family in both modes,
   * lightness adjusts in light mode for contrast)
   * ====================================================================== */
  --cer-color-ok:              #9cd6a8;
  --cer-color-watch:           #e3c06b;
  --cer-color-alert:           #ff8f8f;
  --cer-color-info:            #7fb3e8;
  /* PLAT-173/PLAT-170 hook: a customer brand publishes its raw accent as
     --cer-brand-accent (in a :root rule in <head>, after this file). The accessibility
     "contrast" rules below re-derive the accent FROM that value, so a brand colour can
     never lower the contrast a person asked for. Unbranded, the fallback is Cerulean's own. */
  --cer-color-accent:          var(--cer-brand-accent, #b39bff);

  /* Tinted backgrounds for pills and chips (quiet variants — dark) */
  --cer-color-ok-bg:           rgba(156, 214, 168, 0.12);
  --cer-color-watch-bg:        rgba(227, 192, 107, 0.12);
  --cer-color-alert-bg:        rgba(255, 143, 143, 0.12);
  --cer-color-info-bg:         rgba(127, 179, 232, 0.12);
  --cer-color-accent-bg:       rgba(179, 155, 255, 0.12);

  /* Brand gradient (used on mark + avatar + left accents — same in both modes) */
  --cer-brand-gradient:        linear-gradient(135deg, #6d5ef5 0%, #3b82f6 100%);

  /* ========================================================================
   * Typography
   * ====================================================================== */
  --cer-font-sans:   -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  --cer-font-serif:  "Charter", "Iowan Old Style", "Georgia", serif;
  /* Ledger/ working-instrument surfaces (money figures) — tabular-nums lines
     up columns for comparison. Same value in light mode — not redefined
     under [data-theme="light"] (typography isn't themed, only color is). */
  --cer-font-mono:   ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, monospace;

  --cer-fs-micro:    10px;
  --cer-fs-label:    11px;
  --cer-fs-body-sm:  12px;
  --cer-fs-body:     13px;
  --cer-fs-body-lg:  14px;
  --cer-fs-lead:     16px;
  --cer-fs-metric:   20px;
  --cer-fs-hero:     22px;
  --cer-fs-title:    32px;

  --cer-fw-regular:  400;
  --cer-fw-medium:   500;
  --cer-fw-semibold: 600;
  --cer-fw-bold:     700;

  --cer-lh-tight:    1.25;
  --cer-lh-compact:  1.35;
  --cer-lh-body:     1.55;
  --cer-lh-prose:    1.6;

  --cer-tracking-uppercase: 0.5px;
  --cer-tracking-wide:      1.2px;

  /* ========================================================================
   * Spacing
   * ====================================================================== */
  --cer-space-1:  4px;
  --cer-space-2:  8px;
  --cer-space-3:  12px;
  --cer-space-4:  16px;
  --cer-space-5:  20px;
  --cer-space-6:  24px;
  --cer-space-7:  32px;
  --cer-space-8:  40px;
  --cer-space-9:  64px;
  --cer-space-10: 80px;

  /* ========================================================================
   * Radii
   * ====================================================================== */
  --cer-radius-sm:    5px;
  --cer-radius-md:    6px;
  --cer-radius-lg:    10px;
  --cer-radius-card:  12px;
  --cer-radius-xl:    14px;
  --cer-radius-round: 9999px;

  /* ========================================================================
   * Shadows (depth scale — dark mode uses heavier shadows
   * since contrast comes from light-on-dark, not surface depth)
   * ====================================================================== */
  --cer-shadow-sm:    0 1px 2px 0 rgba(0, 0, 0, 0.40);
  --cer-shadow-md:    0 2px 4px -1px rgba(0, 0, 0, 0.45),
                      0 4px 6px -1px rgba(0, 0, 0, 0.30);
  --cer-shadow-lg:    0 4px 6px -2px rgba(0, 0, 0, 0.45),
                      0 10px 15px -3px rgba(0, 0, 0, 0.50);
  --cer-shadow-focus: 0 0 0 3px rgba(127, 179, 232, 0.25);
  --cer-shadow-panel: -4px 0 24px rgba(0, 0, 0, 0.50);

  /* Modal/panel backdrop overlays — black-with-alpha works in both themes
     since the goal is dim-everything-behind, not theme-aware tinting. */
  --cer-color-overlay:        rgba(0, 0, 0, 0.35);
  --cer-color-overlay-strong: rgba(0, 0, 0, 0.50);

  /* Text color used on brand-gradient backgrounds (always pure white;
     gradient is dark in both light and dark themes). */
  --cer-color-on-brand: #ffffff;

  /* ========================================================================
   * Focus rings (keyboard focus indicator — must remain visible
   * across both themes; uses the info hue so it never collides with
   * primary action colors)
   * ====================================================================== */
  --cer-color-focus:   #7fb3e8;   /* PLAT-173: its own token — colour-safe re-tones "info" */
  --cer-focus-ring:    2px solid var(--cer-color-focus);
  --cer-focus-offset:  2px;
  --cer-focus-radius:  var(--cer-radius-md);

  /* ========================================================================
   * Motion (timing values for transitions and animations)
   *
   * --cer-transition-* are CSS shorthand convenience values (timing fn baked in).
   * --cer-motion-* are bare timing values for granular use in @keyframes
   * or animation properties.
   *
   * `reshape` is the auto-render content-swap timing: slightly slower
   * with an "out-expo" curve so a layout change reads as intentional, not flicker.
   * ====================================================================== */
  --cer-transition-fast:   120ms ease-out;
  --cer-transition-normal: 200ms ease-out;

  --cer-motion-fast:    120ms;
  --cer-motion-base:    200ms;
  --cer-motion-reshape: 240ms;

  --cer-easing-standard: cubic-bezier(0.4, 0, 0.2, 1);
  --cer-easing-out-expo: cubic-bezier(0.2, 0, 0, 1);

  /* ========================================================================
   * Accessibility hooks (PLAT-173). Defaults are "off"; the rules at the end of
   * this file turn them on. Shadow-DOM components cannot see attributes on
   * <html>, but they DO inherit custom properties — so every accessibility
   * effect a component must honour is a token here, not a selector.
   * ====================================================================== */
  --cer-status-symbol-display: none;         /* symbols beside status words (colour-safe) */
  --cer-status-dot-scale:      1;            /* status shapes grow (colour-safe) */
  --cer-status-edge:           none;         /* box-shadow edge on pills/badges (contrast) */
  --cer-link-decoration:       none;         /* links (underline when "links") */
  --cer-chart-series-1:        var(--cer-color-accent);   /* chart series (colour-safe: Okabe-Ito) */
  --cer-chart-series-2:        var(--cer-color-info);
  --cer-chart-series-3:        var(--cer-color-ok);

  /* Sign-in surfaces PLAT-170 paints with brand. Under higher contrast they are the plain
     surface: B's rail tint and C's background photo (see the accessibility rules below). */
  --cer-signin-rail-tint:      var(--cer-color-accent-bg);
  --cer-signin-photo:          var(--cer-brand-photo, none);

  /* ========================================================================
   * Page background (derived — apply to body when using the UI)
   * ====================================================================== */
  color: var(--cer-color-text);
  background: var(--cer-color-stage);
  font-family: var(--cer-font-sans);
  font-size: var(--cer-fs-body-lg);
  line-height: var(--cer-lh-body);
}

/* ==========================================================================
 * Light mode — explicit opt-in via [data-theme="light"]
 *
 * Cerulean is dark-first; light mode keeps the same hue families but
 * inverts surface/text contrast and adjusts tinted-bg alphas (since the same
 * 12% alpha reads very differently on white vs near-black backgrounds).
 * Semantic colors are darkened slightly for AA contrast on white surfaces.
 * ========================================================================== */
:root[data-theme="light"] {
  /* Surface — paper/warm white, not pure #fff (easier on the eye) */
  --cer-color-stage:           #fafaf9;
  --cer-color-surface:         #ffffff;
  --cer-color-surface-hover:   #f5f5f4;
  --cer-color-surface-raised:  #ffffff;
  --cer-color-surface-deep:    #f5f5f4;

  /* Borders — warm light gray scale */
  --cer-color-border:          #e7e5e4;
  --cer-color-border-muted:    #f0eeed;
  --cer-color-border-strong:   #d6d3d1;
  --cer-color-border-sep:      #a8a29e;

  /* Text — warm near-black scale */
  --cer-color-text:            #1c1917;
  --cer-color-text-strong:     #0c0a09;
  --cer-color-text-secondary:  #44403c;
  --cer-color-text-tertiary:   #78716c;
  --cer-color-text-muted:      #a8a29e;
  --cer-color-text-label:      #78716c;

  /* Semantic colors — darker variants for AA contrast on white */
  --cer-color-ok:              #16a34a;
  --cer-color-watch:           #ca8a04;
  --cer-color-alert:           #dc2626;
  --cer-color-info:            #2563eb;
  --cer-color-accent:          var(--cer-brand-accent, #7c3aed);

  /* Tinted backgrounds — lower alpha so they read as quiet on white */
  --cer-color-ok-bg:           rgba(22, 163, 74, 0.10);
  --cer-color-watch-bg:        rgba(202, 138, 4, 0.10);
  --cer-color-alert-bg:        rgba(220, 38, 38, 0.10);
  --cer-color-info-bg:         rgba(37, 99, 235, 0.10);
  --cer-color-accent-bg:       rgba(124, 58, 237, 0.10);

  /* Shadows — softer and darker-on-light, so depth comes from shadow rather than borders */
  --cer-shadow-sm:    0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --cer-shadow-md:    0 2px 4px -1px rgba(0, 0, 0, 0.06),
                      0 4px 6px -1px rgba(0, 0, 0, 0.10);
  --cer-shadow-lg:    0 4px 6px -2px rgba(0, 0, 0, 0.05),
                      0 10px 15px -3px rgba(0, 0, 0, 0.10);
  --cer-shadow-focus: 0 0 0 3px rgba(37, 99, 235, 0.20);
  --cer-color-focus:  #2563eb;
}

/* ==========================================================================
 * Accessibility — PLAT-173 ("More accessible")
 *
 * The server writes these on <html> (Layout.tsx, WorkspaceLayoutLit.tsx), in the
 * first bytes, so nothing flashes:
 *
 *   data-a11y="colour-safe contrast motion links focus"   (any subset)
 *   data-a11y-text="large"
 *   data-a11y-source="person | device"        (absent when nobody is signed in)
 *
 * SELECTORS. The attributes sit on <html> ITSELF, so every rule is a COMPOUND selector:
 * `:root[data-a11y~="x"]` and `:root[data-theme="light"][data-a11y~="x"]` — never a
 * descendant (`:root[data-theme="light"] [data-a11y~="x"]`), which silently never
 * matches. (Pinned by src/__tests__/plat173-tokens-css.test.ts and a headless-Chromium
 * render test.) Every dark rule has a light twin that sets the same properties, because
 * `:root[data-a11y~="x"]` ties in specificity with the light base block above and would
 * otherwise win over it in light mode; the twin (one more attribute) beats it.
 *
 * CASCADE ORDER: Cerulean defaults, then brand (PLAT-170), then accessibility, LAST.
 * Brand writes custom properties on a bare `:root` (specificity 0,1,0) in <head>; these
 * rules are (0,2,0)+, so accessibility wins whatever the injection order — that is the
 * point. Brand publishes its raw accent as --cer-brand-accent; the contrast rules
 * re-derive the accent from THAT, keeping its hue and moving only lightness to >= 7:1
 * on the surface (measured: dark >= 8.2:1, light >= 7.6:1 over a spread of hues,
 * including a pale yellow and a pure white or black brand).
 *
 * NEVER TOUCHED: logos and the favicon (identity; bytes are served unaltered), and the
 * QA/UAT band (envMarker.tsx: fixed colours of its own, no tokens read).
 *
 * DEVICE SIGNALS (login page, before anyone is known): `prefers-contrast: more` and
 * `prefers-reduced-motion: reduce` apply the contrast and motion sets, but only while
 * `data-a11y-source` is not "person" — a saved personal choice always beats the device.
 * The blocks inside the @media rules repeat the attribute blocks' declarations (CSS has
 * no alias for a rule); plat173-tokens-css.test.ts fails if they drift apart.
 * ========================================================================== */

/* ── colour-safe: status colours re-toned (Okabe-Ito family), measured for
      deuteranopia / protanopia / tritanopia; symbols and larger shapes on ── */
:root[data-a11y~="colour-safe"] {
  --cer-color-ok:#8bdbe1; --cer-color-watch:#f6c006; --cer-color-alert:#f0643c; --cer-color-info:#a58bff;
  --cer-color-ok-bg:rgba(139,219,225,.13); --cer-color-watch-bg:rgba(246,192,6,.13);
  --cer-color-alert-bg:rgba(240,100,60,.14); --cer-color-info-bg:rgba(165,139,255,.14);
  --cer-color-focus:#8bdbe1;
  --cer-chart-series-1:#56b4e9; --cer-chart-series-2:#e69f00; --cer-chart-series-3:#f0e9d8;
  --cer-status-symbol-display:inline-flex; --cer-status-dot-scale:1.4;
}
:root[data-theme="light"][data-a11y~="colour-safe"] {
  --cer-color-ok:#2b7bbc; --cer-color-watch:#756c00; --cer-color-alert:#7a1f00; --cer-color-info:#3b1d8f;
  --cer-color-ok-bg:rgba(43,123,188,.10); --cer-color-watch-bg:rgba(117,108,0,.12);
  --cer-color-alert-bg:rgba(122,31,0,.10); --cer-color-info-bg:rgba(59,29,143,.10);
  --cer-color-focus:#2b7bbc;
  --cer-chart-series-1:#0072b2; --cer-chart-series-2:#d55e00; --cer-chart-series-3:#b5b5b5;
}

/* ── contrast: text, edges and status pushed to >= 7:1 on the surface ── */
:root[data-a11y~="contrast"] {
  --cer-color-text:#f4f4f6; --cer-color-text-secondary:#e2e2e6; --cer-color-text-tertiary:#bcbcc2;
  --cer-color-text-muted:#a2a2a9; --cer-color-text-label:#b4b4ba;
  --cer-color-border:#4a4a52; --cer-color-border-muted:#3a3a40; --cer-color-border-strong:#6a6a72; --cer-color-border-sep:#8a8a92;
  --cer-color-ok:#b8f0c4; --cer-color-watch:#ffd98a; --cer-color-alert:#ffb0b0; --cer-color-info:#a8d0ff;
  --cer-color-focus:#a8d0ff;
  --cer-status-edge:inset 0 0 0 1px currentColor;
  /* Brand, subordinate: same hue, lightness moved until >= 7:1; gradient becomes a solid;
     text on it is recomputed; sign-in rail tint and background photo become the plain surface. */
  --cer-signin-rail-tint:transparent; --cer-signin-photo:none;
}
/* PLAT-173: relative colour needs a recent browser; older ones keep the brand accent (4.5:1) instead of losing it */
@supports (color: oklch(from red l c h)) {
  :root[data-a11y~="contrast"] {
    --cer-color-accent:oklch(from var(--cer-brand-accent, #b39bff) max(l, 0.78) c h);
    --cer-color-accent-bg:oklch(from var(--cer-color-accent) l c h / 0.14);
    --cer-brand-gradient:linear-gradient(var(--cer-color-accent), var(--cer-color-accent));
    --cer-color-on-brand:#0c0c0e;
  }
}
:root[data-theme="light"][data-a11y~="contrast"] {
  --cer-color-text:#0c0a09; --cer-color-text-secondary:#1c1917; --cer-color-text-tertiary:#3d3935;
  --cer-color-text-muted:#57534e; --cer-color-text-label:#44403c;
  --cer-color-border:#a8a29e; --cer-color-border-muted:#c9c5c2; --cer-color-border-strong:#78716c; --cer-color-border-sep:#57534e;
  --cer-color-ok:#0b5e2c; --cer-color-watch:#6e4c00; --cer-color-alert:#a51c1c; --cer-color-info:#1d43b8;
  --cer-color-focus:#1d43b8;
  --cer-status-edge:inset 0 0 0 1px currentColor;
  --cer-signin-rail-tint:transparent; --cer-signin-photo:none;
}
/* PLAT-173: relative colour needs a recent browser; older ones keep the brand accent (4.5:1) instead of losing it */
@supports (color: oklch(from red l c h)) {
  :root[data-theme="light"][data-a11y~="contrast"] {
    --cer-color-accent:oklch(from var(--cer-brand-accent, #7c3aed) min(l, 0.41) c h);
    --cer-color-accent-bg:oklch(from var(--cer-color-accent) l c h / 0.14);
    --cer-brand-gradient:linear-gradient(var(--cer-color-accent), var(--cer-color-accent));
    --cer-color-on-brand:#ffffff;
  }
}
/* both on: the colour-safe hues, lifted (declared after the single sets, so they win) */
:root[data-a11y~="colour-safe"][data-a11y~="contrast"] {
  --cer-color-ok:#a6ecf0; --cer-color-watch:#ffd23a; --cer-color-alert:#ff8a66; --cer-color-info:#c4b2ff; --cer-color-focus:#a6ecf0;
}
:root[data-theme="light"][data-a11y~="colour-safe"][data-a11y~="contrast"] {
  --cer-color-ok:#175a96; --cer-color-watch:#5e5600; --cer-color-alert:#5a1500; --cer-color-info:#331a85; --cer-color-focus:#175a96;
}

/* ── links: always underlined (light DOM here; components read --cer-link-decoration) ── */
:root[data-a11y~="links"] { --cer-link-decoration: underline; }
:root[data-a11y~="links"] a { text-decoration: underline; text-underline-offset: 2px; text-decoration-thickness: 1.5px; }

/* ── focus: thicker, offset, with a halo so it shows on any background ── */
:root[data-a11y~="focus"] {
  --cer-focus-ring: 3px solid var(--cer-color-focus);
  --cer-focus-offset: 3px;
  --cer-shadow-focus: 0 0 0 7px color-mix(in srgb, var(--cer-color-focus) 22%, transparent);
}
:root[data-a11y~="focus"] :focus-visible {
  outline: var(--cer-focus-ring);
  outline-offset: var(--cer-focus-offset);
  box-shadow: var(--cer-shadow-focus);
}

/* ── motion: nothing slides, fades or spins ── */
:root[data-a11y~="motion"] {
  --cer-transition-fast: 0ms ease-out; --cer-transition-normal: 0ms ease-out;
  --cer-motion-fast: 0ms; --cer-motion-base: 0ms; --cer-motion-reshape: 0ms;
}
:root[data-a11y~="motion"] *, :root[data-a11y~="motion"] *::before, :root[data-a11y~="motion"] *::after {
  animation: none !important; transition: none !important; scroll-behavior: auto !important;
}

/* ── larger text: the type scale grows 20% and reflows (NOT part of the "More accessible" switch).
      The root font size follows --cer-fs-body-lg, so rem-based pages scale with it. ── */
:root[data-a11y-text="large"] {
  --cer-fs-micro: 12px; --cer-fs-label: 13px; --cer-fs-body-sm: 14px; --cer-fs-body: 16px;
  --cer-fs-body-lg: 17px; --cer-fs-lead: 19px; --cer-fs-metric: 24px; --cer-fs-hero: 26px; --cer-fs-title: 38px;
}

/* ==========================================================================
 * Device signals — the sign-in page before anyone is known (and any page for a
 * person who has saved nothing). They step aside for a saved personal choice.
 * Keep each block identical to its attribute twin above.
 * ========================================================================== */
@media (prefers-contrast: more) {
  :root:not([data-a11y-source="person"]) {
    --cer-color-text:#f4f4f6; --cer-color-text-secondary:#e2e2e6; --cer-color-text-tertiary:#bcbcc2;
    --cer-color-text-muted:#a2a2a9; --cer-color-text-label:#b4b4ba;
    --cer-color-border:#4a4a52; --cer-color-border-muted:#3a3a40; --cer-color-border-strong:#6a6a72; --cer-color-border-sep:#8a8a92;
    --cer-color-ok:#b8f0c4; --cer-color-watch:#ffd98a; --cer-color-alert:#ffb0b0; --cer-color-info:#a8d0ff;
    --cer-color-focus:#a8d0ff;
    --cer-status-edge:inset 0 0 0 1px currentColor;
    --cer-signin-rail-tint:transparent; --cer-signin-photo:none;
  }
  /* PLAT-173: relative colour needs a recent browser; older ones keep the brand accent (4.5:1) instead of losing it */
  @supports (color: oklch(from red l c h)) {
    :root:not([data-a11y-source="person"]) {
      --cer-color-accent:oklch(from var(--cer-brand-accent, #b39bff) max(l, 0.78) c h);
      --cer-color-accent-bg:oklch(from var(--cer-color-accent) l c h / 0.14);
      --cer-brand-gradient:linear-gradient(var(--cer-color-accent), var(--cer-color-accent));
      --cer-color-on-brand:#0c0c0e;
    }
  }
  :root[data-theme="light"]:not([data-a11y-source="person"]) {
    --cer-color-text:#0c0a09; --cer-color-text-secondary:#1c1917; --cer-color-text-tertiary:#3d3935;
    --cer-color-text-muted:#57534e; --cer-color-text-label:#44403c;
    --cer-color-border:#a8a29e; --cer-color-border-muted:#c9c5c2; --cer-color-border-strong:#78716c; --cer-color-border-sep:#57534e;
    --cer-color-ok:#0b5e2c; --cer-color-watch:#6e4c00; --cer-color-alert:#a51c1c; --cer-color-info:#1d43b8;
    --cer-color-focus:#1d43b8;
    --cer-status-edge:inset 0 0 0 1px currentColor;
    --cer-signin-rail-tint:transparent; --cer-signin-photo:none;
  }
  /* PLAT-173: relative colour needs a recent browser; older ones keep the brand accent (4.5:1) instead of losing it */
  @supports (color: oklch(from red l c h)) {
    :root[data-theme="light"]:not([data-a11y-source="person"]) {
      --cer-color-accent:oklch(from var(--cer-brand-accent, #7c3aed) min(l, 0.41) c h);
      --cer-color-accent-bg:oklch(from var(--cer-color-accent) l c h / 0.14);
      --cer-brand-gradient:linear-gradient(var(--cer-color-accent), var(--cer-color-accent));
      --cer-color-on-brand:#ffffff;
    }
  }
}
@media (prefers-reduced-motion: reduce) {
  :root:not([data-a11y-source="person"]) {
    --cer-transition-fast: 0ms ease-out; --cer-transition-normal: 0ms ease-out;
    --cer-motion-fast: 0ms; --cer-motion-base: 0ms; --cer-motion-reshape: 0ms;
  }
  :root:not([data-a11y-source="person"]) *, :root:not([data-a11y-source="person"]) *::before, :root:not([data-a11y-source="person"]) *::after {
    animation: none !important; transition: none !important; scroll-behavior: auto !important;
  }
}
