:root {
  --bg: #eef2ff;
  --bg-2: #f8fafc;
  --surface: rgba(255, 255, 255, 0.88);
  --surface-solid: #ffffff;
  --surface-2: #f1f5f9;
  --ink: #0f172a;
  --ink-2: #334155;
  --muted: #64748b;
  --line: rgba(148, 163, 184, 0.28);
  --brand: #4f46e5;
  --brand-2: #06b6d4;
  --brand-soft: rgba(79, 70, 229, 0.1);
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --shadow: 0 22px 60px rgba(15, 23, 42, 0.12);
  --shadow-sm: 0 8px 22px rgba(15, 23, 42, 0.08);
  --radius: 20px;
  --radius-sm: 12px;
}

[data-theme="dark"] {
  --bg: #070b18;
  --bg-2: #0b1224;
  --surface: rgba(17, 25, 45, 0.86);
  --surface-solid: #11192d;
  --surface-2: #0b1224;
  --ink: #f1f5f9;
  --ink-2: #cbd5e1;
  --muted: #94a3b8;
  --line: rgba(148, 163, 184, 0.18);
  --brand: #818cf8;
  --brand-2: #22d3ee;
  --brand-soft: rgba(129, 140, 248, 0.14);
  --shadow: 0 22px 60px rgba(0, 0, 0, 0.45);
  --shadow-sm: 0 8px 22px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-width: 320px; scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", sans-serif;
  color: var(--ink);
  background: var(--bg);
  overflow-x: hidden;
  transition: background 0.3s, color 0.3s;
}
button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { opacity: 0.5; cursor: not-allowed; transform: none !important; }
h1, h2, h3, p { margin-top: 0; }
a { color: var(--brand); }

.bg-orb {
  position: fixed;
  border-radius: 50%;
  filter: blur(10px);
  pointer-events: none;
  z-index: -1;
  opacity: 0.75;
}
.orb-a {
  width: 480px; height: 480px; top: -160px; left: -120px;
  background: radial-gradient(circle, rgba(79, 70, 229, 0.3), transparent 66%);
}
.orb-b {
  width: 540px; height: 540px; top: 18%; right: -180px;
  background: radial-gradient(circle, rgba(6, 182, 212, 0.24), transparent 66%);
}

/* ---------- Topbar ---------- */
.topbar {
  width: min(1180px, calc(100% - 28px));
  margin: 0 auto;
  padding: 18px 0 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-mark {
  display: grid; place-items: center; flex: none;
  width: 44px; height: 44px; border-radius: 14px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  box-shadow: 0 10px 24px rgba(79, 70, 229, 0.3);
  font-size: 21px;
}
.brand-name { font-size: 15px; font-weight: 900; letter-spacing: 0.06em; white-space: nowrap; }
.brand-sub { font-size: 11px; color: var(--muted); font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.ghost-btn, .icon-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-2);
  border-radius: 12px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 700;
  transition: 0.16s ease;
  backdrop-filter: blur(12px);
}
.ghost-btn:hover { border-color: rgba(79, 70, 229, 0.45); color: var(--brand); transform: translateY(-1px); }
.icon-btn { padding: 9px 11px; }

/* ---------- Shared ---------- */
.app {
  width: min(1180px, calc(100% - 28px));
  margin: 0 auto;
  padding: 18px 0 56px;
}
.page { animation: pageIn 0.28s ease both; }
@keyframes pageIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.card {
  background: var(--surface);
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(16px);
}
[data-theme="dark"] .card { border-color: rgba(148, 163, 184, 0.12); }
.eyebrow {
  font-size: 11px; font-weight: 900; letter-spacing: 0.16em;
  text-transform: uppercase; color: var(--brand); margin-bottom: 6px;
}
h1, h2 { letter-spacing: -0.03em; }
h2 { font-size: clamp(24px, 3vw, 34px); margin-bottom: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 10px; border-radius: 99px;
  background: var(--brand-soft); color: var(--brand);
  font-size: 12px; font-weight: 800; white-space: nowrap;
}
.primary-btn, .secondary-btn, .danger-btn, .danger-outline-btn {
  border: 0; border-radius: 12px; padding: 12px 18px;
  font-size: 14px; font-weight: 800; transition: 0.16s ease;
}
.primary-btn {
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff; box-shadow: 0 10px 24px rgba(79, 70, 229, 0.28);
}
.primary-btn:hover { transform: translateY(-2px); box-shadow: 0 14px 30px rgba(79, 70, 229, 0.34); }
.secondary-btn {
  background: var(--surface-solid); color: var(--ink);
  border: 1px solid var(--line); box-shadow: var(--shadow-sm);
}
.secondary-btn:hover { transform: translateY(-2px); border-color: rgba(6, 182, 212, 0.5); }
.danger-btn {
  background: linear-gradient(135deg, #ef4444, #f97316);
  color: #fff; box-shadow: 0 10px 22px rgba(239, 68, 68, 0.24);
}
.danger-btn:hover { transform: translateY(-2px); }
.danger-outline-btn {
  width: 100%; background: rgba(239, 68, 68, 0.08); color: #dc2626;
  border: 1px solid rgba(239, 68, 68, 0.28);
}
.danger-outline-btn:hover { background: rgba(239, 68, 68, 0.14); }
.primary-btn.small { padding: 10px 15px; font-size: 13px; }
.wide { width: 100%; }
.row { display: flex; gap: 10px; }

.field { margin-bottom: 14px; text-align: left; }
.field label { display: block; font-size: 13px; font-weight: 800; margin-bottom: 6px; }
.field input, .field select, .input-row input, .setting-block input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface-solid);
  color: var(--ink);
  outline: none;
  transition: 0.16s ease;
}
.field input:focus, .field select:focus, .input-row input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 4px var(--brand-soft);
}
.field-hint { margin-top: 5px; font-size: 11.5px; color: var(--muted); }

/* ---------- Landing ---------- */
.hero-card {
  max-width: 620px;
  margin: 26px auto 0;
  padding: clamp(24px, 5vw, 44px);
  text-align: center;
  position: relative;
  overflow: hidden;
}
.hero-card::before {
  content: "";
  position: absolute; inset: -40% -20% auto auto;
  width: 320px; height: 320px; border-radius: 50%;
  background: radial-gradient(circle, rgba(79, 70, 229, 0.16), transparent 68%);
  pointer-events: none;
}
.hero-badge {
  display: inline-flex; padding: 6px 12px; border-radius: 99px;
  background: var(--brand-soft); color: var(--brand);
  font-size: 11.5px; font-weight: 900; letter-spacing: 0.06em;
  text-transform: uppercase; margin-bottom: 18px;
}
.hero-card h1 {
  font-size: clamp(34px, 6vw, 56px);
  line-height: 1.02; margin-bottom: 14px;
}
.hero-card h1 span {
  background: linear-gradient(120deg, var(--brand), var(--brand-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.hero-copy { color: var(--muted); line-height: 1.6; max-width: 470px; margin: 0 auto 24px; }
.hero-card .field { max-width: 420px; margin-left: auto; margin-right: auto; }
.hero-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; max-width: 420px; margin: 18px auto 0; }
.hero-foot { margin-top: 18px; font-size: 12px; color: var(--muted); }

/* ---------- Create room ---------- */
.create-card {
  max-width: 540px;
  margin: 20px auto 0;
  padding: clamp(22px, 4vw, 36px);
  position: relative;
}
.back-btn { margin-bottom: 18px; padding: 7px 12px; font-size: 12px; }
.room-code-display {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 18px; margin: 6px 0 22px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--brand-soft), rgba(6, 182, 212, 0.08));
  border: 1px dashed rgba(79, 70, 229, 0.3);
}
.room-code-display span { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.room-code-display b { font-size: clamp(30px, 7vw, 42px); letter-spacing: 0.18em; color: var(--brand); font-family: ui-monospace, monospace; }
.room-code-display small { font-size: 11.5px; color: var(--muted); }
.setting-block { margin-bottom: 20px; }
.setting-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.setting-head label { font-size: 13px; font-weight: 800; }
.setting-head output { color: var(--brand); font-size: 14px; font-weight: 900; }
input[type="range"] {
  width: 100%; height: 6px; border-radius: 99px; appearance: none;
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  outline: none; cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none; width: 20px; height: 20px; border-radius: 50%;
  background: #fff; border: 3px solid var(--brand);
  box-shadow: 0 4px 10px rgba(79, 70, 229, 0.3);
}
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: #fff; border: 3px solid var(--brand);
}
.range-ticks { display: flex; justify-content: space-between; margin-top: 5px; font-size: 10.5px; color: var(--muted); font-weight: 700; }

/* ---------- Lobby ---------- */
.lobby-shell { display: flex; flex-direction: column; gap: 12px; }
.lobby-head {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 18px 22px; flex-wrap: wrap;
}
.lobby-head h2 { margin: 0; font-size: clamp(21px, 3vw, 28px); }
.room-code-inline { color: var(--brand); font-family: ui-monospace, monospace; letter-spacing: 0.12em; }
.room-meta { display: flex; gap: 8px; flex-wrap: wrap; }
.lobby-grid { display: grid; grid-template-columns: 1fr 1.2fr; gap: 12px; }
.players-card, .lobby-chat { padding: 18px; display: flex; flex-direction: column; min-height: 360px; }
.panel-title {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: 12px; font-weight: 900; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--ink-2); margin-bottom: 12px;
}
.panel-title .muted { text-transform: none; letter-spacing: 0; font-weight: 600; }
.player-list { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; }
.player-item {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 12px; border-radius: 13px;
  background: var(--surface-solid);
  border: 1px solid var(--line);
  transition: 0.16s ease;
}
.player-item.host { border-color: rgba(245, 158, 11, 0.45); background: rgba(245, 158, 11, 0.08); }
.player-item.me { box-shadow: 0 0 0 2px var(--brand-soft); }
.player-item.offline { opacity: 0.55; }
.player-avatar {
  display: grid; place-items: center; flex: none;
  width: 36px; height: 36px; border-radius: 12px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff; font-weight: 900; font-size: 14px;
}
.player-main { min-width: 0; flex: 1; }
.player-name { font-size: 13.5px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-sub { font-size: 11px; color: var(--muted); font-weight: 600; }
.player-tags { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.tag { font-size: 10px; font-weight: 800; padding: 3px 7px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.tag.host { background: rgba(245, 158, 11, 0.16); color: #b45309; }
.tag.me { background: var(--brand-soft); color: var(--brand); }
.lobby-note { margin-top: 12px; font-size: 11.5px; line-height: 1.5; color: var(--muted); }

.chat-card { display: flex; flex-direction: column; min-height: 0; }
.chat-box {
  flex: 1; min-height: 160px; max-height: 420px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 7px;
  padding: 12px; border-radius: 13px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  overflow-wrap: anywhere;
}
.chat-box:empty::before { content: "Chưa có tin nhắn nào..."; color: var(--muted); font-size: 12px; font-style: italic; }
.chat-line { font-size: 13px; line-height: 1.45; }
.chat-line .who { font-weight: 900; color: var(--brand); margin-right: 5px; }
.chat-line.sys { color: var(--muted); font-style: italic; font-size: 12px; }
.chat-line.sys .who { color: var(--muted); }
.chat-line.private .who { color: #a855f7; }
.chat-line .masked { background: rgba(239, 68, 68, 0.14); border-radius: 4px; padding: 0 2px; }
.input-row { display: flex; gap: 8px; margin-top: 10px; }
.input-row input { flex: 1; min-width: 0; }
.lobby-actions { display: flex; gap: 10px; padding: 14px 18px; flex-wrap: wrap; }
.lobby-actions .primary-btn { margin-left: auto; }
.host-settings { padding: 16px 18px; }
.host-settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.setting-block.compact { margin: 0; }

/* ---------- Game ---------- */
.game-page { display: flex; flex-direction: column; gap: 10px; }
.game-topbar {
  display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(120px, auto);
  align-items: center; gap: 14px; padding: 12px 18px;
}
.role-block { min-width: 0; }
.role-text { font-size: 14px; font-weight: 900; }
.turn-label { font-size: 11.5px; color: var(--muted); font-weight: 700; margin-top: 2px; }
.word-wrap { text-align: center; }
.word-label { font-size: 10px; font-weight: 900; letter-spacing: 0.18em; color: var(--muted); margin-bottom: 2px; }
.word-display {
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: clamp(18px, 2.6vw, 28px); font-weight: 900; letter-spacing: 0.12em;
  white-space: pre; color: var(--brand); line-height: 1.2;
}
.timer-block { text-align: right; }
.timer-label { font-size: 10px; font-weight: 900; letter-spacing: 0.14em; color: var(--muted); }
.timer-value { font-size: clamp(24px, 3vw, 34px); font-weight: 900; color: var(--brand); line-height: 1.1; font-variant-numeric: tabular-nums; }
.timer-value.danger { color: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }

.score-strip { display: flex; align-items: center; gap: 12px; padding: 9px 14px; overflow: hidden; }
.score-title { font-size: 10.5px; font-weight: 900; letter-spacing: 0.12em; color: var(--muted); flex: none; }
.score-list { display: flex; gap: 7px; overflow-x: auto; padding-bottom: 2px; }
.score-chip {
  display: flex; align-items: center; gap: 7px; flex: none;
  padding: 6px 10px; border-radius: 99px;
  background: var(--surface-solid); border: 1px solid var(--line);
  font-size: 12px; font-weight: 700;
}
.score-chip.drawer { border-color: rgba(245, 158, 11, 0.5); background: rgba(245, 158, 11, 0.1); }
.score-chip.solved { border-color: rgba(16, 185, 129, 0.45); background: rgba(16, 185, 129, 0.1); }
.score-chip.offline { opacity: 0.45; }
.score-chip b { color: var(--brand); }

.game-main { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 10px; align-items: start; }
.canvas-wrap { position: relative; padding: 8px; overflow: hidden; }
.canvas-wrap iframe { display: block; width: 100%; height: min(70vh, 680px); min-height: 380px; border: 0; border-radius: 14px; background: #fff; }
.turn-overlay {
  position: absolute; inset: 8px; display: flex; align-items: center; justify-content: center;
  padding: 24px; border-radius: 14px;
  background: rgba(15, 23, 42, 0.78); color: #fff;
  font-size: clamp(16px, 2.2vw, 22px); font-weight: 800; line-height: 1.7;
  text-align: center; white-space: pre-line; backdrop-filter: blur(4px);
  z-index: 5;
}
.game-side { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.side-card { padding: 13px; }
.game-chat .chat-box { max-height: 260px; min-height: 150px; }
.guess-card { border: 1.5px solid rgba(79, 70, 229, 0.3); }
.guess-status { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; line-height: 1.45; }
.guess-status.ok { color: var(--success); font-weight: 800; }
.guess-status.warn { color: var(--danger); font-weight: 800; }
.report-card .muted { margin-top: 8px; line-height: 1.4; }

/* ---------- Results ---------- */
.result-card { max-width: 720px; margin: 20px auto 0; padding: clamp(22px, 4vw, 38px); }
.result-note { margin-bottom: 16px; color: var(--muted); font-size: 13px; }
.table-wrap { overflow-x: auto; border-radius: 14px; border: 1px solid var(--line); }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--line); }
th { background: var(--surface-2); font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
tr:last-child td { border-bottom: 0; }
td.rank { font-weight: 900; color: var(--brand); text-align: center; }
td.score { font-weight: 900; color: var(--success); text-align: center; }
tr.podium-1 td { background: rgba(245, 158, 11, 0.09); }
.result-actions { display: flex; gap: 10px; margin-top: 20px; }
.result-actions button { flex: 1; }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.62); backdrop-filter: blur(5px); }
.modal-card {
  position: relative; width: min(620px, 100%); max-height: min(86vh, 900px);
  display: flex; flex-direction: column;
  background: var(--surface-solid); border: 1px solid var(--line);
  border-radius: 20px; box-shadow: var(--shadow);
  padding: 20px; animation: modalIn 0.2s ease both;
}
@keyframes modalIn { from { opacity: 0; transform: translateY(12px) scale(0.98); } to { opacity: 1; transform: none; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.modal-head h2 { margin: 0; font-size: clamp(20px, 3vw, 26px); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.tutorial-card { width: min(760px, 100%); }
.tutorial-body { overflow-y: auto; padding-right: 6px; font-size: 13.5px; line-height: 1.65; color: var(--ink-2); }
.tutorial-body h3 { margin: 18px 0 7px; font-size: 15px; color: var(--ink); }
.tutorial-body ul { margin: 0 0 10px; padding-left: 20px; }
.tutorial-body li { margin-bottom: 5px; }
.tutorial-body p { margin-bottom: 10px; }
.tutorial-body .formula {
  margin: 4px 0 10px; padding: 10px 12px; border-radius: 12px;
  background: var(--brand-soft); color: var(--brand);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px; font-weight: 700; text-align: center; letter-spacing: 0.01em;
}
.tutorial-end { margin-top: 18px; padding: 12px; border-radius: 12px; background: var(--brand-soft); color: var(--brand); text-align: center; }
.report-modal .field { margin-bottom: 12px; }
.vote-body { font-size: 13.5px; line-height: 1.6; color: var(--ink-2); }
.vote-body b { color: var(--ink); }
.vote-meter { display: flex; height: 10px; border-radius: 99px; overflow: hidden; background: var(--surface-2); margin: 14px 0 6px; }
#voteYesBar { width: 0%; background: var(--danger); transition: width 0.25s; }
#voteNoBar { width: 0%; background: var(--success); transition: width 0.25s; }
.vote-counts { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; font-weight: 800; }
#voteYes { color: #dc2626; }
#voteNo { color: #059669; }

/* ---------- Toast ---------- */
.toast-stack { position: fixed; right: 16px; bottom: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; max-width: min(360px, calc(100vw - 32px)); }
.toast-item {
  padding: 11px 14px; border-radius: 13px;
  background: #0f172a; color: #fff; font-size: 12.5px; font-weight: 700;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.28);
  animation: toastIn 0.22s ease both;
}
.toast-item.ok { background: #065f46; }
.toast-item.warn { background: #991b1b; }
.toast-item.info { background: #1e3a8a; }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  .game-main { grid-template-columns: 1fr; }
  .canvas-wrap iframe { height: min(58vh, 560px); }
  .game-side { display: grid; grid-template-columns: 1fr 1fr; }
  .report-card { grid-column: 1 / -1; }
}
@media (max-width: 820px) {
  .lobby-grid { grid-template-columns: 1fr; }
  .players-card, .lobby-chat { min-height: 280px; }
  .host-settings-grid { grid-template-columns: 1fr; }
  .game-topbar { grid-template-columns: 1fr auto; }
  .word-wrap { grid-column: 1 / -1; grid-row: 2; text-align: left; }
  .timer-block { grid-row: 1; grid-column: 2; }
  .brand-sub { display: none; }
}
@media (max-width: 560px) {
  .topbar, .app { width: calc(100% - 18px); }
  .topbar { padding-top: 10px; }
  .brand-name { font-size: 12px; }
  .brand-mark { width: 38px; height: 38px; font-size: 18px; }
  .ghost-btn { padding: 8px 10px; font-size: 12px; }
  .hero-actions { grid-template-columns: 1fr; }
  .game-side { grid-template-columns: 1fr; }
  .game-topbar { grid-template-columns: 1fr; }
  .word-wrap, .timer-block { grid-column: 1; text-align: left; }
  .timer-block { grid-row: auto; }
  .score-strip { align-items: flex-start; flex-direction: column; gap: 6px; }
  .lobby-actions .primary-btn { margin-left: 0; width: 100%; }
  .lobby-actions button { flex: 1; }
  .canvas-wrap iframe { height: 48vh; min-height: 300px; }
  .modal-card { padding: 16px; }
  .result-actions { flex-direction: column; }
}
