/* Kya Bana? - original UI. Tokens first, then layout, then components. */
:root {
  color-scheme: light;
  --bg: #f7f4ee;
  --dot: rgba(60, 45, 25, 0.08);
  --surface: #ffffff;
  --surface-2: #f3efe7;
  --border: #e3dccf;
  --text: #1f1c17;
  --muted: #6c655a;
  --accent: #cf4520;
  --accent-ink: #ffffff;
  --accent-soft: #fde5db;
  --accent-2: #0f766e;
  --good: #1d7f46;
  --good-soft: #e0f3e7;
  --warn: #9a6512;
  --warn-soft: #fbf0d9;
  --danger: #b83232;
  --focus: #2f6fe4;
  --shadow: 0 1px 2px rgba(31, 28, 23, 0.06), 0 10px 28px rgba(31, 28, 23, 0.07);
  --radius: 16px;
  --header-h: 56px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #141519;
    --dot: rgba(255, 255, 255, 0.05);
    --surface: #1d1f25;
    --surface-2: #272a32;
    --border: #343843;
    --text: #efebe4;
    --muted: #a39e95;
    --accent: #ff7a52;
    --accent-ink: #1b0f08;
    --accent-soft: #3b231a;
    --accent-2: #3cc4b3;
    --good: #5bd08a;
    --good-soft: #193227;
    --warn: #f0b955;
    --warn-soft: #3a2f17;
    --danger: #ff7070;
    --focus: #7aa7ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.45), 0 10px 28px rgba(0, 0, 0, 0.28);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #141519;
  --dot: rgba(255, 255, 255, 0.05);
  --surface: #1d1f25;
  --surface-2: #272a32;
  --border: #343843;
  --text: #efebe4;
  --muted: #a39e95;
  --accent: #ff7a52;
  --accent-ink: #1b0f08;
  --accent-soft: #3b231a;
  --accent-2: #3cc4b3;
  --good: #5bd08a;
  --good-soft: #193227;
  --warn: #f0b955;
  --warn-soft: #3a2f17;
  --danger: #ff7070;
  --focus: #7aa7ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.45), 0 10px 28px rgba(0, 0, 0, 0.28);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1.2px, transparent 1.3px);
  background-size: 22px 22px;
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;
  overflow-x: hidden;
}
[hidden] { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.mono { font-family: ui-monospace, "Cascadia Mono", "SFMono-Regular", Menlo, Consolas, monospace; }
.muted { color: var(--muted); }
svg { flex: none; }
.icon-btn svg, .tool-btn svg, .btn svg, .badge svg {
  width: 20px; height: 20px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- header ---------- */
.app-header {
  height: var(--header-h);
  max-width: 1320px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex; align-items: center; justify-content: space-between;
}
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; }
.brand-mark { width: 34px; height: 34px; }
.bm-bg { fill: var(--accent); }
.bm-line { fill: none; stroke: var(--accent-ink); stroke-width: 3.4; stroke-linecap: round; stroke-linejoin: round; }
.bm-dot { fill: #ffd23f; }
.brand-name { font-weight: 850; font-size: 20px; letter-spacing: -0.02em; }
.brand-name .q { color: var(--accent); }
.header-actions { display: flex; gap: 6px; }

.icon-btn {
  width: 40px; height: 40px;
  display: inline-grid; place-items: center;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

main { max-width: 1320px; margin: 0 auto; padding: 0 16px 24px; }

/* ---------- buttons + fields ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  border-radius: 12px; border: 1px solid transparent;
  font-weight: 700; text-decoration: none; cursor: pointer;
  transition: transform 0.06s ease, background-color 0.15s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.06); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn-sm svg { width: 16px; height: 16px; }
.btn-block { width: 100%; }
.btn-icon { width: 46px; padding: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.form { padding: 20px; display: grid; gap: 14px; }
.field { display: grid; gap: 6px; font-weight: 600; font-size: 14px; }
.field small { font-weight: 400; }
input, select, textarea {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-2);
  font-size: 16px; /* 16px stops iOS from zooming on focus */
}
textarea { min-height: 64px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus); outline-offset: 0; border-color: transparent; }
select:disabled, textarea:disabled, input:disabled { opacity: 0.75; }
.row { display: flex; gap: 8px; }
.code-input { text-transform: uppercase; letter-spacing: 0.18em; font-weight: 700; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.form-error { margin: 0; color: var(--danger); font-weight: 600; font-size: 14px; }
.check { display: flex; align-items: center; gap: 10px; font-size: 14px; font-weight: 500; }
.check input { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
.link-small { color: var(--muted); font-size: 14px; text-align: center; }

/* ---------- home ---------- */
.hero { max-width: 480px; margin: 3vh auto 0; display: grid; gap: 18px; }
.hero-title { margin: 0; font-size: clamp(34px, 8vw, 54px); line-height: 1.02; letter-spacing: -0.035em; font-weight: 850; }
.hero-title .accent { color: var(--accent); }
.hero-sub { margin: 0; color: var(--muted); font-size: 16px; }
.how { margin: 0; padding: 0 0 0 20px; color: var(--muted); display: grid; gap: 6px; font-size: 14px; }
.how b { color: var(--text); }

.join-card { max-width: 440px; margin: 6vh auto 0; }
.join-card h2 { margin: 0; font-size: 22px; }

/* ---------- room: top bar ---------- */
.game-bar {
  position: relative;
  display: grid;
  grid-template-columns: auto auto 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 8px 12px 11px;
  margin-bottom: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.bar-round { font-weight: 700; font-size: 14px; color: var(--muted); white-space: nowrap; }
.bar-word { min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center; }
.bar-word .label { font-size: 12px; color: var(--muted); font-weight: 600; letter-spacing: 0.02em; }
.bar-word .status { font-weight: 700; }
.hint {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 4px;
  font: 800 22px/1.1 ui-monospace, "Cascadia Mono", "SFMono-Regular", Menlo, Consolas, monospace;
}
.hint .w { display: inline-flex; gap: 4px; }
.hint .ch { display: inline-block; min-width: 0.85em; text-align: center; border-bottom: 3px solid currentColor; padding: 0 1px; }
.hint .ch.rev { color: var(--accent); }
.hint .ch.sym { border-bottom-color: transparent; }
.hint .gap { width: 0.6em; }
.hint-count { font-size: 12px; color: var(--muted); font-weight: 600; }
.word-known { font-size: 22px; font-weight: 850; letter-spacing: 0.02em; color: var(--accent); }
.bar-timer {
  min-width: 52px; height: 40px; padding: 0 10px;
  display: grid; place-items: center;
  border-radius: 12px; background: var(--surface-2);
  font-weight: 850; font-size: 20px; font-variant-numeric: tabular-nums;
}
.bar-timer.urgent { background: var(--accent); color: var(--accent-ink); }
.timer-track { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: transparent; }
.timer-track i { display: block; height: 100%; width: 100%; background: var(--accent-2); transform-origin: left center; transform: scaleX(0); }

/* ---------- room: grid ---------- */
.game-grid {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) 300px;
  gap: 12px;
  align-items: start;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px;
  min-width: 0;
}
.panel-title { margin: 2px 4px 8px; font-size: 14px; font-weight: 800; }
.panel-title .muted { font-weight: 500; }

.player-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.player {
  display: grid;
  grid-template-columns: 22px 34px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid transparent;
}
.player.me { border-color: var(--accent); }
.player.drawing { background: var(--accent-soft); }
.player.guessed { background: var(--good-soft); }
.player.offline { opacity: 0.55; }
.player .rank { font-weight: 800; color: var(--muted); font-size: 13px; text-align: center; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 800; font-size: 14px; color: #fff;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
  background: #888;
}
.player .who { min-width: 0; display: grid; }
.player .name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .meta { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.badge { display: inline-flex; align-items: center; gap: 3px; font-size: 12px; font-weight: 700; }
.badge svg { width: 14px; height: 14px; }
.badge.host { color: var(--warn); }
.badge.drawing { color: var(--accent); }
.badge.ok { color: var(--good); }
.player .score { font-weight: 850; font-variant-numeric: tabular-nums; text-align: right; }
.kick {
  width: 28px; height: 28px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--muted); cursor: pointer; display: grid; place-items: center; padding: 0;
}
.kick svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
.kick:hover { color: var(--danger); border-color: var(--danger); }
.player .end { display: flex; align-items: center; gap: 6px; }

/* ---------- stage: lobby ---------- */
.stage { min-width: 0; }
.lobby { padding: 18px; display: grid; gap: 14px; }
.lobby-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.eyebrow { margin: 0; font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.room-code { margin: 0; font-size: 34px; font-weight: 850; letter-spacing: 0.14em; color: var(--accent); }
.share-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.share-link { font-size: 14px; min-height: 40px; color: var(--muted); }
.settings { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.field-wide { grid-column: 1 / -1; }
.lobby-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.lobby-foot p { margin: 0; }

/* ---------- stage: canvas ---------- */
.canvas-wrap {
  position: relative;
  width: 100%;
  max-width: calc((100vh - var(--header-h) - 200px) * 4 / 3);
  max-width: calc((100dvh - var(--header-h) - 200px) * 4 / 3);
  min-width: 260px;
  margin: 0 auto;
  aspect-ratio: 4 / 3;
  border-radius: var(--radius);
  overflow: hidden;
  background: #ffffff;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
body.is-drawer .canvas-wrap {
  max-width: calc((100vh - var(--header-h) - 270px) * 4 / 3);
  max-width: calc((100dvh - var(--header-h) - 270px) * 4 / 3);
}
#board { display: block; width: 100%; height: 100%; touch-action: manipulation; cursor: default; }
body.is-drawer #board { touch-action: none; cursor: crosshair; }
.overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(247, 244, 238, 0.9);
  text-align: center;
  overflow-y: auto;
}
:root[data-theme="dark"] .overlay { background: rgba(20, 21, 25, 0.9); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .overlay { background: rgba(20, 21, 25, 0.9); } }
.ov { display: grid; gap: 12px; justify-items: center; max-width: 520px; width: 100%; }
.ov h2 { margin: 0; font-size: clamp(20px, 4vw, 28px); letter-spacing: -0.02em; }
.ov p { margin: 0; color: var(--muted); }
.ov .big-word { font-size: clamp(26px, 6vw, 40px); font-weight: 850; color: var(--accent); letter-spacing: 0.01em; }
.choices { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.choice {
  display: grid; gap: 2px; min-width: 120px; padding: 12px 16px;
  border-radius: 14px; border: 2px solid var(--border); background: var(--surface);
  cursor: pointer; font-weight: 800; font-size: 18px; color: var(--text);
}
.choice small { font-weight: 600; font-size: 12px; color: var(--muted); }
.choice:hover { border-color: var(--accent); }
.gains { list-style: none; margin: 0; padding: 0; width: 100%; max-width: 340px; display: grid; gap: 4px; }
.gains li { display: flex; justify-content: space-between; gap: 10px; padding: 6px 10px; border-radius: 10px; background: var(--surface-2); font-weight: 600; }
.gains .plus { color: var(--good); font-weight: 850; }
.gains .zero { color: var(--muted); }

.podium { display: flex; align-items: flex-end; justify-content: center; gap: 10px; margin-top: 4px; }
.podium .place { display: grid; justify-items: center; gap: 6px; width: 92px; }
.podium .pname { font-weight: 800; max-width: 92px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium .pscore { font-size: 13px; color: var(--muted); font-weight: 700; }
.podium .block { width: 100%; border-radius: 12px 12px 0 0; display: grid; place-items: center; font-size: 26px; font-weight: 850; color: var(--accent-ink); }
.podium .p1 .block { height: 96px; background: var(--accent); }
.podium .p2 .block { height: 70px; background: var(--accent-2); color: #fff; }
.podium .p3 .block { height: 52px; background: #9a7b4f; color: #fff; }
.ranking { list-style: none; margin: 0; padding: 0; width: 100%; max-width: 340px; display: grid; gap: 4px; }
.ranking li { display: flex; justify-content: space-between; padding: 4px 10px; border-radius: 8px; background: var(--surface-2); font-size: 14px; }

/* ---------- toolbar ---------- */
.toolbar {
  margin: 10px auto 0;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.tool-group { display: flex; gap: 6px; }
.tool-btn {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); cursor: pointer;
}
.tool-btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.tool-btn.danger:hover { color: var(--danger); border-color: var(--danger); }
.size-dot { display: block; border-radius: 50%; background: currentColor; }
.swatches { display: grid; grid-template-columns: repeat(8, 26px); gap: 5px; }
.swatch { width: 26px; height: 26px; border-radius: 8px; border: 1px solid rgba(0, 0, 0, 0.18); cursor: pointer; padding: 0; }
.swatch[aria-pressed="true"] { outline: 3px solid var(--text); outline-offset: 2px; }

/* ---------- chat ---------- */
.chat-panel { display: flex; flex-direction: column; height: calc(100vh - var(--header-h) - 110px); height: calc(100dvh - var(--header-h) - 110px); min-height: 380px; }
.chat-log { flex: 1; min-height: 0; overflow-y: auto; list-style: none; margin: 0; padding: 2px; display: flex; flex-direction: column; gap: 4px; overscroll-behavior: contain; }
.msg { padding: 5px 9px; border-radius: 10px; font-size: 14px; overflow-wrap: anywhere; }
.msg .from { font-weight: 800; }
.msg-chat:nth-child(odd) { background: var(--surface-2); }
.msg-system, .msg-reveal { color: var(--muted); font-style: italic; font-size: 13px; }
.msg-reveal { color: var(--accent-2); font-style: normal; font-weight: 700; }
.msg-guessed { background: var(--good-soft); color: var(--good); font-weight: 700; }
.msg-you-guessed { background: var(--good); color: #fff; font-weight: 800; }
.msg-close { background: var(--warn-soft); color: var(--warn); font-weight: 700; }
.msg-notice { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.msg.scope-guessed { border-left: 3px solid var(--good); }
.msg .tag { margin-left: 6px; font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 6px; background: var(--surface); color: var(--muted); font-style: normal; }
.chat-form { display: flex; gap: 8px; padding-top: 8px; }
.chat-form input { flex: 1; min-width: 0; }

/* ---------- rules dialog ---------- */
.rules {
  width: min(560px, calc(100vw - 24px));
  max-height: min(86vh, 760px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.rules::backdrop { background: rgba(10, 10, 12, 0.55); }
.rules-head {
  position: sticky; top: 0;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.rules-head h2 { margin: 0; font-size: 20px; }
.rules-body { padding: 6px 18px 18px; font-size: 15px; }
.rules-body h3 { margin: 16px 0 6px; font-size: 16px; color: var(--accent); }
.rules-body p { margin: 6px 0; }
.rules-steps { margin: 10px 0 0; padding-left: 20px; display: grid; gap: 8px; }
.rules-points { margin: 0; padding-left: 18px; display: grid; gap: 6px; }

/* ---------- banners ---------- */
.net-banner {
  position: fixed; left: 50%; top: 10px; transform: translateX(-50%); z-index: 30;
  padding: 8px 14px; border-radius: 12px; background: var(--warn); color: #1b1307; font-weight: 800; box-shadow: var(--shadow);
}
.toast {
  position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 30;
  max-width: calc(100vw - 32px);
  padding: 10px 16px; border-radius: 12px; background: var(--text); color: var(--bg); font-weight: 700; box-shadow: var(--shadow);
}

/* ---------- phone + tablet ---------- */
@media (max-width: 900px) {
  :root { --header-h: 48px; }
  .app-header { padding: 0 12px; }
  .brand-mark { width: 30px; height: 30px; }
  .brand-name { font-size: 18px; }
  .icon-btn { width: 36px; height: 36px; border-radius: 10px; }
  main { padding: 0 10px 16px; }

  .game-bar { grid-template-columns: auto auto 1fr auto auto; gap: 6px; padding: 6px 8px 9px; margin-bottom: 8px; }
  .bar-round { font-size: 12px; }
  .hint { font-size: 18px; gap: 3px; }
  .hint .w { gap: 3px; }
  .word-known { font-size: 18px; }
  .bar-timer { min-width: 44px; height: 36px; font-size: 17px; }

  .game-grid { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  .players-panel { order: 1; padding: 6px; }
  .players-panel .panel-title { display: none; }
  .player-list { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
  .player-list::-webkit-scrollbar { display: none; }
  .player { flex: 0 0 auto; grid-template-columns: 26px auto auto; gap: 6px; padding: 4px 8px 4px 4px; }
  .player .rank { display: none; }
  .avatar { width: 26px; height: 26px; font-size: 12px; }
  .player .name { max-width: 92px; font-size: 13px; }
  .player .meta { display: none; }
  .player .score { font-size: 13px; }

  .stage { order: 2; }
  .canvas-wrap, body.is-drawer .canvas-wrap { max-width: none; min-width: 0; border-radius: 12px; }
  .toolbar { gap: 6px; padding: 6px; margin-top: 8px; }
  .tool-btn { width: 40px; height: 40px; }
  .swatches { grid-template-columns: repeat(8, 30px); gap: 4px; }
  .swatch { width: 30px; height: 30px; }

  /* Chat: input right under the canvas, newest message first, so the on-screen keyboard never hides the game. */
  .chat-panel { order: 3; height: auto; min-height: 0; padding: 8px; }
  .chat-panel .panel-title { display: none; }
  .chat-form { order: -1; padding: 0 0 8px; }
  .chat-log { height: 32vh; min-height: 140px; flex: none; }

  .lobby { padding: 14px; }
  .room-code { font-size: 28px; }
  .settings { grid-template-columns: 1fr 1fr; gap: 10px; }
  .podium .place { width: 80px; }
}

@media (max-width: 380px) {
  .swatches { grid-template-columns: repeat(8, 26px); }
  .swatch { width: 26px; height: 26px; }
  .tool-btn { width: 36px; height: 36px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
