/* iCodeDev Games — mobile-first */
:root {
  --bg: #f4f1fb; --surface: #fff; --surface-2: #f7f5fc; --text: #1c1530; --muted: #6b6380; --line: #e4def2;
  --brand: #6d28d9; --brand-ink: #fff; --brand-soft: #ede5ff; --spy: #dc2626; --spy-soft: #fde8e8;
  --civ: #059669; --civ-soft: #dcf7ec; --warn: #b45309; --radius: 16px;
  --shadow: 0 1px 2px rgba(30, 20, 60, .06), 0 6px 20px rgba(30, 20, 60, .08);
  --av0: #fde68a; --av1: #bfdbfe; --av2: #fecaca; --av3: #bbf7d0; --av4: #ddd6fe; --av5: #fbcfe8; --av6: #a5f3fc; --av7: #fed7aa;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #120e1d; --surface: #1d1730; --surface-2: #251e3b; --text: #eee9fb; --muted: #a59cbd; --line: #332a4d;
    --brand: #a78bfa; --brand-ink: #160f2a; --brand-soft: #2c2348; --spy: #f87171; --spy-soft: #3f1d24;
    --civ: #34d399; --civ-soft: #143329; --warn: #fbbf24; --shadow: none;
    --av0: #6b5a1c; --av1: #24406b; --av2: #6b2a2a; --av3: #1f5a37; --av4: #44357a; --av5: #6b2a52; --av6: #1c5a66; --av7: #6b4220;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100vh; background: var(--bg); color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Sarabun", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--brand); }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .4em; }
button, input, select { font: inherit; color: inherit; }
.g-muted { color: var(--muted); }
.g-small { font-size: .85rem; }
[hidden] { display: none !important; }

/* แถบบน */
.g-top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px; min-height: 56px;
  padding: 0 12px; padding-top: env(safe-area-inset-top); background: var(--surface); border-bottom: 1px solid var(--line);
}
.g-brand { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; color: var(--text); text-decoration: none; }
.g-brand b { color: var(--brand); }
.g-brand-mark { font-size: 1.3rem; }
.g-back { display: grid; place-items: center; width: 40px; height: 40px; font-size: 2rem; line-height: 1; text-decoration: none; color: var(--text); border-radius: 10px; }
.g-title { flex: 1; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.g-me { border: 0; background: none; padding: 0; cursor: pointer; }
.g-main { max-width: 640px; margin: 0 auto; padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); }

/* หน้ารวมเกม */
.g-hero { text-align: center; padding: 16px 0 8px; }
.g-hero h1 { font-size: clamp(1.7rem, 7vw, 2.4rem); }
.g-games { display: grid; gap: 12px; margin-top: 16px; }
.g-game {
  display: flex; gap: 14px; align-items: center; padding: 16px; background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); border: 1px solid var(--line); color: var(--text); text-decoration: none;
}
a.g-game:active { transform: scale(.99); }
.g-game.is-soon { opacity: .55; }
.g-game-emoji { font-size: 2.6rem; width: 64px; height: 64px; display: grid; place-items: center; background: var(--brand-soft); border-radius: 18px; flex: none; }
.g-game-body { display: grid; gap: 2px; }
.g-game-body strong { font-size: 1.15rem; }
.g-chip { display: inline-block; justify-self: start; margin-top: 4px; padding: 1px 10px; border-radius: 99px; background: var(--brand-soft); color: var(--brand); font-size: .8rem; font-weight: 600; }
.g-foot { text-align: center; margin-top: 32px; }

/* การ์ด + ปุ่ม + ฟอร์ม */
.g-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.g-card h2 { font-size: 1.1rem; }
.g-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 48px; padding: 10px 18px;
  border: 0; border-radius: 12px; background: var(--brand); color: var(--brand-ink); font-weight: 700; cursor: pointer; text-decoration: none;
}
.g-btn:disabled { opacity: .45; cursor: default; }
.g-btn:not(:disabled):active { transform: scale(.98); }
.g-btn-block { display: flex; width: 100%; }
.g-btn-ghost { background: transparent; color: var(--brand); box-shadow: inset 0 0 0 2px var(--brand); }
.g-btn-soft { background: var(--brand-soft); color: var(--brand); }
.g-btn-danger { background: var(--spy-soft); color: var(--spy); }
.g-btn-small { min-height: 36px; padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
.g-row { display: flex; gap: 8px; align-items: center; }
.g-row > .g-grow { flex: 1; min-width: 0; }
.g-stack { display: grid; gap: 10px; }
.g-input, .g-select {
  width: 100%; min-height: 48px; padding: 10px 14px; border: 2px solid var(--line); border-radius: 12px; background: var(--surface-2);
  font-size: 16px; /* >=16px กัน iOS ซูมเอง */
}
.g-input:focus, .g-select:focus { outline: none; border-color: var(--brand); }
.g-code-input { text-align: center; letter-spacing: .4em; font-size: 1.4rem; font-weight: 700; }
.g-label { display: grid; gap: 4px; font-size: .9rem; color: var(--muted); }
.g-check { display: flex; gap: 10px; align-items: center; min-height: 40px; }
.g-check input { width: 22px; height: 22px; accent-color: var(--brand); }
.g-divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .85rem; margin: 6px 0; }
.g-divider::before, .g-divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.g-loading { text-align: center; color: var(--muted); padding: 40px 0; }

/* รูปโปรไฟล์ */
.av {
  --size: 56px; position: relative; display: inline-grid; place-items: center; width: var(--size); height: var(--size); flex: none;
  border-radius: 50%; font-size: calc(var(--size) * .68); line-height: 1; overflow: visible; background: var(--av0);
}
.av img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.av-0 { background: var(--av0); } .av-1 { background: var(--av1); } .av-2 { background: var(--av2); } .av-3 { background: var(--av3); }
.av-4 { background: var(--av4); } .av-5 { background: var(--av5); } .av-6 { background: var(--av6); } .av-7 { background: var(--av7); }
.av-sm { --size: 44px; } .av-lg { --size: 84px; } .av-xl { --size: 112px; } .av-xxl { --size: 128px; }
.av-dot { position: absolute; right: 2%; bottom: 2%; width: 14px; height: 14px; border-radius: 50%; background: #9ca3af; border: 2px solid var(--surface); }
.av-dot.on { background: #22c55e; }
.av-crown { position: absolute; top: -12px; right: -6px; font-size: 20px; }
.av.is-out { filter: grayscale(1); opacity: .5; }

/* โปรไฟล์ของฉัน */
.g-profile { display: flex; align-items: center; gap: 14px; }
.g-profile-name { font-weight: 700; font-size: 1.15rem; }
.g-avatars { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
@media (min-width: 480px) { .g-avatars { grid-template-columns: repeat(6, 1fr); } }
.g-avatars button { border: 3px solid transparent; border-radius: 50%; padding: 2px; background: none; cursor: pointer; aspect-ratio: 1; }
.g-avatars button .av { width: 100%; height: 100%; font-size: clamp(2rem, 11vw, 3.2rem); }
.g-profile-preview { gap: 16px; }
.g-avatars button[aria-pressed="true"] { border-color: var(--brand); }

/* dialog */
.g-dialog {
  width: min(560px, calc(100% - 24px)); max-height: calc(100% - 24px); border: 0; border-radius: 20px; padding: 0;
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
.g-dialog::backdrop { background: rgba(10, 5, 25, .55); }
.g-dialog-body { padding: 18px; display: grid; gap: 14px; }
.g-dialog-head { display: flex; align-items: center; justify-content: space-between; }
.g-x { width: 40px; height: 40px; border: 0; border-radius: 10px; background: var(--surface-2); font-size: 1.3rem; cursor: pointer; }
.g-tabs { display: flex; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.g-tabs button { flex: 1; min-height: 38px; border: 0; border-radius: 9px; background: none; cursor: pointer; font-weight: 600; color: var(--muted); }
.g-tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* toast */
.g-toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); opacity: 0;
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 12px; background: #1c1530; color: #fff; font-weight: 600;
  transition: .2s; pointer-events: none; z-index: 20; text-align: center;
}
.g-toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ห้อง */
.g-roomcode { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.g-roomcode-num { font-size: 1.8rem; font-weight: 800; letter-spacing: .15em; }
.g-players { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.g-player { display: flex; align-items: center; gap: 12px; padding: 8px 10px; border-radius: 12px; background: var(--surface-2); }
.g-player-name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.g-player .g-tag { font-size: .75rem; }
.g-tag { display: inline-block; padding: 1px 8px; border-radius: 99px; font-weight: 700; font-size: .8rem; background: var(--brand-soft); color: var(--brand); }
.g-tag-spy { background: var(--spy-soft); color: var(--spy); }
.g-tag-civ { background: var(--civ-soft); color: var(--civ); }
.g-settings { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.g-settings .g-span { grid-column: 1 / -1; }
.g-settings-ro { display: flex; flex-wrap: wrap; gap: 6px; }

/* การ์ดคำของฉัน */
.g-word {
  text-align: center; padding: 18px 16px; border-radius: 20px; color: #fff; margin-bottom: 14px; cursor: pointer; user-select: none;
  background: linear-gradient(135deg, #7c3aed, #db2777);
}
.g-word.is-spy { background: linear-gradient(135deg, #b91c1c, #7c2d12); }
.g-word-label { font-size: .85rem; opacity: .85; }
.g-word-text { font-size: clamp(1.9rem, 9vw, 2.6rem); font-weight: 800; line-height: 1.25; margin: 4px 0; word-break: break-word; }
.g-word.is-hidden .g-word-text { filter: blur(14px); }
.g-word-hint { font-size: .8rem; opacity: .8; }

/* สถานะรอบ + เวลา */
.g-status { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.g-phase { font-weight: 800; font-size: 1.15rem; }
.g-timer { min-width: 58px; text-align: center; font-weight: 800; font-variant-numeric: tabular-nums; padding: 4px 10px; border-radius: 99px; background: var(--brand-soft); color: var(--brand); }
.g-timer.is-low { background: var(--spy-soft); color: var(--spy); }
.g-bar { height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; margin-bottom: 14px; }
.g-bar > i { display: block; height: 100%; background: var(--brand); transition: width .25s linear; }

/* คำใบ้ */
.g-clues { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.g-clue { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 12px; background: var(--surface-2); }
.g-clue.is-now { box-shadow: inset 0 0 0 2px var(--brand); background: var(--brand-soft); }
.g-clue-body { flex: 1; min-width: 0; }
.g-clue-name { font-size: .8rem; color: var(--muted); }
.g-clue-text { font-weight: 700; word-break: break-word; }
.g-clue-text.is-empty { font-weight: 400; color: var(--muted); font-style: italic; }
.g-history summary { cursor: pointer; font-weight: 700; padding: 4px 0; }
.g-history h3 { font-size: .9rem; color: var(--muted); margin: 12px 0 6px; }
.g-history-line { padding: 3px 0; border-bottom: 1px dashed var(--line); font-size: .95rem; }
.g-history-line b { margin-right: 6px; }

/* โหวต */
.g-votes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.g-vote {
  position: relative; display: grid; justify-items: center; gap: 6px; padding: 12px 6px; border-radius: 14px; border: 3px solid transparent;
  background: var(--surface-2); cursor: pointer; text-align: center;
}
.g-vote[aria-pressed="true"] { border-color: var(--spy); background: var(--spy-soft); }
.g-vote:disabled { cursor: default; }
.g-vote-name { font-weight: 600; font-size: .9rem; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.g-vote-check { position: absolute; top: 6px; right: 8px; font-size: .8rem; }
.g-vote-clue { font-size: .75rem; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* เฉลย / จบเกม */
.g-reveal { text-align: center; padding: 22px 16px; }
.g-reveal .av { margin-bottom: 8px; }
.g-reveal-title { font-size: 1.5rem; font-weight: 800; margin: 6px 0; }
.g-reveal-title.is-spy { color: var(--spy); }
.g-reveal-title.is-civ { color: var(--civ); }
.g-winner { text-align: center; padding: 22px 16px; border-radius: 20px; color: #fff; margin-bottom: 14px; background: linear-gradient(135deg, #059669, #0d9488); }
.g-winner.is-spy { background: linear-gradient(135deg, #b91c1c, #7c2d12); }
.g-winner h2 { font-size: 1.6rem; margin: 0; }
.g-winner-emoji { font-size: 3rem; }
.g-words { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.g-words > div { padding: 12px; border-radius: 14px; text-align: center; }
.g-words strong { display: block; font-size: 1.3rem; }
.g-words .is-civ { background: var(--civ-soft); color: var(--civ); }
.g-words .is-spy { background: var(--spy-soft); color: var(--spy); }
.g-tally { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 4px; text-align: left; font-size: .95rem; }

/* แถบล่าง (ปุ่มหลัก) */
.g-action {
  position: sticky; bottom: 0; margin: 0 -16px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.g-leave { display: flex; margin: 8px auto 0; }
.g-howto ol { padding-left: 1.3em; margin: 0; }
.g-howto li { margin-bottom: 6px; }

/* รายการห้อง */
.g-rooms { display: grid; gap: 8px; }
.g-room {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 14px;
  background: var(--surface-2); text-align: left; cursor: pointer;
}
.g-room:disabled { opacity: .5; cursor: default; }
.g-room-body { flex: 1; min-width: 0; display: grid; }
.g-room-body strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* แท็บในห้อง */
.g-roomtabs { margin-bottom: 14px; position: sticky; top: calc(58px + env(safe-area-inset-top)); z-index: 4; box-shadow: var(--shadow); }
.g-roomtabs button { font-size: .9rem; padding: 0 4px; }

/* ที่นั่ง */
.g-seats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (min-width: 520px) { .g-seats { grid-template-columns: repeat(4, 1fr); } }
.g-seat {
  display: grid; justify-items: center; align-content: start; gap: 6px; padding: 12px 4px; min-height: 150px; border-radius: 16px;
  background: var(--surface-2); border: 2px solid transparent; text-align: center;
}
.g-seat.is-me { border-color: var(--brand); }
.g-seat.is-ready { background: var(--civ-soft); }
.g-seat.is-empty { border: 2px dashed var(--line); background: transparent; cursor: pointer; color: var(--muted); align-content: center; }
.g-seat.is-empty:active { background: var(--brand-soft); }
.g-seat-plus { font-size: 2rem; line-height: 1; color: var(--brand); }
.g-seat-name { font-weight: 600; font-size: .85rem; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.g-seat .g-tag { font-size: .72rem; }
.g-btn-go { background: var(--civ); color: #fff; }
.g-center { text-align: center; margin: 0; }
.g-section-title { font-size: 1.05rem; margin: 18px 0 10px; }
.g-gap { margin-bottom: 14px; }
.g-card-wrap { margin-top: 14px; }
.g-card-wrap > summary { list-style: none; margin-bottom: 14px; }
.g-card-wrap > summary::-webkit-details-marker { display: none; }

/* คนในห้อง */
.g-player-main { flex: 1; min-width: 0; display: grid; gap: 2px; }
.g-player-tags { display: flex; flex-wrap: wrap; gap: 4px; }

/* คะแนน + สถิติ */
.g-scores { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.g-score { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 12px; background: var(--surface-2); }
.g-score.is-me { box-shadow: inset 0 0 0 2px var(--brand); }
.g-score-rank { width: 28px; text-align: center; font-weight: 800; font-size: 1.1rem; }
.g-score-main { flex: 1; min-width: 0; display: grid; }
.g-score-wl { display: grid; justify-items: center; min-width: 40px; color: var(--civ); }
.g-score-wl b { font-size: 1.3rem; line-height: 1.1; }
.g-score-wl.is-loss { color: var(--spy); }
.g-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 10px; }
.g-stat { display: grid; justify-items: center; text-align: center; padding: 10px 4px; border-radius: 12px; background: var(--surface-2); }
.g-stat b { font-size: 1.25rem; }
.g-stat span { font-size: .72rem; color: var(--muted); line-height: 1.3; }

/* ประวัติรอบ */
.g-round { margin-top: 10px; }
.g-round-vote { margin-top: 6px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); }
.g-history summary .g-tag { margin-right: 4px; }

/* ใบ้พร้อมกัน + โหวตสด */
.g-clue.is-done { background: var(--civ-soft); }
.g-error { color: var(--spy); font-weight: 600; margin: 0; }
.av-xs { --size: 26px; }
.av-xs .av-dot, .av-xs .av-crown { display: none; }
.g-vote-count { font-size: .8rem; font-weight: 800; color: var(--spy); }
.g-voters { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; min-height: 26px; }
.g-voter { display: inline-flex; }
.g-voter .av { box-shadow: 0 0 0 2px var(--surface); }
.g-online-h { font-size: .9rem; color: var(--muted); margin: 12px 0 6px; }
.g-online-h:first-child { margin-top: 0; }

/* เสียง + แชต + push-to-talk */
.g-mute { width: 40px; height: 40px; border: 0; border-radius: 10px; background: var(--surface-2); font-size: 1.2rem; cursor: pointer; }
.g-main { padding-bottom: calc(120px + env(safe-area-inset-bottom)); }
.g-chat-fab, .g-talk {
  position: fixed; right: 14px; z-index: 12; display: grid; place-items: center; border: 0; border-radius: 50%; cursor: pointer;
  box-shadow: 0 6px 20px rgba(30, 20, 60, .3);
}
.g-chat-fab { bottom: calc(96px + env(safe-area-inset-bottom)); width: 56px; height: 56px; font-size: 1.6rem; background: var(--surface); }
.g-badge {
  position: absolute; top: -2px; right: -2px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 99px;
  background: var(--spy); color: #fff; font-size: .75rem; font-weight: 800; line-height: 22px;
}
.g-talk {
  bottom: calc(162px + env(safe-area-inset-bottom)); width: 64px; height: 64px; font-size: 1.7rem; background: var(--brand); color: var(--brand-ink);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.g-talk-label { position: absolute; right: 72px; white-space: nowrap; font-size: .75rem; font-weight: 700; padding: 3px 8px; border-radius: 99px; background: var(--surface); color: var(--text); box-shadow: var(--shadow); pointer-events: none; }
.g-talk.is-rec { background: var(--spy); color: #fff; transform: scale(1.15); animation: g-pulse 1s infinite; }
@keyframes g-pulse { 50% { box-shadow: 0 0 0 14px rgba(220, 38, 38, .25); } }
.g-speaking {
  position: fixed; left: 50%; top: calc(66px + env(safe-area-inset-top)); transform: translateX(-50%); z-index: 13;
  display: flex; align-items: center; gap: 6px; padding: 6px 14px 6px 6px; border-radius: 99px; background: #1c1530; color: #fff; font-weight: 600; font-size: .9rem;
}
.g-chat {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; height: min(70vh, 560px); display: flex; flex-direction: column;
  background: var(--surface); border-radius: 20px 20px 0 0; box-shadow: 0 -10px 40px rgba(0, 0, 0, .25); padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 720px) { .g-chat { left: auto; right: 16px; bottom: 16px; width: 380px; border-radius: 20px; } }
body.chat-open .g-chat-fab { display: none; }
body.chat-open .g-talk { bottom: calc(min(70vh, 560px) + 12px); }
@media (min-width: 720px) { body.chat-open .g-talk { right: 410px; bottom: 30px; } }
.g-chat-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.g-chat-list { flex: 1; overflow-y: auto; list-style: none; margin: 0; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.g-chat-form { display: flex; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); }
.g-msg { display: flex; gap: 8px; align-items: flex-end; max-width: 85%; }
.g-msg.is-mine { align-self: flex-end; }
.g-msg-body { display: grid; gap: 2px; padding: 8px 12px; border-radius: 16px 16px 16px 4px; background: var(--surface-2); }
.g-msg.is-mine .g-msg-body { background: var(--brand); color: var(--brand-ink); border-radius: 16px 16px 4px 16px; }
.g-msg-name { font-size: .75rem; font-weight: 700; color: var(--brand); }
.g-chat-text { word-break: break-word; white-space: pre-wrap; }
.g-msg-time { font-size: .65rem; opacity: .6; justify-self: end; }
.g-voice-msg { border: 0; background: none; color: inherit; font: inherit; font-weight: 700; cursor: pointer; padding: 0; }
