:root {
  --bg: #f7f8f9;
  --surface: #ffffff;
  --surface-2: #f0f2f4;
  --surface-3: #e8ebee;
  --line: #e5e8eb;
  --line-2: #d8dde2;
  --ink: #0f1416;
  --ink-2: #5a646b;
  --ink-3: #98a1a8;
  --accent: #0b7a5a;
  --accent-soft: #e6f4ef;
  --accent-line: #b8ded1;
  --danger: #c0392b;
  --danger-soft: #fdeceb;
  --r: 18px;
  --r-sm: 12px;
  --r-xs: 9px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

#app { max-width: 520px; margin: 0 auto; padding: 0 16px calc(32px + var(--safe-b)); }

.view { display: none; }
.view.active { display: block; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.hero { padding: calc(44px + var(--safe-t)) 2px 26px; }
.hero h1 { margin: 0; font-size: 30px; font-weight: 700; letter-spacing: -.8px; }
.hero .sub { margin: 6px 0 0; color: var(--ink-3); font-size: 14px; letter-spacing: .2px; }

.card {
  background: var(--surface);
  border-radius: var(--r);
  padding: 18px;
  margin-bottom: 12px;
}

.lbl { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); letter-spacing: .3px; }
.hint { display: block; font-size: 12.5px; color: var(--ink-3); margin: 10px 0 0; line-height: 1.55; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.hidden { display: none !important; }

input[type="text"], input[type="number"] {
  width: 100%;
  margin-top: 10px;
  padding: 15px 16px;
  font-size: 17px;
  color: var(--ink);
  background: var(--surface-2);
  border: 1.5px solid transparent;
  border-radius: var(--r-sm);
  outline: none;
  transition: border-color .15s, background .15s;
  font-family: inherit;
  appearance: none;
}
input:focus { border-color: var(--accent-line); background: #fff; }
input::placeholder { color: var(--ink-3); font-weight: 400; }

.code-input {
  text-align: center;
  letter-spacing: 9px;
  text-indent: 9px;
  font-size: 26px !important;
  font-weight: 600;
  text-transform: uppercase;
  font-family: ui-monospace, Menlo, Consolas, monospace !important;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 13px 16px;
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  background: var(--surface-2);
  border: none;
  border-radius: var(--r-sm);
  cursor: pointer;
  font-family: inherit;
  transition: transform .07s ease, background .15s, opacity .15s;
  user-select: none;
}
.btn:active { transform: scale(.965); background: var(--surface-3); }
.btn-block { width: 100%; margin-top: 12px; }
.btn-lg { padding: 19px; font-size: 17px; margin-top: 4px; }
.btn-sm { padding: 8px 13px; font-size: 13.5px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:active { background: #0a6b4f; }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-neg { background: var(--danger-soft); color: var(--danger); font-weight: 700; }
.btn-ghost { background: transparent; color: var(--ink-2); font-weight: 500; }
.btn-ghost:active { background: var(--surface-2); }
.btn:disabled { opacity: .35; pointer-events: none; }

.game-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 12px; }
.game-item {
  text-align: left;
  padding: 13px 14px;
  background: var(--surface-2);
  border: 1.5px solid transparent;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: all .15s;
}
.game-item .gi-name { display: block; font-size: 14.5px; font-weight: 600; letter-spacing: -.1px; }
.game-item .gi-desc { display: block; font-size: 11.5px; color: var(--ink-3); margin-top: 3px; line-height: 1.4; }
.game-item.sel { background: var(--accent-soft); border-color: var(--accent-line); }
.game-item.sel .gi-desc { color: var(--accent); }

.msg { min-height: 22px; margin: 10px 2px 0; font-size: 13.5px; color: var(--danger); font-weight: 500; }
.msg.ok { color: var(--accent); }

.foot-hint {
  margin: 18px 2px 0;
  font-size: 12px;
  color: var(--ink-3);
  line-height: 1.6;
}

.sep {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 2px 12px;
  color: var(--ink-3);
  font-size: 12.5px;
}
.sep::before, .sep::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line-2);
}

/* ---------- 昵称输入弹层 ---------- */
.sheet-wrap {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: none;
}
.sheet-wrap.show { display: block; }
.sheet-mask {
  position: absolute;
  inset: 0;
  background: rgba(15, 20, 22, .38);
  opacity: 0;
  transition: opacity .2s;
}
.sheet-wrap.show .sheet-mask { opacity: 1; }
.sheet {
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 100%;
  max-width: 520px;
  transform: translate(-50%, 100%);
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 24px 20px calc(22px + var(--safe-b));
  transition: transform .24s cubic-bezier(.2, .9, .3, 1);
}
.sheet-wrap.show .sheet { transform: translate(-50%, 0); }
.sheet h2 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -.3px; }
.sheet-sub { margin: 6px 0 0; font-size: 13px; color: var(--ink-3); line-height: 1.5; }
.sheet input { font-size: 19px; padding: 16px; text-align: center; font-weight: 600; }
.sheet-msg { min-height: 20px; margin: 8px 0 0; font-size: 13px; color: var(--danger); font-weight: 500; text-align: center; }
.sheet-btns { display: grid; grid-template-columns: 1fr 1.6fr; gap: 9px; margin-top: 12px; }
.sheet-btns .btn { padding: 16px; font-size: 16px; }

.help { margin-top: 8px; }
.help summary { cursor: pointer; color: var(--ink-2); font-weight: 600; font-size: 13.5px; padding: 8px 0; list-style: none; }
.help summary::-webkit-details-marker { display: none; }
.help summary::before { content: "+"; display: inline-block; width: 16px; color: var(--ink-3); }
.help[open] summary::before { content: "−"; }
.help ol { margin: 6px 0 0; padding-left: 22px; color: var(--ink-2); font-size: 13.5px; line-height: 1.7; }
.help li { margin-bottom: 4px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(14px + var(--safe-t)) 0 12px;
}
.topbar-mid { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.room-code-lg {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 33px;
  font-weight: 600;
  letter-spacing: 6px;
  text-indent: 6px;
  color: var(--ink);
}
.room-code {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 3.5px;
  text-indent: 3.5px;
  color: var(--ink);
}
.round-tag {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--ink-2);
  background: var(--surface-2);
  padding: 2px 10px;
  border-radius: 20px;
  letter-spacing: .3px;
}

.qr-card { text-align: center; padding: 22px 18px; }
.qr-card img { width: 196px; height: 196px; display: block; margin: 0 auto; border-radius: 12px; }
.qr-card .hint { margin-top: 14px; }
/* 二维码不可用时（离线/库未加载）：只展示房间码，不留破图 */
.qr-card.no-qr img { display: none !important; }
.qr-card.no-qr::before {
  content: '扫码加入不可用，请分享下方链接或直接输入房间码';
  display: block;
  font-size: 13px;
  color: var(--muted);
  padding: 18px 6px;
}
.qr-card.no-qr .hint { margin-top: 0; }

.badge {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 3px 11px;
  border-radius: 20px;
  letter-spacing: .3px;
}

.player-list { list-style: none; margin: 14px 0 0; padding: 0; }
.player-list li {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 0;
  font-size: 15px;
  font-weight: 500;
}
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--surface-2); color: var(--ink-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; flex: 0 0 auto;
}
.avatar.me { background: var(--accent); color: #fff; }

.rules { margin: 12px 0 0; padding-left: 20px; color: var(--ink-2); font-size: 13.5px; line-height: 1.7; }
.rules li { margin-bottom: 7px; }

.rank-grid { display: grid; gap: 9px; margin-top: 14px; }
.rank-card {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 15px 16px;
  background: var(--surface-2);
  border-radius: var(--r-sm);
  position: relative;
  transition: background .2s;
}
.rank-card.me {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1.5px var(--accent-line);
}
.rank-card.p1 { background: var(--ink); color: #fff; }
.rank-card.p1 .rc-sub { color: rgba(255,255,255,.65); }
.rank-card.p1 .rc-score { color: #fff; }
.rank-card.p1 .rc-badge { background: rgba(255,255,255,.16); color: #fff; }
.rc-rank {
  width: 30px; height: 30px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; flex: 0 0 auto;
  background: var(--surface); color: var(--ink-2);
}
.rank-card.p1 .rc-rank { background: var(--accent); color: #fff; }
.rc-body { flex: 1; min-width: 0; }
.rc-name {
  font-size: 15.5px; font-weight: 600; letter-spacing: -.1px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  display: flex; align-items: center; gap: 6px;
}
.rc-sub { font-size: 11.5px; color: var(--ink-3); margin-top: 2px; letter-spacing: .2px; }
.rc-badge {
  font-size: 10.5px; font-weight: 600; padding: 1px 7px; border-radius: 4px;
  background: #fff; color: var(--accent); flex: 0 0 auto;
  margin-left: 6px;
}
.rc-score {
  font-size: 27px; font-weight: 700; letter-spacing: -1px;
  font-variant-numeric: tabular-nums; flex: 0 0 auto;
}
.rc-score.neg { color: var(--danger); }
.score-bump { animation: bump .38s cubic-bezier(.2,1.4,.4,1); }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.24); } 100% { transform: scale(1); } }

.mycard {
  background: var(--ink);
  color: #fff;
  padding: 20px 18px 18px;
}
.mycard .lbl { color: rgba(255,255,255,.55); }
.mycard .hint { color: rgba(255,255,255,.45); }
.my-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.my-score {
  font-size: 104px;
  font-weight: 700;
  line-height: 1;
  margin: 14px 0 10px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -6px;
  text-indent: -6px;
  text-align: center;
  transition: color .2s;
}
.my-score.neg { color: #ff8f80; }
.my-round-tag {
  font-size: 12px; font-weight: 600;
  background: rgba(255,255,255,.12);
  color: rgba(255,255,255,.8);
  padding: 4px 11px; border-radius: 20px;
}
.mycard .quick-grid { margin-top: 18px; }
.quick-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.quick-grid .btn {
  padding: 18px 0;
  font-size: 19px;
  font-weight: 600;
  background: rgba(255,255,255,.13);
  color: #fff;
  border-radius: var(--r-xs);
  font-variant-numeric: tabular-nums;
}
.quick-grid .btn:active { background: rgba(255,255,255,.26); }

.custom-row { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; margin-top: 9px; }
.custom-row input {
  margin-top: 0;
  background: rgba(255,255,255,.13);
  color: #fff;
  font-weight: 600;
}
.custom-row input::placeholder { color: rgba(255,255,255,.4); }
.custom-row input:focus { background: rgba(255,255,255,.2); border-color: rgba(255,255,255,.35); }
.custom-row .btn { background: rgba(255,255,255,.16); color: #fff; }
.custom-row .btn-primary { background: var(--accent); }
.neg-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 9px; }
.neg-row .btn {
  background: rgba(255,120,100,.16);
  color: #ff9d8d;
  font-weight: 600;
  padding: 13px 0;
  font-variant-numeric: tabular-nums;
}
.neg-row .btn:active { background: rgba(255,120,100,.3); }

.others { margin-top: 12px; }
.other-row {
  display: flex; align-items: center; gap: 9px;
  padding: 10px 0; font-size: 14.5px; font-weight: 500;
}
.other-row .on { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.other-row .os {
  font-weight: 700; font-variant-numeric: tabular-nums;
  min-width: 44px; text-align: right; font-size: 16px;
}
.other-row .btn { padding: 7px 12px; font-size: 13.5px; font-weight: 600; }

.history { list-style: none; margin: 12px 0 0; padding: 0; max-height: 240px; overflow-y: auto; }
.history li {
  font-size: 13px; color: var(--ink-2); padding: 8px 0;
  display: flex; justify-content: space-between; gap: 10px; align-items: center;
}
.history .h-val { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--ink); }
.history .h-up { color: var(--accent); }
.history .h-down { color: var(--danger); }

.host-panel { background: var(--surface); }
.host-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.host-grid .btn { padding: 14px 10px; }

.summary-line {
  cursor: pointer; list-style: none;
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; font-weight: 600; color: var(--ink-2);
}
.summary-line::-webkit-details-marker { display: none; }
.summary-line::after { content: "›"; font-size: 18px; color: var(--ink-3); transform: rotate(90deg); transition: transform .2s; }
details[open] > .summary-line::after { transform: rotate(-90deg); }

.finish-banner {
  background: var(--ink);
  color: #fff;
  border-radius: var(--r);
  padding: 20px 18px;
  margin-bottom: 12px;
  text-align: center;
}
.fb-title { font-size: 12px; font-weight: 600; color: rgba(255,255,255,.55); letter-spacing: 1.5px; }
.fb-name { font-size: 26px; font-weight: 700; margin-top: 6px; letter-spacing: -.5px; }
.fb-score { font-size: 15px; color: rgba(255,255,255,.6); margin-top: 2px; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed;
  left: 50%; bottom: calc(34px + var(--safe-b));
  transform: translate(-50%, 16px);
  background: rgba(15, 20, 22, .94);
  color: #fff;
  font-size: 14px; font-weight: 500;
  padding: 12px 20px;
  border-radius: 24px;
  opacity: 0; pointer-events: none;
  transition: opacity .22s, transform .22s;
  z-index: 99; max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 380px) {
  .my-score { font-size: 78px; letter-spacing: -4px; }
  .quick-grid .btn { padding: 15px 0; font-size: 17px; }
  .game-grid { grid-template-columns: 1fr; }
  .rc-score { font-size: 23px; }
}
