/* T-167 — UI v2 design tokens (epic T-166, design: docs/UI-V2-DESIGN-HANDOFF.md,
   "Themes and tokens"). Tokens are defined ONCE here; v2 components (T-168..T-170)
   only reference var(--v2-*) and never hard-code a colour.

   Scope: everything lives under html[data-ui="v2"]. Nothing sets that attribute
   yet (T-168 owns the `ui` URL parameter and toggles it), so with the attribute
   absent — `ui=classic`, and today's default — no rule here matches and the
   viewer renders exactly as before.

   Theme: follows prefers-color-scheme (same convention as charts.css) through
   light-dark(); html[data-theme="light"|"dark"] forces either one, for a later
   `theme` parameter. Dark text/surface pairs keep >= 4.5:1 contrast
   (T-162 rule); values below are Tailwind slate/blue/red steps from the handoff. */

html[data-ui="v2"] {
  /* Follows the OS; html[data-theme] below forces either. Every colour is
     light-dark(<light>, <dark>), so each token is written once. */
  color-scheme: light dark;

  /* colour */
  --v2-workspace: light-dark(#f8fafc, #020617);        /* slate-50 / slate-950 */
  --v2-surface: light-dark(#ffffff, #0f172a);          /* raised surface; slate-900 */
  --v2-border: light-dark(#cbd5e1, #334155);           /* slate-300 / 700 */
  --v2-border-subtle: light-dark(#e2e8f0, #1e293b);    /* slate-200 / 800 */
  --v2-text: light-dark(#0f172a, #f1f5f9);
  --v2-text-muted: light-dark(#64748b, #94a3b8);       /* slate-500 (4.76:1) / 400 (6.9:1) */
  --v2-primary: #2563eb;                               /* blue-600, both themes */
  --v2-primary-text: #ffffff;
  --v2-action: light-dark(#0f172a, #2563eb);           /* dark button would vanish on a dark surface */
  --v2-action-text: #ffffff;
  --v2-accent-soft: light-dark(#dbeafe, #1e3a8a);      /* active/selected tint */
  --v2-error: light-dark(#dc2626, #ef4444);            /* red-600 / red-500 */
  --v2-error-soft: light-dark(#fef2f2, #450a0a);
  --v2-overlay-bg: light-dark(rgba(255, 255, 255, 0.92), rgba(15, 23, 42, 0.9)); /* map overlays only */
  --v2-overlay-blur: 6px;
  --v2-scrim: light-dark(rgba(15, 23, 42, 0.35), rgba(2, 6, 23, 0.6));

  /* map popups keep a white card in both themes, so their text is fixed dark ink
     (T-179: it used to inherit the page's light text and vanish on the card) */
  --v2-popup-text: #0f172a;

  /* chart panel is dark in both themes (design: "a dark chart panel") */
  --v2-chart-bg: light-dark(#0f172a, #020617);
  --v2-chart-text: #e2e8f0;
  --v2-chart-muted: #94a3b8;
  --v2-chart-grid: light-dark(#334155, #1e293b);

  /* shape */
  --v2-radius-control: 6px;
  --v2-radius-control-lg: 8px;
  --v2-radius-panel: 12px;
  --v2-radius-panel-lg: 16px;
  --v2-shadow-raised: 0 4px 14px light-dark(rgba(15, 23, 42, 0.14), rgba(0, 0, 0, 0.5));
  --v2-shadow-sheet: 0 -6px 20px light-dark(rgba(15, 23, 42, 0.18), rgba(0, 0, 0, 0.55));

  /* spacing (4px base) */
  --v2-space-1: 4px;
  --v2-space-2: 8px;
  --v2-space-3: 12px;
  --v2-space-4: 16px;
  --v2-space-5: 24px;
  --v2-topbar-height: 56px;
  --v2-sidebar-width: 372px;
  --v2-map-control-column: 48px;    /* Google RIGHT_TOP column: 10 px margin + 40 px control (T-201) */
  --v2-hit: 32px;                   /* minimum control height */

  /* type */
  --v2-font: var(--font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif);
  --v2-text-helper: 10px;
  --v2-text-control: 12px;
  --v2-text-label: 11px;            /* dense uppercase section labels */
  --v2-text-section: 14px;
  --v2-label-tracking: 0.06em;

  /* motion: components use this for transitions; reduced-motion zeroes it */
  --v2-motion: 120ms;

  /* focus ring: 2px blue-500, 2px offset */
  --v2-focus: #3b82f6;
  --v2-focus-width: 2px;
  --v2-focus-offset: 2px;
}
html[data-ui="v2"] .tv-popup { color: var(--v2-popup-text); }
html[data-ui="v2"][data-theme="light"] { color-scheme: light; }
html[data-ui="v2"][data-theme="dark"] { color-scheme: dark; }

/* Shared v2 control behaviour. Components opt in with the .v2-* classes so
   nothing here can touch a classic element. */
html[data-ui="v2"] .v2-btn,
html[data-ui="v2"] .v2-icon-btn {
  font: 500 var(--v2-text-control) var(--v2-font);
  min-height: var(--v2-hit);
  border: 1px solid var(--v2-border);
  border-radius: var(--v2-radius-control);
  background: var(--v2-surface);
  color: var(--v2-text);
  cursor: pointer;
}
html[data-ui="v2"] .v2-icon-btn { min-width: var(--v2-hit); padding: 0; }
html[data-ui="v2"] .v2-btn { padding: 0 var(--v2-space-3); }
html[data-ui="v2"] .v2-btn[aria-pressed="true"],
html[data-ui="v2"] .v2-icon-btn[aria-pressed="true"] {
  background: var(--v2-accent-soft);
  border-color: var(--v2-primary);
}
html[data-ui="v2"] .v2-btn.v2-primary {
  background: var(--v2-action);
  color: var(--v2-action-text);
  border-color: var(--v2-action);
}
html[data-ui="v2"] .v2-btn:disabled,
html[data-ui="v2"] .v2-icon-btn:disabled { opacity: 0.45; cursor: default; }
html[data-ui="v2"] .v2-label {
  font: 600 var(--v2-text-label) var(--v2-font);
  letter-spacing: var(--v2-label-tracking);
  text-transform: uppercase;
  color: var(--v2-text-muted);
}
html[data-ui="v2"] .v2-helper { font: 400 var(--v2-text-helper) var(--v2-font); color: var(--v2-text-muted); }
html[data-ui="v2"] .v2-panel {
  background: var(--v2-surface);
  color: var(--v2-text);
  border: 1px solid var(--v2-border-subtle);
  border-radius: var(--v2-radius-panel);
  box-shadow: var(--v2-shadow-raised);
  font-family: var(--v2-font);
}
/* Backdrop blur only on map overlays (design note). */
html[data-ui="v2"] .v2-overlay {
  background: var(--v2-overlay-bg);
  -webkit-backdrop-filter: blur(var(--v2-overlay-blur));
  backdrop-filter: blur(var(--v2-overlay-blur));
}
/* Spanish runs ~30% longer: v2 text may wrap or ellipsise, never overflow. */
html[data-ui="v2"] .v2-btn,
html[data-ui="v2"] .v2-label { min-width: 0; overflow-wrap: anywhere; }

/* Focus-visible for every v2 interactive element (keyboard only, never on mouse click). */
html[data-ui="v2"] :is(button, a[href], input, select, textarea, [role="button"], [tabindex]):focus-visible {
  outline: var(--v2-focus-width) solid var(--v2-focus);
  outline-offset: var(--v2-focus-offset);
}

@media (prefers-reduced-motion: reduce) {
  html[data-ui="v2"] { --v2-motion: 0.01ms; }
}
