:root {
  --bg: #2c3aa8;
  --bg2: #3b4fd8;
  --card: #ffffff;
  --ink: #1e2340;
  --muted: #6b7190;
  --line: #e3e6f3;
  --accent: #5865f2;
  --good: #16a34a;
  --good-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --bad: #dc2626;
  --insider-bg: #ecfdf5;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: Nunito, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at 20% 0%, var(--bg2), var(--bg) 60%) fixed, var(--bg);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 600; }
.error { color: var(--bad); font-weight: 800; min-height: 1.2em; margin: 8px 0 0; }

.screen { display: none; min-height: 100dvh; }
.screen.active { display: block; }

.logo { font-weight: 900; font-size: clamp(40px, 11vw, 64px); margin: 0; color: #fff; letter-spacing: -1px; text-shadow: 0 4px 0 rgba(0,0,0,.18); }
.logo span { color: #ffd84d; }
.logo.small { font-size: 28px; }
.tagline { color: #dfe3ff; font-weight: 700; margin: 4px 0 18px; text-align: center; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: 0 6px 0 rgba(0,0,0,.12); }
.card h2 { margin: 0 0 10px; font-size: 20px; }
.card h3 { margin: 16px 0 8px; font-size: 16px; }

.btn {
  border: 0; border-radius: 10px; padding: 10px 16px; font-weight: 800; cursor: pointer;
  background: #eef0fb; color: var(--ink); box-shadow: 0 3px 0 rgba(0,0,0,.15); transition: transform .05s;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.15); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: #22c55e; color: #fff; }
.btn.big { width: 100%; padding: 14px; font-size: 18px; }
.btn.ghost { background: rgba(255,255,255,.15); color: #fff; box-shadow: none; }
.btn.small { padding: 6px 12px; font-size: 14px; }
.btn.danger { background: var(--bad); color: #fff; }

/* ---------- home ---------- */
.home { max-width: 440px; margin: 0 auto; padding: 32px 16px calc(24px + var(--safe-b)); display: flex; flex-direction: column; align-items: center; }
.home-card { width: 100%; }
.field { display: block; font-weight: 800; }
.field span { display: block; margin-bottom: 6px; }
input[type=text], input:not([type]), select, textarea {
  width: 100%; border: 2px solid var(--line); border-radius: 10px; padding: 10px 12px; background: #fff; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.avatar-pick { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 16px; justify-content: center; }
.avatar-pick button { width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; }
.avatar-pick button.on { border-color: var(--ink); transform: scale(1.1); }
.or { text-align: center; margin: 14px 0 10px; color: var(--muted); font-weight: 700; font-size: 14px; }
.join-row { display: flex; gap: 8px; }
.join-row input { text-transform: uppercase; letter-spacing: 4px; font-weight: 900; text-align: center; }
.join-box p { text-align: center; font-weight: 700; margin: 0 0 10px; }
.howto { width: 100%; margin-top: 16px; }
.howto summary { font-weight: 900; cursor: pointer; }
.howto li { margin: 6px 0; line-height: 1.4; }

/* ---------- lobby ---------- */
.lobby { max-width: 1100px; margin: 0 auto; padding: 16px 16px calc(24px + var(--safe-b)); }
.lobby-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.lobby-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.code { background: #ffd84d; padding: 2px 10px; border-radius: 8px; letter-spacing: 3px; }
.share-row { display: flex; gap: 8px; }
.share-row input { font-size: 13px; color: var(--muted); }
.lobby-players { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.lobby-players li { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 10px; background: #f6f7fd; font-weight: 800; }
.lobby-players li.off { opacity: .5; }
.lobby-players .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar { width: 32px; height: 32px; border-radius: 50%; color: #fff; display: grid; place-items: center; font-weight: 900; flex: none; text-transform: uppercase; }
.badge { font-size: 12px; background: #ffd84d; border-radius: 6px; padding: 1px 6px; }
.settings { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.settings label { font-weight: 800; font-size: 14px; display: flex; flex-direction: column; gap: 4px; }
.settings .wide { grid-column: 1 / -1; }
.settings .check { flex-direction: row; align-items: center; gap: 8px; }
.settings textarea { resize: vertical; font-size: 14px; }
.camtest-view { position: relative; height: 200px; width: auto; max-width: 100%; aspect-ratio: var(--cam-ar, 4 / 3); margin: 0 auto 10px; border-radius: 10px; overflow: hidden; background: #000; }
.camtest-view video, .camtest-view canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; }
.camtest-view video { transform: scaleX(-1); }
.pill { position: absolute; left: 6px; bottom: 6px; background: rgba(0,0,0,.6); color: #fff; font-size: 13px; padding: 2px 8px; border-radius: 99px; font-weight: 800; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.seg { display: inline-flex; background: #eef0fb; border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 10px; border-radius: 8px; font-weight: 800; cursor: pointer; }
.seg button.on { background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.lobby-start { max-width: 420px; margin: 18px auto 0; text-align: center; }
.lobby-start .muted { color: #dfe3ff; }

/* ---------- game ---------- */
/* The game is pinned to the *visible* viewport (above the on-screen keyboard; size set from JS),
   and the page itself can't scroll, so typing never scrolls the board out of view. */
body.in-game { position: fixed; inset: 0; overflow: hidden; }
#screen-game.active {
  display: flex; flex-direction: column; overflow: hidden;
  position: fixed; left: 0; right: 0; top: var(--vv-top, 0px); height: var(--vv-h, 100dvh); min-height: 0;
}
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: #fff; box-shadow: 0 3px 0 rgba(0,0,0,.12);
  padding-top: calc(8px + env(safe-area-inset-top, 0px)); flex: none; position: relative; z-index: 5;
}
.timer { position: relative; width: 44px; height: 44px; flex: none; }
.timer svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.timer circle { fill: none; stroke-width: 4; }
.timer .track { stroke: var(--line); }
.timer .bar { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 100.53; transition: stroke-dashoffset .25s linear; }
.timer.low .bar { stroke: var(--bad); }
.timer span { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 900; font-size: 15px; }
.round { font-weight: 800; font-size: 13px; color: var(--muted); white-space: nowrap; }
.hint-wrap { flex: 1; text-align: center; min-width: 0; }
.hint-label { font-size: 11px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.hint { font-weight: 900; font-size: clamp(16px, 4.6vw, 26px); letter-spacing: .25em; white-space: nowrap; overflow-x: auto; scrollbar-width: none; }
.hint sup { font-size: 11px; letter-spacing: 0; color: var(--muted); margin-left: 4px; }
.icon-btn { border: 0; background: #eef0fb; width: 40px; height: 40px; border-radius: 10px; font-size: 20px; cursor: pointer; flex: none; }
.menu { position: fixed; z-index: 30; background: #fff; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.25); padding: 6px; display: flex; flex-direction: column; min-width: 200px; }
#menu { right: 12px; top: calc(62px + env(safe-area-inset-top, 0px)); }
.menu button { border: 0; background: transparent; text-align: left; padding: 10px 12px; border-radius: 8px; font-weight: 800; cursor: pointer; }
.menu button:hover { background: #f1f2fb; }
.menu .danger { color: var(--bad); }
.menu .title { padding: 6px 12px; font-weight: 900; color: var(--muted); font-size: 13px; }

.game { flex: 1; min-height: 0; display: grid; grid-template-columns: 200px minmax(0, 1fr) 280px; gap: 10px; padding: 10px; }
.players { background: #fff; border-radius: var(--radius); overflow-y: auto; padding: 6px; display: flex; flex-direction: column; gap: 4px; }
.player {
  display: flex; align-items: center; gap: 8px; padding: 6px; border-radius: 10px; cursor: pointer; position: relative;
  border: 0; background: transparent; text-align: left; width: 100%;
}
.player:hover { background: #f6f7fd; }
.player.guessed { background: var(--good-bg); }
.player.off { opacity: .45; }
.player.me .pname { text-decoration: underline; }
.player .rank { font-weight: 900; font-size: 12px; color: var(--muted); width: 22px; }
.player .info { flex: 1; min-width: 0; }
.player .pname { font-weight: 900; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player .pscore { font-size: 12px; font-weight: 700; color: var(--muted); }
.player .tag { font-size: 16px; }

.stage { display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; align-items: center; }
.board {
  position: relative; width: 100%; max-width: calc((100dvh - 210px) * 4 / 3); aspect-ratio: 4 / 3;
  background: #fff; border-radius: var(--radius); overflow: hidden; box-shadow: 0 6px 0 rgba(0,0,0,.12); flex: none;
}
.board canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
#overlay { pointer-events: none; }
.board.touch #overlay { pointer-events: auto; cursor: crosshair; }
/* Box takes the camera's real shape (set from JS): 4:3 on laptops, 3:4 on upright phones. */
.cam { position: absolute; right: 6px; bottom: 6px; height: 38%; min-height: 96px; width: auto; aspect-ratio: var(--cam-ar, 4 / 3); border-radius: 10px; overflow: hidden; background: #000; border: 2px solid #fff; box-shadow: 0 4px 12px rgba(0,0,0,.3); z-index: 2; opacity: .92; }
.cam video, .cam canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; }
.cam video { transform: scaleX(-1); }
.cam-status { position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; font-weight: 800; text-align: center; padding: 2px; }
.board-msg {
  position: absolute; inset: 0; z-index: 3; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: rgba(30, 35, 64, .82); color: #fff; text-align: center; padding: 16px; gap: 10px; overflow-y: auto;
}
.board-msg h2 { margin: 0; font-size: clamp(18px, 4.5vw, 30px); }
.board-msg h2 b { color: #ffd84d; }
.board-msg p { margin: 0; font-weight: 700; color: #d6daf5; }
.choices { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.choices .btn { font-size: clamp(16px, 4vw, 22px); padding: 12px 18px; background: #fff; }
.gains { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: auto auto; gap: 2px 18px; font-weight: 800; }
.gains .plus { color: #4ade80; text-align: right; }
.gains .zero { color: #f87171; text-align: right; }
.podium { display: flex; align-items: flex-end; gap: 10px; }
.podium div { background: rgba(255,255,255,.12); border-radius: 10px 10px 0 0; padding: 8px 12px; min-width: 80px; font-weight: 900; }
.podium .p1 { height: 120px; background: rgba(255, 216, 77, .35); }
.podium .p2 { height: 90px; }
.podium .p3 { height: 70px; }
.podium small { display: block; font-weight: 700; color: #d6daf5; }

.toolbar { width: 100%; max-width: calc((100dvh - 210px) * 4 / 3); background: #fff; border-radius: var(--radius); padding: 8px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: center; }
.colors { display: grid; grid-template-columns: repeat(11, 24px); gap: 3px; }
.colors button { width: 24px; height: 24px; border-radius: 6px; border: 2px solid rgba(0,0,0,.12); cursor: pointer; padding: 0; }
.colors button.on { outline: 3px solid var(--accent); outline-offset: 1px; }
.tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: center; }
.tool { border: 0; background: #eef0fb; width: 38px; height: 38px; border-radius: 10px; font-size: 18px; cursor: pointer; }
.tool.on { background: #c7ccff; }
.sizes { display: flex; gap: 4px; }
.sizes button { width: 38px; height: 38px; border-radius: 10px; border: 0; background: #eef0fb; display: grid; place-items: center; cursor: pointer; }
.sizes button.on { background: #c7ccff; }
.sizes i { display: block; background: var(--ink); border-radius: 50%; }

.chat { background: #fff; border-radius: var(--radius); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.chat-log { flex: 1; overflow-y: auto; padding: 8px; font-size: 14px; display: flex; flex-direction: column; gap: 2px; }
.chat-log div { padding: 3px 6px; border-radius: 6px; word-wrap: break-word; line-height: 1.35; }
.chat-log b { font-weight: 900; }
.chat-log .system { color: var(--accent); font-weight: 800; }
.chat-log .round, .chat-log .reveal { color: #7c3aed; font-weight: 900; }
.chat-log .join { color: var(--good); font-weight: 800; }
.chat-log .leave { color: var(--bad); font-weight: 800; }
.chat-log .correct, .chat-log .good { background: var(--good-bg); color: var(--good); font-weight: 900; }
.chat-log .close { background: var(--warn-bg); color: var(--warn); font-weight: 900; }
.chat-log .warn { color: var(--warn); font-weight: 800; }
.chat-log .insider { background: var(--insider-bg); color: #047857; }
.chat-log div:nth-child(even):not([class]) { background: #f6f7fd; }
.chat-form { display: flex; gap: 6px; padding: 8px; border-top: 2px solid var(--line); padding-bottom: calc(8px + var(--safe-b)); }
.chat-form input { flex: 1; min-width: 0; font-size: 16px; /* 16px stops iOS zooming in */ }
.chat-form .btn { padding: 8px 14px; }
.chat-form.guessed input { background: var(--good-bg); }

.toast {
  position: fixed; left: 50%; top: 16px; transform: translate(-50%, -150%); background: var(--ink); color: #fff; padding: 10px 16px;
  border-radius: 10px; font-weight: 800; z-index: 50; transition: transform .25s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { transform: translate(-50%, 0); }
.conn { position: fixed; left: 50%; bottom: calc(16px + var(--safe-b)); transform: translateX(-50%); background: var(--bad); color: #fff; padding: 8px 16px; border-radius: 99px; font-weight: 900; z-index: 60; }

/* ---------- phones ----------
   The board's width is set from JS (fitBoard) so board + chat + input always fit the
   space that's visible, including when the keyboard is open. */
@media (max-width: 900px), (max-height: 500px) {
  .game { display: flex; flex-direction: column; gap: 6px; padding: 6px; overflow: hidden; }
  .players { flex-direction: row; overflow-x: auto; overflow-y: hidden; flex: none; padding: 4px; scrollbar-width: none; }
  .player { width: auto; flex: none; padding: 4px 8px 4px 4px; }
  .player .rank { display: none; }
  .player .avatar { width: 26px; height: 26px; font-size: 13px; }
  .player .pname { max-width: 90px; font-size: 13px; }
  .stage { flex: none; }
  .board { max-width: 100%; }
  .toolbar { max-width: none; padding: 6px; gap: 6px; flex: none; }
  .colors { grid-template-columns: repeat(11, minmax(0, 1fr)); width: 100%; }
  .colors button { width: 100%; height: 22px; }
  .tool, .sizes button { width: 34px; height: 34px; font-size: 16px; }
  .chat { flex: 1 1 0; min-height: 0; }
  .topbar { padding-top: calc(6px + env(safe-area-inset-top, 0px)); padding-bottom: 6px; }
  .topbar .round { display: none; }
  .timer { width: 38px; height: 38px; }
  .icon-btn { width: 36px; height: 36px; }

  /* Keyboard open: drop the player strip and toolbar so the drawing and the newest messages stay visible. */
  body.kb-open .players, body.kb-open .toolbar { display: none; }
  body.kb-open .chat-form { padding-bottom: 8px; }
  body.kb-open .chat-log { padding: 4px 8px; }

  /* Phone on its side: chat moves to the right. Uses the screen's orientation, not the
     viewport's, so an open keyboard doesn't flip the layout. */
  body.landscape .game { display: grid; grid-template-columns: minmax(0, 1fr) minmax(200px, 34%); grid-template-rows: auto minmax(0, 1fr); }
  body.landscape .players { grid-column: 1 / -1; }
  body.landscape .stage { grid-column: 1; grid-row: 2; min-height: 0; overflow: hidden; }
  body.landscape .chat { grid-column: 2; grid-row: 2; }
}
