/*
 * Design tokens.
 *
 * Every colour, radius, shadow, spacing step and timing used by the
 * stylesheets is a custom property defined here. The light theme is the
 * default; the dark theme applies to html[data-theme="dark"] (set before
 * first paint by assets/js/theme-init.js) and, as a fallback while no
 * data-theme attribute is present, to the system dark preference.
 *
 * Naming:
 *   --bg-*        page level backgrounds (behind cards)
 *   --surface-*   card / control backgrounds, from base to pressed
 *   --text-*      text levels: default, muted (secondary), subtle (meta), disabled
 *   --border-*    hairlines
 *   --primary-*   primary action (buttons); --brand-* the brand navy; --accent-* the highlight blue
 *   --<status>, --<status>-bg, --<status>-border, --<status>-solid
 *                 semantic colours: text on subtle background, subtle background,
 *                 border of the subtle variant, solid fill (white text)
 *   --<status>-icon, --<status>-title, --<status>-fg, --<status>-btn-bg,
 *   --<status>-btn-border, --<status>-btn-hover
 *                 variant palette of tinted boxes (alerts, toasts, callouts): icon,
 *                 heading and links, body text, and the buttons placed on the box.
 *                 Every variant (info, success, warning, danger, neutral, accent) has
 *                 the full set in both themes; backgrounds and borders are solid
 *                 colours, text meets WCAG AA on --<status>-bg.
 *                 The accent variant uses --accent-subtle and --accent-border as
 *                 background and border.
 *   --focus*      focus indicator: --focus is the outline / border colour (at least
 *                 3:1 against the surfaces), --focus-ring the ring of text fields:
 *                 a soft halo in the light theme, a crisp second pixel of the
 *                 focus colour in the dark theme, where a halo looks muddy
 *   --raised-hover  hover and keyboard highlight of rows on --surface-raised
 *                 (menus, select lists), where --surface-hover is too faint
 *   --title-gap*  space between a heading and the description under it
 *   --chart-*     chart series and chrome
 */

:root {
    color-scheme: light;

    /* Brand */
    --brand: #273251;
    --brand-hover: #323f66;
    --brand-fg: #ffffff;
    --accent: #4545ff;
    --accent-hover: #3535e6;
    --accent-text: #3d3df0;
    --accent-subtle: #f0f0ff;
    --accent-solid: #4545ff;
    --accent-solid-hover: #3535e6;
    --accent-subtle-hover: #e6e6ff;
    --accent-border: #d6d6ff;
    --accent-icon: #4545ff;
    --accent-title: #1f1f73;
    --accent-fg: #3a3a8c;
    --accent-btn-bg: #e4e4ff;
    --accent-btn-border: #c4c4fb;
    --accent-btn-hover: #d8d8ff;

    /* Page backgrounds */
    --bg: #f4f6fa;
    --bg-sidebar: #fbfcfe;
    --bg-overlay: rgba(14, 19, 32, 0.45);

    /* Surfaces */
    --surface: #ffffff;
    --surface-muted: #f8f9fc;
    --surface-hover: #f2f4f8;
    --raised-hover: #eef1f6;
    --surface-active: #eceff5;
    --surface-raised: #ffffff;
    --surface-inverse: #1c2333;

    /* Text */
    --text: #1c2333;
    --text-muted: #5b6478;
    --text-subtle: #646d82;
    --text-disabled: #9aa2b4;
    --text-inverse: #ffffff;
    --link: var(--accent-text);
    --link-hover: #2a2ac4;

    /* Borders */
    --border: #e2e6ee;
    --border-muted: #edf0f5;
    --border-strong: #d3d9e3;
    --input-border: #d6dbe5;
    --input-border-hover: #c3cad8;
    --input-bg: #ffffff;
    --control-border: #7d869b;    /* unchecked checkbox, radio and switch: at least 3:1 against the surfaces */

    /* Primary action */
    --primary: var(--brand);
    --primary-hover: var(--brand-hover);
    --primary-fg: #ffffff;

    /* Navigation */
    --nav-active-bg: rgba(69, 69, 255, 0.07);
    --nav-active-text: var(--text);
    --nav-active-icon: var(--accent);
    --nav-hover-bg: #eff1f6;

    /* Semantic colours */
    --success: #15803d;
    --success-bg: #effaf3;
    --success-border: #bfe5cc;
    --success-solid: #16a34a;
    --success-icon: #16a34a;
    --success-title: #14532d;
    --success-fg: #276143;
    --success-btn-bg: #ddf3e5;
    --success-btn-border: #a8dbbb;
    --success-btn-hover: #cdebd8;
    --warning: #b45309;
    --warning-bg: #fffaeb;
    --warning-border: #f3d996;
    --warning-solid: #d97706;
    --warning-icon: #d97706;
    --warning-title: #6f300c;
    --warning-fg: #7a4b10;
    --warning-btn-bg: #fdf0c8;
    --warning-btn-border: #e9c878;
    --warning-btn-hover: #f9e5ab;
    --danger: #c62828;
    --danger-bg: #fef3f3;
    --danger-border: #f4c9c9;
    --danger-solid: #dc2626;
    --danger-solid-hover: #c81e1e;
    --danger-icon: #dc2626;
    --danger-title: #7f1d1d;
    --danger-fg: #8f2b2b;
    --danger-btn-bg: #fde3e3;
    --danger-btn-border: #eeb3b3;
    --danger-btn-hover: #fad3d3;
    --info: #0369a1;
    --info-bg: #f0f8ff;
    --info-border: #c3def4;
    --info-solid: #0284c7;
    --info-icon: #0284c7;
    --info-title: #0c3d5c;
    --info-fg: #285d7f;
    --info-btn-bg: #e0effb;
    --info-btn-border: #a9cfee;
    --info-btn-hover: #cfe5f7;
    --neutral: #4b5468;
    --neutral-bg: #f1f3f7;
    --neutral-border: #dfe3ea;
    --neutral-icon: #646d82;
    --neutral-title: #1c2333;
    --neutral-fg: #485165;
    --neutral-btn-bg: #ffffff;
    --neutral-btn-border: #d3d9e3;
    --neutral-btn-hover: #f6f7fa;

    /* Ratings */
    --star: #f59e0b;
    --star-empty: #dde1e9;
    --positive: #15803d;
    --negative: #c62828;

    /* Focus: buttons and toggles get a 2 px outline with an offset, text fields a coloured border and halo. */
    --focus: #4545ff;
    --focus-danger: #dc2626;
    --focus-width: 2px;
    --focus-offset: 2px;
    --focus-ring: 0 0 0 3px rgba(69, 69, 255, 0.2);
    --focus-ring-danger: 0 0 0 3px rgba(220, 38, 38, 0.18);

    /* Charts */
    --chart-1: #4545ff;
    --chart-2: #eb6834;
    --chart-3: #1baf7a;
    --chart-4: #eda100;
    --chart-5: #e87ba4;
    --chart-6: #6250d6;
    --chart-area-opacity: 0.1;
    --chart-grid: #e8ebf1;
    --chart-axis: #d3d9e3;
    --chart-text: #646d82;
    --chart-surface: #ffffff;

    /* Skeleton shimmer */
    --skeleton: #edf0f5;
    --skeleton-shine: #f6f8fb;

    /* Shadows */
    --shadow-xs: 0 1px 2px rgba(16, 24, 40, 0.04);
    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.04);
    --shadow-md: 0 4px 10px -2px rgba(16, 24, 40, 0.07), 0 2px 4px -2px rgba(16, 24, 40, 0.05);
    --shadow-lg: 0 12px 28px -6px rgba(16, 24, 40, 0.14), 0 4px 10px -4px rgba(16, 24, 40, 0.06);
    --shadow-xl: 0 24px 48px -12px rgba(16, 24, 40, 0.22);

    /* Radii */
    --radius-xs: 4px;
    --radius-sm: 6px;
    --radius: 8px;
    --radius-lg: 10px;
    --radius-xl: 14px;
    --radius-full: 9999px;

    /* Spacing (4 px grid) */
    --space-0: 0;
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;
    --space-12: 48px;
    --space-16: 64px;

    /* Typography */
    --font-sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
    --text-xs: 12px;
    --text-sm: 13px;
    --text-base: 14px;
    --text-md: 15px;
    --text-lg: 17px;
    --text-xl: 20px;
    --text-2xl: 24px;
    --text-3xl: 30px;
    --text-4xl: 36px;
    --leading-tight: 1.25;
    --leading: 1.5;
    --leading-relaxed: 1.65;
    --title-gap-sm: 4px;    /* alerts, toasts, list items */
    --title-gap: 6px;       /* cards, sections, form sections, dialogs, empty states */
    --title-gap-lg: 8px;    /* page header */

    /* Sizes */
    --control-h: 36px;
    --control-h-sm: 30px;
    --control-h-lg: 42px;
    --sidebar-w: 248px;
    --topbar-h: 56px;
    --container: 1200px;
    --container-narrow: 760px;
    --container-wide: 1440px;

    /* Layers */
    --z-sticky: 20;
    --z-sidebar: 30;
    --z-dropdown: 50;
    --z-drawer: 60;
    --z-dialog: 70;
    --z-toast: 80;
    --z-tooltip: 90;

    /* Motion */
    --ease: cubic-bezier(0.2, 0, 0, 1);
    --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
    --duration-fast: 120ms;
    --duration: 180ms;
    --duration-slow: 280ms;
    --transition-colors: color var(--duration-fast) var(--ease), background-color var(--duration-fast) var(--ease), border-color var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease);
}

/* Dark theme values. Applied by data-theme, or by the system preference until a theme is resolved. */
:root[data-theme="dark"] {
    color-scheme: dark;

    --brand: #273251;
    --brand-hover: #323f66;
    --brand-fg: #ffffff;
    --accent: #6b6bff;
    --accent-hover: #7b7bff;
    --accent-text: #9494ff;
    --accent-subtle: #1f2449;
    --accent-solid: #4545ff;
    --accent-solid-hover: #5555ff;
    --accent-subtle-hover: #262c58;
    --accent-border: #383e7c;
    --accent-icon: #9494ff;
    --accent-title: #dedeff;
    --accent-fg: #babaf3;
    --accent-btn-bg: #272c5c;
    --accent-btn-border: #444a91;
    --accent-btn-hover: #2f356c;

    --bg: #0e1320;
    --bg-sidebar: #111726;
    --bg-overlay: rgba(4, 7, 14, 0.62);

    --surface: #161c2c;
    --surface-muted: #1a2133;
    --surface-hover: #1f2639;
    --raised-hover: #2b3554;
    --surface-active: #252e44;
    --surface-raised: #1b2234;
    --surface-inverse: #e6e9f2;

    --text: #e6e9f2;
    --text-muted: #9aa3b8;
    --text-subtle: #8a93a8;
    --text-disabled: #5d667c;
    --text-inverse: #0e1320;
    --link: var(--accent-text);
    --link-hover: #b0b0ff;

    --border: #262f45;
    --border-muted: #1f2739;
    --border-strong: #323c55;
    --input-border: #2f3950;
    --input-border-hover: #3c4763;
    --input-bg: #121828;
    --control-border: #6b7590;

    --primary: #4545ff;
    --primary-hover: #5555ff;
    --primary-fg: #ffffff;

    --nav-active-bg: rgba(110, 110, 255, 0.13);
    --nav-active-text: #f1f3f9;
    --nav-active-icon: #9494ff;
    --nav-hover-bg: #1a2133;

    --success: #4cc38a;
    --success-bg: #10291f;
    --success-border: #1f4f39;
    --success-solid: #16a34a;
    --success-icon: #4cc38a;
    --success-title: #c8f5dc;
    --success-fg: #98d9b5;
    --success-btn-bg: #15372a;
    --success-btn-border: #2a6448;
    --success-btn-hover: #1a4333;
    --warning: #f5b544;
    --warning-bg: #2a2112;
    --warning-border: #5c4519;
    --warning-solid: #d97706;
    --warning-icon: #f5b544;
    --warning-title: #fde7b3;
    --warning-fg: #e6c584;
    --warning-btn-bg: #3a2d14;
    --warning-btn-border: #6d5220;
    --warning-btn-hover: #463719;
    --danger: #f87171;
    --danger-bg: #2c161b;
    --danger-border: #622b33;
    --danger-solid: #dc2626;
    --danger-solid-hover: #e23b3b;
    --danger-icon: #f87171;
    --danger-title: #ffd7d7;
    --danger-fg: #f0aaaa;
    --danger-btn-bg: #3d1d23;
    --danger-btn-border: #77353d;
    --danger-btn-hover: #4a232a;
    --info: #5cb8f0;
    --info-bg: #10233a;
    --info-border: #21466a;
    --info-solid: #0284c7;
    --info-icon: #5cb8f0;
    --info-title: #d4ecff;
    --info-fg: #a3cdee;
    --info-btn-bg: #16304d;
    --info-btn-border: #2b5884;
    --info-btn-hover: #1b3a5c;
    --neutral: #b4bccd;
    --neutral-bg: #1c2335;
    --neutral-border: #2e374e;
    --neutral-icon: #9aa3b8;
    --neutral-title: #e6e9f2;
    --neutral-fg: #b4bccd;
    --neutral-btn-bg: #242c42;
    --neutral-btn-border: #384259;
    --neutral-btn-hover: #2b344c;

    --star: #f5a524;
    --star-empty: #2e374d;
    --positive: #4cc38a;
    --negative: #f87171;

    --focus: #8585ff;
    --focus-danger: #f87171;
    --focus-ring: 0 0 0 1px #8585ff;
    --focus-ring-danger: 0 0 0 1px #f87171;

    --chart-1: #7373ff;
    --chart-2: #d95926;
    --chart-3: #199e70;
    --chart-4: #c98500;
    --chart-5: #d55181;
    --chart-6: #9085e9;
    --chart-area-opacity: 0.14;
    --chart-grid: #222a3d;
    --chart-axis: #323c55;
    --chart-text: #8a93a8;
    --chart-surface: #161c2c;

    --skeleton: #1f2639;
    --skeleton-shine: #262f45;

    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.24);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 3px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 12px -2px rgba(0, 0, 0, 0.4), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 14px 32px -8px rgba(0, 0, 0, 0.55), 0 4px 12px -4px rgba(0, 0, 0, 0.35);
    --shadow-xl: 0 28px 56px -12px rgba(0, 0, 0, 0.65);
}

/*
 * Without the theme script (no JavaScript) the system preference applies: a copy of the dark theme above,
 * token for token and value for value (tests/tokens_test.php fails when the two blocks differ).
 */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme]) {
        color-scheme: dark;

        --brand: #273251;
        --brand-hover: #323f66;
        --brand-fg: #ffffff;
        --accent: #6b6bff;
        --accent-hover: #7b7bff;
        --accent-text: #9494ff;
        --accent-subtle: #1f2449;
        --accent-solid: #4545ff;
        --accent-solid-hover: #5555ff;
        --accent-subtle-hover: #262c58;
        --accent-border: #383e7c;
        --accent-icon: #9494ff;
        --accent-title: #dedeff;
        --accent-fg: #babaf3;
        --accent-btn-bg: #272c5c;
        --accent-btn-border: #444a91;
        --accent-btn-hover: #2f356c;

        --bg: #0e1320;
        --bg-sidebar: #111726;
        --bg-overlay: rgba(4, 7, 14, 0.62);

        --surface: #161c2c;
        --surface-muted: #1a2133;
        --surface-hover: #1f2639;
        --raised-hover: #2b3554;
        --surface-active: #252e44;
        --surface-raised: #1b2234;
        --surface-inverse: #e6e9f2;

        --text: #e6e9f2;
        --text-muted: #9aa3b8;
        --text-subtle: #8a93a8;
        --text-disabled: #5d667c;
        --text-inverse: #0e1320;
        --link: var(--accent-text);
        --link-hover: #b0b0ff;

        --border: #262f45;
        --border-muted: #1f2739;
        --border-strong: #323c55;
        --input-border: #2f3950;
        --input-border-hover: #3c4763;
        --input-bg: #121828;
        --control-border: #6b7590;

        --primary: #4545ff;
        --primary-hover: #5555ff;
        --primary-fg: #ffffff;

        --nav-active-bg: rgba(110, 110, 255, 0.13);
        --nav-active-text: #f1f3f9;
        --nav-active-icon: #9494ff;
        --nav-hover-bg: #1a2133;

        --success: #4cc38a;
        --success-bg: #10291f;
        --success-border: #1f4f39;
        --success-solid: #16a34a;
        --success-icon: #4cc38a;
        --success-title: #c8f5dc;
        --success-fg: #98d9b5;
        --success-btn-bg: #15372a;
        --success-btn-border: #2a6448;
        --success-btn-hover: #1a4333;
        --warning: #f5b544;
        --warning-bg: #2a2112;
        --warning-border: #5c4519;
        --warning-solid: #d97706;
        --warning-icon: #f5b544;
        --warning-title: #fde7b3;
        --warning-fg: #e6c584;
        --warning-btn-bg: #3a2d14;
        --warning-btn-border: #6d5220;
        --warning-btn-hover: #463719;
        --danger: #f87171;
        --danger-bg: #2c161b;
        --danger-border: #622b33;
        --danger-solid: #dc2626;
        --danger-solid-hover: #e23b3b;
        --danger-icon: #f87171;
        --danger-title: #ffd7d7;
        --danger-fg: #f0aaaa;
        --danger-btn-bg: #3d1d23;
        --danger-btn-border: #77353d;
        --danger-btn-hover: #4a232a;
        --info: #5cb8f0;
        --info-bg: #10233a;
        --info-border: #21466a;
        --info-solid: #0284c7;
        --info-icon: #5cb8f0;
        --info-title: #d4ecff;
        --info-fg: #a3cdee;
        --info-btn-bg: #16304d;
        --info-btn-border: #2b5884;
        --info-btn-hover: #1b3a5c;
        --neutral: #b4bccd;
        --neutral-bg: #1c2335;
        --neutral-border: #2e374e;
        --neutral-icon: #9aa3b8;
        --neutral-title: #e6e9f2;
        --neutral-fg: #b4bccd;
        --neutral-btn-bg: #242c42;
        --neutral-btn-border: #384259;
        --neutral-btn-hover: #2b344c;

        --star: #f5a524;
        --star-empty: #2e374d;
        --positive: #4cc38a;
        --negative: #f87171;

        --focus: #8585ff;
        --focus-danger: #f87171;
        --focus-ring: 0 0 0 1px #8585ff;
        --focus-ring-danger: 0 0 0 1px #f87171;

        --chart-1: #7373ff;
        --chart-2: #d95926;
        --chart-3: #199e70;
        --chart-4: #c98500;
        --chart-5: #d55181;
        --chart-6: #9085e9;
        --chart-area-opacity: 0.14;
        --chart-grid: #222a3d;
        --chart-axis: #323c55;
        --chart-text: #8a93a8;
        --chart-surface: #161c2c;

        --skeleton: #1f2639;
        --skeleton-shine: #262f45;

        --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.24);
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 3px rgba(0, 0, 0, 0.2);
        --shadow-md: 0 4px 12px -2px rgba(0, 0, 0, 0.4), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 14px 32px -8px rgba(0, 0, 0, 0.55), 0 4px 12px -4px rgba(0, 0, 0, 0.35);
        --shadow-xl: 0 28px 56px -12px rgba(0, 0, 0, 0.65);
    }
}
