:root {
    --bg-color: #0f172a;
    --card-bg: rgba(30, 41, 59, 0.7);
    --accent-x: #22d3ee; /* Cyan */
    --accent-o: #f472b6; /* Pink */
    --text-color: #f8fafc;
    --glass-border: rgba(255, 255, 255, 0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Poppins', sans-serif;
}

body {
    background-color: var(--bg-color);
    background-image: radial-gradient(circle at top right, #1e1b4b, transparent),
                      radial-gradient(circle at bottom left, #312e81, transparent);
    color: var(--text-color);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 12px 0;
}

.container {
    width: 100%;
    max-width: 980px;
    padding: 12px;
}

.app-shell {
    display: block;
}

.auth-card-shell {
    max-width: 450px;
    margin: 0 auto;
}

.app-screen {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 280px;
    gap: 14px;
    align-items: start;
}

.main-panel {
    min-width: 0;
}

#game-screen {
    width: min(100%, 52vh, 560px);
    margin: 0 auto;
}

#auth-screen {
    margin-bottom: 20px;
}

.auth-badge {
    margin-top: -10px;
    margin-bottom: 18px;
    color: #cbd5e1;
    font-size: 0.92rem;
}

/* Setup Screen */
.card {
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    padding: 24px;
    border-radius: 24px;
    text-align: center;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

h1 { font-weight: 800; font-size: 2.1rem; letter-spacing: 2px; margin-bottom: 10px; }
.accent { color: var(--accent-x); }

p { color: #94a3b8; margin-bottom: 18px; }

.rule-note {
    font-size: 0.92rem;
    line-height: 1.5;
    margin-top: 8px;
}

.setup-hint {
    min-height: 1.5em;
    margin-top: -12px;
    margin-bottom: 18px;
    color: #cbd5e1;
}

.label {
    display: block;
    color: #cbd5e1;
    font-size: 0.82rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    margin-bottom: 10px;
    text-transform: uppercase;
}

.game-type {
    margin-bottom: 18px;
}

.mode-toggle {
    display: flex;
    gap: 10px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}

.mode-btn {
    background: rgba(15, 23, 42, 0.9);
    border: 1px solid #334155;
    padding: 12px;
    flex: 1 1 120px;
}

.mode-btn.active {
    background: linear-gradient(135deg, #38bdf8, #818cf8);
    border-color: transparent;
}

.input-group input {
    width: 100%;
    padding: 15px;
    margin-bottom: 12px;
    border-radius: 12px;
    border: 1px solid #334155;
    background: #0f172a;
    color: white;
    font-size: 1rem;
    outline: none;
    transition: 0.3s;
}

.input-group input:focus { border-color: var(--accent-x); box-shadow: 0 0 10px rgba(34, 211, 238, 0.2); }
.input-group input:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.availability-box {
    margin-top: -2px;
    margin-bottom: 18px;
    padding: 14px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 14px;
    background: rgba(15, 23, 42, 0.55);
}

.availability-status {
    margin-bottom: 10px;
    color: #cbd5e1;
}

.availability-status.good {
    color: #67e8f9;
}

.availability-status.bad {
    color: #fda4af;
}

.username-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.suggestion-chip {
    width: auto;
    padding: 8px 12px;
    border-radius: 999px;
    border: 1px solid rgba(34, 211, 238, 0.2);
    background: rgba(34, 211, 238, 0.14);
    text-transform: none;
    box-shadow: none;
}

button {
    width: 100%;
    padding: 15px;
    border-radius: 12px;
    border: none;
    background: linear-gradient(135deg, #38bdf8, #818cf8);
    color: white;
    font-weight: 700;
    cursor: pointer;
    transition: 0.3s;
    text-transform: uppercase;
}

button:hover { transform: translateY(-2px); box-shadow: 0 10px 20px rgba(56, 189, 248, 0.3); }

/* Game Screen */
.hidden { display: none !important; }

.scoreboard {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.chat-panel {
    padding: 12px;
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 24px);
    text-align: left;
}

.chat-header {
    margin-bottom: 8px;
}

.chat-tabs {
    margin-bottom: 8px;
}

.chat-header h2 {
    margin-bottom: 2px;
    font-size: 1.05rem;
    letter-spacing: 0.04em;
}

.chat-tabs .mode-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.chat-tabs .mode-btn:disabled:hover {
    transform: none;
    box-shadow: none;
}

.chat-connection-status {
    color: #cbd5e1;
    margin-bottom: 0;
    font-size: 0.82rem;
}

.chat-messages {
    flex: 1;
    min-height: 120px;
    max-height: 280px;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 8px;
    padding-right: 2px;
    scrollbar-width: thin;
    scrollbar-color: rgba(129, 140, 248, 0.9) rgba(15, 23, 42, 0.35);
}

.chat-messages::-webkit-scrollbar {
    width: 8px;
}

.chat-messages::-webkit-scrollbar-track {
    background: rgba(15, 23, 42, 0.35);
    border-radius: 999px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: linear-gradient(180deg, rgba(56, 189, 248, 0.95), rgba(129, 140, 248, 0.95));
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.08);
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(180deg, rgba(34, 211, 238, 1), rgba(129, 140, 248, 1));
}

.chat-empty,
.chat-message {
    background: rgba(15, 23, 42, 0.46);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 10px;
    padding: 6px 8px;
    width: fit-content;
    max-width: 72%;
}

.chat-message.mine {
    margin-left: auto;
    background: linear-gradient(135deg, #38bdf8, #818cf8);
    border-color: transparent;
    color: #ffffff;
}

.chat-meta {
    display: flex;
    justify-content: space-between;
    gap: 6px;
    font-size: 0.66rem;
    color: #94a3b8;
    margin-bottom: 2px;
    align-items: center;
}

.chat-message.mine .chat-meta {
    color: rgba(255, 255, 255, 0.78);
}

.chat-message:not(.mine) {
    margin-right: auto;
}

.chat-message:not(.mine) .chat-meta strong,
.chat-message.mine .chat-meta strong {
    font-size: 0.64rem;
    font-weight: 600;
}

.chat-text {
    color: #f8fafc;
    line-height: 1.2;
    font-size: 0.8rem;
    margin-bottom: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.chat-message.mine .chat-text {
    color: #ffffff;
}

.shared-room-card {
    margin-top: 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 8px;
    border-radius: 8px;
    background: rgba(15, 23, 42, 0.22);
}

.shared-room-label {
    margin-bottom: 0;
    font-size: 0.72rem;
    color: inherit;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.join-room-chip {
    width: auto;
    min-width: 64px;
    padding: 6px 10px;
    font-size: 0.72rem;
    border-radius: 8px;
    text-transform: uppercase;
}

.join-room-status {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 92px;
    padding: 6px 10px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.14);
    color: inherit;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.chat-compose textarea {
    width: 100%;
    min-height: 56px;
    margin-bottom: 8px;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid #334155;
    background: rgba(15, 23, 42, 0.88);
    color: white;
    font-size: 0.9rem;
    outline: none;
    resize: none;
    line-height: 1.35;
}

.chat-compose button {
    padding: 11px;
    border-radius: 10px;
    font-size: 0.9rem;
}

.chat-compose textarea:focus {
    border-color: var(--accent-x);
    box-shadow: 0 0 10px rgba(34, 211, 238, 0.2);
}

.score-card {
    background: var(--card-bg);
    padding: clamp(10px, 1.8vw, 15px);
    border-radius: 15px;
    width: 40%;
    text-align: center;
    border-bottom: 4px solid transparent;
}

#p1-card.active { border-color: var(--accent-x); }
#p2-card.active { border-color: var(--accent-o); }

.score { display: block; font-size: clamp(1.1rem, 2.4vw, 1.5rem); font-weight: 800; }

.vs { font-weight: 800; color: #475569; }

#status-text { margin-bottom: 10px; text-align: center; font-size: 1rem; }
#status-text.result {
    color: #f8fafc;
    font-weight: 700;
    text-shadow: 0 0 18px rgba(248, 250, 252, 0.22);
}
#status-text.result.x {
    color: var(--accent-x);
    text-shadow: 0 0 18px rgba(34, 211, 238, 0.45);
}
#status-text.result.o {
    color: var(--accent-o);
    text-shadow: 0 0 18px rgba(244, 114, 182, 0.45);
}

.room-banner {
    margin-bottom: 10px;
    text-align: center;
    color: #cbd5e1;
    font-size: 0.95rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

/* The Board */
.board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: clamp(6px, 1.2vw, 10px);
    margin-bottom: 14px;
    width: 100%;
    max-width: min(100%, 52vh, 560px);
    margin-left: auto;
    margin-right: auto;
}

.cell {
    aspect-ratio: 1;
    background: var(--card-bg);
    border-radius: clamp(10px, 1.5vw, 15px);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(1.15rem, 3vw, 1.7rem);
    font-weight: 800;
    cursor: pointer;
    transition: 0.2s;
    border: 1px solid var(--glass-border);
}

.cell:hover { background: rgba(255, 255, 255, 0.05); }

.cell.x { color: var(--accent-x); text-shadow: 0 0 15px var(--accent-x); }
.cell.o { color: var(--accent-o); text-shadow: 0 0 15px var(--accent-o); }
.cell.disabled { cursor: not-allowed; }
.cell.disabled:hover { background: var(--card-bg); }
.cell.oldest {
    position: relative;
    box-shadow: inset 0 0 0 3px #fbbf24, 0 0 24px rgba(251, 191, 36, 0.35);
    background: linear-gradient(180deg, rgba(251, 191, 36, 0.18), rgba(255, 255, 255, 0.04));
    animation: oldestPulse 1s ease-in-out infinite;
}
.cell.oldest::after {
    content: "NEXT OUT";
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    padding: 3px 8px;
    border-radius: 999px;
    background: #fbbf24;
    color: #1e293b;
    font-size: 0.58rem;
    font-weight: 800;
    letter-spacing: 0.08em;
}
.cell.oldest.x {
    color: #67e8f9;
}
.cell.oldest.o {
    color: #f9a8d4;
}
.cell.winning {
    transform: scale(1.03);
    box-shadow: 0 0 0 2px rgba(248, 250, 252, 0.22), 0 0 26px rgba(248, 250, 252, 0.35);
    background: rgba(255, 255, 255, 0.08);
}

@keyframes oldestPulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(0.96);
    }
}

.controls { display: flex; gap: 8px; }
.secondary { background: transparent; border: 1px solid #334155; }

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(15, 23, 42, 0.9);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.modal-content {
    background: var(--card-bg);
    padding: 50px;
    border-radius: 30px;
    text-align: center;
    border: 1px solid var(--glass-border);
}

@media (max-width: 900px) {
    .app-screen {
        grid-template-columns: 1fr;
    }

    .chat-messages {
        height: 180px;
    }

    #game-screen,
    .board {
        max-width: 100%;
    }
}

@media (max-width: 520px) {
    .app-screen {
        grid-template-columns: 1fr;
    }

    .card,
    .modal-content {
        padding: 20px;
    }

    .mode-toggle,
    .controls {
        flex-direction: column;
    }

    #game-screen {
        width: min(100%, 78vw);
    }

    .chat-panel {
        padding: 10px;
    }

    .chat-empty,
    .chat-message {
        padding: 7px 8px;
    }
}
