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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #e94560;
  --accent2: #f5a623;
  --text: #eaeaea;
  --muted: #8892a4;
  --border: #2a3a5c;
  --success: #4caf50;
  --radius: 10px;
}

html, body { height: 100%; font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); }

button { cursor: pointer; font-family: inherit; border: none; border-radius: var(--radius); transition: opacity .15s, transform .1s; }
button:hover:not(:disabled) { opacity: .88; }
button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .4; cursor: not-allowed; }

input { font-family: inherit; background: var(--surface2); border: 1.5px solid var(--border); border-radius: var(--radius); color: var(--text); padding: 10px 14px; outline: none; transition: border-color .15s; width: 100%; }
input:focus { border-color: var(--accent); }
input::placeholder { color: var(--muted); }

::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: var(--surface); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Screens ────────────────────────────────────────── */
.screen { display: none; height: 100vh; }
.screen.active { display: flex; flex-direction: column; }

/* ── Lobby Screen ───────────────────────────────────── */
.lobby-bg {
  flex: 1; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at top, #1e2d5a 0%, #1a1a2e 60%);
  padding: 20px;
}
.lobby-card {
  background: var(--surface); border: 1.5px solid var(--border); border-radius: 18px;
  padding: 40px 36px; width: 100%; max-width: 420px; box-shadow: 0 8px 40px rgba(0,0,0,.4);
}
.lobby-title { text-align: center; margin-bottom: 28px; }
.palette-icon { font-size: 48px; display: block; margin-bottom: 8px; }
.lobby-title h1 {
  font-size: 2.2rem; font-weight: 800;
  background: linear-gradient(135deg, #e94560, #f5a623);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.lobby-title p { color: var(--muted); font-size: .95rem; margin-top: 4px; }

/* ── Auth bar ───────────────────────────────────────────────────────────────── */
.auth-bar { margin-bottom: 20px; }

.btn-google {
  display: flex; align-items: center; gap: 10px; justify-content: center;
  width: 100%; padding: 10px 16px;
  background: #fff; color: #3c4043;
  border: 1.5px solid #dadce0; border-radius: var(--radius);
  font-size: .93rem; font-weight: 600; text-decoration: none;
  transition: box-shadow .15s, background .15s;
}
.btn-google:hover { background: #f8f9fa; box-shadow: 0 1px 6px rgba(0,0,0,.2); }

.auth-user { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--surface2); border-radius: var(--radius); }
.auth-avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; display: none; }
.auth-name { font-weight: 600; font-size: .9rem; flex: 1; }
.auth-premium { font-size: .78rem; color: var(--accent2); font-weight: 700; }
.auth-signout { font-size: .8rem; color: var(--muted); text-decoration: none; }
.auth-signout:hover { color: var(--accent); }

.tab-bar { display: flex; background: var(--bg); border-radius: 8px; padding: 4px; margin-bottom: 24px; gap: 4px; }
.tab { flex: 1; padding: 9px; background: transparent; color: var(--muted); font-size: .9rem; font-weight: 600; border-radius: 6px; }
.tab.active { background: var(--surface2); color: var(--text); }

#lobby-form { display: flex; flex-direction: column; gap: 18px; }
.field-label { display: flex; flex-direction: column; gap: 7px; font-size: .85rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }

.rounds-row { display: flex; gap: 8px; }
.round-btn { flex: 1; padding: 9px; background: var(--bg); color: var(--muted); font-size: 1rem; font-weight: 600; border-radius: 8px; }
.round-btn.active { background: var(--accent); color: white; }

.btn-primary { width: 100%; padding: 13px; background: linear-gradient(135deg, #e94560, #c73652); color: white; font-size: 1rem; font-weight: 700; border-radius: var(--radius); }

.error-box { background: rgba(233,69,96,.15); border: 1px solid var(--accent); color: var(--accent); padding: 10px 14px; border-radius: 8px; font-size: .9rem; }

/* ── Game Screen ────────────────────────────────────── */
.game-layout { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }

.game-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px; background: var(--surface); border-bottom: 1.5px solid var(--border);
  gap: 16px; flex-shrink: 0; min-height: 56px;
}
.header-room { font-size: .9rem; color: var(--muted); display: flex; align-items: center; gap: 6px; min-width: 140px; }
.header-room strong { color: var(--text); font-size: 1rem; letter-spacing: .1em; }
.copy-btn { background: var(--surface2); color: var(--muted); font-size: .85rem; padding: 3px 8px; border-radius: 5px; }
.header-center { display: flex; align-items: center; gap: 16px; flex: 1; justify-content: center; }
.state-label { color: var(--muted); font-size: .95rem; }
.word-hint { font-size: 1.5rem; font-weight: 700; letter-spacing: .25em; font-family: monospace; }
.timer { font-size: 1.4rem; font-weight: 800; min-width: 50px; text-align: center; transition: color .3s; }
.header-round { font-size: .9rem; color: var(--muted); min-width: 100px; text-align: right; display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.quit-btn { background: #ff4444; color: #fff; font-size: .78rem; font-weight: 600; padding: 4px 10px; border-radius: 6px; white-space: nowrap; }
.quit-btn:hover { background: #cc0000; }

.game-body {
  flex: 1; display: grid;
  grid-template-columns: 180px 1fr 280px;
  grid-template-rows: 1fr;
  gap: 10px; padding: 10px; overflow: hidden;
}

.game-sidebar { overflow-y: auto; }
.game-main { display: flex; align-items: stretch; justify-content: center; overflow: hidden; position: relative; }
.game-chat { overflow: hidden; display: flex; }

/* ── Panel ──────────────────────────────────────────── */
.panel { background: var(--surface); border: 1.5px solid var(--border); border-radius: var(--radius); overflow: hidden; height: 100%; display: flex; flex-direction: column; }
.panel-header { padding: 10px 14px; font-weight: 700; font-size: .85rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); border-bottom: 1px solid var(--border); flex-shrink: 0; }

/* ── Player list ────────────────────────────────────── */
.player-row { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.player-row:last-child { border-bottom: none; }
.player-row.me { background: rgba(233,69,96,.06); }
.avatar { width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: .9rem; color: white; flex-shrink: 0; }
.player-info { flex: 1; min-width: 0; }
.player-name { font-size: .88rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.you-badge { color: var(--muted); font-weight: 400; font-size: .78rem; }
.host-badge { color: var(--accent2); }
.drawing-badge { font-size: .8rem; }
.player-score { font-size: .78rem; color: var(--muted); margin-top: 2px; }
.guess-check { color: var(--success); font-size: .95rem; font-weight: 700; }

/* ── Overlays ───────────────────────────────────────── */
.overlay-panel {
  background: var(--surface); border: 1.5px solid var(--border); border-radius: 16px;
  padding: 36px 32px; text-align: center; max-width: 460px; width: 100%;
  display: flex; flex-direction: column; align-items: center; gap: 20px;
}
.overlay-panel h2 { font-size: 1.6rem; font-weight: 800; }
.muted-text { color: var(--muted); font-size: .92rem; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.player-chip { background: var(--surface2); border-radius: 20px; padding: 6px 14px; font-size: .88rem; font-weight: 600; }

.word-choices { display: flex; flex-direction: column; gap: 12px; width: 100%; }
.word-choice-btn {
  width: 100%; padding: 16px; background: var(--surface2); color: var(--text);
  font-size: 1.15rem; font-weight: 700; border-radius: var(--radius);
  border: 2px solid var(--border); text-transform: capitalize; transition: border-color .15s, background .15s;
}
.word-choice-btn:hover { border-color: var(--accent); background: rgba(233,69,96,.1); }

.spinner { width: 44px; height: 44px; border: 4px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.final-scores { width: 100%; display: flex; flex-direction: column; gap: 8px; }
.score-row { display: flex; align-items: center; gap: 12px; background: var(--surface2); padding: 12px 16px; border-radius: 10px; }
.score-row.gold { background: rgba(245,166,35,.15); border: 1px solid rgba(245,166,35,.4); }
.score-rank { font-size: 1.2rem; width: 28px; text-align: center; }
.score-name { flex: 1; font-weight: 700; }
.score-pts { color: var(--accent2); font-weight: 700; }

/* ── Canvas area ────────────────────────────────────── */
.canvas-area { display: flex; flex-direction: column; gap: 8px; width: 100%; min-height: 0; }
#draw-canvas {
  width: 100%;
  height: auto;                /* let width drive; height follows aspect-ratio */
  aspect-ratio: 800 / 550;    /* matches internal canvas resolution — no distortion */
  max-width: 100%;
  max-height: 100%;
  align-self: center;          /* centre vertically when container is taller than canvas */
  margin: 0 auto;              /* centre horizontally too */
  background: #fff;
  border-radius: var(--radius);
  display: block;
  touch-action: none;
  cursor: default;
}
#draw-canvas.drawable { cursor: crosshair; }

.toolbar { background: var(--surface); border: 1.5px solid var(--border); border-radius: var(--radius); padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; flex-shrink: 0; }
.color-grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 4px; }
.color-swatch { width: 100%; height: 28px; border-radius: 4px; border: 2px solid transparent; padding: 0; min-width: 0; transition: transform .1s, outline-color .1s; }
/* Use outline so it works even when the swatch has an inline border (e.g. white) */
.color-swatch.selected { outline: 3px solid var(--accent); outline-offset: 2px; transform: scale(1.15); }

.tool-row { display: flex; align-items: center; gap: 8px; }
.size-row { display: flex; gap: 6px; }
.size-btn { background: var(--surface2); border-radius: 6px; padding: 0; display: flex; align-items: center; justify-content: center; width: 34px; height: 34px; border: 2px solid transparent; }
.size-btn.selected { border-color: var(--accent); }
.size-btn.eraser-mode.selected { border-color: #60a5fa; }
.size-dot { border-radius: 50%; background: var(--text); display: block; }

.tool-btn { background: var(--surface2); color: var(--text); font-size: .95rem; padding: 0 10px; height: 34px; border-radius: 6px; border: 2px solid transparent; transition: background .1s, border-color .1s; }
/* Selected tool: solid accent background + white text — unmissable on all screens */
.tool-btn.selected { background: var(--accent); color: #fff; border-color: var(--accent); }
#undo-btn, #redo-btn { font-size: 1.05rem; }
.clear-btn { margin-left: auto; }

/* Eraser icon — classic pink-over-white block */
.eraser-icon {
  display: inline-block;
  width: 22px;
  height: 14px;
  background: linear-gradient(to bottom, #ffb3c1 55%, #f0f0f0 55%);
  border: 1.5px solid #999;
  border-radius: 2px;
  vertical-align: middle;
}

/* Toolbar divider */
.tool-divider { width: 1px; height: 24px; background: var(--border); flex-shrink: 0; }


/* ── Chat ───────────────────────────────────────────── */
.chat-panel { height: 100%; }
.chat-messages { flex: 1; overflow-y: auto; padding: 10px 12px; display: flex; flex-direction: column; gap: 5px; }
.chat-msg { font-size: .88rem; line-height: 1.4; word-break: break-word; }
.chat-msg.system { color: var(--muted); font-style: italic; font-size: .82rem; }
.chat-msg.correct { color: var(--success); font-weight: 600; font-style: normal; }
.chat-name { font-weight: 700; color: var(--accent2); }
.close-tag { font-size: .76rem; color: var(--accent2); font-weight: 600; }

.chat-input-row { display: flex; gap: 6px; padding: 10px 12px; border-top: 1px solid var(--border); flex-shrink: 0; }
.chat-input-row input { font-size: .88rem; padding: 8px 12px; }
.send-btn { background: var(--accent); color: white; font-size: 1.1rem; font-weight: 700; width: 36px; height: 36px; border-radius: 8px; flex-shrink: 0; }

/* ── Chat close button (mobile only) ────────────────── */
.chat-close-btn { display: none; }
.chat-fab { display: none; }

/* ── Tablet (≤ 900px) ───────────────────────────────── */
@media (max-width: 900px) {
  .game-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr 220px;
  }
  .game-sidebar { max-height: 100px; overflow-x: auto; overflow-y: hidden; }
  #player-list { display: flex; flex-direction: row; }
  .player-row { flex-direction: column; align-items: center; text-align: center; min-width: 80px; }
  .game-chat { height: 220px; }
}

/* ── Mobile (≤ 600px) ───────────────────────────────── */
@media (max-width: 600px) {

  /* Lobby card full-width */
  .lobby-card { padding: 24px 16px; border-radius: 12px; }
  .lobby-title h1 { font-size: 1.8rem; }

  /* ── Header: row 1 = room + round, row 2 = hint/state ── */
  .game-header {
    flex-wrap: wrap;
    padding: 6px 12px;
    min-height: auto;
    gap: 0;
    align-items: center;
  }
  .header-room {
    flex: 1 1 auto;
    font-size: .82rem;
    min-width: 0;
  }
  .header-round {
    flex: 0 0 auto;
    font-size: .82rem;
    text-align: right;
    min-width: 0;
  }
  /* Push state/hint/timer to its own full-width second row */
  .header-center {
    flex: 0 0 100%;
    order: 3;
    justify-content: center;
    padding: 4px 0 2px;
    gap: 10px;
  }
  .word-hint { font-size: 1.1rem; letter-spacing: .15em; }
  .timer { font-size: 1.05rem; min-width: 32px; }
  .state-label { font-size: .88rem; }

  /* ── Game body: flex column, not grid ── */
  .game-body {
    display: flex;
    flex-direction: column;
    padding: 6px;
    gap: 6px;
    overflow: hidden;
  }

  /* ── Players: fixed-height horizontal chip strip ── */
  .game-sidebar {
    flex: 0 0 auto;
    height: 52px;
    overflow: hidden;
  }
  /* Hide the "PLAYERS" label on mobile — no room */
  .game-sidebar .panel-header { display: none; }
  .game-sidebar .panel {
    flex-direction: row;
    align-items: stretch;
    overflow: hidden;
    height: 52px;
  }
  #player-list {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    flex: 1;
    height: 52px;
    align-items: center;
  }
  .player-row {
    flex-direction: row;
    align-items: center;
    min-width: 0;
    width: auto;
    padding: 6px 10px;
    gap: 6px;
    border-bottom: none;
    border-right: 1px solid var(--border);
    flex-shrink: 0;
  }
  .player-row:last-child { border-right: none; }
  .avatar { width: 24px; height: 24px; font-size: .75rem; flex-shrink: 0; }
  .player-info { min-width: 0; }
  .player-name { font-size: .78rem; white-space: nowrap; }
  .player-score { font-size: .7rem; }

  /* ── Main canvas area: fills remaining space ── */
  .game-main {
    flex: 1 1 0;
    min-height: 0;
    overflow: hidden;
  }
  .canvas-area { gap: 6px; height: 100%; }

  /* ── Toolbar: larger touch targets ── */
  .toolbar { padding: 8px 10px; gap: 8px; flex-shrink: 0; }
  .color-grid {
    grid-template-columns: repeat(6, 1fr);
    gap: 5px;
  }
  .color-swatch { height: 32px; border-radius: 5px; }
  .tool-row { flex-wrap: wrap; gap: 6px; }
  .size-btn { width: 40px; height: 40px; }
  .tool-btn { height: 40px; padding: 0 10px; font-size: .95rem; }
  #undo-btn, #redo-btn { font-size: 1.1rem; }

  /* ── Chat: hidden fixed drawer, slides up ── */
  .game-chat {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 55vh;
    max-height: 440px;
    transform: translateY(100%);
    transition: transform .28s cubic-bezier(.4,0,.2,1);
    z-index: 200;
    border-radius: 16px 16px 0 0;
    overflow: hidden;
    /* Remove from grid/flex flow */
    flex: none;
  }
  .game-chat.open { transform: translateY(0); }

  /* Chat panel header with close button */
  .panel-header { display: flex; align-items: center; justify-content: space-between; }
  .chat-close-btn {
    display: block;
    background: transparent;
    color: var(--muted);
    font-size: 1rem;
    padding: 2px 6px;
    border-radius: 4px;
    line-height: 1;
  }

  /* ── Floating chat FAB ── */
  .chat-fab {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: 16px; right: 16px;
    width: 52px; height: 52px;
    background: var(--accent);
    border-radius: 50%;
    font-size: 1.35rem;
    box-shadow: 0 4px 16px rgba(0,0,0,.5);
    z-index: 199;
    border: none;
    cursor: pointer;
  }
  .chat-fab.hidden { display: none; }

  /* Unread badge */
  .chat-badge {
    position: absolute;
    top: 2px; right: 2px;
    background: var(--accent2);
    color: #000;
    font-size: .62rem;
    font-weight: 800;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex !important;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
  }

  /* Overlay panels full-width */
  .overlay-panel { padding: 20px 16px; max-width: 100%; }
  .overlay-panel h2 { font-size: 1.3rem; }
  .word-choice-btn { font-size: 1rem; padding: 14px; }
}
