:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #000000;
  --secondary: #8e8e93;
  --separator: #d1d1d6;
  --accent: #007aff;
  --red: #ff3b30;
  --green: #34c759;
  --gold: rgba(255, 204, 0, 0.3);
  --overlay: rgba(0, 0, 0, 0.4);
  --locked: rgba(142, 142, 147, 0.18);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #ffffff;
    --secondary: #8e8e93;
    --separator: #38383a;
    --accent: #0a84ff;
    --red: #ff453a;
    --green: #30d158;
    --gold: rgba(255, 214, 10, 0.3);
    --locked: rgba(142, 142, 147, 0.22);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}
.page {
  display: none;
  flex-direction: column;
  max-width: 640px;
  margin: 0 auto;
  min-height: 100%;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom) + var(--keyboard-height, 0px));
}
.page.active { display: flex; }
.hidden { display: none !important; }

.navbar {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding-top: env(safe-area-inset-top);
  background: var(--bg);
  z-index: 1;
}
.navbar h1 { flex: 1; margin: 0; font-size: 17px; font-weight: 600; text-align: center; white-space: nowrap; }
.nav-btn {
  min-width: 72px;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 17px;
  padding: 8px 0;
  cursor: pointer;
  white-space: nowrap;
}
.navbar > .nav-btn:first-child { text-align: left; }
.navbar > .nav-btn:last-child, .nav-right { text-align: right; justify-content: flex-end; }
.nav-right { display: flex; gap: 4px; min-width: 72px; }
.nav-right .nav-btn { min-width: 0; padding: 8px 4px; }
.nav-btn:disabled { color: var(--secondary); opacity: 0.5; cursor: default; }

.btn {
  border: none;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 600;
  padding: 14px;
  cursor: pointer;
  width: 100%;
}
.btn:active { opacity: 0.7; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-filled { background: var(--accent); color: #fff; }
.btn-tinted { background: rgba(0, 122, 255, 0.15); color: var(--accent); }
.btn-red { background: rgba(255, 59, 48, 0.15); color: var(--red); }
.btn-tinted { background: color-mix(in srgb, var(--accent) 15%, transparent); }
.btn-red { background: color-mix(in srgb, var(--red) 15%, transparent); }
.btn + .btn { margin-top: 12px; }

.section-header { margin: 20px 4px 8px; font-size: 13px; color: var(--secondary); }
.list { background: var(--card); border-radius: 12px; overflow: hidden; }
.list-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  min-height: 48px;
  border-bottom: 0.5px solid var(--separator);
}
.list-row:last-child { border-bottom: none; }
.list-row .name { flex: 1; min-width: 0; word-break: break-all; }
.list-row .value { color: var(--secondary); font-variant-numeric: tabular-nums; }
.list-row .value.big { color: var(--text); font-size: 20px; font-weight: 600; }
.tag {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--gold);
}
.tag.me { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.spacer { flex: 1; min-height: 20px; }
.hint { margin: 12px 4px; font-size: 13px; color: var(--secondary); text-align: center; line-height: 1.5; }

/* 表单 */
.form { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; }
.field {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border: 0.5px solid var(--separator);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  font-size: 17px;
}
.brand { margin: 40px 0 24px; text-align: center; }
.brand .logo { font-size: 56px; }
.brand h2 { margin: 8px 0 0; font-size: 26px; }
.segmented {
  display: flex;
  padding: 2px;
  border-radius: 9px;
  background: var(--locked);
  margin-bottom: 8px;
}
.segmented button {
  flex: 1;
  border: none;
  background: none;
  color: var(--text);
  font-size: 15px;
  padding: 7px;
  border-radius: 7px;
  cursor: pointer;
}
.segmented button.active { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }

/* 大厅 */
.welcome-row { display: flex; align-items: center; gap: 8px; margin: 16px 4px 4px; }
.welcome { flex: 1; min-width: 0; font-size: 22px; font-weight: 700; word-break: break-all; }
.welcome-row .nav-btn { min-width: 0; font-size: 15px; }
.resume {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
  padding: 14px 16px;
  border-radius: 12px;
  background: var(--gold);
  cursor: pointer;
}
.resume .name { flex: 1; font-weight: 600; }
.entries { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 16px; }
.entry {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  aspect-ratio: 1 / 0.8;
  border: none;
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
}
.entry:active { opacity: 0.7; }
.entry .icon { font-size: 40px; }
.entry.wide { grid-column: span 2; flex-direction: row; aspect-ratio: auto; min-height: 72px; }
.entry.wide .icon { font-size: 28px; }

/* 历史战绩 */
.game-row { display: block; cursor: pointer; }
.game-row .top { display: flex; align-items: baseline; gap: 8px; }
.game-row .rank { font-size: 17px; font-weight: 600; }
.game-row .when { flex: 1; font-size: 13px; color: var(--secondary); text-align: right; }
.game-row .score { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.game-row .others { margin-top: 4px; font-size: 13px; color: var(--secondary); word-break: break-all; }
.round-row { display: block; }
.round-row .head { font-size: 15px; font-weight: 600; }
.round-row .head .destructive { font-weight: 600; }
.round-row .deltas { margin-top: 4px; font-size: 13px; color: var(--secondary); font-variant-numeric: tabular-nums; word-break: break-all; }
.round-row .deltas b { color: var(--text); }

/* 房间 */
.room-code {
  margin: 20px 0 4px;
  text-align: center;
  font-size: 56px;
  font-weight: 800;
  letter-spacing: 12px;
  font-variant-numeric: tabular-nums;
}
.settings { display: flex; flex-direction: column; gap: 12px; }
.setting-row { display: flex; align-items: center; gap: 12px; }
.setting-row label { width: 60px; font-size: 17px; }
.setting-row input {
  flex: 1;
  height: 40px;
  border: 0.5px solid var(--separator);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  font-size: 17px;
  text-align: center;
}
.round-label { margin: 20px 0 8px; font-size: 17px; font-weight: 600; color: var(--secondary); }
.players { display: grid; gap: 8px; margin-bottom: 20px; }
.player {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
  padding: 10px 6px;
  border-radius: 10px;
  background: var(--card);
  text-align: center;
  word-break: break-all;
}
.player.winner { background: var(--gold); }
.player.penalty { background: rgba(142, 142, 147, 0.35); }
.player.me { box-shadow: inset 0 0 0 2px var(--accent); }
.player.away { opacity: 0.55; }
.player.clickable, .list-row.clickable { cursor: pointer; }
.player .away-tag { font-size: 12px; color: var(--secondary); }
.player .score { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.score.pos { color: var(--green); }
.score.neg { color: var(--red); }
.button-row { display: flex; gap: 16px; }
.button-row .btn + .btn { margin-top: 0; }

/* 成就 */
.progress { margin: 16px 4px 0; font-size: 15px; color: var(--secondary); }
.progress b { color: var(--text); font-size: 22px; }
.badges { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 64px;
  padding: 6px 2px;
  border: none;
  border-radius: 10px;
  background: var(--locked);
  color: var(--secondary);
  font-size: 13px;
  text-align: center;
  cursor: default;
}
.badge .icon { font-size: 20px; filter: grayscale(1); opacity: 0.5; }
.badge.unlocked { background: var(--gold); color: var(--text); font-weight: 600; cursor: pointer; }
.badge.unlocked .icon { filter: none; opacity: 1; }
.badge.unlocked.penalty { background: rgba(142, 142, 147, 0.35); }
.badge.hidden-type { grid-column: span 5; flex-direction: row; gap: 8px; min-height: 44px; }
@media (max-width: 360px) {
  .badges { grid-template-columns: repeat(3, 1fr); }
  .badge.hidden-type { grid-column: span 3; }
}

/* 弹窗 */
.overlay {
  position: fixed;
  left: 0;
  right: 0;
  /* 只覆盖键盘以上的可见区域（由 visualViewport 计算），避免输入框被键盘挡住 */
  top: var(--viewport-top, 0px);
  height: var(--viewport-height, 100%);
  background: var(--overlay);
  display: flex;
  z-index: 10;
  animation: fade 0.2s;
}
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(100%); } }
@keyframes pop { from { transform: scale(1.1); opacity: 0; } }
.alert {
  margin: auto;
  width: 280px;
  max-height: 90%;
  display: flex;
  flex-direction: column;
  border-radius: 14px;
  background: var(--card);
  overflow: hidden;
  text-align: center;
  animation: pop 0.2s;
}
.alert.wide { width: min(360px, calc(100% - 32px)); }
.alert-body { padding: 20px 16px 16px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.alert-title { font-size: 17px; font-weight: 600; }
.alert-message { margin-top: 4px; font-size: 13px; line-height: 1.4; white-space: pre-line; }
.alert-banner { margin-top: 10px; font-size: 14px; line-height: 1.55; white-space: pre-line; word-break: break-all; }
.alert-footer { margin-top: 10px; font-size: 13px; color: var(--secondary); white-space: pre-line; }
.alert input {
  width: 100%;
  margin-top: 14px;
  padding: 8px;
  border: 0.5px solid var(--separator);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
.alert-actions { display: flex; flex-shrink: 0; border-top: 0.5px solid var(--separator); }
.alert-actions button {
  flex: 1;
  border: none;
  background: none;
  padding: 12px;
  font-size: 17px;
  color: var(--accent);
  cursor: pointer;
}
.alert-actions button + button { border-left: 0.5px solid var(--separator); }
.sheet {
  margin-top: auto;
  width: 100%;
  max-width: 640px;
  max-height: 90%;
  display: flex;
  flex-direction: column;
  margin-left: auto;
  margin-right: auto;
  padding: 0 8px calc(8px + env(safe-area-inset-bottom));
  animation: rise 0.25s;
}
.sheet-group { background: var(--card); border-radius: 14px; overflow: hidden; margin-bottom: 8px; flex-shrink: 0; }
.sheet-group.scroll { flex-shrink: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheet-header { padding: 14px 16px; text-align: center; color: var(--secondary); border-bottom: 0.5px solid var(--separator); }
.sheet-header .t { font-size: 13px; font-weight: 600; }
.sheet-header .m { font-size: 13px; margin-top: 2px; }
.sheet button {
  display: block;
  width: 100%;
  border: none;
  background: none;
  padding: 16px;
  font-size: 20px;
  color: var(--accent);
  cursor: pointer;
}
.sheet-group button + button { border-top: 0.5px solid var(--separator); }
.destructive { color: var(--red) !important; }
.rules-body {
  padding: 4px 16px 16px;
  white-space: pre-line;
  font-size: 15px;
  line-height: 1.55;
  text-align: left;
}
.bold { font-weight: 600; }

.toast {
  position: fixed;
  left: 50%;
  top: calc(60px + env(safe-area-inset-top));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.78);
  color: #fff;
  font-size: 14px;
  z-index: 20;
  pointer-events: none;
  animation: fade 0.2s;
}
