/* ============================================
   OpeningMastermind — Shared Base Styles
   ============================================
   Loaded before every page-specific stylesheet.
   Contains design tokens, reset, and all patterns
   that were previously duplicated across files.

   DARK-FIRST: :root is the dark theme (default).
   `body.theme-light` opts into the light palette.
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;400;500;600;700&family=Inter:wght@300;400;500;600;700&display=swap');

/* --- Design Tokens (dark-first) --- */
:root {
    /* Core palette — single signature blue accent */
    --accent: #5b9bd5;
    --accent-light: #7eb8e8;
    --accent-soft: rgba(91, 155, 213, 0.28);
    --accent-glow: rgba(91, 155, 213, 0.18);

    /* Semantic colors */
    --success: #4ade80;
    --success-soft: rgba(74, 222, 128, 0.12);
    --error: #f87171;
    --error-soft: rgba(248, 113, 113, 0.12);
    --warning: #fbbf24;

    /* Text hierarchy */
    --text: #e9edf6;
    --text-secondary: #b6c0d2;
    --text-muted: #8794a8;
    --text-faint: #5e6a7d;

    /* Surfaces — true black */
    --bg: #000000;
    --bg-2: #060708;
    --surface: rgba(255, 255, 255, 0.03);
    --surface-hover: rgba(255, 255, 255, 0.06);
    --surface-active: rgba(91, 155, 213, 0.14);
    --border: rgba(255, 255, 255, 0.08);
    --border-hover: rgba(91, 155, 213, 0.40);
    --card-bg: #0a0a0c;
    --panel-bg: rgba(10, 10, 12, 0.92);

    /* Radius scale */
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;
    --radius-round: 50%;

    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.40), 0 4px 14px rgba(0, 0, 0, 0.30);
    --shadow-md: 0 2px 10px rgba(0, 0, 0, 0.50), 0 12px 34px rgba(0, 0, 0, 0.42);
    --shadow-glow: 0 0 0 1px var(--accent-soft), 0 8px 30px var(--accent-glow);

    /* Typography */
    --font-brand: 'Space Grotesk', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;

    /* Transitions */
    --ease-default: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    --ease-bounce: 0.5s cubic-bezier(0.16, 1, 0.3, 1);
    --ease-snappy: 0.18s cubic-bezier(0.34, 1.4, 0.64, 1);
}

/* --- Light theme override --- */
body.theme-light {
    --accent: #3b7fd4;
    --accent-light: #5b9bd5;
    --accent-soft: rgba(59, 127, 212, 0.26);
    --accent-glow: rgba(59, 127, 212, 0.14);

    --success: #2f9e44;
    --success-soft: rgba(47, 158, 68, 0.10);
    --error: #e03131;
    --error-soft: rgba(224, 49, 49, 0.10);
    --warning: #b7791f;

    --text: #1a2332;
    --text-secondary: #4a5568;
    --text-muted: #7a8a9e;
    --text-faint: #a0aec0;

    --bg: #f0f4f8;
    --bg-2: #e8eef5;
    --surface: rgba(59, 127, 212, 0.04);
    --surface-hover: rgba(59, 127, 212, 0.08);
    --surface-active: rgba(59, 127, 212, 0.12);
    --border: #dce3eb;
    --border-hover: rgba(59, 127, 212, 0.3);
    --card-bg: #ffffff;
    --panel-bg: rgba(255, 255, 255, 0.95);

    --shadow-sm: 0 1px 3px rgba(26, 35, 50, 0.05), 0 4px 12px rgba(26, 35, 50, 0.04);
    --shadow-md: 0 2px 8px rgba(26, 35, 50, 0.06), 0 8px 24px rgba(26, 35, 50, 0.05);
    --shadow-glow: 0 0 0 1px var(--accent-soft), 0 8px 24px var(--accent-glow);
}

/* --- Reset --- */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    /* Mobile polish: kill the grey tap-flash on iOS/Android for every element. */
    -webkit-tap-highlight-color: transparent;
}

/* --- Body Base --- */
html {
    /* Contain overscroll so a standalone web app doesn't rubber-band the whole
       page when an inner list reaches its end. */
    overscroll-behavior: none;
}
body {
    font-family: var(--font-body);
    background: var(--bg);
    color: var(--text);
    overflow-x: hidden;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background var(--ease-default), color var(--ease-default);
}

/* Honor iOS safe areas (notch / home indicator) by default. Pages that pin
   their own fixed chrome add env(safe-area-inset-*) where needed; this is the
   shared fallback so nothing hugs the very edges in standalone (home-screen)
   mode. */
@supports (padding: env(safe-area-inset-top)) {
    body { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
}

/* --- Film Grain Overlay --- */
body::before {
    content: '';
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    pointer-events: none;
    z-index: 9998;
    opacity: 0.035;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* --- Wave Shimmer --- */
body::after {
    content: '';
    position: fixed;
    bottom: env(safe-area-inset-bottom);
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--accent-soft), var(--accent-glow), transparent);
    animation: waveShimmer 8s ease-in-out infinite;
    z-index: 9999;
}
@keyframes waveShimmer {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 0.7; }
}

/* --- Background Skeleton --- */
.background {
    position: fixed;
    inset: 0;
    background:
        radial-gradient(1200px 800px at 15% -10%, rgba(91, 155, 213, 0.07), transparent 60%),
        radial-gradient(1000px 700px at 100% 110%, rgba(91, 155, 213, 0.05), transparent 55%),
        linear-gradient(160deg, var(--bg) 0%, var(--bg-2) 100%);
    z-index: -1;
}

.background-image {
    position: absolute;
    inset: 0;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    opacity: 0.22;
}

.background-overlay {
    position: absolute;
    inset: 0;
    background: linear-gradient(160deg, rgba(10, 14, 22, 0.78) 0%, rgba(13, 18, 28, 0.86) 100%);
}

body.theme-light .background-overlay {
    background: rgba(240, 244, 248, 0.65);
}

body.theme-light .background-image {
    opacity: 0.45;
}

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.12); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-soft); background-clip: padding-box; }
body.theme-light ::-webkit-scrollbar-thumb { background: #cdd6e0; }
body.theme-light ::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

/* --- Home Button --- */
.home-button-container {
    position: fixed;
    top: calc(20px + env(safe-area-inset-top));
    left: calc(20px + env(safe-area-inset-left));
    z-index: 1000;
}

.home-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text);
    cursor: pointer;
    padding: 10px 16px;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all var(--ease-default);
}

.home-btn:hover {
    background: var(--surface-hover);
    border-color: var(--border-hover);
    transform: translateY(-2px);
    box-shadow: var(--shadow-sm);
}

.home-btn:active {
    transform: translateY(0);
}

.home-btn svg {
    transition: transform var(--ease-default);
}

.home-btn:hover svg {
    transform: scale(1.1);
}

/* --- Theme Toggle --- */
.theme-toggle-btn {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom));
    left: calc(20px + env(safe-area-inset-left));
    z-index: 300;
    background: var(--panel-bg);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    border-radius: var(--radius-round);
    width: 40px;
    height: 40px;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--ease-default);
}

.theme-toggle-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.theme-toggle-icon {
    font-family: 'Cormorant Garamond', serif;
    font-weight: 500;
}

.theme-solid .background-image {
    display: none !important;
}

/* --- Chess Piece Sprites (Lichess cburnett) --- */
.piece-wK { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/wK.svg'); }
.piece-wQ { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/wQ.svg'); }
.piece-wR { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/wR.svg'); }
.piece-wB { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/wB.svg'); }
.piece-wN { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/wN.svg'); }
.piece-wP { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/wP.svg'); }
.piece-bK { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/bK.svg'); }
.piece-bQ { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/bQ.svg'); }
.piece-bR { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/bR.svg'); }
.piece-bB { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/bB.svg'); }
.piece-bN { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/bN.svg'); }
.piece-bP { background-image: url('https://lichess1.org/assets/_YbzQVl/piece/cburnett/bP.svg'); }

/* --- Focus States (accessibility) --- */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* --- Common Animation --- */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* --- Board Move FX (used by sound.js / omBoardFx) --- */
@keyframes fxPlacePop {
    0%   { transform: scale(0.84); opacity: 0.45; }
    55%  { transform: scale(1.06); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}
.chess-piece.fx-place {
    animation: fxPlacePop 0.24s cubic-bezier(0.34, 1.4, 0.64, 1);
}
.chess-piece.fx-ghost {
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

/* ============================================
   Native form-control legibility (site-wide)
   The app is dark-first: without color-scheme, the browser renders <select>
   popups/options with OS-default light backgrounds + inherited light text —
   unreadable. color-scheme makes native controls follow the theme, and the
   explicit option colors are a belt-and-braces fallback.
   ============================================ */
:root { color-scheme: dark; }
body.theme-light { color-scheme: light; }

select option {
    background-color: #14181f;
    color: #e9edf6;
}
body.theme-light select option {
    background-color: #ffffff;
    color: #1a2332;
}

/* ============================================
   Mobile: prevent iOS Safari's focus-zoom.
   Safari zooms the viewport whenever a focused form control's font-size is
   below 16px. Every text-entry control in the app is 0.85–0.95rem (< 16px),
   so on phones we floor them at 16px. Buttons are excluded (they don't zoom),
   and this only applies at phone widths so the desktop look is untouched.
   ============================================ */
@media (max-width: 768px) {
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    select,
    textarea {
        font-size: 16px !important;
    }
}
