:root {
  --bg: #f4f2ff;
  --surface: #ffffff;
  --text: #23213a;
  --muted: #6b6885;
  --primary: #6c4cf1;
  --primary-dark: #5636d0;
  --accent: #ff6f91;
  --success: #22b573;
  --danger: #e8503f;
  --border: #e3e0f5;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17162a;
    --surface: #211f3b;
    --text: #f0eefc;
    --muted: #a7a3c7;
    --border: #34315a;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: "Segoe UI", "Tahoma", "Geeza Pro", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button, input, textarea {
  font-family: inherit;
  font-size: 16px; /* prevents iOS auto-zoom on focus */
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.screen {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
  overflow-y: auto;
  padding: env(safe-area-inset-top, 12px) 14px calc(14px + var(--safe-bottom));
}
.screen.active { display: flex; }

/* ---------- shared elements ---------- */

.btn {
  border: none;
  border-radius: var(--radius);
  padding: 12px 18px;
  font-weight: 700;
  min-height: 44px;
  cursor: pointer;
  transition: transform .05s ease, opacity .15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); color: var(--primary); border: 2px solid var(--primary); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-link { background: none; border: none; color: var(--primary); font-weight: 700; cursor: pointer; padding: 4px 8px; }
.btn-icon { background: none; border: none; font-size: 22px; cursor: pointer; padding: 4px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.field { display: flex; flex-direction: column; gap: 6px; margin-block-end: 14px; }
.field > span { font-weight: 600; font-size: 14px; color: var(--muted); }
.field-inline { display: flex; align-items: center; gap: 8px; margin-block-end: 14px; font-weight: 600; }

input[type="text"], textarea {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
}
input[type="text"]:focus, textarea:focus { outline: none; border-color: var(--primary); }
input[type="range"] { width: 100%; }

.error-text { color: var(--danger); font-weight: 700; text-align: center; }
.hint-text { color: var(--muted); text-align: center; margin: 6px 0 0; font-size: 14px; }

/* ---------- lobby ---------- */

#screen-lobby { align-items: center; justify-content: center; }
.lobby-card {
  background: var(--surface);
  border-radius: 20px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 10px 30px rgba(0,0,0,.08);
}
.app-title { text-align: center; margin: 0 0 4px; font-size: 28px; }
.app-subtitle { text-align: center; color: var(--muted); margin: 0 0 20px; }

.avatar-preview {
  width: 64px; height: 64px;
  border-radius: 50%;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: 32px;
  margin: 0 auto 10px;
  border: 3px solid var(--primary);
}
.avatar-picker { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 8px; }
.avatar-picker button {
  border: 2px solid transparent;
  background: var(--bg);
  border-radius: 10px;
  font-size: 22px;
  width: 40px; height: 40px;
  cursor: pointer;
}
.avatar-picker button.selected { border-color: var(--primary); }
.avatar-colors button { width: 28px; height: 28px; border-radius: 50%; }

.lobby-actions { display: flex; flex-direction: column; gap: 12px; margin-block: 16px; }
.join-row { display: flex; gap: 8px; }
.join-row .code-input { flex: 1; text-align: center; letter-spacing: 3px; text-transform: uppercase; }

.public-rooms { margin-top: 10px; }
.public-rooms-header { display: flex; justify-content: space-between; align-items: center; font-weight: 700; color: var(--muted); }
.public-rooms-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 160px; overflow-y: auto; }
.public-rooms-list li {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg); border-radius: 10px; padding: 8px 12px; font-size: 14px;
}
.public-rooms-list button { padding: 6px 12px; min-height: 32px; }
.public-rooms-list .empty { color: var(--muted); text-align: center; padding: 8px; }

/* ---------- room (waiting) ---------- */

.room-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.room-code-box { display: flex; align-items: center; gap: 8px; background: var(--surface); border-radius: 12px; padding: 8px 14px; }
.room-code-box strong { font-size: 22px; letter-spacing: 3px; color: var(--primary); }

.room-qr-box {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  background: var(--surface); border-radius: var(--radius);
  padding: 12px; margin-bottom: 16px;
}
.room-qr-box img { width: 120px; height: 120px; background: #fff; border-radius: 8px; padding: 6px; }
.room-qr-box span { font-size: 13px; color: var(--muted); }

.room-body { display: flex; flex-direction: column; gap: 16px; flex: 1; }
.room-players, .room-settings { background: var(--surface); border-radius: var(--radius); padding: 14px; }
.room-players h2, .room-settings h2 { margin: 0 0 10px; font-size: 16px; }

.player-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.player-list li { display: flex; align-items: center; gap: 10px; padding: 6px; border-radius: 10px; }
.player-list li.is-drawer { background: rgba(108,76,241,.12); }
.player-avatar-chip {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
}
.player-name { font-weight: 600; flex: 1; }
.player-score { color: var(--muted); font-size: 13px; }
.player-name .host-tag { color: var(--accent); font-size: 12px; }
.player-list li.disconnected { opacity: .45; }

.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: flex; align-items: center; gap: 6px; background: var(--bg); border-radius: 20px; padding: 6px 12px; font-size: 14px; cursor: pointer; }

.room-footer { padding: 12px 0 4px; text-align: center; }
#btn-start-game { width: 100%; max-width: 320px; }

/* ---------- game screen ---------- */

.game-screen { padding: 0; }
.game-topbar {
  display: flex; align-items: center; gap: 14px;
  padding: calc(var(--safe-top) + 8px) 12px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-item { display: flex; flex-direction: column; align-items: center; font-size: 12px; color: var(--muted); }
.topbar-item strong, .word-mask, .timer { font-size: 18px; color: var(--text); }
.word-mask { flex: 1; text-align: center; font-size: 20px; letter-spacing: 4px; font-weight: 700; }
.timer { min-width: 40px; text-align: center; font-weight: 800; color: var(--primary); }
.timer.low { color: var(--danger); }

.game-body { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.game-canvas-area { order: 1; display: flex; flex-direction: column; flex: 1; min-height: 0; }
.game-side-panel { order: 2; display: flex; flex-direction: column; height: 38dvh; border-top: 1px solid var(--border); background: var(--surface); }

.toolbar { display: flex; align-items: center; gap: 6px; padding: 8px; flex-wrap: wrap; background: var(--surface); }
.toolbar-colors { display: flex; gap: 4px; }
.toolbar-colors button { width: 26px; height: 26px; border-radius: 50%; border: 2px solid #0002; cursor: pointer; }
.toolbar-colors button.selected { outline: 2px solid var(--primary); outline-offset: 2px; }
.toolbar-sizes { display: flex; gap: 4px; }
.tool-btn { min-width: 40px; min-height: 40px; border: none; background: var(--bg); border-radius: 10px; cursor: pointer; font-size: 16px; }
.tool-btn.selected { background: var(--primary); color: #fff; }

.canvas-wrap {
  position: relative; flex: 1; min-height: 0; background: #fff; touch-action: none;
  border: 3px solid var(--primary);
  border-radius: 10px;
  margin: 8px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.06);
  overflow: hidden;
}
#draw-canvas { width: 100%; height: 100%; display: block; touch-action: none; background: #fff; }
.canvas-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(255,255,255,.55);
  font-weight: 700; color: var(--muted); pointer-events: none;
}
.canvas-overlay[hidden] { display: none; }

.game-player-list { display: flex; gap: 6px; overflow-x: auto; padding: 8px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.game-player-list .player-chip {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: var(--bg); border-radius: 10px; padding: 6px 10px; font-size: 12px; min-width: 60px; flex-shrink: 0;
}
.game-player-list .player-chip.is-drawer { outline: 2px solid var(--primary); }
.game-player-list .player-chip.guessed-correct { background: rgba(34,181,115,.15); }

.chat-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-log { flex: 1; overflow-y: auto; padding: 8px 12px; display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.chat-log .msg-system { color: var(--muted); font-style: italic; }
.chat-log .msg-correct { color: var(--success); font-weight: 700; }
.chat-log .msg-name { font-weight: 700; }
.chat-form { display: flex; gap: 6px; padding: 8px 10px calc(8px + var(--safe-bottom)); border-top: 1px solid var(--border); }
.chat-form input { flex: 1; }
.chat-form button { padding: 8px 14px; min-height: 40px; }

@media (min-width: 800px) {
  .game-body { display: grid; grid-template-columns: 320px 1fr; grid-template-rows: 1fr; }
  .game-side-panel { height: auto; border-top: none; border-inline-start: 1px solid var(--border); order: 1; }
  .game-canvas-area { order: 2; }
}

/* ---------- modals ---------- */

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(20,18,40,.55);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 50;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: var(--surface);
  border-radius: 18px;
  padding: 22px;
  width: 100%; max-width: 420px;
  max-height: 80dvh;
  overflow-y: auto;
  text-align: center;
}
.modal h2 { margin-top: 0; }

.word-choice-options { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.word-choice-options button {
  padding: 14px; font-size: 18px; border-radius: 12px; border: 2px solid var(--border);
  background: var(--bg); cursor: pointer; font-weight: 700;
}
.word-choice-options button:hover { border-color: var(--primary); }

.winner-banner {
  font-size: 20px; font-weight: 800; color: var(--primary);
  background: rgba(108,76,241,.12); border-radius: 12px;
  padding: 10px 14px; margin: 4px 0 0;
}

.scores-list { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; text-align: start; }
.scores-list .score-row.is-winner { background: rgba(255,176,32,.2); outline: 2px solid #ffb020; }
.scores-list .score-row { display: flex; align-items: center; gap: 10px; background: var(--bg); border-radius: 10px; padding: 8px 12px; }
.scores-list .score-row .rank { font-weight: 800; color: var(--primary); min-width: 24px; }
.scores-list .score-row .name { flex: 1; font-weight: 600; }
.scores-list .score-row .delta { color: var(--success); font-weight: 700; }

.toast {
  position: fixed; bottom: calc(20px + var(--safe-bottom)); left: 50%;
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 30px;
  font-weight: 600; z-index: 100;
}

/* elements that should never mirror in RTL */
[dir="ltr"] { unicode-bidi: isolate; }
