:root {
  --saffron: #ff9933;
  --green: #138808;
  --navy: #0a3d62;
  --bg1: #ffeede;
  --bg2: #ffe1c9;
  --ink: #2d2a32;
  --card: #ffffff;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  --radius: 16px;
}

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

html, body { height: 100%; }

body {
  font-family: "Nunito", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(135deg, var(--bg1), var(--bg2));
  min-height: 100vh;
  overflow-x: hidden;
}
/* When the game screen is showing, lock the whole page to the viewport
   so nothing scrolls behind the fixed game layout. --app-height tracks the
   *visible* viewport (set from JS) so the layout fits even when the mobile
   keyboard is open. */
body.in-game { overflow: hidden; height: var(--app-height, 100dvh); }

h1, h2, h3 { font-family: "Baloo 2", cursive; }

.screen { display: none; min-height: 100vh; }
.screen.active { display: flex; }

.btn {
  font-family: "Baloo 2", cursive;
  border: none;
  border-radius: 12px;
  padding: 10px 18px;
  font-size: 16px;
  cursor: pointer;
  transition: transform .08s ease, filter .15s ease, box-shadow .15s;
  font-weight: 700;
  box-shadow: 0 4px 0 rgba(0,0,0,0.15);
}
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,0.15); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--saffron); color: #fff; }
.btn-primary:hover { filter: brightness(1.05); }
.btn-secondary { background: #fff; color: var(--navy); border: 2px solid var(--saffron); box-shadow: 0 4px 0 rgba(255,153,51,.3); }
.btn.big { padding: 14px 28px; font-size: 20px; width: 100%; }
.btn.tiny { padding: 6px 12px; font-size: 13px; box-shadow: 0 3px 0 rgba(0,0,0,0.12); }
.btn.ghost { background: rgba(255,255,255,.7); color: var(--navy); }

/* ===================== HOME ===================== */
#home { align-items: center; justify-content: center; padding: 24px; }
.home-card {
  background: var(--card);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 32px;
  width: 100%;
  max-width: 460px;
  border-top: 8px solid var(--saffron);
  border-bottom: 8px solid var(--green);
}
.logo { text-align: center; margin-bottom: 20px; }
.logo-art { font-size: 56px; display: block; animation: bob 2.5s ease-in-out infinite; }
@keyframes bob { 0%,100%{transform:translateY(0)} 50%{transform:translateY(-8px)} }
.logo h1 { font-size: 42px; color: var(--saffron); line-height: 1; }
.logo h1 span { color: var(--green); }
.tag { color: #777; margin-top: 6px; font-size: 15px; }
.tag strong { color: var(--navy); }

.field { margin-bottom: 16px; }
.field label { display: block; font-weight: 700; margin-bottom: 6px; font-size: 14px; color: var(--navy); }
input, select {
  font-family: "Nunito", sans-serif;
  font-size: 16px;
  padding: 11px 14px;
  border: 2px solid #e4d4c4;
  border-radius: 12px;
  width: 100%;
  outline: none;
  background: #fffdfb;
}
input:focus, select:focus { border-color: var(--saffron); }

.avatar-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.avatar-picker .av {
  font-size: 26px; width: 42px; height: 42px; line-height: 42px; text-align: center;
  border-radius: 10px; cursor: pointer; background: #f6efe8; border: 2px solid transparent;
}
.avatar-picker .av.selected { border-color: var(--green); background: #e9f7e4; transform: scale(1.1); }

.home-actions { display: flex; flex-direction: column; gap: 10px; margin: 18px 0 12px; }
.join-row { display: flex; gap: 8px; }
.join-row input { text-transform: uppercase; letter-spacing: 3px; font-weight: 700; }
.join-row .btn { white-space: nowrap; }
.error-text { color: #d63031; font-weight: 700; min-height: 20px; margin-top: 8px; text-align: center; }
.credit { text-align: center; color: #aaa; font-size: 12px; margin-top: 12px; }

/* ===================== LOBBY ===================== */
#lobby { align-items: center; justify-content: center; padding: 24px; }
.lobby-card {
  background: var(--card); border-radius: 24px; box-shadow: var(--shadow);
  padding: 28px; width: 100%; max-width: 820px;
}
.lobby-card h2 { color: var(--navy); text-align: center; font-size: 32px; }
.room-code-box {
  text-align: center; margin: 14px 0; font-weight: 700; color: var(--navy);
  display: flex; gap: 12px; justify-content: center; align-items: center; flex-wrap: wrap;
}
#lobbyCode { font-family: "Baloo 2"; font-size: 28px; color: var(--saffron); letter-spacing: 4px; }
.lobby-grid { display: grid; grid-template-columns: 1fr 1.2fr; gap: 24px; margin: 18px 0; }
.player-pills { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.player-pills li { background: #f6efe8; border-radius: 12px; padding: 8px 12px; display: flex; align-items: center; gap: 10px; font-weight: 700; }
.player-pills .av { font-size: 22px; }
.player-pills .host-tag { margin-left: auto; font-size: 11px; background: var(--saffron); color: #fff; padding: 2px 8px; border-radius: 20px; }
.setting { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 10px; }
.setting label { font-weight: 700; font-size: 14px; color: var(--navy); }
.setting select { width: 140px; }
.setting.full { flex-direction: column; align-items: stretch; }
.setting.full label { margin-bottom: 6px; }
.hint-text { font-size: 12px; color: #999; margin-top: 6px; }

@media (max-width: 720px) { .lobby-grid { grid-template-columns: 1fr; } }

/* ===================== GAME ===================== */
/* Fixed to the viewport height — the game never causes the page to scroll.
   100dvh accounts for mobile browser chrome (address bar). */
#game {
  flex-direction: column;
  /* Add the device safe-area insets (notch / status bar / home indicator) on
     top of the base padding so the top bar's first row is never hidden behind
     a notch. env() is 0 on devices without insets, so nothing changes there. */
  padding-top: calc(8px + env(safe-area-inset-top));
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  padding-left: calc(8px + env(safe-area-inset-left));
  padding-right: calc(8px + env(safe-area-inset-right));
  gap: 8px;
  height: 100vh;
  height: 100dvh;
  height: var(--app-height, 100dvh);
  min-height: 0;
  max-height: var(--app-height, 100dvh);
  overflow: hidden;
}
.game-top {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card); border-radius: var(--radius); padding: 8px 16px; box-shadow: var(--shadow);
  gap: 10px;
  flex-shrink: 0;
}
.top-left { display: flex; align-items: center; gap: 14px; }
.round-badge { font-family: "Baloo 2"; color: var(--navy); font-size: 15px; }
.timer {
  font-family: "Baloo 2"; font-size: 26px; color: #fff; background: var(--green);
  min-width: 56px; text-align: center; border-radius: 12px; padding: 2px 10px;
}
.timer.low { background: #d63031; animation: pulse .6s infinite; }
@keyframes pulse { 0%,100%{transform:scale(1)} 50%{transform:scale(1.08)} }
.word-display {
  font-family: "Baloo 2"; font-size: 28px; letter-spacing: 6px; color: var(--navy); text-align: center;
}
.word-display .len { font-size: 13px; letter-spacing: 0; color: #999; margin-left: 8px; }

.game-body { display: grid; grid-template-columns: 200px 1fr 280px; gap: 10px; flex: 1; min-height: 0; }

.players-col { background: var(--card); border-radius: var(--radius); padding: 8px; box-shadow: var(--shadow); overflow-y: auto; }
#playerList { list-style: none; display: flex; flex-direction: column; gap: 6px; }
#playerList li { display: flex; align-items: center; gap: 8px; padding: 8px; border-radius: 10px; background: #f6efe8; position: relative; }
#playerList li.me { outline: 2px solid var(--saffron); }
#playerList li.drawing { background: #e9f7e4; }
#playerList li.guessed { background: #d4f5d0; }
#playerList .av { font-size: 22px; }
#playerList .pinfo { display: flex; flex-direction: column; line-height: 1.15; flex: 1; min-width: 0; }
#playerList .pname { font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#playerList .pscore { font-size: 12px; color: #777; }
#playerList .pencil { font-size: 16px; }
#playerList .rank { font-size: 11px; color: #aaa; font-weight: 700; }
#playerList .mic { font-size: 14px; }
#playerList li.speaking { box-shadow: 0 0 0 2px var(--green); }
#playerList li.speaking .mic { animation: micpulse .7s infinite; }
@keyframes micpulse { 0%,100%{transform:scale(1)} 50%{transform:scale(1.25)} }

/* A player whose connection dropped (waiting out the reconnect grace window) */
#playerList li.disconnected, .player-pills li.disconnected { opacity: .45; filter: grayscale(1); }

/* Reconnect banner pinned to the top of the viewport */
.conn-status {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1000;
  background: var(--saffron, #ff9933); color: #fff; text-align: center;
  font-weight: 700; padding: 8px 12px; font-size: 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}
.conn-status.hidden { display: none; }

/* Voice buttons in the header */
.top-right { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.btn.ghost.active { background: var(--green); color: #fff; }

.canvas-col { display: flex; flex-direction: column; gap: 8px; min-width: 0; min-height: 0; }
.canvas-wrap { position: relative; background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
/* object-fit: contain keeps the drawing's 900:640 aspect ratio regardless of
   whether the container is wide (laptop) or tall (mobile) — it letterboxes
   instead of stretching. pos() compensates for the letterbox when mapping
   pointer coords. */
#board { width: 100%; height: 100%; display: block; object-fit: contain; cursor: crosshair; touch-action: none; background:#fff; }
.overlay {
  position: absolute; inset: 0; background: rgba(10,61,98,.78); color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-family: "Baloo 2"; text-align: center; padding: 20px; gap: 10px; z-index: 5;
  overflow-y: auto;
}
.overlay.hidden { display: none; }
.overlay h2 { font-size: 34px; }
.overlay .big-word { font-size: 28px; color: var(--saffron); }
.overlay table { color:#fff; margin-top:8px; border-collapse: collapse; }
.overlay td { padding: 3px 12px; }

.tools { background: var(--card); border-radius: var(--radius); padding: 8px 10px; box-shadow: var(--shadow); display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap; flex-shrink: 0; }
.tools.hidden { display: none; }
.palette { display: grid; grid-template-columns: repeat(13, 1fr); gap: 4px; }
.palette .sw { width: 22px; height: 22px; border-radius: 5px; cursor: pointer; border: 2px solid rgba(0,0,0,.1); }
.palette .sw.selected { transform: scale(1.18); border-color: var(--navy); }
.tool-group { display: flex; gap: 6px; align-items: center; }
.tool { font-size: 18px; width: 38px; height: 38px; border-radius: 10px; border: 2px solid #e4d4c4; background: #fffdfb; cursor: pointer; }
.tool.active { background: #e9f7e4; border-color: var(--green); }
.sizes .dot { background: var(--ink); border-radius: 50%; cursor: pointer; display: inline-block; border: 2px solid transparent; }
.sizes .dot.selected { border-color: var(--saffron); }

.chat-col { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; min-height: 0; }
.chat-list { list-style: none; flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 4px; }
.chat-list li { font-size: 14px; padding: 4px 8px; border-radius: 8px; word-break: break-word; }
.chat-list li:nth-child(even) { background: #faf5f0; }
.chat-list .name { font-weight: 700; color: var(--navy); }
.chat-list .system { color: #888; font-style: italic; font-size: 13px; }
.chat-list .correct { background: #d4f5d0 !important; color: var(--green); font-weight: 700; }
.chat-list .close { background: #fff3cd !important; color: #8a6d00; font-weight: 700; }
.chat-form { display: flex; gap: 6px; padding: 8px; border-top: 2px solid #f0e6da; }
.chat-form input { flex: 1; }

/* Ephemeral chat toasts — hidden on desktop (the chat list shows history),
   enabled on phones where the list is hidden to give the canvas more room. */
.toasts {
  position: absolute; left: 0; right: 0; bottom: 8px; z-index: 5;
  display: none; flex-direction: column; align-items: center; gap: 5px;
  padding: 0 10px; pointer-events: none;
}
.toast {
  max-width: 92%; background: rgba(20,20,30,.78); color: #fff;
  font-size: 14px; line-height: 1.3; padding: 6px 12px; border-radius: 14px;
  word-break: break-word; text-align: center; box-shadow: 0 2px 8px rgba(0,0,0,.25);
  animation: toastIn .18s ease-out, toastOut .4s ease-in 2.6s forwards;
}
.toast b { font-weight: 700; }
.toast.correct { background: rgba(34,139,67,.92); }
.toast.close { background: rgba(180,140,0,.92); }
.toast.system { background: rgba(20,20,30,.6); font-style: italic; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes toastOut { to { opacity: 0; transform: translateY(-6px); } }

/* ===================== RESPONSIVE ===================== */
/* Header buttons show icon + label; on mobile the label collapses to save room. */
.top-right .btn { display: inline-flex; align-items: center; gap: 5px; }

/* ---- Tablets / small laptops: narrower side columns ---- */
@media (max-width: 900px) {
  .game-body { grid-template-columns: 150px 1fr 230px; }
  .word-display { font-size: 22px; letter-spacing: 4px; }
}

/* ---- Phones (portrait): single column, canvas-first ---- */
@media (max-width: 640px) {
  #game {
    padding-top: calc(6px + env(safe-area-inset-top));
    padding-bottom: calc(6px + env(safe-area-inset-bottom));
    padding-left: calc(6px + env(safe-area-inset-left));
    padding-right: calc(6px + env(safe-area-inset-right));
    gap: 6px;
  }

  /* Header: explicit grid so round+timer (left) and voice/leave (right) sit on
     a guaranteed first row, with the word/hints spanning its own row below.
     (Grid areas avoid the fragile flex-wrap + order tricks used before.) */
  .game-top {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas: "left right" "word word";
    align-items: center;
    padding: 6px 8px; gap: 4px 8px;
  }
  .top-left   { grid-area: left;  gap: 10px; }
  .top-right  { grid-area: right; gap: 5px; flex-wrap: nowrap; }
  .top-center { grid-area: word; min-width: 0; }
  .round-badge { font-size: 13px; white-space: nowrap; }
  .timer { font-size: 20px; min-width: 42px; padding: 2px 8px; }
  .word-display { font-size: 19px; letter-spacing: 3px; white-space: nowrap; overflow-x: auto; }
  .word-display .len { display: block; font-size: 11px; margin: 2px 0 0; }

  /* Compact header buttons; keep the icon and show short labels so the voice
     control is recognisable. */
  .btn.tiny { padding: 7px 9px; font-size: 13px; box-shadow: 0 2px 0 rgba(0,0,0,.12); }
  .top-right .btn .lbl { display: inline; }
  .top-right .btn .ic { display: inline; font-size: 16px; }

  /* Layout: canvas fills all available height, with only the guess input
     pinned below it. Players strip and chat history are hidden — guesses from
     others surface as ephemeral toasts over the canvas instead. */
  .game-body { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; gap: 6px; }
  .players-col { display: none; }

  /* Ephemeral guess toasts replace the scrolling chat history on phones */
  .toasts { display: flex; }

  /* Tools: ONE horizontally-scrollable row so they never eat canvas height.
     Colours pack into 2 rows that scroll sideways. */
  .tools { flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; gap: 8px; padding: 6px; -webkit-overflow-scrolling: touch; }
  .palette { grid-template-columns: none; grid-template-rows: repeat(2, 1fr); grid-auto-flow: column; gap: 3px; flex-shrink: 0; }
  .palette .sw { width: 17px; height: 17px; }
  .tool-group, .sizes { flex-shrink: 0; }
  .tool { width: 36px; height: 36px; font-size: 17px; flex-shrink: 0; }

  /* Chat: no history list, just the input pinned at the bottom (≥16px so iOS
     doesn't zoom on focus) */
  .chat-col { height: auto; }
  .chat-list { display: none; }
  .chat-form { padding: 6px; border-top: none; }
  .chat-form input { font-size: 16px; }

  .overlay h2 { font-size: 26px; }
  .overlay .big-word { font-size: 22px; }
}

/* ---- Phones in landscape: keep the canvas tall by going side-by-side ---- */
@media (max-height: 500px) and (orientation: landscape) {
  .game-body { grid-template-columns: 1fr 190px; grid-template-rows: minmax(0, 1fr); gap: 6px; }
  .canvas-col { grid-column: 1; grid-row: 1; }
  .chat-col { grid-column: 2; grid-row: 1; height: auto; }
  .players-col { display: none; }     /* scores remain on round/game overlays */
  /* Landscape has room for the side chat panel — show history, drop toasts */
  .chat-list { display: flex; }
  .toasts { display: none; }
  .game-top { padding: 4px 8px; }
  .top-center { order: 0; flex-basis: auto; }
  .word-display { font-size: 16px; letter-spacing: 2px; }
  .word-display .len { display: inline; }
  .top-right .btn .lbl { display: none; }
  .top-right .btn .ic { display: inline; }
  .tools { flex-wrap: nowrap; overflow-x: auto; }
  .palette { grid-template-columns: none; grid-template-rows: repeat(2, 1fr); grid-auto-flow: column; }
}

/* ===================== MODAL ===================== */
.modal { position: fixed; inset: 0; background: rgba(10,61,98,.6); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal.hidden { display: none; }
.modal-card { background: #fff; border-radius: 20px; padding: 28px; text-align: center; box-shadow: var(--shadow); max-width: 90%; }
.modal-card h3 { color: var(--navy); margin-bottom: 18px; font-size: 26px; }
.word-choices { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.word-choices button {
  font-family: "Baloo 2"; font-size: 20px; padding: 14px 22px; border-radius: 14px;
  border: 3px solid var(--saffron); background: #fff; color: var(--navy); cursor: pointer; transition: all .12s;
}
.word-choices button:hover { background: var(--saffron); color: #fff; transform: translateY(-3px); }
