/* ═══════════════════════════════════════════════
   CODENAMES — Premium Dark UI
   ═══════════════════════════════════════════════ */

/* ── Reset & Base ───────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg:        #0f1117;
    --surface:   #181a20;
    --surface-2: #1f2129;
    --border:    #2a2d38;
    --text:      #e8eaed;
    --text-dim:  #8b8f9a;
    --red:       #ef4444;
    --red-soft:  #3b1c1c;
    --red-glow:  rgba(239,68,68,.25);
    --blue:      #3b82f6;
    --blue-soft: #1c2a3b;
    --blue-glow: rgba(59,130,246,.25);
    --black-card:#1e1e2e;
    --accent:    #a78bfa;
    --green:     #22c55e;
    --green-soft:#143222;
    --green-glow:rgba(34,197,94,.25);
    --cyan:      #22d3ee;
    --cyan-soft: #0f2a33;
    --cyan-glow: rgba(34,211,238,.25);
    --neutral:   #eab308;
    --neutral-soft:#3a2f0b;
    --neutral-text:#1a1a1a;
    --radius:    12px;
    --radius-lg: 16px;
    --shadow:    0 4px 24px rgba(0,0,0,.35);
}

[data-theme="light"] {
    --bg:        #f8fafc;
    --surface:   #ffffff;
    --surface-2: #f1f5f9;
    --border:    #cbd5e1;
    --text:      #0f172a;
    --text-dim:  #64748b;
    --red:       #dc2626;
    --red-soft:  #fee2e2;
    --red-glow:  rgba(220,38,38,.2);
    --blue:      #2563eb;
    --blue-soft: #dbeafe;
    --blue-glow: rgba(37,99,235,.2);
    --black-card:#1e293b;
    --accent:    #7c3aed;
    --green:     #16a34a;
    --green-soft:#dcfce7;
    --green-glow:rgba(22,163,74,.2);
    --cyan:      #0891b2;
    --cyan-soft: #cffafe;
    --cyan-glow: rgba(8,145,178,.2);
    --neutral:   #ca8a04;
    --neutral-soft:#fef9c3;
    --neutral-text:#713f12;
    --shadow:    0 4px 24px rgba(15,23,42,.1);
}

html { font-size: 93.75%; }

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    transition: background-color .2s ease, color .2s ease;
}

/* ── Utility ────────────────────────────────── */
.hidden { display: none !important; }
.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;
}

/* Team colour mapping — JS only sets data-team */
[data-team="red"]   { --tc: var(--red);   --tc-soft: var(--red-soft);   --tc-glow: var(--red-glow); }
[data-team="blue"]  { --tc: var(--blue);  --tc-soft: var(--blue-soft);  --tc-glow: var(--blue-glow); }
[data-team="green"] { --tc: var(--green); --tc-soft: var(--green-soft); --tc-glow: var(--green-glow); }
[data-team="cyan"]  { --tc: var(--cyan);  --tc-soft: var(--cyan-soft);  --tc-glow: var(--cyan-glow); }

/* ── Buttons ────────────────────────────────── */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    font-family: inherit; font-weight: 600; font-size: .95rem;
    border: none; border-radius: var(--radius); cursor: pointer;
    padding: 11px 22px;
    transition: transform .15s, box-shadow .15s, background .2s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }

.btn-primary {
    background: linear-gradient(135deg, var(--accent), #8b5cf6);
    color: #fff;
    box-shadow: 0 4px 14px rgba(167,139,250,.35);
}
.btn-primary:hover:not(:disabled) {
    box-shadow: 0 6px 20px rgba(167,139,250,.5);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) {
    background: var(--border);
    transform: translateY(-1px);
}

.btn-end-turn {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    font-size: .85rem;
    padding: 8px 16px;
}
.btn-end-turn:hover { background: var(--border); }

.btn-send {
    background: var(--green);
    color: #fff;
    padding: 8px 18px;
    font-size: .85rem;
}
.btn-send:hover { background: #16a34a; }

.btn-small { padding: 4px 10px; font-size: .8rem; }

.theme-toggle-btn {
    gap: 6px;
    font-size: .8rem;
    padding: 6px 12px;
    border-radius: 20px;
    cursor: pointer;
    user-select: none;
    transition: background .2s, border-color .2s, transform .15s;
}

#role-toggle-btn {
    transition: background .2s, border-color .2s, color .2s, transform .15s;
}
#role-toggle-btn.is-spymaster {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.btn-start-timer {
    background: linear-gradient(135deg, #10b981, #059669);
    color: #fff;
    box-shadow: 0 4px 14px rgba(16, 185, 129, .35);
    border: none;
    animation: pulse-start 2s infinite ease-in-out;
}
.btn-start-timer:hover:not(:disabled) {
    background: linear-gradient(135deg, #059669, #047857);
    box-shadow: 0 6px 20px rgba(16, 185, 129, .5);
    transform: translateY(-1px);
}
.btn-start-timer:disabled {
    background: var(--surface-2);
    color: var(--text-dim);
    border: 1px solid var(--border);
    box-shadow: none;
    animation: none;
    cursor: default;
    opacity: 0.85;
}
@keyframes pulse-start {
    0%, 100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4); }
    50% { box-shadow: 0 0 0 5px rgba(16, 185, 129, 0); }
}

.lobby-topbar {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 12px;
}

.team-spymaster-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin: 10px 0 6px 0;
    padding: 8px 12px;
    border-radius: 8px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    font-size: .8rem;
    color: var(--text-dim);
}
.team-spymaster-banner strong {
    color: var(--text);
}
.team-spymaster-banner[data-team] {
    border-left: 3px solid var(--tc);
}

.lobby-footer { text-align: center; margin-top: 30px; }

/* ═══════════════════════════════════════════════
   LOBBY
   ═══════════════════════════════════════════════ */
#lobby-screen {
    display: flex; align-items: center; justify-content: center;
    min-height: 100vh;
    padding: 30px 20px;
}

.lobby-wrapper {
    width: 100%; max-width: 720px;
}

.lobby-brand {
    text-align: center;
    margin-bottom: 40px;
}
.lobby-logo { font-size: 3.5rem; margin-bottom: 8px; }
.lobby-brand h1 {
    font-size: 2.6rem; font-weight: 800;
    background: linear-gradient(135deg, var(--accent), var(--blue));
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
    background-clip: text;
}
.lobby-brand p { color: var(--text-dim); margin-top: 8px; font-size: 1rem; }

.lobby-panels {
    display: flex; gap: 20px; align-items: stretch;
}

.lobby-panel {
    flex: 1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 28px;
    display: flex; flex-direction: column; gap: 18px;
}

.panel-header {
    display: flex; align-items: center; gap: 10px;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--border);
}
.panel-icon { font-size: 1.4rem; }
.panel-header h2 { font-size: 1.2rem; font-weight: 700; }

.lobby-divider {
    display: flex; align-items: center; justify-content: center;
    writing-mode: vertical-lr;
    color: var(--text-dim); font-size: .8rem; font-weight: 600;
    letter-spacing: 2px; text-transform: uppercase;
}
.lobby-divider span {
    background: var(--bg); padding: 8px 0;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: .8rem; font-weight: 600; color: var(--text-dim); text-transform: uppercase; letter-spacing: .5px; }

.field input, .field select {
    padding: 11px 14px;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: inherit; font-size: .95rem;
    transition: border-color .2s, box-shadow .2s;
    outline: none;
}
.field input:focus, .field select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(167,139,250,.2);
}
.field input::placeholder { color: var(--text-dim); }
.field select option { background: var(--surface); }

/* ═══════════════════════════════════════════════
   GAME SCREEN
   ═══════════════════════════════════════════════ */
#game-screen {
    display: flex; flex-direction: column;
    height: 100vh;
    height: 100dvh; /* dynamic viewport height — respects mobile address bar */
    overflow: hidden;
}

/* ── Top Bar ─────────────────────────────────── */
.topbar {
    display: flex; justify-content: space-between; align-items: center;
    padding: 14px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 14px; }
.topbar-brand { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }

.badge {
    padding: 5px 14px; border-radius: 20px;
    font-size: .8rem; font-weight: 600;
    white-space: nowrap;
}
.badge-code {
    background: var(--surface-2); border: 1px solid var(--border);
    font-family: 'Courier New', monospace; font-size: .9rem; letter-spacing: 2px;
}
.badge-mode {
    background: var(--surface-2); border: 1px solid var(--border);
    color: var(--text-dim); font-weight: 700; text-transform: uppercase; font-size: .75rem; letter-spacing: .5px;
}
.badge-mode.suspense {
    border-color: #f59e0b;
    color: #f59e0b;
    background: rgba(245, 158, 11, 0.12);
}
.badge-mode.easy {
    border-color: #10b981;
    color: #10b981;
    background: rgba(16, 185, 129, 0.12);
}
.badge-mode.adult,
.badge-mode.sss {
    border-color: #ec4899;
    color: #ec4899;
    background: rgba(236, 72, 153, 0.12);
}
.badge-role { background: var(--accent); color: #fff; text-transform: uppercase; letter-spacing: .5px; }

/* ── Scoreboard ──────────────────────────────── */
.scoreboard { display: flex; align-items: center; gap: 16px; }
.score { font-weight: 700; font-size: 1.1rem; color: var(--tc); }
.score.eliminated { opacity: .45; text-decoration: line-through; }

.turn-pill {
    padding: 6px 18px; border-radius: 20px;
    font-weight: 700; font-size: .85rem; text-transform: uppercase; letter-spacing: 1px;
    transition: background .3s, box-shadow .3s;
}
.turn-pill {
    background: var(--tc, var(--surface-2)); color: #fff;
    box-shadow: 0 0 12px var(--tc-glow, transparent);
}

.turn-timer {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: 20px;
    background: var(--surface-2, rgba(255, 255, 255, 0.08));
    border: 1px solid var(--border, rgba(255, 255, 255, 0.12));
    font-weight: 700;
    font-size: 0.88rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    letter-spacing: 0.5px;
    transition: color 0.2s, border-color 0.2s, background 0.2s, box-shadow 0.2s;
}
.turn-timer.low {
    color: #ff6b6b;
    border-color: rgba(255, 107, 107, 0.4);
    background: rgba(255, 107, 107, 0.12);
    box-shadow: 0 0 10px rgba(255, 107, 107, 0.3);
    animation: pulse-low 1s infinite alternate;
}
@keyframes pulse-low {
    from { opacity: 0.9; }
    to { opacity: 1; }
}

/* ── Game Body (Board + Chat) ────────────────── */
.game-body {
    display: flex; flex: 1; overflow: hidden;
    max-width: 100vw;
}

.board-area {
    flex: 1;
    min-width: 0; /* allow flex item to shrink below content size */
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    padding: 24px;
    gap: 16px;
    overflow: auto;
}

/* ── Board ────────────────────────────────────── */
.board {
    display: grid;
    grid-template-columns: repeat(var(--cols, 5), minmax(0, 1fr));
    gap: 10px;
    width: 100%; max-width: calc(var(--cols, 5) * 136px);
}
.board[data-grid="6"] .card { font-size: .78rem; padding: 14px 6px; }
.board[data-grid="7"] .card { font-size: .7rem; padding: 12px 4px; letter-spacing: 0; }

.card {
    position: relative;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 8px;
    text-align: center;
    font-weight: 700;
    font-size: .85rem;
    text-transform: uppercase;
    letter-spacing: .5px;
    cursor: pointer;
    user-select: none;
    display: flex; align-items: center; justify-content: center;
    aspect-ratio: 5/3;
    color: var(--text);
    font: inherit; font-weight: 700; font-size: .85rem; appearance: none;
    min-width: 0; overflow-wrap: anywhere; hyphens: auto;
    transition: transform .15s, border-color .2s, box-shadow .2s, background .3s;
}
.card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Not allowed to guess */
body:not(.can-guess) .card:not(.revealed) { cursor: default; opacity: .85; }

/* Guesser hover */
body.can-guess .card:not(.revealed):hover {
    transform: translateY(-3px);
    border-color: var(--accent);
    box-shadow: 0 6px 20px rgba(167,139,250,.2);
}

/* Suspense Mode Card Selection */
.card.selected {
    border: 2px solid #f59e0b !important;
    background: rgba(245, 158, 11, 0.18) !important;
    color: var(--text);
    box-shadow: 0 0 16px rgba(245, 158, 11, 0.45);
    transform: translateY(-2px);
}
[data-theme="light"] .card.selected {
    background: #fef3c7 !important;
    border-color: #d97706 !important;
    color: #92400e;
}
.card.selected::before {
    content: '✓ ';
    font-weight: 800;
    color: #f59e0b;
    margin-right: 4px;
}
[data-theme="light"] .card.selected::before {
    color: #d97706;
}

/* Suspense bar */
.suspense-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 20px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    transition: opacity .2s, transform .2s;
}
.suspense-bar.hidden { display: none !important; }
.suspense-count {
    font-size: .88rem;
    font-weight: 700;
    color: var(--text);
}

/* Card reveal animation */
@keyframes cardReveal {
    0% { transform: scale(0.92); opacity: 0.8; }
    50% { transform: scale(1.04); }
    100% { transform: scale(1); opacity: 1; }
}
.card.revealing {
    animation: cardReveal .4s ease forwards;
}

/* Revealed cards */
.card.revealed {
    pointer-events: none;
    color: #fff;
    border-color: transparent;
}
.card.revealed::after {
    content: ''; position: absolute; top: 0; right: 0; bottom: 0; left: 0; border-radius: var(--radius);
    opacity: .12; background: #fff;
}
/* Revealed colours */
.card.revealed[data-team] { background: var(--tc); box-shadow: 0 0 18px var(--tc-glow); color: #fff; }
.card.revealed[data-team="neutral"] { background: var(--neutral); color: var(--neutral-text); box-shadow: none; }
.card.revealed[data-team="black"]   { background: var(--black-card); color: #ddd; box-shadow: 0 0 18px rgba(0,0,0,.5); }

/* Spymaster peek (unrevealed) */
body.spymaster .card:not(.revealed)[data-team] { border-color: var(--tc); background: var(--tc-soft); color: var(--tc); }
body.spymaster .card:not(.revealed)[data-team="neutral"] { border-color: var(--neutral); background: var(--neutral-soft); color: var(--neutral); }
body.spymaster .card:not(.revealed)[data-team="black"]   { border-color: #555; background: #1a1a2a; color: #888; }
[data-theme="light"] body.spymaster .card:not(.revealed)[data-team="black"] { border-color: #475569; background: #e2e8f0; color: #1e293b; font-weight: 800; }
[data-theme="light"] .card.revealed[data-team="black"] { background: #1e293b; color: #f8fafc; }
[data-theme="light"] .overlay { background: rgba(15, 23, 42, 0.45); }
[data-theme="light"] .lobby-panel,
[data-theme="light"] .topbar,
[data-theme="light"] .chat-panel,
[data-theme="light"] .modal-card { box-shadow: var(--shadow); }

@keyframes reveal { from { transform: scale(.92); } to { transform: scale(1); } }
.card.revealed { animation: reveal .25s ease; }
@media (prefers-reduced-motion: reduce) { .card.revealed { animation: none; } }

/* ── Chat Panel ──────────────────────────────── */
.chat-panel {
    width: 320px; flex-shrink: 0;
    display: flex; flex-direction: column;
    background: var(--surface);
    border-left: 1px solid var(--border);
    overflow: hidden;
}

.chat-title {
    padding: 16px 20px;
    font-size: 1rem; font-weight: 700;
    border-bottom: 1px solid var(--border);
}

.players-section {
    padding: 15px 20px;
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.02);
    flex-shrink: 0;
}
.players-list {
    list-style: none;
    margin-top: 10px;
    font-size: 0.85rem;
    color: var(--text-dim);
    max-height: 100px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.player-item {
    display: flex;
    align-items: center;
    gap: 8px;
}
.player-item[data-team] { color: var(--tc); }

.chat-log {
    flex: 1; overflow-y: auto;
    padding: 16px;
    display: flex; flex-direction: column; gap: 10px;
}
.chat-log::-webkit-scrollbar { width: 5px; }
.chat-log::-webkit-scrollbar-track { background: transparent; }
.chat-log::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

.hint-msg {
    padding: 10px 14px;
    border-radius: 10px;
    font-size: .88rem;
    line-height: 1.5;
}
.hint-msg[data-team] { background: var(--tc-soft); border-left: 4px solid var(--tc); }
.hint-msg.chat { font-style: normal; }
.hint-msg.chat strong { color: var(--tc); }
.hint-msg.system {
    background: transparent; color: var(--text-dim);
    text-align: center; font-style: italic; font-size: .8rem;
    padding: 6px;
}
.hint-msg.selection {
    background: rgba(59, 130, 246, 0.08);
    border-left: 3px solid #3b82f6;
    font-size: .83rem;
    padding: 6px 10px;
}
.hint-msg.selection strong {
    color: var(--text);
}

/* ── Spymaster Lounge (Desktop only, Spymaster role only) ── */
.spymaster-panel {
    width: 250px;
    min-width: 230px;
    max-width: 280px;
    background: var(--surface-2);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0;
}
.spymaster-panel-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 16px 4px 16px;
}
.spymaster-icon { font-size: 1.2rem; }
.spymaster-panel-header h2 {
    font-size: .95rem;
    font-weight: 700;
    margin: 0;
    color: var(--text);
}
.spymaster-panel-sub {
    margin: 0;
    padding: 0 16px 12px 16px;
    font-size: .75rem;
    color: var(--text-dim);
    border-bottom: 1px solid var(--border);
}
.spymaster-chat-log {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 150px;
}
.spymaster-chat-log::-webkit-scrollbar { width: 5px; }
.spymaster-chat-log::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }
.spymaster-msg {
    padding: 8px 10px;
    border-radius: 8px;
    background: var(--surface);
    border-left: 3px solid var(--accent);
    font-size: .84rem;
    line-height: 1.4;
    color: var(--text);
}
.spymaster-msg[data-team] {
    background: var(--tc-soft);
    border-left-color: var(--tc);
}
.spymaster-msg strong {
    color: var(--tc, var(--accent));
}
.spymaster-chat-form {
    display: flex;
    gap: 6px;
    padding: 12px 14px;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
    flex-shrink: 0;
}
.spymaster-chat-form input {
    flex: 1;
    padding: 8px 10px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: inherit;
    font-size: .84rem;
    min-width: 0;
    outline: none;
}
.spymaster-chat-form input:focus {
    border-color: var(--accent);
}

.chat-input-row {
    display: flex; gap: 8px;
    padding: 14px 16px;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
    flex-shrink: 0;
}
.chat-input-row input {
    padding: 9px 12px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: inherit; font-size: .88rem;
    outline: none;
    transition: border-color .2s;
    min-width: 0; /* prevent input from overflowing flex container */
}
.chat-input-row input:focus { border-color: var(--accent); }
#hint-word   { flex: 1; }
#hint-number { width: 50px; flex-shrink: 0; text-align: center; }

/* ═══════════════════════════════════════════════
   GAME-OVER MODAL
   ═══════════════════════════════════════════════ */
.overlay {
    position: fixed; top: 0; right: 0; bottom: 0; left: 0;
    background: rgba(0,0,0,.7);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000;
    animation: fadeIn .25s ease;
}
.overlay.hidden { display: none !important; }

.modal-card {
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 50px 60px;
    border-radius: var(--radius-lg);
    text-align: center;
    box-shadow: var(--shadow);
    animation: popIn .3s cubic-bezier(.175,.885,.32,1.275);
}
.modal-card h2 {
    font-size: 2.2rem; font-weight: 800;
    margin-bottom: 28px;
}
.modal-card h2[data-team] { color: var(--tc); text-shadow: 0 0 30px var(--tc-glow); }
.modal-card h2.tie {
    background: linear-gradient(135deg, #f59e0b, #ec4899, #06b6d4);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 0 30px rgba(245, 158, 11, 0.4);
}
.modal-actions { display: flex; gap: 15px; justify-content: center; margin-top: 20px; flex-wrap: wrap; }

.rules-card { text-align: left; max-width: 600px; padding: 40px; }
.rules-card h2 { font-size: 1.8rem; margin-bottom: 20px; }
.rules-content { font-size: .95rem; line-height: 1.6; max-height: 60vh; overflow-y: auto; padding-right: 15px; }
.rules-list { margin: 10px 0 20px 20px; display: flex; flex-direction: column; gap: 8px; }
.rules-footer { text-align: right; margin-top: 30px; }

.toast {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
    background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
    padding: 10px 16px; border-radius: 10px; z-index: 2000;
    box-shadow: var(--shadow); max-width: 90vw;
}
.toast.error { border-color: var(--red); }

@keyframes fadeIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn   { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ═══════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════ */

/* ── Tablet / narrow desktop (≤ 1100px) ────── */
@media (max-width: 1100px) {
    .chat-panel { width: 280px; }
    .board { max-width: 560px; gap: 8px; }
    .card { font-size: .78rem; padding: 14px 6px; }
}

/* ── Default Mobile Component Styles (Desktop: hidden) ── */
.mobile-tabs { display: none; }
.mobile-hint-banner { display: none; }

/* ── Small tablet / landscape mobile (≤ 900px) ──── */
@media (max-width: 900px) {
    /* Lobby */
    .lobby-panels { flex-direction: column; gap: 16px; }
    .lobby-divider {
        writing-mode: horizontal-tb;
        width: 100%; text-align: center;
        padding: 4px 0;
    }
    .lobby-panel { padding: 22px; }
    .spymaster-panel { display: none !important; }
}

/* ── Mobile Phone & Tablet Portrait (≤ 768px) ────── */
@media (max-width: 768px) {
    /* Prevent iOS auto-zoom on input focus */
    input[type="text"], input[type="number"], select, textarea {
        font-size: 16px !important;
    }

    button, .card, .mobile-tab-btn, .badge-code {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    /* Lobby */
    #lobby-screen { padding: 18px 12px; }
    .lobby-brand h1 { font-size: 2.1rem; }
    .lobby-logo { font-size: 2.4rem; }
    .lobby-panel { padding: 18px; border-radius: var(--radius); gap: 12px; }
    .field input, .field select, .btn { min-height: 44px; }

    /* Topbar: Streamlined 2-tier mobile header */
    .topbar {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
        padding: 8px 12px;
    }
    .topbar-left {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        flex-wrap: wrap;
        gap: 6px;
        width: 100%;
    }
    .topbar-brand { display: none; }
    .badge-code {
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 4px 8px;
        letter-spacing: 1px;
    }
    .badge-code::after {
        content: '📋';
        font-size: 0.75rem;
    }
    .badge-code:active {
        transform: scale(0.95);
    }
    .topbar-left .btn {
        padding: 6px 10px;
        font-size: 0.78rem;
        min-height: 32px;
    }
    .topbar-right {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        width: 100%;
    }
    .scoreboard {
        flex: 1 1 auto;
        min-width: 0;
        display: flex;
        align-items: center;
        gap: 8px;
        overflow-x: auto;
        scrollbar-width: none;
    }
    .scoreboard::-webkit-scrollbar { display: none; }
    .score { font-size: 0.95rem; }
    .turn-pill {
        font-size: 0.7rem;
        padding: 4px 8px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        max-width: 150px;
    }
    .btn-end-turn {
        flex: none;
        padding: 7px 14px;
        font-size: 0.82rem;
        min-height: 34px;
        border-radius: 8px;
    }

    /* Mobile Segmented Navigation Tabs */
    .mobile-tabs {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 8px 12px;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        position: sticky;
        top: 0;
        z-index: 30;
    }
    .mobile-tab-btn {
        flex: 1;
        max-width: 240px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 9px 16px;
        font-size: 0.88rem;
        font-weight: 600;
        color: var(--text-dim);
        background: var(--surface-2);
        border: 1px solid var(--border);
        border-radius: 9999px;
        cursor: pointer;
        transition: all 0.15s ease;
        position: relative;
    }
    .mobile-tab-btn.active {
        color: var(--text);
        background: var(--border);
        border-color: var(--accent);
        box-shadow: 0 2px 8px rgba(0,0,0,0.25);
    }
    .tab-unread-dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background: var(--red);
        box-shadow: 0 0 8px var(--red);
        position: absolute;
        top: 6px;
        right: 14px;
        animation: pulse 1.5s infinite;
    }

    /* Glanceable Mobile Active Clue & Status Banner */
    .mobile-hint-banner {
        display: flex;
        flex-direction: column;
        gap: 6px;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        padding: 8px 12px;
        margin-bottom: 8px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }
    .banner-status-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
    }
    .banner-turn-badge {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        font-size: 0.72rem;
        font-weight: 800;
        letter-spacing: 0.5px;
        padding: 3px 8px;
        border-radius: 9999px;
        background: var(--surface-2);
        color: var(--text);
    }
    .banner-turn-badge[data-team="red"] { background: var(--red-soft); color: var(--red); }
    .banner-turn-badge[data-team="blue"] { background: var(--blue-soft); color: var(--blue); }
    .banner-turn-badge[data-team="green"] { background: var(--green-soft); color: var(--green); }
    .banner-turn-badge[data-team="cyan"] { background: var(--cyan-soft); color: var(--cyan); }

    .banner-timer-badge {
        font-size: 0.78rem;
        font-weight: 700;
        padding: 3px 8px;
        border-radius: 6px;
        background: var(--surface-2);
        color: var(--text);
    }
    .banner-timer-badge.low {
        color: var(--red);
        animation: pulse 1s infinite;
    }
    .banner-clue-row {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 0.92rem;
        font-weight: 700;
        color: var(--text);
    }
    .banner-clue-text {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Game Layout & Mobile Tab Switching */
    .game-body {
        flex-direction: column;
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .game-body[data-mobile-tab="board"] .board-area {
        display: flex;
        flex-direction: column;
        width: 100%;
        padding: 10px 8px calc(70px + env(safe-area-inset-bottom, 0px)) 8px;
    }
    .game-body[data-mobile-tab="board"] .chat-panel {
        display: contents;
    }
    .game-body[data-mobile-tab="board"] .chat-panel > *:not(#hint-controls) {
        display: none !important;
    }
    .game-body[data-mobile-tab="board"] .chat-input-row#hint-controls.hidden {
        display: none !important;
    }

    .game-body[data-mobile-tab="chat"] .board-area {
        display: none !important;
    }
    .game-body[data-mobile-tab="chat"] .chat-panel {
        display: flex;
        flex-direction: column;
        flex: 1;
        width: 100%;
        height: 100%;
        max-height: none;
        border-left: none;
        border-top: none;
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    .game-body[data-mobile-tab="chat"] #chat-log {
        flex: 1;
        min-height: 250px;
    }

    /* Docked Spymaster Clue Bar on Board Tab */
    body.spymaster .game-body[data-mobile-tab="board"] .chat-input-row#hint-controls:not(.hidden) {
        display: flex !important;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 50;
        background: var(--surface);
        box-shadow: 0 -4px 20px rgba(0,0,0,0.45);
        border-top: 1px solid var(--border);
        padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px));
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
    }

    /* Docked Suspense Mode Bar on Board */
    .suspense-bar:not(.hidden) {
        position: sticky;
        bottom: 8px;
        z-index: 40;
        border-radius: var(--radius);
        box-shadow: 0 4px 18px rgba(0,0,0,0.4);
    }

    /* Board & Cards Touch Polish */
    .board-area { flex: none; width: 100%; }
    .board { width: 100%; max-width: 100%; }
    .board[data-grid="5"] { gap: 5px; }
    .board[data-grid="5"] .card {
        font-size: clamp(0.72rem, 2.9vw, 0.95rem);
        min-height: 52px;
        padding: 8px 3px;
        border-radius: 8px;
        hyphens: auto;
        overflow-wrap: break-word;
        word-break: normal;
    }
    .board[data-grid="6"] { gap: 4px; }
    .board[data-grid="6"] .card {
        font-size: clamp(0.6rem, 2.3vw, 0.8rem);
        min-height: 44px;
        padding: 6px 2px;
        border-radius: 6px;
        hyphens: auto;
        overflow-wrap: break-word;
        word-break: normal;
    }
    .board[data-grid="7"] { gap: 3px; }
    .board[data-grid="7"] .card {
        font-size: clamp(0.5rem, 1.8vw, 0.68rem);
        min-height: 38px;
        padding: 4px 1px;
        border-radius: 5px;
        line-height: 1.12;
        hyphens: auto;
        overflow-wrap: break-word;
        word-break: normal;
    }
    .card:active:not([disabled]) {
        transform: scale(0.95);
        transition: transform 0.08s ease;
    }

    /* Modal dialogs */
    .modal-card {
        padding: 24px 18px;
        width: min(94vw, 480px);
        max-height: 85vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ── Extra Small Phones (≤ 400px) ─────────────────── */
@media (max-width: 400px) {
    .topbar { padding: 6px 8px; gap: 5px; }
    .topbar-left { gap: 4px; }
    .topbar .badge { padding: 3px 6px; font-size: 0.68rem; }
    .topbar-left .btn { padding: 4px 8px; font-size: 0.72rem; }
    .scoreboard { gap: 6px; }
    .score { font-size: 0.85rem; }
    .btn-end-turn { padding: 6px 10px; font-size: 0.78rem; }
    .board[data-grid="5"] { gap: 4px; }
    .board[data-grid="5"] .card { font-size: 0.7rem; min-height: 46px; }
    .board[data-grid="6"] { gap: 3px; }
    .board[data-grid="6"] .card { font-size: 0.58rem; min-height: 40px; }
    .board[data-grid="7"] { gap: 2px; }
    .board[data-grid="7"] .card { font-size: 0.48rem; min-height: 34px; }
}

/* ── Connecting state (S4) ─────────────────── */
body.connecting .board-area { position: relative; }
body.connecting .board-area::before {
    content: 'Connecting…';
    position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
    color: var(--text-dim); font-weight: 600; z-index: 1;
}
body.connecting .board { pointer-events: none; opacity: .4; }

/* ── Accessibility (S5) ────────────────────── */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.field input:focus-visible, .field select:focus-visible, .chat-input-row input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.chat-log:focus-visible, .rules-content:focus-visible, .board:focus-visible { outline-offset: -2px; }
.toast { cursor: pointer; }

/* dark text where white fails 4.5:1 (white on cyan 1.8, on green 2.3) */
.card.revealed[data-team="green"], .card.revealed[data-team="cyan"],
.turn-pill[data-team="green"], .turn-pill[data-team="cyan"] { color: #0b1220; }

/* axe: white on these fills is 1.8–3.8:1; dark text reaches 4.9:1 or better on every one */
.badge-role, .btn-send, .turn-pill,
.card.revealed[data-team="red"], .card.revealed[data-team="blue"] { color: #0b1220; }

/* First-connection failure panel */
.connect-error { text-align: center; color: var(--text-dim); }
.connect-error p { font-weight: 600; }
