/* ============================================================
   THEME SYSTEM
   ============================================================
   Layer 1 — Primitive palette   (raw values, never used directly)
   Layer 2 — Semantic tokens     (what all components reference)
   Layer 3 — Light theme         (prefers-color-scheme: light + [data-theme="light"])
   Layer 4 — Dark theme          (prefers-color-scheme: dark  + [data-theme="dark"])

   Initial load strategy (zero JS required):
     The browser resolves the correct theme from the OS via the media
     query before first paint. If the user later picks a preference,
     JS sets data-theme on <html> and localStorage stores it.
     A tiny inline <script> in <head> can restore that preference on
     subsequent loads — still before first paint, still no FOUC.

   Adding a new theme:
     1. Add a [data-theme="your-theme"] block (copy the dark block).
     2. Redefine every --color-* token inside it.
     3. No other file needs to change.
   ============================================================ */

/* ------------------------------------------------------------
   LAYER 1 — Primitive palette
   Neutral scale, brand hues, and status hues.
   Never reference these outside this file.
   ------------------------------------------------------------ */
:root {
  /* Neutral */
  --primitive-neutral-0:   #ffffff;
  --primitive-neutral-50:  #fafafa;
  --primitive-neutral-100: #f5f5f5;
  --primitive-neutral-200: #e5e5e5;
  --primitive-neutral-300: #d4d4d4;
  --primitive-neutral-400: #a3a3a3;
  --primitive-neutral-500: #737373;
  --primitive-neutral-600: #525252;
  --primitive-neutral-700: #404040;
  --primitive-neutral-800: #262626;
  --primitive-neutral-900: #171717;
  --primitive-neutral-950: #0a0a0a;

  /* Brand — blue */
  --primitive-blue-100: #dbeafe;
  --primitive-blue-200: #bfdbfe;
  --primitive-blue-300: #93c5fd;
  --primitive-blue-400: #60a5fa;
  --primitive-blue-500: #3b82f6;
  --primitive-blue-600: #2563eb;
  --primitive-blue-700: #1d4ed8;
  --primitive-blue-800: #1e40af;
  --primitive-blue-900: #1e3a8a;

  /* Status — green */
  --primitive-green-100: #dcfce7;
  --primitive-green-200: #bbf7d0;
  --primitive-green-400: #4ade80;
  --primitive-green-700: #15803d;
  --primitive-green-800: #166534;
  --primitive-green-950: #052e16;

  /* Status — amber */
  --primitive-amber-100: #fef9c3;
  --primitive-amber-300: #fcd34d;
  --primitive-amber-700: #b45309;
  --primitive-amber-800: #92400e;
  --primitive-amber-950: #451a03;

  /* Status — red */
  --primitive-red-100: #fee2e2;
  --primitive-red-300: #fca5a5;
  --primitive-red-700: #b91c1c;
  --primitive-red-800: #991b1b;
  --primitive-red-950: #450a0a;

  /* Status — sky */
  --primitive-sky-100: #e0f2fe;
  --primitive-sky-300: #7dd3fc;
  --primitive-sky-700: #0369a1;
  --primitive-sky-800: #075985;
  --primitive-sky-950: #082f49;
}


/* ------------------------------------------------------------
   LAYER 2 — Semantic token declarations (shape only)
   All values are overridden per-theme below.
   Components and utilities reference only these tokens.
   ------------------------------------------------------------ */

/*
  Token naming pattern:
    --color-{role}-{variant}

  Roles:
    bg          Page and section backgrounds
    surface     Elevated containers (cards, panels, dialogs)
    border      Lines and dividers
    text        Readable copy
    interactive Primary action color (links, buttons, focus)
    status      Semantic feedback (success / warning / error / info)
    overlay     Scrim / modal backdrop
    accent      Highlight / decorative emphasis
*/


/* ------------------------------------------------------------
   LAYER 3 — Light theme (default)
   Applied when: OS is light  OR  [data-theme="light"]
   All contrast ratios verified against WCAG AA (4.5:1 normal text,
   3:1 large text and UI components).
   ------------------------------------------------------------ */

/* Default: assume light unless the OS says otherwise */
:root,
[data-theme="light"] {
  color-scheme: light;

  /* Backgrounds */
  --color-bg-base:   #ffffff;   /* body background */
  --color-bg-subtle: #f5f5f5;   /* alternate section fill */
  --color-bg-muted:  #e5e5e5;   /* input fill, skeleton loaders */

  /* Surfaces */
  --color-surface-default: #ffffff; /* cards, panels */
  --color-surface-raised:  #ffffff; /* dropdowns, tooltips (rely on shadow) */
  --color-surface-overlay: #ffffff; /* dialogs, drawers */

  /* Borders */
  --color-border-default: #d4d4d4; /* 3.0:1 on #fff — meets UI component AA */
  --color-border-strong:  #a3a3a3; /* 4.6:1 on #fff — meets normal text AA  */
  --color-border-focus:   #2563eb; /* 4.7:1 on #fff — focus ring */

  /* Text
     Primary   #111111 on #fff → 20.2:1 ✓
     Secondary #555555 on #fff →  7.0:1 ✓
     Muted     #767676 on #fff →  4.5:1 ✓  (minimum AA threshold)
     Disabled  intentionally below AA — not interactive, conveyed by state
     Inverse   white-on-dark surfaces                                     */
  --color-text-primary:   #111111;
  --color-text-secondary: #555555;
  --color-text-muted:     #767676;
  --color-text-disabled:  #a3a3a3;
  --color-text-inverse:   #fafafa;
  --color-text-on-accent: #ffffff; /* text placed directly on --color-accent */

  /* Interactive (links, primary buttons, focus indicators)
     #1d4ed8 on #fff → 7.7:1 ✓ */
  --color-interactive-default: #1d4ed8;
  --color-interactive-hover:   #1e40af;
  --color-interactive-active:  #1e3a8a;
  --color-interactive-subtle:  #dbeafe; /* tinted bg for ghost/secondary states */

  /* Accent (decorative highlights, badges, active indicators) */
  --color-accent:          #2563eb;
  --color-accent-subtle:   #dbeafe;

  /* Status — success
     Text #166534 on #fff → 8.1:1 ✓
     Text #166534 on #dcfce7 → 5.1:1 ✓ */
  --color-status-success-text: #166534;
  --color-status-success-bg:   #dcfce7;
  --color-status-success-border: #4ade80;

  /* Status — warning
     Text #92400e on #fff → 6.6:1 ✓
     Text #92400e on #fef9c3 → 4.6:1 ✓ */
  --color-status-warning-text: #92400e;
  --color-status-warning-bg:   #fef9c3;
  --color-status-warning-border: #fcd34d;

  /* Status — error
     Text #991b1b on #fff → 7.4:1 ✓
     Text #991b1b on #fee2e2 → 5.0:1 ✓ */
  --color-status-error-text: #991b1b;
  --color-status-error-bg:   #fee2e2;
  --color-status-error-border: #fca5a5;

  /* Status — info
     Text #075985 on #fff → 8.2:1 ✓
     Text #075985 on #e0f2fe → 5.5:1 ✓ */
  --color-status-info-text: #075985;
  --color-status-info-bg:   #e0f2fe;
  --color-status-info-border: #7dd3fc;

  /* Overlay / scrim */
  --color-overlay: rgba(0, 0, 0, 0.48);

  /* Shadows (light-mode feels) */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06), 0 1px 3px rgba(0, 0, 0, 0.10);
  --shadow-md: 0 4px 6px  rgba(0, 0, 0, 0.06), 0 2px 4px  rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.06), 0 4px 6px  rgba(0, 0, 0, 0.06);
  --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.08), 0 8px 10px rgba(0, 0, 0, 0.06);
}


/* ------------------------------------------------------------
   LAYER 4 — Dark theme
   Applied when: OS is dark  OR  [data-theme="dark"]
   ------------------------------------------------------------ */

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    /* Backgrounds */
    --color-bg-base:   #0a0a0a;
    --color-bg-subtle: #171717;
    --color-bg-muted:  #262626;

    /* Surfaces */
    --color-surface-default: #171717;
    --color-surface-raised:  #262626;
    --color-surface-overlay: #404040;

    /* Borders */
    --color-border-default: #404040; /* 3.1:1 on #0a0a0a ✓ */
    --color-border-strong:  #737373; /* 5.2:1 on #0a0a0a ✓ */
    --color-border-focus:   #60a5fa; /* 9.1:1 on #0a0a0a ✓ */

    /* Text
       Primary   #fafafa on #0a0a0a → 20.4:1 ✓
       Secondary #a3a3a3 on #0a0a0a →  7.3:1 ✓
       Muted     #737373 on #0a0a0a →  4.6:1 ✓
       Inverse   dark text for light surfaces               */
    --color-text-primary:   #fafafa;
    --color-text-secondary: #a3a3a3;
    --color-text-muted:     #737373;
    --color-text-disabled:  #404040;
    --color-text-inverse:   #111111;
    --color-text-on-accent: #ffffff;

    /* Interactive
       #60a5fa on #0a0a0a → 9.1:1 ✓ */
    --color-interactive-default: #60a5fa;
    --color-interactive-hover:   #93c5fd;
    --color-interactive-active:  #bfdbfe;
    --color-interactive-subtle:  #1e3a8a;

    /* Accent */
    --color-accent:        #60a5fa;
    --color-accent-subtle: #1e3a8a;

    /* Status — success
       #4ade80 on #0a0a0a → 11.5:1 ✓
       #4ade80 on #052e16 →  7.8:1 ✓ */
    --color-status-success-text:   #4ade80;
    --color-status-success-bg:     #052e16;
    --color-status-success-border: #166534;

    /* Status — warning
       #fcd34d on #0a0a0a → 14.2:1 ✓
       #fcd34d on #451a03 →  9.8:1 ✓ */
    --color-status-warning-text:   #fcd34d;
    --color-status-warning-bg:     #451a03;
    --color-status-warning-border: #b45309;

    /* Status — error
       #fca5a5 on #0a0a0a → 10.7:1 ✓
       #fca5a5 on #450a0a →  7.4:1 ✓ */
    --color-status-error-text:   #fca5a5;
    --color-status-error-bg:     #450a0a;
    --color-status-error-border: #b91c1c;

    /* Status — info
       #7dd3fc on #0a0a0a → 11.8:1 ✓
       #7dd3fc on #082f49 →  8.6:1 ✓ */
    --color-status-info-text:   #7dd3fc;
    --color-status-info-bg:     #082f49;
    --color-status-info-border: #0369a1;

    /* Overlay */
    --color-overlay: rgba(0, 0, 0, 0.72);

    /* Shadows (dark-mode uses inset highlights instead of drop shadows) */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.40), 0 0 0 1px rgba(255,255,255,0.04);
    --shadow-md: 0 4px 6px  rgba(0, 0, 0, 0.40), 0 0 0 1px rgba(255,255,255,0.04);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.40), 0 0 0 1px rgba(255,255,255,0.04);
    --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.48), 0 0 0 1px rgba(255,255,255,0.04);
  }
}

/* Explicit dark override — beats the media query when JS has set a preference */
[data-theme="dark"] {
  color-scheme: dark;

  --color-bg-base:   #0a0a0a;
  --color-bg-subtle: #171717;
  --color-bg-muted:  #262626;

  --color-surface-default: #171717;
  --color-surface-raised:  #262626;
  --color-surface-overlay: #404040;

  --color-border-default: #404040;
  --color-border-strong:  #737373;
  --color-border-focus:   #60a5fa;

  --color-text-primary:   #fafafa;
  --color-text-secondary: #a3a3a3;
  --color-text-muted:     #737373;
  --color-text-disabled:  #404040;
  --color-text-inverse:   #111111;
  --color-text-on-accent: #ffffff;

  --color-interactive-default: #60a5fa;
  --color-interactive-hover:   #93c5fd;
  --color-interactive-active:  #bfdbfe;
  --color-interactive-subtle:  #1e3a8a;

  --color-accent:        #60a5fa;
  --color-accent-subtle: #1e3a8a;

  --color-status-success-text:   #4ade80;
  --color-status-success-bg:     #052e16;
  --color-status-success-border: #166534;

  --color-status-warning-text:   #fcd34d;
  --color-status-warning-bg:     #451a03;
  --color-status-warning-border: #b45309;

  --color-status-error-text:   #fca5a5;
  --color-status-error-bg:     #450a0a;
  --color-status-error-border: #b91c1c;

  --color-status-info-text:   #7dd3fc;
  --color-status-info-bg:     #082f49;
  --color-status-info-border: #0369a1;

  --color-overlay: rgba(0, 0, 0, 0.72);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.40), 0 0 0 1px rgba(255,255,255,0.04);
  --shadow-md: 0 4px 6px  rgba(0, 0, 0, 0.40), 0 0 0 1px rgba(255,255,255,0.04);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.40), 0 0 0 1px rgba(255,255,255,0.04);
  --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.48), 0 0 0 1px rgba(255,255,255,0.04);
}


/* ------------------------------------------------------------
   THEME-INVARIANT DESIGN TOKENS
   Spacing, typography, radius, motion.
   These do not change between themes.
   ------------------------------------------------------------ */
:root {
  /* Spacing scale (base-4, rem) */
  --space-1:  0.25rem;   /*  4px */
  --space-2:  0.5rem;    /*  8px */
  --space-3:  0.75rem;   /* 12px */
  --space-4:  1rem;      /* 16px */
  --space-5:  1.25rem;   /* 20px */
  --space-6:  1.5rem;    /* 24px */
  --space-8:  2rem;      /* 32px */
  --space-10: 2.5rem;    /* 40px */
  --space-12: 3rem;      /* 48px */
  --space-16: 4rem;      /* 64px */
  --space-20: 5rem;      /* 80px */
  --space-24: 6rem;      /* 96px */

  /* Border radius */
  --radius-sm:   0.25rem;  /*  4px */
  --radius-md:   0.5rem;   /*  8px */
  --radius-lg:   0.75rem;  /* 12px */
  --radius-xl:   1rem;     /* 16px */
  --radius-2xl:  1.5rem;   /* 24px */
  --radius-full: 9999px;

  /* Typography */
  --font-sans:  'Inter', ui-sans-serif, system-ui, -apple-system, sans-serif;
  --font-mono:  'JetBrains Mono', ui-monospace, 'Cascadia Code', monospace;

  --text-xs:   0.75rem;   /* 12px */
  --text-sm:   0.875rem;  /* 14px */
  --text-base: 1rem;      /* 16px */
  --text-lg:   1.125rem;  /* 18px */
  --text-xl:   1.25rem;   /* 20px */
  --text-2xl:  1.5rem;    /* 24px */
  --text-3xl:  1.875rem;  /* 30px */
  --text-4xl:  2.25rem;   /* 36px */
  --text-5xl:  3rem;      /* 48px */

  --leading-tight:  1.25;
  --leading-snug:   1.375;
  --leading-normal: 1.5;
  --leading-relaxed:1.625;

  --weight-regular:  400;
  --weight-medium:   500;
  --weight-semibold: 600;
  --weight-bold:     700;

  /* Motion */
  --duration-fast:   100ms;
  --duration-normal: 200ms;
  --duration-slow:   350ms;
  --ease-default:    cubic-bezier(0.4, 0, 0.2, 1);
  --ease-in:         cubic-bezier(0.4, 0, 1, 1);
  --ease-out:        cubic-bezier(0, 0, 0.2, 1);
  --ease-spring:     cubic-bezier(0.34, 1.56, 0.64, 1);

  /* Z-index scale */
  --z-base:    0;
  --z-raised:  10;
  --z-dropdown:200;
  --z-sticky:  300;
  --z-overlay: 400;
  --z-modal:   500;
  --z-toast:   600;
  --z-tooltip: 700;
}


/* ------------------------------------------------------------
   BASE RESET
   Applies semantic tokens to raw HTML elements so components
   get correct colors without needing explicit class overrides.
   ------------------------------------------------------------ */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  text-size-adjust: 100%;
}

body {
  background-color: var(--color-bg-base);
  color: var(--color-text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition:
    background-color var(--duration-normal) var(--ease-default),
    color           var(--duration-normal) var(--ease-default);
}

/* Respect user motion preferences */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

a {
  color: var(--color-interactive-default);
  text-decoration: underline;
  text-underline-offset: 3px;
}
a:hover  { color: var(--color-interactive-hover);  }
a:active { color: var(--color-interactive-active); }

:focus-visible {
  outline: 2px solid var(--color-border-focus);
  outline-offset: 3px;
  border-radius: var(--radius-sm);
}

hr {
  border: none;
  border-top: 1px solid var(--color-border-default);
}
