/* ==========================================================================
   HAGE design tokens: the single source of colour, type, space, shape and
   motion for the admin UI. Components use these tokens only.

   Load order (layout/default.php): legacy CSS -> hage-tokens.css ->
   theme/<slug>.css (accent-only override) -> hage-shell.css -> hage-components.css

   Contrast for every pair here is checked by tools/ui/contrast.mjs.
   ========================================================================== */

/* ---- Fonts: self-hosted WOFF2, swap, Latin subsets only ----------------- */
/* Dugsora type roles:
   Rubik             -> the logo / wordmark only (--font-brand)
   Plus Jakarta Sans -> page, section and card titles (--font-ui)
   Inter             -> text, labels, buttons and every number (--font-body, --font-num) */
@font-face {
    font-family: "Plus Jakarta Sans";
    font-style: normal;
    font-weight: 200 800;
    font-display: swap;
    src: url("fonts/hage/plus-jakarta-sans-latin-var.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
    font-family: "Rubik";
    font-style: normal;
    font-weight: 300 900;
    font-display: swap;
    src: url("fonts/hage/rubik-latin-var.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
    font-family: "Inter";
    font-style: normal;
    font-weight: 100 900;
    font-display: swap;
    src: url("fonts/hage/inter-latin-var.woff2") format("woff2");
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* Arabic: only downloaded when a page actually renders Arabic glyphs
   (the browser fetches a face only for characters inside its unicode-range).
   One weight keeps the font budget under 150 KB; bold is synthesised. */
@font-face {
    font-family: "IBM Plex Sans Arabic";
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url("fonts/hage/ibm-plex-sans-arabic-400.woff2") format("woff2");
    unicode-range: U+0600-06FF, U+0750-077F, U+0870-088E, U+0890-0891, U+0897-08E1, U+08E3-08FF, U+FB50-FDFF, U+FE70-FE74, U+FE76-FEFC;
}

/* Metric-matched local fallbacks, used while the web fonts load, so text
   does not reflow when they swap in. Values computed from the fonts'
   real metrics vs Arial (size-adjust = avg-width ratio). */
@font-face {
    font-family: "Inter Fallback";
    src: local("Arial"), local("Helvetica"), local("Liberation Sans");
    size-adjust: 107.12%;
    ascent-override: 90.44%;
    descent-override: 22.52%;
    line-gap-override: 0%;
}

@font-face {
    font-family: "Plus Jakarta Sans Fallback";
    src: local("Arial"), local("Helvetica"), local("Liberation Sans");
    size-adjust: 104.98%;
    ascent-override: 98.88%;
    descent-override: 21.15%;
    line-gap-override: 0%;
}

@font-face {
    font-family: "Rubik Fallback";
    src: local("Arial"), local("Helvetica"), local("Liberation Sans");
}

/* Dugsora palette: Blue #2754F5, Deep Blue #1D3FC4, Periwinkle #5D7FF7,
   Mist #DFE5FD, Navy #0F172A, Slate #64748B, Cloud #F8FAFC. */
/* ---- Light (default) ---------------------------------------------------- */
:root {
    color-scheme: light;

    /* Brand */
    --brand: #1D3FC4;            /* Deep Blue: logo, sidebar tint, headings accent */
    --brand-ink: #FFFFFF;
    --accent: #2754F5;           /* Dugsora Blue: icons, charts, borders, large text (3:1+) */
    --accent-strong: #2754F5;    /* filled buttons, badges, small text (white on it 5.7:1) */
    --accent-ink: #FFFFFF;       /* text on --accent-strong */

    /* Surfaces */
    --bg: #F8FAFC;               /* Cloud page */
    --surface: #FFFFFF;          /* cards, tables, inputs */
    --surface-2: #F1F5F9;        /* subtle fills, table head, hover rows */
    --surface-3: #E2E8F0;        /* pressed / selected */
    --overlay: rgba(15, 23, 42, .45);

    /* Lines */
    --border: #E2E8F0;
    --border-strong: #C4CEDB;

    /* Text */
    --text: #0F172A;             /* Navy */
    --text-2: #334155;
    --text-3: #5B6B80;           /* meta, helper text (Slate, darkened to 4.5:1 on all light surfaces) */
    --text-disabled: #94A3B8;

    /* Semantic (Dugsora status colours) */
    --ok: #1E7A4C;      --ok-soft: #E7F4EC;
    --warn: #B45309;    --warn-soft: #FCF1E3;
    --bad: #B93A2A;     --bad-soft: #FBECE9;
    --info: #0369A1;    --info-soft: #E5F2FA;
    --money-in: var(--ok);
    --money-out: var(--bad);

    /* Sidebar (always dark, both themes) */
    --sidebar-bg: #0F172A;
    --sidebar-bg-2: #1E293B;
    --sidebar-text: #C2CCDC;
    --sidebar-text-strong: #FFFFFF;
    --sidebar-muted: #8D9AB3;
    --sidebar-line: rgba(194, 204, 220, .14);
    --sidebar-hover: rgba(194, 204, 220, .10);
    /* Derived from --accent so every school theme follows automatically */
    --sidebar-active: color-mix(in srgb, var(--accent) 22%, transparent);
    --sidebar-accent: color-mix(in srgb, var(--accent) 55%, #FFFFFF); /* marker on the dark sidebar */
    --link: var(--accent-strong);
    --focus-ring: 0 0 0 3px color-mix(in srgb, var(--accent) 40%, transparent);
    --accent-soft: color-mix(in srgb, var(--accent) 12%, transparent);

    /* Charts: categorical, brand-led; validated for 3:1 vs --surface */
    --chart-1: #2754F5;
    --chart-2: #0F172A;
    --chart-3: #D97706;
    --chart-4: #1E7A4C;
    --chart-5: #DB2777;
    --chart-6: #64748B;
    --chart-grid: #E2E8F0;

    /* Type */
    --font-ui: "Plus Jakarta Sans", "Plus Jakarta Sans Fallback", "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
    --font-brand: "Rubik", "Rubik Fallback", "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
    --font-num: "Inter", "Inter Fallback", "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
    --font-body: "Inter", "Inter Fallback", "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
    --fs-xs: 12px;
    --fs-sm: 13px;
    --fs-base: 14px;
    --fs-md: 16px;
    --fs-lg: 20px;
    --fs-xl: 24px;
    --fs-2xl: 30px;
    --lh-tight: 1.2;
    --lh-base: 1.5;
    --fw-regular: 400;
    --fw-medium: 500;
    --fw-semibold: 600;
    --fw-bold: 700;

    /* Shape */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-pill: 999px;

    /* Space: 4px grid */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;

    /* Elevation: soft, layered */
    --shadow-xs: 0 1px 2px rgba(21, 26, 36, .05);
    --shadow-sm: 0 1px 2px rgba(21, 26, 36, .04), 0 2px 8px rgba(21, 26, 36, .04);
    --shadow-md: 0 1px 2px rgba(21, 26, 36, .04), 0 8px 24px rgba(21, 26, 36, .07);
    --shadow-pop: 0 2px 4px rgba(21, 26, 36, .05), 0 16px 40px rgba(21, 26, 36, .14);

    /* Motion */
    --ease: cubic-bezier(.2, .7, .2, 1);
    --dur-fast: 120ms;
    --dur: 200ms;

    /* Layout */
    --sidebar-w: 230px;          /* matches the legacy fixed width (erp-ui.css uses 230px !important) */
    --sidebar-w-rail: 70px;
    --topbar-h: 64px;
    --touch: 44px;
}

/* ---- Dark: real dark palette, not an inversion -------------------------- */
:root[data-theme="dark"] {
    color-scheme: dark;

    --brand: #93A9FA;
    --brand-ink: #0B1220;
    --accent: #7D99F8;
    --accent-strong: #2754F5;
    --accent-ink: #FFFFFF;

    --bg: #0B1220;
    --surface: #111A2C;
    --surface-2: #16213A;
    --surface-3: #1C2944;
    --overlay: rgba(0, 0, 0, .6);

    --border: #233150;
    --border-strong: #31436A;

    --text: #E8ECF4;
    --text-2: #BAC4D6;
    --text-3: #8A96AE;
    --text-disabled: #5A6680;

    --ok: #4CC38A;      --ok-soft: #10281D;
    --warn: #F0B04A;    --warn-soft: #2B2112;
    --bad: #FF8A80;     --bad-soft: #2E1618;
    --info: #7DD3FC;    --info-soft: #0E2A3C;

    --sidebar-bg: #080F1D;
    --sidebar-bg-2: #0F1B33;
    --sidebar-line: rgba(194, 204, 220, .10);

    --link: var(--accent);

    --chart-1: #7D99F8;
    --chart-2: #DFE5FD;
    --chart-3: #FBBF24;
    --chart-4: #4CC38A;
    --chart-5: #F9A8D4;
    --chart-6: #9CA3AF;
    --chart-grid: #1E2A44;

    --shadow-xs: 0 1px 2px rgba(0, 0, 0, .3);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .3), 0 2px 8px rgba(0, 0, 0, .2);
    --shadow-md: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
    --shadow-pop: 0 2px 4px rgba(0, 0, 0, .35), 0 16px 40px rgba(0, 0, 0, .5);
}

/* ---- Legacy bridge: point the old --erp-* tokens at the new ones so the
   existing erp-ui.css follows the new palette (incl. dark) until each page
   is migrated. html:root beats erp-ui.css's :root / [data-theme] blocks. -- */
html:root {
    --erp-bg: var(--bg);
    --erp-surface: var(--surface);
    --erp-sidebar: var(--sidebar-bg);
    --erp-sidebar-soft: var(--sidebar-bg-2);
    --erp-primary: var(--brand);
    --erp-border: var(--border);
    --erp-text: var(--text);
    --erp-muted: var(--text-2);
    --erp-soft: var(--text-3);
    --erp-radius: var(--radius-md);
    --erp-shadow: var(--shadow-md);
    --erp-shadow-sm: var(--shadow-sm);
    --erp-sidebar-width: var(--sidebar-w);
    --erp-sidebar-collapsed: var(--sidebar-w-rail);
    --erp-transition: var(--dur) var(--ease);
}

/* ---- Base typography ---------------------------------------------------- */
html,
body {
    background: var(--bg);
}

/* Reserve the scrollbar's space from the first frame: on load the page grows
   past the viewport, the scrollbar appears and the content width shrinks
   ~15px, which re-flowed auto-fill grids (measured CLS 0.32 on the dashboard). */
html {
    scrollbar-gutter: stable;
    /* The legacy overflow-x:hidden on html/body stops Chrome honouring the
       gutter on the root, so also keep the scrollbar track always present. */
    overflow-y: scroll;
}

body {
    color: var(--text);
    font-family: var(--font-body);
    font-size: var(--fs-base);
    line-height: var(--lh-base);
    font-feature-settings: "cv11", "ss01";
}

/* Titles in Plus Jakarta Sans; buttons, menus and the top bar stay in Inter.
   Rubik is reserved for the Dugsora wordmark (--font-brand). */
h1, h2, h3, h4, h5, h6,
.x_title h2 {
    font-family: var(--font-ui);
}

/* Money, counts and dates line up in columns */
table td,
table th,
.hg-num,
.erp-kpi-value,
.badge {
    font-variant-numeric: tabular-nums;
}

a {
    color: var(--link);
}

::selection {
    background: var(--accent-soft);
}

/* Visible keyboard focus everywhere; mouse clicks stay clean */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ---- Print: always light, whatever the screen theme --------------------- */
@media print {
    :root,
    :root[data-theme="dark"] {
        color-scheme: light;
        --brand: #1D3FC4;
        --accent: #2754F5;
        --accent-strong: #2754F5;
        --bg: #FFFFFF;
        --surface: #FFFFFF;
        --surface-2: #F1F5F9;
        --surface-3: #E2E8F0;
        --border: #E2E8F0;
        --border-strong: #C4CEDB;
        --text: #0F172A;
        --text-2: #334155;
        --text-3: #5B6B80;
        --ok: #1E7A4C;   --ok-soft: #E7F4EC;
        --warn: #B45309; --warn-soft: #FCF1E3;
        --bad: #B93A2A;  --bad-soft: #FBECE9;
        --info: #0369A1; --info-soft: #E5F2FA;
        --link: #2754F5;
        --chart-grid: #E2E8F0;
        --shadow-xs: none;
        --shadow-sm: none;
        --shadow-md: none;
        --shadow-pop: none;
    }

    html,
    body {
        background: #FFFFFF !important;
        color: #0F172A !important;
    }
}

/* Numbers (KPIs, money, counts): Inter with equal-width lining figures, so
   columns line up and figures stay calm next to the titles. */
.erp-kpi-value,
.hg-num {
    font-family: var(--font-num);
    font-variant-numeric: tabular-nums lining-nums;
}
