/* ==========================================
   Base — Design Tokens, Reset, Typography, Utilities
   Soft Night theme: deep navy/midnight blues with warm accents
   ========================================== */

:root {
    /* Surfaces — Dark (default) */
    --bg-main: #0b1222;
    --bg-surface: #111b2e;
    --bg-surface-hover: #192440;
    --bg-surface-raised: #1e2d4a;
    --border-color: #1e2d4a;

    /* Text — Dark */
    --text-main: #d4dbe6;
    --text-muted: #7b8a9e;
    --text-heading: #edf1f7;

    /* Patient accent — soft blue */
    --primary: #5b8def;
    --primary-hover: #4a7de0;
    --primary-muted: rgba(91, 141, 239, 0.12);

    /* Sleep grid colors */
    --color-sleep: #1a3a6e;
    --color-bed: #4a7dcf;
    --color-pre-sleep: #162a4e;
    --color-nap: #c9a23e;
    --color-awake: #0e1a2f;

    /* Status */
    --color-success: #5bb88a;
    --color-warning: #d4a054;
    --color-danger: #c75c5c;
    --color-success-bg: rgba(91, 184, 138, 0.10);
    --color-warning-bg: rgba(212, 160, 84, 0.10);
    --color-danger-bg: rgba(199, 92, 92, 0.10);

    /* Chart tokens (read by JS via getComputedStyle) */
    --chart-se: #5bb88a;
    --chart-se-fill: rgba(91, 184, 138, 0.15);
    --chart-tib: #4a7dcf;
    --chart-tst: #2d5a9e;
    --chart-tst-fill: rgba(45, 90, 158, 0.25);
    --chart-sol: #d4a054;
    --chart-waso: #c75c5c;
    --chart-quality: #7b6bb5;
    --chart-energy: #c7879b;
    --chart-steps: rgba(91, 184, 138, 0.5);
    --chart-steps-border: #5bb88a;
    --chart-awake: #eab308;
    --chart-grid: rgba(123, 138, 158, 0.15);
    --chart-text: #7b8a9e;

    /* Typography */
    --font-display: 'DM Serif Display', Georgia, serif;
    --font-body: 'Plus Jakarta Sans', system-ui, sans-serif;
    --font-mono: 'JetBrains Mono', 'Consolas', monospace;

    /* Spacing scale */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;
    --space-2xl: 48px;

    /* Radii */
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-full: 999px;

    /* Shadows — dark mode */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.25);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.3);
}

/* Light theme — "Morning Warmth"
   Warm linen, golden accents, sunrise energy.
   Dark = cozy midnight, Light = warm morning encouraging you to start the day */
.theme-light {
    /* Surfaces — warm linen & cream */
    --bg-main: #faf5ef;
    --bg-surface: #fffbf6;
    --bg-surface-hover: #f3ebe1;
    --bg-surface-raised: #fff8f0;
    --border-color: #e4d9cc;

    /* Text — warm espresso tones, not cold navy */
    --text-main: #3d2e1e;
    --text-muted: #8a7560;
    --text-heading: #2a1d0e;

    /* Accent — warm amber-gold instead of cold blue */
    --primary: #c08330;
    --primary-hover: #a8711f;
    --primary-muted: rgba(192, 131, 48, 0.10);

    /* Grid — warm tones */
    --color-sleep: #3d5a8a;
    --color-bed: #7ba0cc;
    --color-pre-sleep: #b0c4d8;
    --color-nap: #d4a040;
    --color-awake: #f5efe6;

    /* Status — warmer versions */
    --color-success: #5a9e6e;
    --color-warning: #c89030;
    --color-danger: #b85050;
    --color-success-bg: rgba(90, 158, 110, 0.10);
    --color-warning-bg: rgba(200, 144, 48, 0.10);
    --color-danger-bg: rgba(184, 80, 80, 0.08);

    /* Chart tokens — morning warmth palette */
    --chart-se: #5a9e6e;
    --chart-se-fill: rgba(90, 158, 110, 0.12);
    --chart-tib: #7ba0cc;
    --chart-tst: #3d5a8a;
    --chart-tst-fill: rgba(61, 90, 138, 0.12);
    --chart-sol: #c08330;
    --chart-waso: #b85050;
    --chart-quality: #7b68a8;
    --chart-energy: #c07878;
    --chart-steps: rgba(90, 158, 110, 0.30);
    --chart-steps-border: #5a9e6e;
    --chart-grid: rgba(138, 117, 96, 0.10);
    --chart-text: #8a7560;

    /* Shadows — warm, soft */
    --shadow-sm: 0 1px 4px rgba(60, 40, 20, 0.06);
    --shadow-md: 0 4px 14px rgba(60, 40, 20, 0.08);
    --shadow-lg: 0 8px 28px rgba(60, 40, 20, 0.10);
}

/* Browser-native light theme detection — no inline JS needed (CSP-safe).
   Applied before any JS loads; theme.js overrides with explicit .theme-light/.theme-dark. */
@media (prefers-color-scheme: light) {
    :root {
        --bg-main: #faf5ef;
        --bg-surface: #fffbf6;
        --bg-surface-hover: #f3ebe1;
        --bg-surface-raised: #fff8f0;
        --border-color: #e4d9cc;
        --text-main: #3d2e1e;
        --text-muted: #8a7560;
        --text-heading: #2a1d0e;
        --primary: #c08330;
        --primary-hover: #a8711f;
        --primary-muted: rgba(192, 131, 48, 0.10);
        --color-sleep: #3d5a8a;
        --color-bed: #7ba0cc;
        --color-pre-sleep: #b0c4d8;
        --color-nap: #d4a040;
        --color-awake: #f5efe6;
        --color-success: #5a9e6e;
        --color-warning: #c89030;
        --color-danger: #b85050;
        --color-success-bg: rgba(90, 158, 110, 0.10);
        --color-warning-bg: rgba(200, 144, 48, 0.10);
        --color-danger-bg: rgba(184, 80, 80, 0.08);
        --chart-se: #5a9e6e;
        --chart-se-fill: rgba(90, 158, 110, 0.12);
        --chart-tib: #7ba0cc;
        --chart-tst: #3d5a8a;
        --chart-tst-fill: rgba(61, 90, 138, 0.12);
        --chart-sol: #c08330;
        --chart-waso: #b85050;
        --chart-quality: #7b68a8;
        --chart-energy: #c07878;
        --chart-steps: rgba(90, 158, 110, 0.30);
        --chart-steps-border: #5a9e6e;
        --chart-grid: rgba(138, 117, 96, 0.10);
        --chart-text: #8a7560;
        --shadow-sm: 0 1px 4px rgba(60, 40, 20, 0.06);
        --shadow-md: 0 4px 14px rgba(60, 40, 20, 0.08);
        --shadow-lg: 0 8px 28px rgba(60, 40, 20, 0.10);
    }
}

/* .theme-light / .theme-dark override the @media query when the user
   explicitly picks a theme (or theme.js resolves 'auto' after load). */
.theme-dark {
    --bg-main: #0b1222;
    --bg-surface: #111b2e;
    --bg-surface-hover: #192440;
    --bg-surface-raised: #1e2d4a;
    --border-color: #1e2d4a;
    --text-main: #d4dbe6;
    --text-muted: #7b8a9e;
    --text-heading: #edf1f7;
    --primary: #5b8def;
    --primary-hover: #4a7de0;
    --primary-muted: rgba(91, 141, 239, 0.12);
    --color-sleep: #1a3a6e;
    --color-bed: #4a7dcf;
    --color-pre-sleep: #162a4e;
    --color-nap: #c9a23e;
    --color-awake: #0e1a2f;
    --color-success: #5bb88a;
    --color-warning: #d4a054;
    --color-danger: #c75c5c;
    --color-success-bg: rgba(91, 184, 138, 0.10);
    --color-warning-bg: rgba(212, 160, 84, 0.10);
    --color-danger-bg: rgba(199, 92, 92, 0.10);
    --chart-se: #5bb88a;
    --chart-se-fill: rgba(91, 184, 138, 0.15);
    --chart-tib: #4a7dcf;
    --chart-tst: #2d5a9e;
    --chart-tst-fill: rgba(45, 90, 158, 0.25);
    --chart-sol: #d4a054;
    --chart-waso: #c75c5c;
    --chart-quality: #7b6bb5;
    --chart-energy: #c7879b;
    --chart-steps: rgba(91, 184, 138, 0.5);
    --chart-steps-border: #5bb88a;
    --chart-grid: rgba(123, 138, 158, 0.15);
    --chart-text: #7b8a9e;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.25);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.3);
}

/* ==========================================
   Reset
   ========================================== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* ==========================================
   Base Typography
   ========================================== */
body {
    font-family: var(--font-body);
    font-size: 16px;
    background-color: var(--bg-main);
    color: var(--text-main);
    line-height: 1.5;
    transition: background-color 0.3s, color 0.3s;
}

h1, h2, h3 {
    font-family: var(--font-display);
    color: var(--text-heading);
    font-weight: 400;
    line-height: 1.25;
}

h2 {
    margin-bottom: 20px;
    font-size: 24px;
}

/* ==========================================
   Utility Classes
   ========================================== */
.hidden {
    display: none !important;
}

.mono {
    font-family: var(--font-mono);
}

.text-success { color: var(--color-success) !important; font-weight: bold; }
.text-warning { color: var(--color-warning) !important; font-weight: bold; }
.text-danger  { color: var(--color-danger)  !important; font-weight: bold; }

.placeholder-text {
    color: var(--text-muted);
    font-style: italic;
}

/* ==========================================
   Base Components
   ========================================== */
.card {
    background-color: var(--bg-surface);
    border-radius: var(--radius-md);
    padding: 20px;
    border: 1px solid var(--border-color);
    margin-bottom: 20px;
    box-shadow: var(--shadow-sm);
}

/* Primary button */
button.primary {
    background-color: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    padding: 10px 16px;
    font-family: var(--font-body);
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
}
button.primary:hover {
    background-color: var(--primary-hover);
}
button.primary:active {
    transform: scale(0.98);
}
button.primary:disabled,
.btn-secondary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Secondary button */
.btn-secondary {
    background-color: var(--bg-main);
    color: var(--text-main);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    font-family: var(--font-body);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}
.btn-secondary:hover {
    background-color: var(--bg-surface-hover);
}
.btn-secondary:active {
    transform: scale(0.98);
}

/* Danger small button */
.btn-danger-sm {
    background-color: transparent;
    color: var(--color-danger);
    border: 1px solid var(--color-danger);
    border-radius: 4px;
    padding: 4px 8px;
    cursor: pointer;
    font-size: 12px;
    transition: background-color 0.15s;
}
.btn-danger-sm:hover {
    background-color: var(--color-danger-bg);
}

/* ==========================================
   Form Inputs (global)
   ========================================== */
input[type="time"],
input[type="date"],
input[type="number"],
input[type="text"],
input[type="email"],
input[type="password"],
select,
textarea {
    width: 100%;
    padding: 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    background-color: var(--bg-main);
    color: var(--text-main);
    font-family: var(--font-body);
    font-size: 15px;
    transition: border-color 0.2s, box-shadow 0.2s;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-muted);
}

input.input-invalid {
    border-color: var(--color-danger);
    box-shadow: 0 0 0 1px rgba(199, 92, 92, 0.2);
}
input.input-valid {
    border-color: var(--color-success);
    box-shadow: 0 0 0 1px rgba(91, 184, 138, 0.15);
}

input[type="range"] {
    width: 100%;
    margin: 10px 0;
    accent-color: var(--primary);
}

/* ==========================================
   Focus visible (accessibility)
   ========================================== */
:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}
input:focus-visible, select:focus-visible, textarea:focus-visible {
    outline: none; /* These use box-shadow instead */
}

/* ==========================================
   Animation Keyframes
   ========================================== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes skeleton-pulse {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 0.8; }
}

/* ==========================================
   Conflict Resolution Toast
   ========================================== */
.conflict-toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%) translateY(1rem);
    background: var(--primary);
    color: #fff;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 9999;
    pointer-events: none;
}
.conflict-toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ==========================================
   Loading Overlay
   ========================================== */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg-main);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    transition: opacity 0.4s ease-out;
}
.loading-overlay--hiding {
    opacity: 0;
    pointer-events: none;
}
.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ==========================================
   Responsive Base
   ========================================== */
@media (max-width: 700px) {
    body {
        font-size: 15px;
    }
}
