/* Layer: core | tokens.css
 * Responsibility: Pure-CSS design tokens (Dark default + Light mode) and @layer cascade order.
 * Loaded: Globally via templates/layouts/base.html on every page.
 * Limit: Hard < 700 lines. Zero hardcoded colors outside this file.
 */

@layer tokens, base, layout, components;

@layer tokens {
  :root,
  :root[data-theme="dark"] {
    color-scheme: dark;

    /* Surfaces */
    --bg-canvas: #0b0d11;
    --bg-subtle: #10131a;
    --bg-surface: #141821;
    --bg-surface-hover: #191e2a;
    --bg-inset: #0d1017;
    --bg-overlay: rgba(11, 13, 17, 0.84);
    --bg-ambient: radial-gradient(
      900px 420px at 50% -120px,
      rgba(99, 148, 255, 0.1),
      transparent 72%
    );

    /* Typography colors (WCAG AA/AAA verified on dark surfaces) */
    --text-primary: #eef1f6;
    --text-secondary: #a4aebd;
    --text-muted: #8b96a8;
    --text-inverse: #0b0d11;

    /* Hairline borders */
    --border-subtle: rgba(255, 255, 255, 0.06);
    --border-default: rgba(255, 255, 255, 0.11);
    --border-strong: rgba(255, 255, 255, 0.2);
    --border-accent: rgba(104, 157, 255, 0.42);

    /* Brand & interactive accent */
    --accent: #6da1ff;
    --accent-hover: #90b8ff;
    --accent-soft: rgba(109, 161, 255, 0.12);
    --accent-border: rgba(109, 161, 255, 0.28);
    --focus-ring: #82b0ff;
    --focus-ring-offset: #0b0d11;

    /* Verification & availability status colors */
    --status-verified-fg: #4ade80;
    --status-verified-bg: rgba(34, 197, 94, 0.12);
    --status-verified-border: rgba(74, 222, 128, 0.28);
    --status-verified-dot: #22c55e;

    --status-failed-fg: #fb7185;
    --status-failed-bg: rgba(244, 63, 94, 0.13);
    --status-failed-border: rgba(251, 113, 133, 0.32);
    --status-failed-dot: #f43f5e;

    --status-unverified-fg: #a4aebd;
    --status-unverified-bg: rgba(164, 174, 189, 0.1);
    --status-unverified-border: rgba(164, 174, 189, 0.22);
    --status-unverified-dot: #7c879a;

    --status-unavailable-fg: #fbbf24;
    --status-unavailable-bg: rgba(245, 158, 11, 0.12);
    --status-unavailable-border: rgba(251, 191, 36, 0.3);
    --status-unavailable-dot: #f59e0b;

    /* Note-type semantic colors (decision / dead_end / bug / todo / neutral) */
    --note-decision-fg: #c4b5fd;
    --note-decision-bg: rgba(167, 139, 250, 0.13);
    --note-decision-border: rgba(167, 139, 250, 0.28);

    --note-dead-end-fg: #fdba74;
    --note-dead-end-bg: rgba(251, 146, 60, 0.12);
    --note-dead-end-border: rgba(251, 146, 60, 0.28);

    --note-bug-fg: #fda4af;
    --note-bug-bg: rgba(244, 63, 94, 0.14);
    --note-bug-border: rgba(244, 63, 94, 0.32);

    --note-todo-fg: #7dd3fc;
    --note-todo-bg: rgba(56, 189, 248, 0.12);
    --note-todo-border: rgba(56, 189, 248, 0.28);

    --note-neutral-fg: #a4aebd;
    --note-neutral-bg: rgba(164, 174, 189, 0.09);
    --note-neutral-border: rgba(164, 174, 189, 0.2);

    /* Syntax highlighting & inline word-diff (WCAG AA on --bg-inset & diff tints) */
    --sy-kw: #d2a8ff;
    --sy-ty: #f0c674;
    --sy-fn: #79c0ff;
    --sy-str: #8ddb8c;
    --sy-num: #f2a679;
    --sy-cmt: #8b96a8;
    --sy-mac: #76d7c4;
    --sy-const: #ffa198;
    --sy-attr: #b8a9f5;
    --sy-op: #93b4d8;
    --sy-punc: #9ca6b8;
    --diff-word-add-bg: rgba(46, 160, 67, 0.34);
    --diff-word-del-bg: rgba(248, 81, 73, 0.34);

    /* Elevation */
    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.36);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.03) inset;
    --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.44), 0 1px 0 rgba(255, 255, 255, 0.04) inset;

    /* Typography scale */
    --font-sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
      Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", "Cascadia Code",
      Menlo, Monaco, Consolas, "Liberation Mono", monospace;

    --text-2xs: 0.6875rem;
    --text-xs: 0.75rem;
    --text-sm: 0.8125rem;
    --text-base: 0.875rem;
    --text-md: 0.9375rem;
    --text-lg: 1.0625rem;
    --text-xl: 1.375rem;
    --text-2xl: 1.625rem;

    --leading-tight: 1.25;
    --leading-snug: 1.4;
    --leading-normal: 1.55;

    /* Spacing scale */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.25rem;
    --space-6: 1.5rem;
    --space-7: 1.75rem;
    --space-8: 2rem;
    --space-10: 2.5rem;
    --space-12: 3rem;

    /* Radii */
    --radius-xs: 4px;
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 12px;
    --radius-pill: 9999px;

    /* Layout & motion */
    --container-max: 1280px;
    --header-height: 56px;
    --transition-fast: 140ms cubic-bezier(0.2, 0, 0, 1);
    --transition-base: 200ms cubic-bezier(0.2, 0, 0, 1);
  }

  /* Light mode — explicit attribute override */
  :root[data-theme="light"] {
    color-scheme: light;

    --bg-canvas: #f5f7fa;
    --bg-subtle: #ffffff;
    --bg-surface: #ffffff;
    --bg-surface-hover: #fbfcfe;
    --bg-inset: #f0f3f7;
    --bg-overlay: rgba(255, 255, 255, 0.86);
    --bg-ambient: radial-gradient(
      900px 420px at 50% -120px,
      rgba(37, 99, 235, 0.06),
      transparent 72%
    );

    --text-primary: #111827;
    --text-secondary: #475569;
    --text-muted: #5a677b;
    --text-inverse: #ffffff;

    --border-subtle: rgba(15, 23, 42, 0.07);
    --border-default: rgba(15, 23, 42, 0.13);
    --border-strong: rgba(15, 23, 42, 0.24);
    --border-accent: rgba(37, 99, 235, 0.36);

    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --accent-soft: rgba(37, 99, 235, 0.08);
    --accent-border: rgba(37, 99, 235, 0.24);
    --focus-ring: #2563eb;
    --focus-ring-offset: #ffffff;

    --status-verified-fg: #15803d;
    --status-verified-bg: rgba(22, 163, 74, 0.1);
    --status-verified-border: rgba(22, 163, 74, 0.28);
    --status-verified-dot: #16a34a;

    --status-failed-fg: #be123c;
    --status-failed-bg: rgba(225, 29, 72, 0.09);
    --status-failed-border: rgba(225, 29, 72, 0.26);
    --status-failed-dot: #e11d48;

    --status-unverified-fg: #475569;
    --status-unverified-bg: rgba(71, 85, 105, 0.09);
    --status-unverified-border: rgba(71, 85, 105, 0.22);
    --status-unverified-dot: #64748b;

    --status-unavailable-fg: #b45309;
    --status-unavailable-bg: rgba(217, 119, 6, 0.1);
    --status-unavailable-border: rgba(217, 119, 6, 0.28);
    --status-unavailable-dot: #d97706;

    --note-decision-fg: #6d28d9;
    --note-decision-bg: rgba(124, 58, 237, 0.09);
    --note-decision-border: rgba(124, 58, 237, 0.24);

    --note-dead-end-fg: #c2410c;
    --note-dead-end-bg: rgba(234, 88, 12, 0.09);
    --note-dead-end-border: rgba(234, 88, 12, 0.24);

    --note-bug-fg: #be123c;
    --note-bug-bg: rgba(225, 29, 72, 0.09);
    --note-bug-border: rgba(225, 29, 72, 0.24);

    --note-todo-fg: #0369a1;
    --note-todo-bg: rgba(2, 132, 199, 0.09);
    --note-todo-border: rgba(2, 132, 199, 0.24);

    --note-neutral-fg: #475569;
    --note-neutral-bg: rgba(71, 85, 105, 0.08);
    --note-neutral-border: rgba(71, 85, 105, 0.19);

    --sy-kw: #6d28d9;
    --sy-ty: #9a5b13;
    --sy-fn: #1d4ed8;
    --sy-str: #157347;
    --sy-num: #b45309;
    --sy-cmt: #5a677b;
    --sy-mac: #0f766e;
    --sy-const: #be123c;
    --sy-attr: #5b21b6;
    --sy-op: #334155;
    --sy-punc: #475569;
    --diff-word-add-bg: rgba(22, 163, 74, 0.24);
    --diff-word-del-bg: rgba(225, 29, 72, 0.21);

    --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.05);
    --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 10px 24px rgba(15, 23, 42, 0.09);
  }

  /* OS-level light preference fallback when user has not forced dark mode */
  @media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) {
      color-scheme: light;

      --bg-canvas: #f5f7fa;
      --bg-subtle: #ffffff;
      --bg-surface: #ffffff;
      --bg-surface-hover: #fbfcfe;
      --bg-inset: #f0f3f7;
      --bg-overlay: rgba(255, 255, 255, 0.86);
      --bg-ambient: radial-gradient(
        900px 420px at 50% -120px,
        rgba(37, 99, 235, 0.06),
        transparent 72%
      );

      --text-primary: #111827;
      --text-secondary: #475569;
      --text-muted: #5a677b;
      --text-inverse: #ffffff;

      --border-subtle: rgba(15, 23, 42, 0.07);
      --border-default: rgba(15, 23, 42, 0.13);
      --border-strong: rgba(15, 23, 42, 0.24);
      --border-accent: rgba(37, 99, 235, 0.36);

      --accent: #2563eb;
      --accent-hover: #1d4ed8;
      --accent-soft: rgba(37, 99, 235, 0.08);
      --accent-border: rgba(37, 99, 235, 0.24);
      --focus-ring: #2563eb;
      --focus-ring-offset: #ffffff;

      --status-verified-fg: #15803d;
      --status-verified-bg: rgba(22, 163, 74, 0.1);
      --status-verified-border: rgba(22, 163, 74, 0.28);
      --status-verified-dot: #16a34a;

      --status-failed-fg: #be123c;
      --status-failed-bg: rgba(225, 29, 72, 0.09);
      --status-failed-border: rgba(225, 29, 72, 0.26);
      --status-failed-dot: #e11d48;

      --status-unverified-fg: #475569;
      --status-unverified-bg: rgba(71, 85, 105, 0.09);
      --status-unverified-border: rgba(71, 85, 105, 0.22);
      --status-unverified-dot: #64748b;

      --status-unavailable-fg: #b45309;
      --status-unavailable-bg: rgba(217, 119, 6, 0.1);
      --status-unavailable-border: rgba(217, 119, 6, 0.28);
      --status-unavailable-dot: #d97706;

      --note-decision-fg: #6d28d9;
      --note-decision-bg: rgba(124, 58, 237, 0.09);
      --note-decision-border: rgba(124, 58, 237, 0.24);

      --note-dead-end-fg: #c2410c;
      --note-dead-end-bg: rgba(234, 88, 12, 0.09);
      --note-dead-end-border: rgba(234, 88, 12, 0.24);

      --note-bug-fg: #be123c;
      --note-bug-bg: rgba(225, 29, 72, 0.09);
      --note-bug-border: rgba(225, 29, 72, 0.24);

      --note-todo-fg: #0369a1;
      --note-todo-bg: rgba(2, 132, 199, 0.09);
      --note-todo-border: rgba(2, 132, 199, 0.24);

      --note-neutral-fg: #475569;
      --note-neutral-bg: rgba(71, 85, 105, 0.08);
      --note-neutral-border: rgba(71, 85, 105, 0.19);

      --sy-kw: #6d28d9;
      --sy-ty: #9a5b13;
      --sy-fn: #1d4ed8;
      --sy-str: #157347;
      --sy-num: #b45309;
      --sy-cmt: #5a677b;
      --sy-mac: #0f766e;
      --sy-const: #be123c;
      --sy-attr: #5b21b6;
      --sy-op: #334155;
      --sy-punc: #475569;
      --diff-word-add-bg: rgba(22, 163, 74, 0.24);
      --diff-word-del-bg: rgba(225, 29, 72, 0.21);

      --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.05);
      --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.06);
      --shadow-md: 0 10px 24px rgba(15, 23, 42, 0.09);
    }
  }
}
