:root {
    color-scheme: dark;
    --bg-color: #020617; /* Deepest black */
    --text-color: #FFFFFF; /* Pure white text for maximum contrast */
    --sub-text: #CBD5E1; /* High-contrast secondary text */
    --board-bg: #0F172A; /* Darker board background */
    --cell-bg: #2D3748; /* Lighter gray board squares */
    --cell-hover: #334155;
    --accent-color: #1E3A8A; /* Standard Navy Blue */
    --selected-navy: #16285A; /* Intermediate Navy for selected states */
    --heading-color: #FFFFFF; /* Pure white headings */
    --border-color: #475569; /* Visible borders */
    --light-blue-border: #60A5FA; /* Light blue for selected border */
    --accent-text: #FFFFFF;
    --x-color: #00E5FF;
    --o-color: #F87171;
    --reset-bg: #991B1B;
    --modal-bg: rgba(2, 6, 23, 0.98);
    --watching-color: #FDE047; /* Light Yellow */
    --create-room-bg: #064E3B; /* Dark Green */
    --unselected-btn: #0F172A;
    --hover-state: rgba(0, 0, 0, 0.5); /* Black with 50% opacity */
    --mode-btn-bg: #0F172A; /* Leave Room button background (previously undefined) */
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    text-align: center;
}

/* Small, unobtrusive link back to the info page. Sits in normal document
   flow above the game header rather than as a fixed overlay, so it never
   competes with the "Ultimate Tic Tac Toe" heading for space. */
.back-link-game {
    display: inline-block;
    align-self: flex-start;
    color: var(--light-blue-border);
    font-weight: bold;
    font-size: 0.95rem;
    text-decoration: none;
    margin-bottom: 10px;
}
.back-link-game:hover,
.back-link-game:focus-visible {
    text-decoration: underline;
}

.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;
}

/* <main> is a flex item of body (align-items:center), which sizes it by
   shrink-to-fit with no explicit width - giving a width:100% child (like
   .game-shell below) an unambiguous number to resolve against requires
   breaking that ambiguity at the source, on <main> itself, rather than
   leaving it to nested flex children. Confirmed this was the actual
   cause of Settings/Login overlapping the heading at wide viewports
   (reproduced at 1920px) - .game-shell alone wasn't sufficient. */
main {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}

.game-shell {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 10px;
    gap: 10px;
}

.header h1 { flex: 1 1 auto; min-width: 0; }

.header-actions { display: flex; gap: 10px; align-items: center; flex-shrink: 0; }

h1 { margin: 0; color: var(--heading-color); font-size: 1.5rem; letter-spacing: 1px; font-weight: 800; }
h2 { color: var(--heading-color); margin-bottom: 20px; font-weight: 700; }

.status-bar {
    background: var(--board-bg);
    padding: 12px 24px;
    border-radius: 12px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 4px 10px rgba(0,0,0,0.5);
    border: 2px solid var(--border-color);
    width: 100%;
    max-width: 600px;
    min-height: 55px;
}

.player-turn { font-weight: 900; font-size: 1.1rem; }
.player-turn.x { color: var(--x-color); text-shadow: 0 0 5px rgba(0,229,255,0.3); }
.player-turn.o { color: var(--o-color); text-shadow: 0 0 5px rgba(248,113,113,0.3); }
.player-turn.watching { color: var(--watching-color); }

.mode-status {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    color: var(--sub-text);
}

.room-info {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 2px;
}

.spectator-count {
    font-size: 0.75rem;
    color: var(--sub-text);
    font-weight: bold;
}

.room-tag {
    background: #020617;
    padding: 4px 12px;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: bold;
    color: #FFFFFF;
    border: 2px solid var(--border-color);
    letter-spacing: 1px;
}

#game-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 12px;
    background-color: var(--bg-color);
    padding: 12px;
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.8);
    max-width: 600px;
    width: 100%;
    aspect-ratio: 1/1;
    margin: 0 auto;
    border: 1px solid var(--border-color);
}

.sub-board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 4px;
    background-color: var(--board-bg);
    padding: 6px;
    border-radius: 8px;
    position: relative;
    transition: outline 0.2s ease;
    outline: 5px solid transparent;
    outline-offset: -5px;
    border: 1px solid var(--border-color);
}

.sub-board.active {
    outline: 2px solid #3B82F6;
    outline-offset: -4px;
    box-shadow: 0 0 25px rgba(59, 130, 246, 0.4);
    z-index: 10;
}

/* Capture indicator: Finalized boards on top */
.sub-board.won-x::after, .sub-board.won-o::after {
    content: attr(data-winner);
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 6rem;
    font-weight: 900;
    background: rgba(15, 23, 42, 0.98);
    border-radius: 8px;
    z-index: 20;
    pointer-events: none;
}

.sub-board.won-x::after { color: var(--x-color); text-shadow: 0 0 20px var(--x-color); }
.sub-board.won-o::after { color: var(--o-color); text-shadow: 0 0 20px var(--o-color); }

.cell {
    aspect-ratio: 1/1;
    background-color: var(--cell-bg);
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2.2rem;
    font-weight: 900;
    cursor: pointer;
    transition: background 0.2s;
    user-select: none;
    line-height: 0;
    position: relative;
    border: 1px solid rgba(255,255,255,0.05);
}

@media (max-width: 600px) { .cell { font-size: 1.5rem; } }

.cell:hover:not(.filled) { background-color: var(--cell-hover); }
.cell.x { color: var(--x-color); text-shadow: 0 0 10px rgba(0, 229, 255, 0.5); }
.cell.o { color: var(--o-color); text-shadow: 0 0 10px rgba(248, 113, 113, 0.5); }
.cell.filled { cursor: default; }

/* Keyboard focus ring for grid cells - high contrast against every cell
   background variant, and against the finalized-board overlay. */
.cell:focus-visible {
    outline: 3px solid var(--light-blue-border);
    outline-offset: -3px;
    z-index: 25;
}

.cell.last-move-x { outline: 3px dashed var(--x-color); outline-offset: -3px; z-index: 6; }
.cell.last-move-o { outline: 3px dashed var(--o-color); outline-offset: -3px; z-index: 6; }

.controls { margin-top: 40px; display: flex; flex-direction: column; gap: 15px; align-items: center; width: 100%; }

button {
    background-color: var(--accent-color);
    color: var(--accent-text);
    border: 1px solid rgba(255,255,255,0.2);
    padding: 14px 28px;
    font-size: 1rem;
    font-weight: bold;
    border-radius: 10px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    transition: transform 0.1s, background-color 0.2s, box-shadow 0.2s;
    outline: 2px solid transparent;
    outline-offset: 2px;
}

button:hover:not(:disabled) {
    background-color: #2563EB;
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(0, 0, 0, 0.6);
    outline-color: #FFFFFF;
}
button:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.reset-btn { background-color: var(--reset-bg); border: 1px solid transparent; }
.reset-btn:hover:not(:disabled) { background-color: #B91C1C; outline-color: #FFFFFF; }

#google-login-btn {
    background-color: #1e293b;
    color: #ffffff;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    font-size: 0.9rem;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    outline: 2px solid transparent;
    outline-offset: 2px;
}
#google-login-btn:hover { outline-color: #FFFFFF; }

.modal {
    display: none;
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: var(--modal-bg);
    z-index: 100;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.modal-content {
    background: #111827;
    padding: 35px;
    border-radius: 20px;
    width: 95%;
    max-width: 450px;
    box-shadow: 0 20px 60px rgba(0,0,0,1);
    border: 2px solid var(--divider-color);
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
}

.modal-content p { margin-bottom: 12px; margin-top: 15px; font-weight: 700; color: #FFFFFF; font-size: 1.1rem; }

.modal-content h2:focus-visible {
    outline: 3px solid var(--light-blue-border);
    outline-offset: 4px;
}

.settings-close-x {
    position: absolute;
    top: 10px;
    right: 10px;
    background: transparent;
    border: none;
    color: #94A3B8;
    font-size: 1.8rem;
    cursor: pointer;
    padding: 20px;
    line-height: 1;
    transition: color 0.2s, transform 0.2s;
    outline: none;
    box-shadow: none;
    display: block;
    z-index: 10;
}
.settings-close-x:hover { color: #FFFFFF; transform: scale(1.1); }

.selection-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
.difficulty-options { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 10px; }

.selection-btn {
    background: var(--unselected-btn);
    color: #FFFFFF;
    padding: 18px;
    border: 2px solid var(--border-color);
    font-size: 1rem;
    transition: all 0.1s;
    border-radius: 12px;
    cursor: pointer;
    outline: 2px solid transparent;
    outline-offset: 2px;
}

.selection-btn:hover {
    background: var(--hover-state) !important;
    color: #FFFFFF;
    outline-color: #FFFFFF;
}

.selection-btn.selected {
    background: var(--selected-navy);
    color: #FFFFFF;
    border: 2px solid var(--light-blue-border);
    font-weight: 800;
    box-shadow: 0 0 20px rgba(59, 130, 246, 0.4);
}

.selection-btn.selected:hover { outline-color: #FFFFFF; }

.online-controls {
    background: rgba(0, 0, 0, 0.4);
    padding: 15px;
    border-radius: 12px;
    margin-top: 15px;
    border: 2px dashed var(--border-color);
}

.name-input-group {
    margin-bottom: 25px;
    text-align: left;
}

.name-input-group label {
    display: block;
    font-size: 0.8rem;
    color: var(--sub-text);
    margin-bottom: 5px;
    font-weight: bold;
    text-transform: uppercase;
}

.name-input {
    width: 100%;
    background: #020617;
    border: 2px solid var(--border-color);
    padding: 12px;
    border-radius: 8px;
    color: white;
    font-size: 1rem;
    outline: none;
}
.name-input:focus { border-color: var(--light-blue-border); }

input[type="text"].code-input {
    width: 100%;
    padding: 16px;
    background: #000000;
    border: 3px solid #64748B;
    border-radius: 12px;
    color: #FFFFFF;
    font-size: 2.5rem;
    font-weight: 800;
    text-align: center;
    margin-bottom: 10px;
    outline: none;
    letter-spacing: 8px;
}

input[type="text"].code-input::placeholder {
    font-size: 2.5rem;
    letter-spacing: normal;
    color: #94A3B8;
    opacity: 1;
    font-weight: 800;
}

.create-room-btn {
    background-color: var(--create-room-bg);
    width: 100%;
    margin-bottom: 15px;
    border: 1px solid transparent;
    outline: 2px solid transparent;
    outline-offset: 2px;
}
.create-room-btn:hover:not(:disabled) { background-color: var(--create-room-bg); outline-color: #FFFFFF; }

.public-games-list {
    margin-top: 20px;
    max-height: 200px;
    overflow-y: auto;
    background: rgba(0,0,0,0.3);
    border-radius: 12px;
    padding: 10px;
    border: 1px solid var(--border-color);
}

.public-game-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    background: var(--unselected-btn);
    border-radius: 8px;
    margin-bottom: 8px;
    border: 1px solid #334155;
    cursor: pointer;
    text-align: left;
}
.public-game-item:hover { border-color: var(--light-blue-border); background: #1e293b; }
.public-game-item .host-name { font-weight: bold; color: var(--light-blue-border); display: block; margin-bottom: 2px; }
.public-game-item .match-status { font-size: 0.7rem; color: #4ADE80; text-transform: uppercase; font-weight: 800; }
.public-game-item .people-count { font-size: 0.75rem; color: var(--sub-text); margin-top: 4px; display: block; }

.help-link {
    display: inline-block;
    margin-top: 25px;
    color: #3B82F6;
    text-decoration: underline;
    cursor: pointer;
    font-size: 1rem;
    font-weight: bold;
}
.help-link:focus-visible { outline: 3px solid var(--light-blue-border); outline-offset: 2px; }

.waiting-status {
    font-size: 1.1rem;
    color: #FFFFFF;
    margin: 20px 0;
    padding: 20px;
    background: #020617;
    border-radius: 12px;
    border: 2px solid var(--border-color);
}

.ready-badge {
    display: inline-block;
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: bold;
    margin-top: 10px;
    background: #064E3B;
    color: #ECFDF5;
    transition: all 0.3s;
}

.ready-badge.is-ready {
    background: transparent;
    color: #4ADE80;
    font-weight: 800;
    padding: 6px 0;
    font-size: 1rem;
}

.ready-badge.is-empty { background: #991B1B; }

.footer { margin-top: auto; padding: 40px 0; color: #64748B; font-size: 0.85rem; }
.sync-status { font-size: 0.8rem; margin-top: 8px; display: flex; align-items: center; justify-content: center; gap: 8px; color: #94A3B8; }
.sync-dot { width: 10px; height: 10px; border-radius: 50%; background: #ef4444; }
.sync-dot.online { background: #10B981; box-shadow: 0 0 10px #10B981; }
