@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
  /* Tells the UA which scheme native UI (form controls, scrollbars) should
     follow; the light theme flips it (MDN: color-scheme). */
  color-scheme: dark;
  --brand: 47 193 207;
  --brand-light: 93 211 222;
  --brand-dark: 26 157 170;
  --surface-0: 11 17 32;
  --surface-1: 17 24 39;
  --surface-2: 26 34 53;
  --surface-3: 35 45 63;
  --border: rgba(255, 255, 255, 0.06);
  --border-hover: rgba(255, 255, 255, 0.12);
  --text-primary: #F1F5F9;
  --text-secondary: #94A3B8;
  /* #64748B measured 3.96:1 on the page bg and 3.73:1 on surface-1 cards —
     under WCAG AA for the body-size labels it decorates. #78879C holds
     5.15:1 / 4.85:1 while staying visibly quieter than --text-secondary. */
  --text-muted: #78879C;
}

html {
  font-family: 'DM Sans', sans-serif;
  background: rgb(var(--surface-0));
  color: var(--text-primary);
  scroll-behavior: smooth;
}

.body-background {
  background-color: rgb(var(--surface-0));
  background-image:
    radial-gradient(ellipse 60% 50% at 25% 0%, rgb(var(--brand) / 0.03), transparent),
    radial-gradient(ellipse 50% 40% at 75% 100%, rgb(var(--brand) / 0.02), transparent);
}

h1, h2, h3, h4, h5, h6 {
  font-family: 'Plus Jakarta Sans', sans-serif;
}

code {
  font-family: 'Fira Code', 'Menlo', 'Monaco', 'Lucida Console', monospace;
}

.code-block {
  @apply bg-slate-900 p-4 rounded-lg overflow-x-auto text-sm text-brand;
}

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .fade-up,
  .fade-up-delay-1,
  .fade-up-delay-2 {
    animation: none;
  }
}

.fade-up {
  animation: fade-up 0.5s ease-out both;
}

.fade-up-delay-1 { animation-delay: 0.08s; }
.fade-up-delay-2 { animation-delay: 0.16s; }

.glass-card {
  background: linear-gradient(135deg, rgba(255,255,255,0.03) 0%, rgba(255,255,255,0.01) 100%);
  border: 1px solid var(--border);
  backdrop-filter: blur(8px);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.glass-card:hover {
  border-color: var(--border-hover);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
}

pre {
  padding: 1rem;
  border-radius: 0.5rem;
  overflow-x: auto;
}

.pre-wrapper {
  position: relative;
}

.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 1;
  padding: 7px;
  border-radius: 6px;
  background: rgb(var(--surface-2));
  border: 1px solid var(--border-hover);
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease;
  line-height: 0;
}

.copy-btn:hover {
  color: var(--text-primary);
  background: rgb(var(--surface-3));
}

.copy-btn:focus-visible {
  outline: 2px solid rgb(var(--brand));
  outline-offset: 2px;
}

.brand-glow {
  box-shadow: 0 0 24px rgb(var(--brand) / 0.15), 0 0 48px rgb(var(--brand) / 0.05);
}

/* ---------------------------------------------------------------------------
 * Light color scheme (#1572)
 *
 * Mechanism: an inline head script (Layout.astro) resolves the saved theme
 * preference (localStorage; "auto" falls back to prefers-color-scheme) and
 * sets data-theme on <html> before CSS evaluates — the web.dev theme-switch
 * pattern. Everything below keys off :root[data-theme="light"].
 *
 * The Tailwind palette itself is theme-routed in tailwind.config.mjs:
 * themed shades compile to rgb(var(--tw-<family>-<shade>, <dark channels>)),
 * so in dark mode (vars undefined) stock Tailwind values render, and the
 * --tw-* definitions below remap them for light. This reaches every
 * utility variant (hover:, /alpha, gradients) and every @apply-baked
 * class; only utilities that hard-code non-palette colors (text-white,
 * white-alpha chrome) need the explicit overrides further down.
 * Coverage: tests/src/unit/test_light_scheme_token_coverage.py.
 * ------------------------------------------------------------------------ */

:root[data-theme="light"] {
  color-scheme: light;

  /* Site design tokens (consumed by html/body, cards, copy buttons, Nav).
     Brand is darkened so text-brand holds AA on white; the hue is kept. */
  --brand: 14 91 106;
  --brand-light: 25 124 137;
  --brand-dark: 8 67 78;
  --surface-0: 248 250 252;
  --surface-1: 255 255 255;
  --surface-2: 241 245 249;
  --surface-3: 226 232 240;
  --border: rgba(15, 23, 42, 0.10);
  --border-hover: rgba(15, 23, 42, 0.20);
  --text-primary: #0F172A;
  --text-secondary: #475569;
  /* #64748B measured 4.34:1 on surface-2 card fills; #58687E holds 5.19:1
     there (5.68:1 on white) and keeps the muted/secondary distinction. */
  --text-muted: #58687E;

  /* Tailwind palette remaps (companion of tailwind.config.mjs). Foreground
     shades (x-200..400, authored light-on-dark) drop to the family's
     700/800 value — 5–7.7:1 against white. Container shades (x-900/950,
     authored as dark fills) lift to the family's 100 tint so colored
     callouts stay tinted callouts. slate maps onto the surface scale. */
  --tw-slate-100: 51 65 85;
  --tw-slate-200: 51 65 85;
  --tw-slate-300: 51 65 85;
  --tw-slate-400: 51 65 85;
  --tw-slate-500: 71 85 105; /* measured 4.34:1 as slate-500 on surface-2 cards; slate-600 holds 6.6:1 */
  --tw-slate-700: 226 232 240;
  --tw-slate-800: 241 245 249;
  --tw-slate-900: 255 255 255;
  --tw-slate-950: 241 245 249;
  --tw-blue-200: 29 78 216;
  --tw-blue-300: 29 78 216;
  --tw-blue-400: 29 78 216;
  --tw-blue-900: 219 234 254;
  --tw-green-300: 22 101 52; /* green-700 measured 4.47:1 on the green-100 selected-badge tint; green-800 clears AA */
  --tw-green-400: 22 101 52;
  --tw-green-900: 220 252 231;
  --tw-amber-200: 146 64 14;
  --tw-amber-300: 146 64 14;
  --tw-amber-400: 146 64 14;
  --tw-amber-900: 254 243 199;
  --tw-red-200: 185 28 28;
  --tw-red-300: 185 28 28;
  --tw-red-400: 185 28 28;
  --tw-red-900: 254 226 226;
  --tw-purple-200: 126 34 206;
  --tw-purple-300: 126 34 206;
  --tw-purple-400: 126 34 206;
  --tw-purple-900: 243 232 255;
  --tw-violet-300: 109 40 217;
  --tw-violet-400: 109 40 217;
  --tw-cyan-300: 14 116 144;
  --tw-cyan-500: 14 116 144;
  --tw-cyan-900: 207 250 254;
  --tw-emerald-500: 4 120 87;
  --tw-yellow-300: 133 77 14; /* yellow-700 measured 4.49:1 on surface-2 badges; yellow-800 clears AA */
  --tw-yellow-400: 133 77 14;
  --tw-yellow-900: 254 249 195;
  --tw-orange-300: 194 65 12;
  --tw-orange-900: 255 237 213;
}

/* Non-palette utilities — these hard-code white, so the token remap can't
   reach them. In light mode "white text" means "primary text"; the :is()
   compound re-whitens it on saturated fills that stay saturated. The
   @apply-baked white texts (.optional-badge, .step-number) are immune to
   these class overrides and keep their dark chips — correct in both schemes.
   Only blue fills appear in the re-whitening compound because blue-500/600
   are the only saturated bgs sharing an element with a DOM-level text-white
   (grep-verified; the coverage test pins the other keep-white cases). */
:root[data-theme="light"] .text-white,
:root[data-theme="light"] .text-white\/70,
:root[data-theme="light"] .hover\:text-white:hover { color: var(--text-primary); }
:root[data-theme="light"] :is(.bg-blue-500, .bg-blue-600).text-white { color: #ffffff; }

/* @apply-BAKED white text is immune to the .text-white class override
   above (the compiled class carries the color declaration itself, no
   .text-white class in the DOM). These three sit on containers that turn
   light, so their text must darken; the other baked-white classes
   (.step-number, .section-number, .quick-start-badge) keep saturated
   chip fills and correctly stay white. The coverage test forces every
   @apply'd text-white into one of the two lists. */
:root[data-theme="light"] :is(.section-header, .instruction-title, .arch-label) { color: var(--text-primary); }

/* Client logos in the setup wizard are monochrome white SVGs (built for the
   dark theme). On a light page they vanish, so invert them to dark. */
:root[data-theme="light"] .client-logo { filter: invert(1); }

/* White-alpha chrome (arbitrary values: nav hairline, ghost buttons, icon
   bubbles, search field) — flip the tint base from white to slate-950 so
   the chrome stays visible on a light page. Complete set per grep of
   site/src; the coverage test fails on any new white-alpha value. */
:root[data-theme="light"] :is(.border-white\/\[0\.04\], .border-white\/\[0\.06\]) { border-color: rgba(15, 23, 42, 0.10); }
:root[data-theme="light"] .border-white\/\[0\.08\] { border-color: rgba(15, 23, 42, 0.12); }
:root[data-theme="light"] :is(.bg-white\/\[0\.03\], .bg-white\/\[0\.05\]) { background-color: rgba(15, 23, 42, 0.04); }
:root[data-theme="light"] .hover\:bg-white\/\[0\.04\]:hover { background-color: rgba(15, 23, 42, 0.06); }
:root[data-theme="light"] .hover\:bg-white\/\[0\.08\]:hover { background-color: rgba(15, 23, 42, 0.08); }

/* Pinned exception — text-slate-700 (tools.astro design-mode separator) is
   the one slate-700 use that is a foreground, not a container; the token
   remap would turn it light-on-light. */
:root[data-theme="light"] .text-slate-700 { color: rgb(51 65 85); }

/* Glass-card's white-tinted gradient becomes invisible on a near-white
   page; flip the tint to slate-900-low-alpha so cards stay visible. */
:root[data-theme="light"] .glass-card {
  background: linear-gradient(135deg, rgba(15, 23, 42, 0.04) 0%, rgba(15, 23, 42, 0.02) 100%);
}

/* Theme control in Nav — visible "Theme" label + small <select>, styled to
   match the existing nav pill controls. Pulls from the surface palette so
   it auto-fits each scheme. */
.theme-control { display: inline-flex; align-items: center; gap: 6px; }
.theme-control-label { font-size: 0.8rem; color: var(--text-secondary); }
.theme-toggle {
  background: rgb(var(--surface-1));
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  outline: none;
}
.theme-toggle:focus { border-color: rgb(var(--brand)); }

/* Accessibility popover (#1572). Anchored to the Accessibility button in
   Nav. Mirrors the four-knob layout of the settings UI's Accessibility tab
   so users get the same controls on both surfaces (same localStorage keys,
   same applied attributes). */
.a11y-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  z-index: 60;
  width: 18rem;
  max-width: calc(100vw - 2rem);
  background: rgb(var(--surface-1));
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
}
.a11y-panel[hidden] { display: none; }
.a11y-section { margin-bottom: 10px; }
.a11y-section:last-of-type { margin-bottom: 12px; }
.a11y-section-title { font-size: 0.85rem; font-weight: 600; margin-bottom: 6px; color: var(--text-primary); }
.a11y-options { display: flex; flex-wrap: wrap; gap: 4px; }
/* The option groups are native fieldsets (group name in a hidden legend,
   announced by screen readers); strip the default chrome. */
fieldset.a11y-options { border: 0; padding: 0; margin: 0; min-inline-size: auto; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0;
  margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
/* Skip-to-content link: visually hidden until focused, then pinned to the
   top-left above the sticky nav (z-50) so keyboard users can bypass it. */
.skip-link {
  position: absolute; left: 8px; top: -56px; z-index: 60;
  padding: 8px 16px; border-radius: 8px; font-size: 0.875rem; font-weight: 600;
  background: rgb(var(--surface-0)); color: var(--text-primary);
  border: 1px solid var(--border); transition: top 0.15s ease;
}
.skip-link:focus { top: 8px; outline: 2px solid rgb(var(--brand)); outline-offset: 2px; }
.a11y-option {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 8px; border: 1px solid var(--border); border-radius: 6px;
  cursor: pointer; font-size: 0.75rem; background: rgb(var(--surface-0));
  color: var(--text-secondary); user-select: none;
}
.a11y-option:hover { background: rgb(var(--surface-2)); color: var(--text-primary); }
.a11y-option:has(input:checked) {
  border-color: rgb(var(--brand)); color: rgb(var(--brand));
  background: rgb(var(--brand) / 0.08);
}
.a11y-option input { accent-color: rgb(var(--brand)); }

/* Preset chips (#1574 review): one-click theme presets modeled on Firefox
   Reader View's Colors menu. Each chip shows a miniature swatch of its
   actual scheme — fixed preview colors by design. Mirrors the same chip
   palette in src/ha_mcp/settings_ui/settings.css. */
.a11y-preset-chip {
  width: 20px; height: 20px; border-radius: 5px; display: inline-flex;
  align-items: center; justify-content: center; font-size: 0.65rem; font-weight: 600;
  border: 1px solid var(--border); flex-shrink: 0;
}
.a11y-preset-chip[data-chip="dark"] { background: #1c1c1e; color: #f5f5f7; }
.a11y-preset-chip[data-chip="light"] { background: #f5f5f7; color: #1d1d1f; }
.a11y-preset-chip[data-chip="auto"] { background: linear-gradient(135deg, #1c1c1e 50%, #f5f5f7 50%); color: #8e8e93; }
.a11y-preset-chip[data-chip="paper"] { background: #f4ede0; color: #1d1d1f; }
.a11y-preset-chip[data-chip="gray"] { background: #d4d6da; color: #1d1d1f; }
.a11y-preset-chip[data-chip="contrast"] { background: #ffffff; color: #000000; font-weight: 800; border-color: #000000; }

/* Custom color swatches — native pickers so the OS provides the visual
   selection UI; no hex typing required. */
.a11y-swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.a11y-swatch { display: inline-flex; align-items: center; gap: 6px; font-size: 0.75rem; color: var(--text-secondary); }
.a11y-swatch input[type="color"] {
  width: 34px; height: 24px; border: 1px solid var(--border);
  border-radius: 6px; padding: 1px; background: rgb(var(--surface-0)); cursor: pointer;
}
.a11y-contrast-warning { color: rgb(180 83 9); font-size: 0.7rem; margin: 8px 0 0; }
:root[data-theme="light"] .a11y-contrast-warning { color: rgb(146 64 14); }
.a11y-section .a11y-reset { margin-top: 10px; }
.a11y-reset {
  width: 100%; padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border);
  background: rgb(var(--surface-0)); color: var(--text-secondary); cursor: pointer;
  font-size: 0.75rem; font-family: inherit;
}
.a11y-reset:hover { background: rgb(var(--surface-2)); color: var(--text-primary); }

/* Light-mode background shade picker (#1572). Mirrors the same selectors
   used in src/ha_mcp/settings_ui/settings.css so the docs site and the settings UI
   honor the same localStorage shade key. Only the page background flips —
   surfaces (cards, glass-card) keep their tuned palette. */
:root[data-theme="light"][data-shade="paper"] body.body-background { background-color: #f4ede0; }
:root[data-theme="light"][data-shade="gray"]  body.body-background { background-color: #d4d6da; }
:root[data-theme="light"][data-shade="pure"]  body.body-background { background-color: #ffffff; }

/* High-contrast tier (#1572). Set via the Accessibility panel
   (data-contrast="high"). Strengthens primary text and the remapped slate
   body-text tokens toward the scheme's extreme; brand and surfaces stay so
   accent semantics carry across tiers. Body weight bumps to 500 for stroke
   contrast. */
:root[data-contrast="high"] body { font-weight: 500; }
:root[data-contrast="high"][data-theme="dark"] {
  --text-primary: #ffffff; --text-secondary: #e0e0e0; --text-muted: #94A3B8;
  --border: rgba(255, 255, 255, 0.18);
  --tw-slate-100: 255 255 255;
  --tw-slate-200: 255 255 255;
  --tw-slate-300: 241 245 249;
  --tw-slate-400: 241 245 249;
  /* The stock dark palette carries a few sub-AA pairs by design choice
     (e.g. text-slate-500 labels at 3.96:1, transport-badge blue-400 on
     blue-900/50 at 4.07:1). The default scheme keeps the approved look;
     this tier is where those pairs get lifted clear of AA. */
  --tw-slate-500: 148 163 184;
  --tw-blue-400: 147 197 253;
  --tw-amber-400: 252 211 77;
  --tw-green-400: 134 239 172;
  --tw-red-400: 252 165 165;
  --tw-violet-400: 196 181 253;
  --tw-yellow-400: 253 224 71;
  --tw-purple-400: 216 180 254;
}
:root[data-contrast="high"][data-theme="light"] {
  --text-primary: #000000; --text-secondary: #1a1a1a;
  --tw-slate-100: 15 23 42;
  --tw-slate-200: 15 23 42;
  --tw-slate-300: 15 23 42;
  --tw-slate-400: 15 23 42;
}
