@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

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

:root {
    --bg:       #08081e;
    --surface:  #0e0e2e;
    --surface2: #18184a;
    --border:   #3a3a9a;
    --text:     #d0d0ff;
    --muted:    #5050a0;
    --x-color:  #ff3355;
    --o-color:  #00ccff;
    --accent:   #ffdd00;
    --green:    #00ff66;
    --gold:     #ffd700;
    --cell-size: min(100px, calc((100vw - 56px) / 3));
}

/* ── Base ──────────────────────────────────────────── */
body {
    font-family: 'Press Start 2P', monospace;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    /* subtle pixel grid */
    background-image:
        linear-gradient(rgba(48, 48, 160, 0.15) 1px, transparent 1px),
        linear-gradient(90deg, rgba(48, 48, 160, 0.15) 1px, transparent 1px);
    background-size: 32px 32px;
}

/* scanline overlay */
body::after {
    content: '';
    position: fixed;
    inset: 0;
    background: repeating-linear-gradient(
        0deg,
        rgba(0, 0, 0, 0.04) 0px,
        rgba(0, 0, 0, 0.04) 1px,
        transparent 1px,
        transparent 3px
    );
    pointer-events: none;
    z-index: 9999;
}

/* ── Header ────────────────────────────────────────── */
header {
    padding: 1.25rem 2rem;
    border-bottom: 4px solid var(--accent);
    background: var(--surface);
    text-align: center;
}

.logo {
    font-size: 1.25rem;
    text-decoration: none;
    color: var(--accent);
    letter-spacing: 4px;
    text-shadow:
        4px 4px 0 #7a5800,
        0 0 24px rgba(255, 221, 0, 0.45);
    display: inline-block;
}

main {
    max-width: 680px;
    margin: 2rem auto;
    padding: 0 1rem;
}

/* ── Lobby panel ───────────────────────────────────── */
.lobby {
    background: var(--surface);
    border: 4px solid var(--border);
    box-shadow: 6px 6px 0 #000;
    padding: 2rem;
    margin-bottom: 2rem;
}

.lobby h1 {
    margin-bottom: 1.5rem;
    font-size: 0.85rem;
    color: var(--accent);
    text-shadow: 3px 3px 0 #7a5800;
    letter-spacing: 2px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1.25rem;
}

.field label {
    font-size: 0.45rem;
    color: var(--green);
    text-transform: uppercase;
    letter-spacing: 2px;
}

select {
    padding: 0.65rem 0.75rem;
    border: 3px solid var(--border);
    font-family: 'Press Start 2P', monospace;
    font-size: 0.75rem;
    background: var(--bg);
    color: var(--text);
    width: 100%;
    cursor: pointer;
}

select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(255, 221, 0, 0.25);
}

input[type="text"] {
    padding: 0.65rem 0.75rem;
    border: 3px solid var(--border);
    font-family: 'Press Start 2P', monospace;
    font-size: 0.55rem;
    background: var(--bg);
    color: var(--text);
    appearance: none;
    -webkit-appearance: none;
    width: 100%;
}

input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(255, 221, 0, 0.25);
}

input[type="text"]::placeholder {
    color: var(--muted);
}

/* ── Buttons ───────────────────────────────────────── */
.btn-primary,
.btn-secondary {
    display: inline-block;
    padding: 0.75rem 1.4rem;
    font-family: 'Press Start 2P', monospace;
    font-size: 0.55rem;
    cursor: pointer;
    text-decoration: none;
    border: none;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: transform 0.06s, box-shadow 0.06s;
    white-space: nowrap;
}

.btn-primary {
    background: var(--green);
    color: #000;
    box-shadow: 0 5px 0 #007733, 5px 5px 0 #000;
}

.btn-primary:hover { filter: brightness(1.1); }

.btn-primary:active {
    transform: translateY(5px);
    box-shadow: none;
}

.btn-secondary {
    background: var(--surface2);
    color: var(--text);
    border: 3px solid var(--border);
    box-shadow: 0 5px 0 #000, 5px 5px 0 #000;
}

.btn-secondary:hover { border-color: var(--muted); }

.btn-secondary:active {
    transform: translateY(5px);
    box-shadow: none;
}

/* ── Setup page extras ─────────────────────────────── */
.setup-subtitle {
    color: var(--muted);
    font-size: 0.4rem;
    margin-bottom: 1.75rem;
    line-height: 2.4;
}

.setup-actions {
    display: flex;
    gap: 1rem;
    align-items: center;
    margin-top: 1.75rem;
    flex-wrap: wrap;
}

/* ── High Score screen ─────────────────────────────── */
.history {
    /* screen-bezel look */
    overflow-x: auto;
    background: #000;
    border: 6px solid #555;
    box-shadow:
        0 0 0 3px #222,
        0 0 0 6px #444,
        8px 8px 0 #000,
        0 0 40px rgba(0, 204, 255, 0.12);
    padding: 0;
    overflow: hidden;
}

.score-header {
    background: var(--surface);
    border-bottom: 4px solid var(--accent);
    padding: 1.25rem 1rem;
    text-align: center;
}

.score-header h2 {
    font-size: 1rem;
    color: var(--accent);
    letter-spacing: 4px;
    text-shadow: 3px 3px 0 #7a5800, 0 0 20px rgba(255,221,0,0.4);
    animation: flicker 4s infinite;
}

.score-header .score-subtext {
    font-size: 0.5rem;
    color: var(--muted);
    margin-top: 0.75rem;
    letter-spacing: 2px;
}

@keyframes flicker {
    0%, 95%, 100% { opacity: 1; }
    96%            { opacity: 0.7; }
    97%            { opacity: 1; }
    98%            { opacity: 0.5; }
    99%            { opacity: 1; }
}

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
}

thead tr {
    background: var(--surface2);
    border-bottom: 3px solid var(--border);
}

th {
    padding: 0.75rem 1rem;
    text-align: left;
    font-size: 0.55rem;
    font-family: 'Press Start 2P', monospace;
    color: var(--o-color);
    text-transform: uppercase;
    letter-spacing: 1px;
}

td {
    padding: 0.75rem 1rem;
    text-align: left;
    border-bottom: 1px solid #1a1a50;
    font-size: 0.55rem;
    font-family: 'Press Start 2P', monospace;
    line-height: 2;
}

/* rank column */
td:first-child, th:first-child {
    color: var(--muted);
    width: 3rem;
    padding-right: 0;
}

tbody tr:nth-child(1) td { color: var(--gold); }
tbody tr:nth-child(2) td { color: #c0c0c0; }
tbody tr:nth-child(3) td { color: #cd7f32; }

tbody tr:last-child td { border-bottom: none; }

tbody tr.score-row {
    cursor: pointer;
}

/* ── Pagination ─────────────────────────────────────── */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    padding: 1rem;
    border-top: 4px solid var(--border);
    background: var(--surface);
}

.btn-page {
    font-family: inherit;
    font-size: 0.5rem;
    color: var(--accent);
    background: var(--surface2);
    border: 3px solid var(--border);
    padding: 0.5rem 0.75rem;
    box-shadow: 4px 4px 0 #000;
    text-decoration: none;
    cursor: pointer;
    transition: background 0.1s;
}

.btn-page:hover {
    background: var(--accent);
    color: #000;
}

.btn-page--disabled {
    color: var(--muted);
    cursor: not-allowed;
    box-shadow: none;
}

.btn-page.btn-page--disabled:hover {
    background: var(--surface2);
    color: var(--muted);
}

.page-num {
    font-size: 0.5rem;
    color: var(--muted);
    letter-spacing: 2px;
}

tbody tr.score-row:hover {
    background: var(--surface2);
    outline: 2px solid var(--border);
    outline-offset: -2px;
}

.status-x_wins { color: var(--x-color) !important; }
.status-o_wins { color: var(--o-color) !important; }
.status-draw   { color: var(--muted)   !important; }
.status-in_progress { color: var(--green) !important; }

/* ── Game ──────────────────────────────────────────── */
.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
}

/* Status banner */
.status-banner {
    font-size: 0.55rem;
    padding: 0.85rem 2rem;
    background: var(--surface);
    border: 4px solid var(--border);
    box-shadow: 4px 4px 0 #000;
    min-width: 260px;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 2px;
    line-height: 2;
}

.status-in_progress { border-color: var(--green); color: var(--green); }
.status-x_wins      { border-color: var(--x-color); color: var(--x-color); }
.status-o_wins      { border-color: var(--o-color); color: var(--o-color); }
.status-draw        { border-color: var(--muted); color: var(--muted); }

/* ── End-game actions (inside status banner) ───────── */
.end-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: 1.25rem;
}

.end-actions form,
.end-actions .btn-secondary {
    display: block;
    width: 100%;
    box-sizing: border-box;
    text-align: center;
}

.end-actions .btn-primary,
.end-actions .btn-secondary {
    width: 100%;
    box-sizing: border-box;
}

/* ── Board ─────────────────────────────────────────── */
#board, .board {
    display: grid;
    grid-template-columns: repeat(3, var(--cell-size));
    grid-template-rows: repeat(3, var(--cell-size));
    gap: 6px;
    background: var(--accent);
    border: 6px solid var(--accent);
    box-shadow:
        6px 6px 0 #000,
        0 0 30px rgba(255, 221, 0, 0.25);
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.25rem;
    font-family: 'Press Start 2P', monospace;
    background: var(--surface);
}

button.cell {
    cursor: pointer;
    border: none;
    transition: background 0.05s;
}

button.cell:hover  { background: var(--surface2); }
button.cell:active { background: #0a0a30; }

.cell-X {
    color: var(--x-color);
    text-shadow: 3px 3px 0 #880022, 0 0 16px rgba(255, 51, 85, 0.5);
}

.cell-O {
    color: var(--o-color);
    text-shadow: 3px 3px 0 #006688, 0 0 16px rgba(0, 204, 255, 0.5);
}

.cell-_ { color: transparent; }


/* ── Share modal ───────────────────────────────────── */
.share-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(8, 8, 30, 0.85);
    z-index: 10000;
    align-items: center;
    justify-content: center;
}

.share-modal {
    background: var(--surface);
    border: 4px solid var(--accent);
    box-shadow: 8px 8px 0 #000, 0 0 40px rgba(255, 221, 0, 0.2);
    padding: 2.5rem 2rem;
    max-width: 360px;
    width: calc(100% - 2rem);
    text-align: center;
}

.share-modal-title {
    font-size: 0.7rem;
    color: var(--accent);
    text-shadow: 3px 3px 0 #7a5800;
    letter-spacing: 2px;
    margin-bottom: 1rem;
    line-height: 2;
}

.share-modal-subtitle {
    font-size: 0.4rem;
    color: var(--muted);
    letter-spacing: 1px;
    line-height: 2.2;
    margin-bottom: 2rem;
}

.share-modal-actions {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
}

/* ── Responsive ────────────────────────────────────── */
@media (max-width: 420px) {
    header { padding: 1rem; }

    .logo {
        font-size: 0.85rem;
        letter-spacing: 2px;
    }

    .lobby { padding: 1.25rem; }

    .score-header h2 {
        font-size: 0.7rem;
        letter-spacing: 2px;
    }

    th, td { padding: 0.6rem 0.6rem; font-size: 0.45rem; }

    .status-banner {
        font-size: 0.45rem;
        padding: 0.75rem 1rem;
        min-width: 0;
        width: 100%;
    }
}
