@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;800&family=Press+Start+2P&family=Plus+Jakarta+Sans:wght@400;500;600&display=swap');

/* --- THEME VARIATION VARIABLES --- */
:root {
    /* Theme: Cyberpunk Neon (Default) */
    --font-heading: 'Outfit', sans-serif;
    --font-body: 'Plus Jakarta Sans', sans-serif;
    
    --bg-gradient: radial-gradient(circle at center, #0f0c1b 0%, #05020a 100%);
    --panel-bg: rgba(20, 16, 38, 0.7);
    --panel-border: rgba(255, 255, 255, 0.08);
    --text-primary: #ffffff;
    --text-secondary: #9f9bbd;
    --grid-bg: rgba(30, 24, 56, 0.4);
    --grid-border: rgba(255, 255, 255, 0.05);
    
    --color-x: #00f3ff;
    --glow-x: 0 0 15px rgba(0, 243, 255, 0.6), 0 0 30px rgba(0, 243, 255, 0.3);
    --color-o: #ff007b;
    --glow-o: 0 0 15px rgba(255, 0, 123, 0.6), 0 0 30px rgba(255, 0, 123, 0.3);
    
    --accent: #7000ff;
    --accent-glow: 0 0 20px rgba(112, 0, 255, 0.5);
    --cell-hover-bg: rgba(255, 255, 255, 0.03);
    --cell-active-bg: rgba(112, 0, 255, 0.1);
}

[data-theme="cosmo"] {
    --bg-gradient: radial-gradient(circle at center, #110e2e 0%, #070514 100%);
    --panel-bg: rgba(22, 19, 49, 0.75);
    --panel-border: rgba(212, 175, 55, 0.15);
    --text-primary: #fbfbfd;
    --text-secondary: #a49fc4;
    --grid-bg: rgba(28, 24, 61, 0.5);
    --grid-border: rgba(212, 175, 55, 0.08);
    
    --color-x: #ffd700;
    --glow-x: 0 0 15px rgba(255, 215, 0, 0.5), 0 0 30px rgba(255, 215, 0, 0.2);
    --color-o: #ffffff;
    --glow-o: 0 0 15px rgba(255, 255, 255, 0.6), 0 0 30px rgba(255, 255, 255, 0.2);
    
    --accent: #d4af37;
    --accent-glow: 0 0 20px rgba(212, 175, 55, 0.4);
    --cell-hover-bg: rgba(212, 175, 55, 0.03);
    --cell-active-bg: rgba(212, 175, 55, 0.08);
}

[data-theme="retro"] {
    --font-heading: 'Press Start 2P', monospace;
    --font-body: 'Press Start 2P', monospace;
    
    --bg-gradient: linear-gradient(180deg, #111 0%, #000 100%);
    --panel-bg: rgba(0, 0, 0, 0.95);
    --panel-border: #33ff33;
    --text-primary: #33ff33;
    --text-secondary: #00aa00;
    --grid-bg: #0a0a0a;
    --grid-border: #33ff33;
    
    --color-x: #ff3333;
    --glow-x: 0 0 10px rgba(255, 51, 51, 0.8);
    --color-o: #3333ff;
    --glow-o: 0 0 10px rgba(51, 51, 255, 0.8);
    
    --accent: #33ff33;
    --accent-glow: 0 0 10px rgba(51, 255, 51, 0.8);
    --cell-hover-bg: rgba(51, 255, 51, 0.1);
    --cell-active-bg: rgba(51, 255, 51, 0.2);
}

[data-theme="minimalist"] {
    --bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
    --panel-bg: rgba(255, 255, 255, 0.92);
    --panel-border: rgba(0, 0, 0, 0.06);
    --text-primary: #1a202c;
    --text-secondary: #718096;
    --grid-bg: rgba(255, 255, 255, 0.7);
    --grid-border: rgba(0, 0, 0, 0.05);
    
    --color-x: #2b6cb0;
    --glow-x: 0 4px 10px rgba(43, 108, 176, 0.2);
    --color-o: #dd6b20;
    --glow-o: 0 4px 10px rgba(221, 107, 32, 0.2);
    
    --accent: #4a5568;
    --accent-glow: 0 4px 10px rgba(74, 85, 104, 0.15);
    --cell-hover-bg: rgba(0, 0, 0, 0.02);
    --cell-active-bg: rgba(0, 0, 0, 0.04);
}

/* --- RESET & BASE STYLES --- */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: var(--font-body);
    background: var(--bg-gradient);
    color: var(--text-primary);
    /* 100vh is broken on iOS Safari (includes the URL bar) and pushes the
       bottom controls (Restart) behind the browser chrome / home indicator.
       100dvh tracks the actually-visible viewport. */
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* Centering done via .app-container { margin: auto 0 } so the page
       remains scrollable when the content exceeds the viewport. */
    overflow-x: hidden;
    padding: 10px 15px;
    /* Honour the home-indicator safe area so the action panel is reachable */
    padding-top: max(10px, env(safe-area-inset-top));
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    transition: background 0.5s ease, color 0.3s ease;
}

/* Sizing tokens — board adapts to BOTH width and height so it never overflows.
   Chrome is ~40px smaller than before because the inline Difficulty bar moved
   into the header, so we can afford a bigger board cap without pushing the
   Restart button off-screen. */
:root {
    --chrome-height: 332px;
    --viewport-h: 100vh;
    --board-size: min(540px, calc(100vw - 40px), calc(var(--viewport-h) - var(--chrome-height)));
}

@supports (height: 100dvh) {
    :root { --viewport-h: 100dvh; }
}

@media (max-height: 720px) {
    :root { --chrome-height: 302px; }
}

@media (max-height: 600px) {
    :root { --chrome-height: 262px; }
}

/* Background Canvas for Particles */
#particle-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    pointer-events: none;
}

/* Helper Class to Hide Elements */
.hidden {
    display: none !important;
}

/* --- APP CONTAINER (COMPACT & CENTERED) --- */
.app-container {
    width: 100%;
    max-width: calc(var(--board-size) + 40px);
    z-index: 1;
    display: flex;
    flex-direction: column;
    gap: 12px;
    /* margin auto centers vertically when there's room; collapses to 0 when
       content overflows so the page scrolls naturally on short viewports. */
    margin: auto 0;
    /* Extra clearance below action panel so the Restart row never sits flush
       against the iPhone home indicator or Safari toolbar. */
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    opacity: 0;
    transform: translateY(15px);
    animation: fadeInUp 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes fadeInUp {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* --- GLASS CARD AND PANELS --- */
.glass-panel {
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    padding: 16px;
    transition: all 0.3s ease;
}

[data-theme="retro"] .glass-panel {
    border: 3px solid var(--panel-border);
    border-radius: 0;
    box-shadow: 6px 6px 0px #000;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}

[data-theme="minimalist"] .glass-panel {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
    border-radius: 16px;
}

.main-game-card {
    display: flex;
    flex-direction: column;
    gap: 14px;
    /* Breathing room between the header row and the game card */
    margin-top: 12px;
}

/* --- HEADER AND NAVIGATION CONTROLS --- */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 4px;
}

.logo h1 {
    font-family: var(--font-heading);
    font-weight: 800;
    font-size: 1.6rem; 
    letter-spacing: -0.5px;
    background: linear-gradient(135deg, var(--color-x), var(--color-o));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 0 25px rgba(255,255,255,0.05);
}

[data-theme="retro"] .logo h1 {
    font-size: 0.85rem;
    -webkit-text-fill-color: var(--text-primary);
    background: none;
}

.controls {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--panel-border);
    color: var(--text-primary);
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.icon-btn svg {
    width: 18px;
    height: 18px;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-1.5px);
    box-shadow: var(--accent-glow);
}

[data-theme="retro"] .icon-btn {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    box-shadow: 2px 2px 0px var(--text-primary);
}

[data-theme="retro"] .icon-btn svg {
    width: 14px;
    height: 14px;
}

[data-theme="retro"] .icon-btn:hover {
    box-shadow: none;
    transform: translate(2px, 2px);
}

/* Theme Dropdown Overlay */
.theme-menu {
    position: relative;
}

.theme-dropdown {
    position: absolute;
    top: 44px;
    right: 0;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(20px);
    border-radius: 14px;
    width: 160px;
    display: none;
    flex-direction: column;
    padding: 6px;
    gap: 3px;
    z-index: 10;
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
    animation: slideDown 0.15s ease forwards;
}

@keyframes slideDown {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

[data-theme="retro"] .theme-dropdown {
    border: 3px solid var(--text-primary);
    border-radius: 0;
    box-shadow: 4px 4px 0px #000;
}

.theme-option {
    background: transparent;
    border: none;
    color: var(--text-primary);
    text-align: left;
    padding: 8px 10px;
    border-radius: 8px;
    font-family: var(--font-body);
    font-size: 0.85rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.2s ease;
}

[data-theme="retro"] .theme-option {
    font-size: 0.55rem;
    border-radius: 0;
}

.theme-option:hover, .theme-option.active {
    background: rgba(255, 255, 255, 0.08);
}

[data-theme="retro"] .theme-option:hover, [data-theme="retro"] .theme-option.active {
    background: var(--text-primary);
    color: #000;
}

.theme-option .color-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

/* Section header used inside the Settings (gear) submenu to label
   Language and Theme groups. Discreet, uppercase, separator-style. */
.dropdown-section-label {
    font-size: 0.6rem;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    font-weight: 700;
    color: var(--text-secondary);
    padding: 8px 10px 4px;
    opacity: 0.7;
    pointer-events: none;
    user-select: none;
}

.dropdown-section-label:not(:first-child) {
    margin-top: 4px;
    border-top: 1px solid var(--panel-border);
    padding-top: 8px;
}

[data-theme="retro"] .dropdown-section-label {
    font-size: 0.4rem;
}

/* The Settings dropdown holds more rows than the others; widen slightly so
   "Cosmo Royale" and "Retro 8-Bit" never wrap. */
.settings-dropdown {
    width: 180px;
}

/* --- HORIZONTAL SCOREBAR SYSTEM --- */
.score-bar {
    display: flex;
    justify-content: space-between;
    align-items: stretch;
    background: rgba(0, 0, 0, 0.15);
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    padding: 3px;
    margin-bottom: 2px;
}

[data-theme="retro"] .score-bar {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    background: transparent;
}

[data-theme="minimalist"] .score-bar {
    background: rgba(0, 0, 0, 0.03);
}

.score-item {
    flex: 1.2;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 8px 4px;
    border-radius: 11px;
    border: 1px solid transparent;
    transition: all 0.35s cubic-bezier(0.16, 1, 0.3, 1);
    opacity: 0.45;
}

[data-theme="retro"] .score-item {
    border-radius: 0;
}

.score-item.active {
    opacity: 1;
}

/* Pulse Glowing outlines on active turns */
.score-item.player-x.active {
    background: rgba(0, 243, 255, 0.05);
    border-color: rgba(0, 243, 255, 0.3);
    box-shadow: 0 0 15px rgba(0, 243, 255, 0.1), inset 0 0 8px rgba(0, 243, 255, 0.05);
    animation: pulseX 2s infinite ease-in-out;
}

.score-item.player-o.active {
    background: rgba(255, 0, 123, 0.05);
    border-color: rgba(255, 0, 123, 0.3);
    box-shadow: 0 0 15px rgba(255, 0, 123, 0.1), inset 0 0 8px rgba(255, 0, 123, 0.05);
    animation: pulseO 2s infinite ease-in-out;
}

@keyframes pulseX {
    0%, 100% { box-shadow: 0 0 8px rgba(0, 243, 255, 0.08); }
    50% { box-shadow: 0 0 18px rgba(0, 243, 255, 0.22), inset 0 0 8px rgba(0, 243, 255, 0.08); }
}

@keyframes pulseO {
    0%, 100% { box-shadow: 0 0 8px rgba(255, 0, 123, 0.08); }
    50% { box-shadow: 0 0 18px rgba(255, 0, 123, 0.22), inset 0 0 8px rgba(255, 0, 123, 0.08); }
}

.score-divider {
    flex: 0.8;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0 8px;
    border-left: 1px solid var(--panel-border);
    border-right: 1px solid var(--panel-border);
    color: var(--text-secondary);
}

[data-theme="retro"] .score-divider {
    border-left: 2px dashed var(--text-primary);
    border-right: 2px dashed var(--text-primary);
}

.player-name {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin-bottom: 2px;
}

[data-theme="retro"] .player-name {
    font-size: 0.45rem;
}

.player-score {
    font-size: 1.3rem;
    font-weight: 800;
    font-family: var(--font-heading);
}

[data-theme="retro"] .player-score {
    font-size: 0.75rem;
}

.score-item.player-x .player-score {
    color: var(--color-x);
    text-shadow: var(--glow-x);
}

.score-item.player-o .player-score {
    color: var(--color-o);
    text-shadow: var(--glow-o);
}

.score-divider .player-score {
    font-size: 1.1rem;
    color: var(--text-primary);
}

.score-draw-label {
    font-size: 0.65rem;
    text-transform: uppercase;
    font-weight: 600;
    letter-spacing: 0.5px;
}

[data-theme="retro"] .score-draw-label {
    font-size: 0.45rem;
}

/* --- CONFIG & CONTROLS --- */
.config-panel {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.mode-selector {
    width: 100%;
    display: flex;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    padding: 3px;
    border: 1px solid var(--panel-border);
}

[data-theme="retro"] .mode-selector {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    background: transparent;
}

.mode-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 7px;
    border-radius: 9px;
    font-family: var(--font-body);
    font-weight: 600;
    font-size: 0.8rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.mode-btn svg {
    width: 14px;
    height: 14px;
}

[data-theme="retro"] .mode-btn {
    font-size: 0.55rem;
    border-radius: 0;
}

.mode-btn:hover {
    color: var(--text-primary);
}

.mode-btn.active {
    background: var(--accent);
    color: #fff;
    box-shadow: var(--accent-glow);
}

[data-theme="retro"] .mode-btn.active {
    background: var(--text-primary);
    color: #000;
    box-shadow: none;
}

/* Difficulty Selector (IA Only) */
.difficulty-selector {
    width: 100%;
    display: flex;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    padding: 3px;
    border: 1px solid var(--panel-border);
    animation: fadeIn 0.2s ease forwards;
}

.difficulty-selector.hidden {
    display: none;
}

.diff-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 6px 3px;
    border-radius: 9px;
    font-size: 0.78rem;
    font-family: var(--font-body);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

[data-theme="retro"] .diff-btn {
    font-size: 0.5rem;
    border-radius: 0;
}

.diff-btn:hover {
    color: var(--text-primary);
}

.diff-btn.active {
    border: 1px solid rgba(255,255,255,0.08);
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
}

[data-theme="retro"] .diff-btn.active {
    background: var(--text-primary);
    color: #000;
    border: none;
}

/* --- WEBRTC ONLINE PANEL DESIGN --- */
.online-status-panel {
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: rgba(0, 0, 0, 0.18);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    padding: 10px 12px;
    width: 100%;
    animation: fadeIn 0.25s ease-out forwards;
}

[data-theme="retro"] .online-status-panel {
    border: 2px dashed var(--text-primary);
    border-radius: 0;
}

.status-header {
    display: flex;
    align-items: center;
    gap: 8px;
}

.status-indicator-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #ff9900;
    box-shadow: 0 0 8px #ff9900;
    transition: all 0.3s ease;
}

.status-indicator-dot.connected {
    background: #33ff33;
    box-shadow: 0 0 8px #33ff33;
}

.status-indicator-dot.waiting {
    background: #ff9900;
    box-shadow: 0 0 8px #ff9900;
    animation: pulseAlert 1.5s infinite ease-in-out;
}

.status-indicator-dot.error {
    background: #ff3333;
    box-shadow: 0 0 8px #ff3333;
}

@keyframes pulseAlert {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

.status-message {
    font-size: 0.76rem;
    font-weight: 600;
    color: var(--text-secondary);
}

[data-theme="retro"] .status-message {
    font-size: 0.5rem;
}

.invite-link-box {
    display: flex;
    gap: 6px;
    width: 100%;
    animation: fadeIn 0.2s ease forwards;
}

.invite-url-input {
    flex: 1;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--panel-border);
    color: var(--text-primary);
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 0.75rem;
    font-family: var(--font-body);
    outline: none;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

[data-theme="retro"] .invite-url-input {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    background: #000;
    font-size: 0.5rem;
}

.copy-btn {
    width: 32px !important;
    height: 32px !important;
    border-radius: 8px !important;
    background: var(--accent) !important;
    border: none !important;
    color: white !important;
    flex-shrink: 0;
}

.copy-btn svg {
    width: 14px !important;
    height: 14px !important;
}

.copy-btn:hover {
    filter: brightness(1.15);
}

.copy-btn.copied {
    background: #33ff33 !important;
    box-shadow: 0 0 10px #33ff33 !important;
    color: #000 !important;
}

[data-theme="retro"] .copy-btn {
    border: 2px solid var(--text-primary) !important;
    border-radius: 0 !important;
    box-shadow: 2px 2px 0px var(--text-primary) !important;
}

[data-theme="retro"] .copy-btn.copied {
    background: var(--text-primary) !important;
    box-shadow: none !important;
    color: #000 !important;
}

/* Invite microcopy under status message */
.invite-hint {
    font-size: 0.72rem;
    color: var(--text-secondary);
    line-height: 1.35;
    margin: 0;
}

[data-theme="retro"] .invite-hint {
    font-size: 0.45rem;
}

/* Online action row — now just a Cancel button while waiting/connecting. */
.online-actions {
    display: flex;
    padding-top: 6px;
}

.online-actions .secondary-btn {
    flex: 1;
    padding: 8px;
    font-size: 0.78rem;
}

[data-theme="retro"] .online-actions .secondary-btn {
    font-size: 0.5rem;
}

/* Difficulty suggestion shown inside the game-over modal */
.difficulty-hint {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    background: rgba(255, 215, 0, 0.06);
    border: 1px solid rgba(255, 215, 0, 0.18);
    border-radius: 10px;
    padding: 8px 12px;
    margin-bottom: 16px;
    font-size: 0.8rem;
    color: var(--text-primary);
}

[data-theme="retro"] .difficulty-hint {
    border-radius: 0;
    border: 2px dashed #ffd700;
    font-size: 0.5rem;
}

.hint-btn {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 5px 10px;
    font-family: var(--font-body);
    font-weight: 600;
    font-size: 0.75rem;
    cursor: pointer;
    box-shadow: var(--accent-glow);
    transition: filter 0.2s ease, transform 0.2s ease;
    flex-shrink: 0;
}

.hint-btn:hover {
    filter: brightness(1.1);
    transform: translateY(-1px);
}

[data-theme="retro"] .hint-btn {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    background: var(--text-primary);
    color: #000;
    box-shadow: 2px 2px 0px #000;
    font-size: 0.5rem;
}

/* Lightweight transient toast (used for clipboard fallback etc) */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    color: var(--text-primary);
    padding: 10px 16px;
    border-radius: 12px;
    font-size: 0.82rem;
    font-weight: 600;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
    z-index: 200;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

[data-theme="retro"] .toast {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    box-shadow: 4px 4px 0px #000;
    font-size: 0.5rem;
}

/* --- THE GAME BOARD (TIGHT FIT) --- */
.board-container {
    position: relative;
    width: var(--board-size);
    height: var(--board-size);
    margin: 0 auto;
}

.board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 10px; 
    width: 100%;
    height: 100%;
}

[data-theme="retro"] .board {
    gap: 6px;
    background: var(--text-primary);
    padding: 6px;
}

.cell {
    background: var(--grid-bg);
    border: 1px solid var(--grid-border);
    border-radius: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

[data-theme="retro"] .cell {
    background: #000;
    border: none;
    border-radius: 0;
}

.cell:hover {
    background: var(--cell-hover-bg);
    transform: translateY(-1.5px);
    border-color: rgba(255, 255, 255, 0.1);
}

[data-theme="retro"] .cell:hover {
    transform: none;
    background: #111;
}

.cell:active {
    transform: scale(0.96);
    background: var(--cell-active-bg);
}

/* Winning Strike-through SVG Overlay */
.winning-line-svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 5;
}

.winning-line {
    stroke: var(--accent);
    stroke-width: 7px;
    stroke-linecap: round;
    fill: none;
    filter: drop-shadow(0 0 8px var(--accent));
    stroke-dasharray: 600;
    stroke-dashoffset: 600;
    animation: drawLine 0.5s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

[data-theme="retro"] .winning-line {
    stroke: var(--text-primary);
    filter: none;
    stroke-width: 9px;
}

@keyframes drawLine {
    to {
        stroke-dashoffset: 0;
    }
}

/* Cell Piece Elements */
.piece {
    width: 58%;
    height: 58%;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: piecePop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes piecePop {
    from {
        transform: scale(0) rotate(-30deg);
        opacity: 0;
    }
    to {
        transform: scale(1) rotate(0deg);
        opacity: 1;
    }
}

.piece-x {
    color: var(--color-x);
    filter: drop-shadow(var(--glow-x));
}

.piece-o {
    color: var(--color-o);
    filter: drop-shadow(var(--glow-o));
}

/* SVG icons path styles */
.piece svg {
    width: 100%;
    height: 100%;
    fill: none;
    stroke: currentColor;
    stroke-width: 11;
    stroke-linecap: round;
    stroke-linejoin: round;
}

[data-theme="retro"] .piece svg {
    stroke-width: 16;
}

/* Hover Preview Symbol Hint */
.cell-preview::after {
    content: '';
    position: absolute;
    width: 38%;
    height: 38%;
    opacity: 0.1;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.cell-preview-x:hover::after {
    content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="none" stroke="%2300f3ff" stroke-width="13" stroke-linecap="round"><line x1="15" y1="15" x2="85" y2="85"/><line x1="85" y1="15" x2="15" y2="85"/></svg>');
    opacity: 0.2;
    transform: scale(1.05);
}

[data-theme="cosmo"] .cell-preview-x:hover::after {
    content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="none" stroke="%23ffd700" stroke-width="13" stroke-linecap="round"><line x1="15" y1="15" x2="85" y2="85"/><line x1="85" y1="15" x2="15" y2="85"/></svg>');
}

[data-theme="retro"] .cell-preview-x:hover::after {
    content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="none" stroke="%23ff3333" stroke-width="18" stroke-linecap="round"><line x1="15" y1="15" x2="85" y2="85"/><line x1="85" y1="15" x2="15" y2="85"/></svg>');
}

.cell-preview-o:hover::after {
    content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="none" stroke="%23ff007b" stroke-width="13"><circle cx="50" cy="50" r="34"/></svg>');
    opacity: 0.2;
    transform: scale(1.05);
}

[data-theme="cosmo"] .cell-preview-o:hover::after {
    content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="none" stroke="%23ffffff" stroke-width="13"><circle cx="50" cy="50" r="34"/></svg>');
}

[data-theme="retro"] .cell-preview-o:hover::after {
    content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="none" stroke="%233333ff" stroke-width="18"><circle cx="50" cy="50" r="34"/></svg>');
}

.cell.winner {
    background: rgba(255, 255, 255, 0.05);
    box-shadow: inset 0 0 12px rgba(255,255,255,0.04);
}

/* --- ACTION PANEL --- */
/* Now hosts a single Restart button; make it expand to fill the row. */
.action-panel {
    display: flex;
    justify-content: stretch;
    align-items: center;
}

.action-panel .primary-btn {
    flex: 1;
    width: 100%;
}

/* Reserved slot for a future banner ad. Stays out of the flow until populated. */
.ad-slot {
    display: none;
}

.ad-slot:not(:empty) {
    display: block;
    margin: 8px auto 0;
    width: 100%;
    max-width: calc(var(--board-size) + 40px);
    text-align: center;
}

.secondary-btn {
    flex: 1;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--panel-border);
    color: var(--text-primary);
    padding: 10px;
    border-radius: 11px;
    font-family: var(--font-body);
    font-weight: 600;
    cursor: pointer;
    font-size: 0.8rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: all 0.2s ease;
}

.secondary-btn svg {
    width: 14px;
    height: 14px;
}

[data-theme="retro"] .secondary-btn {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    box-shadow: 2px 2px 0px var(--text-primary);
    font-size: 0.55rem;
    background: transparent;
}

.secondary-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.08);
    transform: translateY(-1px);
}

[data-theme="retro"] .secondary-btn:hover:not(:disabled) {
    transform: translate(2px, 2px);
    box-shadow: none;
}

.secondary-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* iOS Human Interface Guidelines recommend ≥44px tap targets. The default
   padding gives ~33px which is unreliable on small thumbs. Bump to 44px on
   touch-primary devices so Restart/Undo/Redo are always tappable. */
@media (hover: none) and (pointer: coarse) {
    .primary-btn,
    .secondary-btn {
        min-height: 44px;
        padding: 12px 10px;
    }
    .action-panel {
        gap: 12px;
    }
}

.primary-btn {
    flex: 1.3;
    background: var(--accent);
    color: #ffffff;
    border: none;
    padding: 10px;
    border-radius: 11px;
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    box-shadow: var(--accent-glow);
    transition: all 0.2s ease;
}

.primary-btn svg {
    width: 14px;
    height: 14px;
}

[data-theme="retro"] .primary-btn {
    border: 2px solid var(--text-primary);
    border-radius: 0;
    background: var(--text-primary);
    color: #000;
    box-shadow: 2px 2px 0px #000;
    font-size: 0.55rem;
}

.primary-btn:hover {
    transform: translateY(-1.5px);
    filter: brightness(1.08);
}

[data-theme="retro"] .primary-btn:hover {
    transform: translate(2px, 2px);
    box-shadow: none;
}

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

/* --- GAME OVER MODAL --- */
.game-over-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    /* iOS Safari has a long-standing bug where a full-viewport fixed element
       with backdrop-filter still captures touches even with pointer-events:
       none. Using visibility: hidden takes it fully out of the hit-test
       pipeline. The delayed transition keeps the fade-out animation. */
    visibility: hidden;
    transition: opacity 0.35s ease, visibility 0s linear 0.35s;
    padding: 15px;
}

.game-over-overlay.active {
    opacity: 1;
    pointer-events: auto;
    visibility: visible;
    transition: opacity 0.35s ease, visibility 0s linear 0s;
}

.game-over-card {
    width: 100%;
    max-width: 360px;
    text-align: center;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 24px;
    padding: 25px 20px;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.35);
    transform: scale(0.9) translateY(15px);
    transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.game-over-overlay.active .game-over-card {
    transform: scale(1) translateY(0);
}

[data-theme="retro"] .game-over-card {
    border: 4px solid var(--text-primary);
    border-radius: 0;
    box-shadow: 8px 8px 0px #000;
    background: #000;
}

.winner-crown {
    color: var(--color-x);
    margin-bottom: 12px;
    animation: float 2s ease-in-out infinite;
    display: inline-block;
}

.winner-crown svg {
    width: 44px;
    height: 44px;
    filter: drop-shadow(var(--glow-x));
}

[data-theme="retro"] .winner-crown svg {
    width: 32px;
    height: 32px;
    filter: none;
}

/* Restart confirmation modal uses the accent colour */
.restart-icon {
    color: var(--accent);
}

.restart-icon svg {
    filter: drop-shadow(var(--accent-glow));
}

[data-theme="retro"] .restart-icon svg {
    filter: none;
}

/* Disconnect modal uses an error-coloured icon */
.disconnect-icon {
    color: #ff3344;
}

.disconnect-icon svg {
    filter: drop-shadow(0 0 12px rgba(255, 51, 68, 0.55));
}

[data-theme="retro"] .disconnect-icon {
    color: var(--color-x);
}

[data-theme="retro"] .disconnect-icon svg {
    filter: none;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.game-over-title {
    font-family: var(--font-heading);
    font-weight: 800;
    font-size: 1.8rem;
    margin-bottom: 6px;
}

[data-theme="retro"] .game-over-title {
    font-size: 0.85rem;
}

.winner-highlight {
    font-weight: bold;
}

.winner-highlight.x {
    color: var(--color-x);
    text-shadow: var(--glow-x);
}

.winner-highlight.o {
    color: var(--color-o);
    text-shadow: var(--glow-o);
}

.game-over-subtitle {
    color: var(--text-secondary);
    font-size: 0.88rem;
    margin-bottom: 20px;
    line-height: 1.45;
}

[data-theme="retro"] .game-over-subtitle {
    font-size: 0.5rem;
}

.modal-actions {
    display: flex;
    gap: 10px;
}

/* Streak badge */
.streak-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 0.8rem;
    font-weight: bold;
    padding: 5px 12px;
    border-radius: 10px;
    background: rgba(255, 215, 0, 0.08);
    color: #ffd700;
    border: 1px solid rgba(255, 215, 0, 0.15);
    margin-bottom: 15px;
}

[data-theme="retro"] .streak-badge {
    border-radius: 0;
    border: 2px dashed #ffd700;
    font-size: 0.5rem;
}

/* --- FOOTER (TIGHT HEIGHT) --- */
footer {
    margin-top: auto;
    padding: 15px 0 5px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.72rem;
    z-index: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

[data-theme="retro"] footer {
    font-size: 0.42rem;
}

.build-tag {
    display: inline-block;
    margin-left: 6px;
    padding: 1px 6px;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.3px;
    opacity: 0.7;
}

footer a {
    color: var(--text-primary);
    text-decoration: none;
    font-weight: 600;
}

footer a:hover {
    color: var(--accent);
}

/* Animations helpers */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* --- NOSCRIPT FALLBACK --- */
.noscript-banner {
    max-width: 640px;
    margin: 40px auto;
    padding: 24px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 16px;
    color: var(--text-primary);
    text-align: center;
    position: relative;
    z-index: 2;
}
.noscript-banner h1 {
    font-family: var(--font-heading);
    font-size: 1.4rem;
    margin-bottom: 12px;
    background: linear-gradient(135deg, var(--color-x), var(--color-o));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}
.noscript-banner p {
    color: var(--text-secondary);
    line-height: 1.5;
    margin-bottom: 10px;
}

/* --- LONG-FORM CONTENT SECTIONS (SEO, below the fold) --- */
.content-section {
    width: 100%;
    margin-top: 32px;
    padding: 22px 24px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
    color: var(--text-primary);
    z-index: 1;
    position: relative;
}

[data-theme="retro"] .content-section {
    border: 3px solid var(--panel-border);
    border-radius: 0;
    box-shadow: 6px 6px 0px #000;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}

[data-theme="minimalist"] .content-section {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
    border-radius: 16px;
}

.content-section h2 {
    font-family: var(--font-heading);
    font-size: 1.15rem;
    font-weight: 800;
    margin-bottom: 14px;
    background: linear-gradient(135deg, var(--color-x), var(--color-o));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    letter-spacing: 0.3px;
}

[data-theme="retro"] .content-section h2,
[data-theme="retro"] .content-section h3 {
    background: none;
    -webkit-text-fill-color: var(--text-primary);
    color: var(--text-primary);
    font-size: 0.7rem;
}

.content-section h3 {
    font-family: var(--font-heading);
    font-size: 0.92rem;
    font-weight: 600;
    margin: 18px 0 8px;
    color: var(--text-primary);
}

.content-section p,
.content-section li {
    font-size: 0.86rem;
    line-height: 1.6;
    color: var(--text-secondary);
}

.content-section p + p {
    margin-top: 10px;
}

.content-section ul {
    margin: 4px 0 6px 18px;
    padding-left: 4px;
}

.content-section li {
    margin: 4px 0;
}

.content-section strong {
    color: var(--text-primary);
    font-weight: 600;
}

.content-section em {
    color: var(--text-primary);
    font-style: normal;
    font-weight: 500;
    opacity: 0.92;
}

[data-theme="retro"] .content-section p,
[data-theme="retro"] .content-section li,
[data-theme="retro"] .content-section summary {
    font-size: 0.5rem;
    line-height: 1.6;
}

/* FAQ collapsibles */
.content-section details {
    border-top: 1px solid var(--panel-border);
    padding: 12px 0;
}

.content-section details:first-of-type {
    border-top: none;
    padding-top: 4px;
}

.content-section summary {
    font-family: var(--font-body);
    font-weight: 600;
    color: var(--text-primary);
    font-size: 0.9rem;
    cursor: pointer;
    list-style: none;
    padding-right: 22px;
    position: relative;
}

.content-section summary::-webkit-details-marker {
    display: none;
}

.content-section summary::after {
    content: "+";
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    color: var(--accent);
    font-family: var(--font-heading);
    font-size: 1.1rem;
    transition: transform 0.2s ease;
}

.content-section details[open] summary::after {
    content: "−";
}

.content-section details > p {
    margin-top: 8px;
}

/* Desktop: break content-sections out of the narrow app-container so prose
   gets a comfortable reading width (~720px). The board card stays at
   board-width on purpose — it's the focal piece. */
@media (min-width: 768px) {
    .content-section {
        position: relative;
        left: 50%;
        transform: translateX(-50%);
        width: min(720px, calc(100vw - 64px));
        padding: 28px 32px;
    }
}

/* ============================================================
   MODE PICKER MODAL — opens on entry, lets the user pick 1P/2P/Online
   Reuses .game-over-overlay/.game-over-card structure for visibility,
   but the option buttons have a distinct horizontal-tile layout.
   ============================================================ */

.mode-picker-card {
    max-width: 420px;
    position: relative;
    padding-top: 30px;
}

.mode-picker-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--panel-border);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s ease, border-color 0.2s ease, background 0.2s ease;
    padding: 0;
}

.mode-picker-close:hover {
    color: var(--text-primary);
    border-color: var(--text-primary);
    background: rgba(255, 255, 255, 0.04);
}

[data-theme="retro"] .mode-picker-close {
    border-radius: 0;
    border-width: 2px;
}

.mode-picker-options {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 18px;
}

.mode-picker-option {
    display: flex;
    align-items: center;
    gap: 14px;
    width: 100%;
    padding: 14px 16px;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--panel-border);
    border-radius: 16px;
    color: var(--text-primary);
    text-align: left;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.18s ease, border-color 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
}

.mode-picker-option:hover {
    transform: translateY(-2px);
    border-color: var(--accent);
    background: rgba(255, 255, 255, 0.06);
}

.mode-picker-option:active {
    transform: translateY(0);
}

.mode-picker-option.featured {
    border-color: var(--accent);
    background: linear-gradient(135deg, rgba(112, 0, 255, 0.18), rgba(112, 0, 255, 0.06));
    box-shadow: 0 6px 24px rgba(112, 0, 255, 0.18);
}

.mode-picker-option.featured:hover {
    box-shadow: 0 10px 32px rgba(112, 0, 255, 0.28);
}

[data-theme="retro"] .mode-picker-option {
    border-radius: 0;
    border-width: 2px;
    background: #000;
}

[data-theme="retro"] .mode-picker-option.featured {
    background: #000;
    box-shadow: 4px 4px 0 var(--accent);
}

.mode-picker-icon {
    flex: 0 0 auto;
    width: 44px;
    height: 44px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.mode-picker-option.featured .mode-picker-icon {
    background: rgba(112, 0, 255, 0.22);
    color: var(--text-primary);
}

[data-theme="retro"] .mode-picker-icon {
    border-radius: 0;
    border: 2px solid currentColor;
}

.mode-picker-label {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1 1 auto;
    min-width: 0;
}

.mode-picker-title-text {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.2;
}

.mode-picker-sub {
    font-size: 0.82rem;
    color: var(--text-secondary);
    line-height: 1.3;
}

@media (max-width: 380px) {
    .mode-picker-option {
        padding: 12px 12px;
        gap: 10px;
    }
    .mode-picker-icon {
        width: 38px;
        height: 38px;
    }
    .mode-picker-title-text {
        font-size: 0.98rem;
    }
    .mode-picker-sub {
        font-size: 0.78rem;
    }
}

.mode-picker-icon {
    position: relative;
}

.online-status-dot {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 10px;
    height: 10px;
    background: #22c55e;
    border-radius: 50%;
    border: 2px solid var(--panel-bg);
    box-shadow: 0 0 8px rgba(34, 197, 94, 0.7);
    animation: online-dot-pulse 1.8s ease-in-out infinite;
}

@keyframes online-dot-pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50%      { transform: scale(1.15); opacity: 0.75; }
}

[data-theme="retro"] .online-status-dot {
    border-radius: 0;
    background: #33ff33;
    border-color: #000;
    box-shadow: 0 0 6px #33ff33;
}

[data-theme="minimalist"] .online-status-dot {
    background: #38a169;
    border-color: #ffffff;
    box-shadow: 0 0 6px rgba(56, 161, 105, 0.5);
}

/* Dot sizing for smaller contexts (header button + dropdown items) */
.mode-icon-slot {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.mode-icon-slot .online-status-dot {
    top: -1px;
    right: -2px;
    width: 6px;
    height: 6px;
    border-width: 1.5px;
}

#mode-btn-icon {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

#mode-btn-icon .online-status-dot {
    top: 0;
    right: 0;
    width: 7px;
    height: 7px;
    border-width: 1.5px;
}
