/* ===== Reset & Base ===== */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    color-scheme: dark;
    --bg-primary: #0f111a;
    --bg-secondary: #1a1d2e;
    --bg-card: #232741;
    --bg-card-hover: #2c3154;
    --accent: #6a4ce0;
    --accent-hover: #5a3ec9;
    --accent-glow: rgba(124, 92, 252, 0.25);
    --accent-subtle: rgba(124, 92, 252, 0.15);
    --accent-light: #a78bfa;
    --text-primary: #e2e8f0;
    --text-secondary: #a8b5c9;
    /* Chosen for headroom: 5.47:1 on --bg-card, the worst surface it sits on */
    --text-muted: #8ea0b8;
    --border: #2e3348;
    --border-strong: #4c5470;
    --success: #34d399;
    --warning: #fbbf24;
    --danger: #f87171;
    /* Lightened text variants for use on tinted badge backgrounds */
    --danger-text: #fca5a5;
    --radius: 12px;
    --radius-sm: 8px;
    --transition: 0.2s ease;
    --sidebar-width: 240px;
    --focus-ring: 0 0 0 3px var(--accent-glow);

    /* ===== Mobile chrome =====
       The app bar and tab bar only exist at the mobile breakpoint, so their
       dimensions live here rather than being repeated in layout.css.

       The two env() reads resolve to 0 on any browser without a display cutout,
       which keeps the safe-area maths valid everywhere else. --app-safe-top is
       the notch inset; --app-safe-bottom is the home-indicator inset. */
    --app-bar-height: 3.25rem;
    --tab-bar-height: 3.5rem;
    --app-safe-top: env(safe-area-inset-top, 0px);
    --app-safe-bottom: env(safe-area-inset-bottom, 0px);
    /* Total height the tab bar occupies, notch included. main reserves this so
       the last card is never trapped underneath it. */
    --tab-bar-total: calc(var(--tab-bar-height) + var(--app-safe-bottom));
}

:root[data-theme="light"] {
    color-scheme: light;
    --bg-primary: #e9eef6;
    --bg-secondary: #e9edf5;
    --bg-card: #f7faff;
    --bg-card-hover: #edf1f8;
    --accent: #244a78;
    --accent-hover: #193656;
    --accent-glow: rgba(36, 74, 120, 0.14);
    --accent-subtle: rgba(36, 74, 120, 0.12);
    --accent-light: #244a78;
    --text-primary: #182235;
    --text-secondary: #5c687d;
    --text-muted: #626f83;
    --border: #d6ddea;
    --border-strong: #aab5c8;
    --success: #187653;
    --warning: #8a5a00;
    --danger: #b42336;
    --danger-text: #b42336;
    --focus-ring: 0 0 0 3px rgba(36, 74, 120, 0.2);
}

/* Z-index ladder: drawer scrim 450 < drawer 500 < form backdrop 900 <
   form dialog 950 < confirmation modal 1000 < toast 2000 < skip link 3000.
   The scrim sits above the app bar (200) and tab bar (340), dimming the page. */

/* ===== Custom scrollbar (all pages) ===== */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: transparent;
    border-radius: 999px;
    border: 2px solid transparent;
    transition: background-color 0.2s ease, border-color 0.2s ease;
}
html.is-scrolling::-webkit-scrollbar-thumb,
html.is-scrolling *::-webkit-scrollbar-thumb {
    background: var(--accent);
    border-color: var(--bg-card);
}
html.is-scrolling::-webkit-scrollbar-thumb:hover,
html.is-scrolling *::-webkit-scrollbar-thumb:hover {
    background: var(--accent-light);
}
::-webkit-scrollbar-corner { background: transparent; }
* { scrollbar-width: thin; scrollbar-color: transparent transparent; }
html.is-scrolling,
html.is-scrolling * {
    scrollbar-color: var(--accent) transparent;
}

@media (prefers-reduced-motion: reduce) {
    ::-webkit-scrollbar-thumb {
        transition: none;
    }
}

html {
    font-size: 16px;
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    display: flex;
    min-height: 100vh;
    line-height: 1.6;
    /* The sidebar is fixed and out of flow, so pad the body by its width.
       That lets main centre itself in the remaining space (see layout.css). */
    padding-left: var(--sidebar-width);
}

a {
    color: inherit;
    text-decoration: none;
}

ul, ol {
    list-style: none;
}

button {
    cursor: pointer;
    font-family: inherit;
    border: none;
    background: none;
    color: inherit;
}

input, textarea, select {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.75rem;
    outline: none;
    transition: border-color var(--transition);
}

select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23a8b5c9' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    padding-right: 2.5rem;
    cursor: pointer;
}

:root[data-theme="light"] select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%235c687d' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
}

input:focus, textarea:focus, select:focus {
    border-color: var(--accent);
    box-shadow: var(--focus-ring);
}

/* ===== Focus Visibility ===== */
:focus-visible {
    outline: 2px solid var(--accent-light);
    outline-offset: 2px;
    border-radius: 4px;
}

/* Remove the ring only where a custom, higher-contrast replacement is applied */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
    outline: none;
    border-color: var(--accent);
    box-shadow: var(--focus-ring);
}

/* ===== Accessibility Utilities ===== */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

.skip-link {
    position: absolute;
    top: -100%;
    left: 0.5rem;
    z-index: 3000;
    padding: 0.6rem 1rem;
    background: var(--accent);
    color: #fff;
    font-size: 0.85rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    transition: top var(--transition);
}

.skip-link:focus {
    top: 0.5rem;
}

/* ===== Reduced Motion ===== */
@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
