
/* ─── Tokens ───────────────────────────────────────────────────────────
   Every layout token aliases the site's own custom properties instead of
   inventing a parallel palette; only the game-specific accents are new.
   Game pages never load assets/settings.js, so the default (dark) theme
   tokens are the ones that resolve here. */
:root {
    --territorio-bg: var(--bg-deep, #050d1a);
    --territorio-text: var(--text-primary, #e8f4ff);
    --territorio-radius-sm: var(--radius-sm, 6px);
    --territorio-radius-md: var(--radius-md, 12px);
    --territorio-radius-lg: var(--radius-lg, 20px);
    --territorio-shadow: var(--shadow-lg, 0 16px 40px rgba(0, 0, 0, 0.5));

    --territorio-cyan: #22d3ee;
    --territorio-cyan-dim: rgba(34, 211, 238, 0.35);
    --territorio-coral: #fb7185;
    --territorio-coral-dim: rgba(251, 113, 133, 0.35);
    --territorio-gold: #fbbf24;
    --territorio-blocked: #64748b;
    --territorio-board: #061320;
    --territorio-muted: #8fb3cc;
    --territorio-glass: rgba(7, 20, 38, 0.86);
    --territorio-border: rgba(34, 211, 238, 0.22);
    --territorio-font: 'Outfit', 'Segoe UI', system-ui, sans-serif;
}

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

body {
    font-family: var(--territorio-font);
    background:
        radial-gradient(at 15% 0%, rgba(34, 211, 238, 0.07) 0px, transparent 55%),
        radial-gradient(at 85% 0%, rgba(251, 113, 133, 0.06) 0px, transparent 55%),
        var(--territorio-bg);
    min-height: 100vh;
    color: var(--territorio-text);
    /* _base.css resets the body padding shorthand on mobile, which would push
       the shell under the fixed .app-header — keep the site's own token. */
    padding-top: var(--app-header-h, 72px);
}

/* ─── Shell ─── */
.game-shell {
    width: 100%;
    max-width: 820px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.territorio-topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.territorio-back-btn {
    display: inline-flex;
    align-items: center;
    padding: 8px 14px;
    background: var(--territorio-glass);
    border: 1px solid var(--territorio-border);
    border-radius: 10px;
    color: var(--territorio-text);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.85rem;
    white-space: nowrap;
    transition: border-color 0.18s ease, background 0.18s ease;
}

.territorio-back-btn:hover {
    border-color: var(--territorio-cyan);
    background: rgba(34, 211, 238, 0.12);
}

.territorio-chips {
    display: flex;
    gap: 6px;
    flex: 1;
    justify-content: center;
    flex-wrap: wrap;
}

.chip {
    display: inline-flex;
    align-items: center;
    padding: 5px 12px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 1.4px;
    text-transform: uppercase;
    border: 1px solid var(--territorio-border);
    background: rgba(34, 211, 238, 0.08);
    color: var(--territorio-cyan);
}

.hud-btn {
    min-width: 44px;
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid var(--territorio-border);
    background: var(--territorio-glass);
    color: var(--territorio-text);
    font-family: inherit;
    font-size: 1rem;
    font-weight: 800;
    cursor: pointer;
    transition: border-color 0.18s ease, transform 0.12s ease, opacity 0.18s ease;
}

.hud-btn-wide {
    min-width: 96px;
    padding: 0 16px;
    font-size: 0.78rem;
    letter-spacing: 1.4px;
    text-transform: uppercase;
    background: rgba(34, 211, 238, 0.12);
    border-color: rgba(34, 211, 238, 0.45);
    color: var(--territorio-cyan);
}

.hud-btn:hover:not(:disabled) {
    border-color: var(--territorio-gold);
    transform: translateY(-1px);
}

.hud-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.hud-btn:disabled:hover {
    transform: none;
    border-color: var(--territorio-border);
}

/* ─── HUD ─── */
.hud-top {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    flex-wrap: wrap;
    padding: 10px 14px;
    background: var(--territorio-glass);
    border: 1px solid var(--territorio-border);
    border-radius: 16px;
    backdrop-filter: blur(6px);
}

.hud-caption {
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 2.4px;
    color: var(--territorio-cyan);
    text-transform: uppercase;
}

.hud-caption.is-thinking {
    color: var(--territorio-gold);
    animation: territorioPulse 1.1s ease-in-out infinite alternate;
}

.hud-caption.is-waiting {
    color: var(--territorio-coral);
}

@keyframes territorioPulse {
    from { opacity: 0.45; }
    to { opacity: 1; }
}

.score-side {
    display: flex;
    align-items: baseline;
    gap: 8px;
}

.score-name {
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 1.6px;
    color: var(--territorio-muted);
    text-transform: uppercase;
}

.score-digits {
    font-size: 1.9rem;
    font-weight: 900;
    line-height: 1;
    min-width: 1.1ch;
    text-align: center;
    color: var(--territorio-coral);
    font-variant-numeric: tabular-nums;
}

.score-side:first-of-type .score-digits {
    color: var(--territorio-cyan);
}

.score-dash {
    color: var(--territorio-muted);
    font-weight: 700;
}

.hud-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 4px 10px;
    border-left: 1px solid rgba(143, 179, 204, 0.18);
}

.hud-label {
    font-size: 0.62rem;
    font-weight: 800;
    letter-spacing: 1.4px;
    color: var(--territorio-muted);
    text-transform: uppercase;
    white-space: nowrap;
}

.hud-value {
    font-size: 1.05rem;
    font-weight: 800;
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
    color: var(--territorio-text);
}

/* ─── Board ─── */
.board-wrap {
    position: relative;
    width: 100%;
    /* A 9x9 board is square, so cap it against the viewport height: without
       this the HUD plus the board always pushes the bottom rows off-screen
       on a laptop. width-limited viewports (phones) are unaffected. */
    max-width: min(100%, max(320px, calc(100vh - var(--app-header-h, 72px) - 210px)));
    max-width: min(100%, max(320px, calc(100dvh - var(--app-header-h, 72px) - 210px)));
    margin: 0 auto;
    border-radius: var(--territorio-radius-lg);
    overflow: hidden;
    border: 1px solid var(--territorio-border);
    background: var(--territorio-board);
    box-shadow: var(--territorio-shadow), 0 0 46px rgba(34, 211, 238, 0.12);
}

#gameCanvas {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
    touch-action: none;
    background: var(--territorio-board);
    cursor: crosshair;
}

/* The site ships no global .hidden utility, so every runtime-toggled
   non-overlay element needs its own rule. */
.chip.hidden,
.hud-btn.hidden {
    display: none;
}

/* ─── Overlays ─── */
.overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(3, 8, 15, 0.9);
    backdrop-filter: blur(9px);
    overflow-y: auto;
    z-index: var(--z-game-overlay, 100);
    padding: 16px;
    padding-top: max(16px, var(--safe-area-inset-top));
    padding-bottom: max(16px, var(--safe-area-inset-bottom));
    cursor: default;
}

.overlay.hidden {
    display: none;
}

.overlay-card {
    background: linear-gradient(150deg, rgba(7, 20, 38, 0.97) 0%, rgba(3, 8, 15, 0.99) 100%);
    border: 1px solid var(--territorio-border);
    border-radius: var(--territorio-radius-lg);
    padding: 32px 30px;
    max-width: min(470px, 92vw);
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    text-align: center;
    box-shadow: 0 0 60px rgba(34, 211, 238, 0.14);
    animation: territorioCardIn 0.36s cubic-bezier(0.34, 1.4, 0.64, 1);
}

@keyframes territorioCardIn {
    from {
        opacity: 0;
        transform: scale(0.9) translateY(16px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.overlay-kicker {
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 2.4px;
    text-transform: uppercase;
    color: var(--territorio-gold);
    margin-bottom: 10px;
}

.overlay-card h2 {
    font-size: 2rem;
    font-weight: 900;
    letter-spacing: -0.5px;
    margin-bottom: 8px;
    background: linear-gradient(135deg, #ffffff 10%, var(--territorio-cyan) 90%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.overlay-sub,
.overlay-card p {
    color: var(--territorio-muted);
    font-size: 0.95rem;
    line-height: 1.55;
    margin-bottom: 16px;
}

.overlay-card.fail-card {
    border-color: rgba(251, 113, 133, 0.32);
    box-shadow: 0 0 60px rgba(251, 113, 133, 0.12);
}

.overlay-card.fail-card h2 {
    background: linear-gradient(135deg, #ffffff 10%, var(--territorio-coral) 90%);
    -webkit-background-clip: text;
    background-clip: text;
}

.overlay-card.victory-card {
    border-color: rgba(251, 191, 36, 0.34);
    box-shadow: 0 0 60px rgba(251, 191, 36, 0.16);
}

.overlay-card.victory-card h2 {
    background: linear-gradient(135deg, #ffffff 5%, var(--territorio-gold) 95%);
    -webkit-background-clip: text;
    background-clip: text;
}

.overlay-card.draw-card {
    border-color: rgba(34, 211, 238, 0.34);
    box-shadow: 0 0 60px rgba(34, 211, 238, 0.16);
}

/* ─── Start card controls ─── */
.option-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}

.option-label {
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 2px;
    color: var(--territorio-muted);
    text-transform: uppercase;
}

.option-tabs {
    display: flex;
    gap: 6px;
    background: rgba(34, 211, 238, 0.06);
    border: 1px solid rgba(34, 211, 238, 0.16);
    border-radius: var(--territorio-radius-md);
    padding: 4px;
}

.option-btn {
    border: none;
    background: transparent;
    color: var(--territorio-muted);
    font-family: inherit;
    font-size: 0.74rem;
    font-weight: 800;
    letter-spacing: 1.2px;
    text-transform: uppercase;
    padding: 9px 14px;
    border-radius: 9px;
    cursor: pointer;
    transition: background 0.16s ease, color 0.16s ease;
}

.option-btn.active {
    background: linear-gradient(135deg, rgba(34, 211, 238, 0.9), rgba(34, 211, 238, 0.6));
    color: #04121c;
    box-shadow: 0 4px 16px rgba(34, 211, 238, 0.3);
}

.option-btn.active[data-diff="hard"] {
    background: linear-gradient(135deg, rgba(251, 113, 133, 0.92), rgba(251, 113, 133, 0.62));
    color: #2b050c;
    box-shadow: 0 4px 16px rgba(251, 113, 133, 0.3);
}

.option-btn.active[data-diff="easy"] {
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.92), rgba(251, 191, 36, 0.62));
    color: #2a1c02;
    box-shadow: 0 4px 16px rgba(251, 191, 36, 0.3);
}

.how-to {
    text-align: left;
    background: rgba(34, 211, 238, 0.05);
    border: 1px solid rgba(34, 211, 238, 0.15);
    border-radius: 14px;
    padding: 14px 16px;
    margin-bottom: 16px;
}

.how-to h3 {
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--territorio-cyan);
    margin-bottom: 10px;
}

.how-to ul {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 7px;
}

.how-to li {
    position: relative;
    padding-left: 16px;
    font-size: 0.85rem;
    line-height: 1.45;
    color: var(--territorio-muted);
}

.how-to li::before {
    content: '';
    position: absolute;
    left: 0;
    top: 8px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--territorio-coral);
}

.start-meta {
    display: flex;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.meta-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 1.4px;
    text-transform: uppercase;
    color: var(--territorio-gold);
    border: 1px solid rgba(251, 191, 36, 0.3);
    background: rgba(251, 191, 36, 0.08);
}

.meta-pill strong {
    color: var(--territorio-text);
    font-variant-numeric: tabular-nums;
}

.btn-primary,
.btn-secondary {
    display: block;
    width: 100%;
    padding: 14px 22px;
    border-radius: 14px;
    font-family: inherit;
    font-size: 0.98rem;
    font-weight: 800;
    letter-spacing: 1.6px;
    text-transform: uppercase;
    text-decoration: none;
    text-align: center;
    cursor: pointer;
    transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease;
}

.btn-primary {
    border: none;
    color: #04121c;
    background: linear-gradient(135deg, var(--territorio-cyan), #06b6d4);
    box-shadow: 0 8px 26px rgba(34, 211, 238, 0.35);
    margin-bottom: 10px;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 32px rgba(34, 211, 238, 0.45);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    background: transparent;
    border: 1px solid var(--territorio-border);
    color: var(--territorio-muted);
    font-size: 0.86rem;
    margin-bottom: 10px;
}

.btn-secondary:hover {
    border-color: var(--territorio-cyan);
    color: var(--territorio-text);
}

.btn-primary:last-child,
.btn-secondary:last-child {
    margin-bottom: 0;
}

.stat-grid {
    display: flex;
    flex-direction: column;
    gap: 6px;
    background: rgba(34, 211, 238, 0.05);
    border: 1px solid rgba(34, 211, 238, 0.15);
    border-radius: 14px;
    padding: 12px 16px;
    margin-bottom: 18px;
    font-size: 0.9rem;
    text-align: left;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    color: var(--territorio-muted);
}

.stat-row strong {
    color: var(--territorio-gold);
    font-variant-numeric: tabular-nums;
}

/* ─── Bottom hint ─── */
.territorio-bottombar {
    display: flex;
    justify-content: center;
}

.controls-hint {
    font-size: 0.78rem;
    color: var(--territorio-muted);
    text-align: center;
    padding: 6px 12px;
    border-radius: 10px;
    background: rgba(7, 20, 38, 0.6);
    border: 1px solid rgba(143, 179, 204, 0.14);
}

/* ─── Focus ─── */
button:focus-visible,
a:focus-visible,
.option-btn:focus-visible {
    outline: 2px solid var(--territorio-gold);
    outline-offset: 2px;
}

/* ─── Responsive ─── */
@media (max-width: 1280px) {
    .game-shell {
        max-width: 760px;
    }
}

@media (max-width: 1024px) {
    .game-shell {
        max-width: 720px;
    }
}

@media (max-width: 768px) {
    .game-shell {
        gap: 8px;
    }

    .territorio-back-btn {
        font-size: 0.75rem;
        padding: 6px 10px;
    }

    .territorio-chips {
        order: 3;
        flex-basis: 100%;
    }

    .chip {
        font-size: 0.66rem;
        padding: 4px 10px;
        letter-spacing: 1px;
    }

    .hud-top {
        gap: 8px;
        padding: 8px 10px;
        border-radius: 14px;
    }

    .hud-caption {
        flex-basis: 100%;
        text-align: center;
    }

    .score-digits {
        font-size: 1.5rem;
    }

    .hud-stat {
        padding: 2px 6px;
    }

    .hud-value {
        font-size: 0.95rem;
    }

    .hud-label {
        font-size: 0.56rem;
        letter-spacing: 1px;
    }

    .board-wrap {
        border-radius: 14px;
    }

    .overlay {
        padding: 12px;
    }

    .overlay-card {
        padding: 24px 18px;
        max-width: 96vw;
        border-radius: 16px;
    }

    .overlay-card h2 {
        font-size: 1.5rem;
    }

    .option-row {
        flex-direction: column;
        align-items: stretch;
        gap: 6px;
    }

    .option-tabs {
        justify-content: center;
    }

    .option-btn {
        flex: 1;
        padding: 10px 8px;
        font-size: 0.7rem;
    }
}

@media (max-width: 640px) {
    .hud-btn-wide {
        min-width: 84px;
        font-size: 0.72rem;
    }

    .score-digits {
        font-size: 1.4rem;
    }

    .controls-hint {
        font-size: 0.74rem;
    }
}

@media (max-width: 480px) {
    .score-digits {
        font-size: 1.25rem;
    }

    .hud-label,
    .score-name {
        font-size: 0.58rem;
        letter-spacing: 1.2px;
    }

    .controls-hint {
        font-size: 0.7rem;
    }

    .overlay-card {
        padding: 20px 14px;
    }

    .overlay-card h2 {
        font-size: 1.35rem;
    }
}

/* ─── Reduced motion ─── */
@media (prefers-reduced-motion: reduce) {
    .overlay-card,
    .hud-caption.is-thinking {
        animation: none;
    }

    .btn-primary,
    .btn-secondary,
    .hud-btn,
    .option-btn,
    .territorio-back-btn {
        transition: none;
    }
}
