:root {
  --bg: #14121f;
  --surface: #1f1c30;
  --surface-2: #2a2640;
  --text: #ece9f7;
  --muted: #9a94b8;
  --primary: #e0a43b;
  --primary-text: #1b1405;
  --danger: #e2595b;
  --ok: #4cc38a;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Sarabun", "Noto Sans Thai", system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

[hidden] { display: none !important; }

.app {
  flex: 1;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 16px 16px;
}

.screen { display: flex; flex-direction: column; gap: 16px; }

.brand { text-align: center; margin: 24px 0 8px; }
.brand-icon { font-size: 56px; line-height: 1; }
.brand h1 { margin: 8px 0 4px; font-size: 28px; letter-spacing: .5px; }

.muted { color: var(--muted); }
.small { font-size: 14px; }
.center { text-align: center; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 14px; color: var(--muted); }

input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border: 1px solid var(--surface-2);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 17px; /* >= 16px avoids iOS zoom */
}
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.btn {
  min-height: 48px;
  padding: 0 18px;
  border: 0;
  border-radius: var(--radius);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-secondary { background: var(--surface-2); color: var(--text); }
.btn-ghost { background: transparent; color: var(--primary); border: 1px solid var(--surface-2); }
.btn-danger-ghost { background: transparent; color: var(--danger); border: 1px solid var(--surface-2); margin-top: 8px; }
.btn-small { min-height: 36px; padding: 0 12px; font-size: 14px; }

.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 14px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--surface-2); }

.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: 3px; font-weight: 600; }
.join-row input::placeholder { letter-spacing: normal; font-weight: 400; text-transform: none; }

.lobby-head {
  text-align: center;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.lobby-head p { margin: 0; }
.room-code { font-size: 36px; font-weight: 700; letter-spacing: 6px; font-variant-numeric: tabular-nums; }

.players-head { display: flex; align-items: center; justify-content: space-between; }
.players-head h2 { margin: 0; font-size: 18px; }
.pill { background: var(--surface-2); border-radius: 999px; padding: 2px 10px; font-size: 14px; }

.player-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.player {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 12px;
  min-height: 56px;
}
.player.offline { opacity: .55; }
.avatar {
  flex: none;
  width: 36px; height: 36px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface-2);
  font-weight: 700;
}
.player-main { flex: 1; min-width: 0; }
.player-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 2px; }
.tag { font-size: 12px; border-radius: 6px; padding: 0 6px; background: var(--surface-2); color: var(--muted); }
.tag-host { background: var(--primary); color: var(--primary-text); font-weight: 600; }
.tag-you { color: var(--text); }
.tag-offline { color: var(--danger); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.offline .dot { background: var(--muted); }
.kick-btn { background: transparent; color: var(--danger); border: 1px solid var(--surface-2); }

.banner {
  position: sticky; top: 0; z-index: 10;
  background: var(--danger); color: #fff;
  text-align: center; font-size: 14px;
  padding: calc(6px + env(safe-area-inset-top)) 12px 6px;
}

.toast {
  position: fixed;
  left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg);
  border-radius: 12px; padding: 10px 16px;
  font-size: 15px; box-shadow: 0 6px 20px rgba(0,0,0,.4);
  z-index: 20;
}

.footer { text-align: center; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); word-break: break-all; }

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

body.night { --bg: #0b0a14; --surface: #16142a; --surface-2: #221f3a; }
body { transition: background-color .6s; }

.game-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--surface); border-radius: var(--radius); padding: 12px 16px;
}
.phase-title { font-size: 20px; font-weight: 700; }
.timer {
  flex: none; min-width: 64px; text-align: center;
  font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums;
  background: var(--surface-2); border-radius: 12px; padding: 4px 10px;
}
.timer.low { color: var(--danger); }

.game-main { display: flex; flex-direction: column; gap: 12px; }
.game-main p { margin: 0; }
.prompt { font-size: 18px; font-weight: 600; text-align: center; }
.note { background: var(--surface); border-radius: var(--radius); padding: 12px 14px; }
.center-block { text-align: center; padding: 16px 8px; }
.big-icon { font-size: 56px; line-height: 1.1; }

.role-card {
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border-radius: 20px; padding: 24px 16px; text-align: center;
  display: flex; flex-direction: column; gap: 8px; align-items: center;
}
.role-card .role-icon { font-size: 72px; line-height: 1; }
.role-card .role-name { font-size: 28px; font-weight: 700; }
.team-village { color: var(--ok); }
.team-wolf { color: var(--danger); }

.targets { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.target {
  min-height: 56px; padding: 8px 10px; border-radius: var(--radius);
  border: 2px solid var(--surface-2); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; text-align: left; cursor: pointer;
  display: flex; flex-direction: column; justify-content: center; gap: 2px;
  overflow: hidden; -webkit-tap-highlight-color: transparent;
}
.target span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.target small { color: var(--muted); font-weight: 400; font-size: 12px; }
.target.selected { border-color: var(--primary); background: var(--surface-2); }
.target.abstain { grid-column: 1 / -1; align-items: center; color: var(--muted); }
.target.abstain.selected { color: var(--text); }

.player.dead { opacity: .5; }
.player.dead .player-name { text-decoration: line-through; }
.tag-role { color: var(--text); }
.tag-dead { color: var(--danger); }

.tally { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.winner { font-size: 26px; font-weight: 700; }

.role-counts { background: var(--surface); border-radius: var(--radius); padding: 10px 14px; }
.role-counts summary { cursor: pointer; color: var(--muted); font-size: 14px; }
.role-counts ul { margin: 8px 0 0; padding-left: 20px; }

/* ---------- M3–M6 additions ---------- */

body.day {
  --bg: #f3ece0; --surface: #fffaf1; --surface-2: #eadfcd;
  --text: #2b2420; --muted: #75685c; --primary: #c9851a; --primary-text: #fff;
  color-scheme: light;
}
body.day .tag-host { color: #fff; }

.brand-logo { width: 72px; height: 72px; border-radius: 18px; }
.icon-btn {
  flex: none; width: 40px; height: 40px; border-radius: 12px; border: 0;
  background: var(--surface-2); color: var(--text); font-size: 18px; cursor: pointer;
}
.game-head-text { flex: 1; min-width: 0; }
.pill-warn { color: var(--danger); }
.plain-list { margin: 8px 0 0; padding-left: 20px; }

/* settings */
.settings { display: flex; flex-direction: column; gap: 12px; background: var(--surface); border-radius: var(--radius); padding: 14px; }
.settings .players-head h2 { font-size: 17px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 36px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--surface-2);
  background: transparent; color: var(--text); font: inherit; font-size: 14px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.chip.on { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.chip.match { border-color: var(--primary); }
.chip:disabled { opacity: .6; cursor: default; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; }

.role-rows { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.role-row { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 2px 4px; border-radius: 10px; }
.role-row.zero { opacity: .55; }
.role-row-icon { width: 34px; text-align: center; font-size: 20px; flex: none; }
.role-row-name { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.role-row-name small { font-size: 11px; }
.role-row-count { width: 28px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.step {
  width: 40px; height: 40px; flex: none; border-radius: 10px; border: 0;
  background: var(--surface-2); color: var(--text); font-size: 20px; font-weight: 700; cursor: pointer;
}
.step:disabled { opacity: .35; cursor: default; }
.team-solo { color: #b78cf0; }
.team-lovers { color: #f06a9b; }

.balance-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 14px; }
.balance-head small { font-weight: 400; color: var(--muted); }
.balance-bar { position: relative; height: 10px; margin-top: 6px; border-radius: 999px; background: linear-gradient(90deg, var(--danger), #e0a43b 30%, var(--ok) 42%, var(--ok) 58%, #e0a43b 70%, var(--danger)); }
.balance-zone { display: none; }
.balance-mark { position: absolute; top: -4px; width: 4px; height: 18px; border-radius: 2px; background: var(--text); transform: translateX(-50%); box-shadow: 0 0 0 2px var(--surface); }
.bal-ok { color: var(--ok); }
.bal-warn { color: #e08a3b; }

.issues { list-style: none; margin: 0; padding: 10px 12px; border-radius: 10px; background: rgba(226, 89, 91, .12); color: var(--danger); font-size: 14px; display: flex; flex-direction: column; gap: 4px; }
.ok-line { margin: 0; color: var(--ok); font-size: 14px; }

.fold { background: var(--surface); border-radius: var(--radius); padding: 10px 14px; }
.settings .fold { padding: 0; }
.fold summary { cursor: pointer; color: var(--muted); font-size: 14px; min-height: 32px; display: flex; align-items: center; }
.opts { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.opt { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 40px; font-size: 14px; }
.opt.dim { opacity: .5; }
.opt input[type=checkbox] { width: 22px; height: 22px; min-height: 0; accent-color: var(--primary); flex: none; }
.opt select { min-height: 36px; max-width: 55%; border-radius: 10px; border: 1px solid var(--surface-2); background: var(--surface); color: var(--text); font: inherit; font-size: 14px; padding: 0 8px; }
.opt input.num { width: 90px; min-height: 36px; text-align: center; flex: none; }
.tpl-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.tpl-list li { display: flex; align-items: center; gap: 6px; }
.tpl-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* role card flip */
.role-card {
  background: none; border: 0; padding: 0; color: inherit; font: inherit; cursor: pointer;
  perspective: 900px; display: block; width: 100%; min-height: 300px;
}
.card-inner { position: relative; width: 100%; min-height: 300px; transition: transform .6s; transform-style: preserve-3d; }
.role-card.flipped .card-inner { transform: rotateY(180deg); }
.card-face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border-radius: 20px; padding: 24px 16px; display: flex; flex-direction: column; gap: 8px;
  align-items: center; justify-content: center; text-align: center;
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
}
.card-front { transform: rotateY(180deg); }
.card-face .role-icon { font-size: 64px; line-height: 1.1; }
.card-face .role-name { font-size: 28px; font-weight: 700; }
.card-face p { margin: 0; }
@media (prefers-reduced-motion: reduce) { .card-inner { transition: none; } }

.ability { display: flex; flex-direction: column; gap: 10px; padding-bottom: 8px; border-bottom: 1px solid var(--surface-2); }
.ability:last-child { border-bottom: 0; }
.done-line { color: var(--ok); font-size: 14px; text-align: center; }
.notes p { margin: 4px 0 0; }
.winner-row { outline: 2px solid var(--primary); }

.timeline { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.tl-when { margin-top: 6px; color: var(--muted); font-size: 12px; font-weight: 700; }

/* chat bottom sheet */
.chat-fab {
  position: fixed; z-index: 30;
  right: max(16px, calc((100vw - 480px) / 2 + 16px));
  bottom: calc(16px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--primary); color: var(--primary-text); font-size: 24px;
  box-shadow: 0 6px 18px rgba(0,0,0,.35); cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.chat-fab:active { transform: scale(.94); }
.chat-fab .badge { position: absolute; top: -2px; right: -2px; min-width: 20px; height: 20px; line-height: 20px; font-size: 12px; text-align: center; box-shadow: 0 0 0 2px var(--bg); }
.chat-fab.fab-wolf { background: var(--danger); color: #fff; }

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.5);
  animation: fade .2s ease-out;
}
.sheet {
  position: fixed; z-index: 41; left: 0; right: 0; margin: 0 auto;
  bottom: var(--kb, 0px); width: 100%; max-width: 480px;
  height: min(75dvh, calc(var(--vvh, 100dvh) - 48px));
  background: var(--surface); border-radius: 20px 20px 0 0;
  padding: 6px 16px calc(12px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 10px;
  box-shadow: 0 -8px 30px rgba(0,0,0,.4);
  animation: sheet-up .22s ease-out;
}
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--surface-2); margin: 2px auto 0; flex: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; flex: none; }
.chat-title { margin: 0; font-size: 18px; }
.chat-tabs { display: flex; gap: 6px; flex-wrap: wrap; flex: none; }
.chat-list {
  list-style: none; margin: 0; padding: 4px 2px; flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 6px; font-size: 15px; overscroll-behavior: contain;
}
.chat-list li { overflow-wrap: anywhere; }
.chat-list li.mine b { color: var(--primary); }
.chat-empty { margin: auto; }
.chat-form { display: flex; gap: 8px; flex: none; }
.chat-form input { flex: 1; min-width: 0; min-height: 44px; }
.chat-form .btn { min-height: 44px; }
body.sheet-open { overflow: hidden; }
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet, .sheet-backdrop { animation: none; } }

/* mobile layout */
body.in-room .app { padding-bottom: calc(88px + env(safe-area-inset-bottom)); }
.toast { z-index: 50; }
body.in-room .toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
.game-head {
  position: sticky; top: env(safe-area-inset-top); z-index: 5;
  box-shadow: 0 4px 14px rgba(0,0,0,.25);
}
#game-players { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#game-players .player { padding: 8px 10px; gap: 8px; align-items: flex-start; }
#game-players .avatar { width: 30px; height: 30px; font-size: 14px; }
#game-players .tag { font-size: 11px; }
@media (max-width: 340px) { #game-players { grid-template-columns: 1fr; } }
