:root {
  color-scheme: light;
  --ink: #16211d;
  --muted: #7a8580;
  --paper: #f6f3ec;
  --board: #fbfaf6;
  --line: #d9ded8;
  --accent: #eb6d4c;
  --accent-dark: #ba4a35;
  --mint: #b5d8c5;
  --yellow: #f2cc65;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; touch-action: manipulation; }
body { touch-action: manipulation; }

.home-screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0;
  padding: 8px 24px max(44px, env(safe-area-inset-bottom));
  background: var(--paper);
}

.home-brand { width: 100%; min-height: 42dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.home-brand h1 { margin: 0; font-family: "HanziPen SC", "STKaiti", "Kaiti SC", "KaiTi", "Songti SC", serif; font-size: clamp(56px, 17vw, 150px); font-style: italic; font-weight: 600; line-height: .9; letter-spacing: 0; text-shadow: 0 6px 0 rgba(22,33,29,.08); }

.home-profile { display: flex; align-items: center; gap: 9px; width: min(86vw, 380px); min-height: 42px; margin-bottom: 14px; color: var(--muted); }
.home-profile-label { font-size: 11px; letter-spacing: .08em; }
.home-profile strong { min-width: 0; overflow: hidden; flex: 1; color: var(--ink); font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.nickname-button { flex: 0 0 auto; padding: 7px 11px; border: 1px solid var(--line); border-radius: 7px; background: rgba(255,255,255,.55); color: var(--ink); font-size: 12px; font-weight: 700; cursor: pointer; }
.nickname-button:hover { background: #fff; border-color: #afbcb2; }

.start-button {
  width: min(86vw, 380px);
  min-height: 124px;
  border: 0;
  border-radius: 8px;
  background: #d94836;
  color: #fffdf8;
  box-shadow: 0 22px 0 #a93025, 0 30px 42px rgba(169, 48, 37, .22);
  font-size: clamp(28px, 9vw, 48px);
  font-weight: 900;
  letter-spacing: .02em;
  cursor: pointer;
  transition: transform .16s ease, box-shadow .16s ease, background .16s ease;
}

.start-button:hover { background: #e15340; }
.start-button:active { transform: translateY(9px); box-shadow: 0 13px 0 #a93025, 0 22px 32px rgba(169, 48, 37, .2); }

.home-leaderboard-button { width: min(86vw, 380px); min-height: 46px; margin-top: 22px; border: 1px solid var(--line); border-radius: 8px; background: rgba(255,255,255,.55); color: var(--ink); font-weight: 800; cursor: pointer; }
.home-leaderboard-button:hover { background: #fff; border-color: #afbcb2; }
.taibang-button { width: min(86vw, 380px); min-height: 44px; margin-top: 26px; border: 1px solid #d8b85a; border-radius: 8px; background: #f2cc65; color: #4b3a12; font-weight: 900; cursor: pointer; box-shadow: 0 5px 0 #c6a33e; }
.taibang-button:hover { background: #f6d676; }
.taibang-button:active { transform: translateY(3px); box-shadow: 0 2px 0 #c6a33e; }

.chain-picker {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(22, 33, 29, .34);
  backdrop-filter: blur(6px);
}

.picker-panel {
  width: min(100%, 430px);
  padding: 25px 20px 20px;
  border: 1px solid rgba(217, 222, 216, .9);
  border-radius: 10px;
  background: #fffdf8;
  box-shadow: 0 26px 72px rgba(23, 31, 27, .2);
}

.picker-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; padding: 3px 4px 17px; }
.picker-heading h2 { margin-top: 7px; font-size: 30px; line-height: 1; letter-spacing: -.045em; }

.text-icon-button { flex: 0 0 auto; min-height: 34px; padding: 0 5px; border: 0; background: transparent; color: var(--muted); font-size: 13px; font-weight: 800; cursor: pointer; }
.text-icon-button:hover { color: var(--ink); }

.chain-options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.chain-option {
  min-height: 106px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f7f6f0;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  transition: transform .16s ease, border-color .16s ease, background .16s ease;
}

.chain-option span { font-size: clamp(30px, 7vw, 42px); font-weight: 900; line-height: 1; letter-spacing: -.04em; white-space: nowrap; }
.chain-option:hover { transform: translateY(-2px); background: #fff; border-color: #afbcb2; }
.chain-option:active { transform: translateY(0); }
.chain-cx { border-top: 4px solid #e77959; }
.chain-724 { border-top: 4px solid #e0ad50; }
.chain-726 { border-top: 4px solid #6ba9b7; }
.chain-mix { border-top: 4px solid #9b82b9; }
.chain-community { border-top: 4px solid #d95742; grid-column: 1 / -1; }

.community-screen { position: fixed; inset: 0; z-index: 21; display: grid; place-items: center; padding: 20px; background: rgba(22,33,29,.34); backdrop-filter: blur(6px); }
.community-panel { width: min(100%, 500px); max-height: min(720px, calc(100dvh - 40px)); overflow: auto; padding: 25px 20px 20px; border: 1px solid rgba(217,222,216,.9); border-radius: 10px; background: #fffdf8; box-shadow: 0 26px 72px rgba(23,31,27,.2); }
.community-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.community-heading h2 { margin-top: 7px; font-size: 30px; line-height: 1; }
.community-upload-button { width: 100%; min-height: 48px; margin-top: 22px; border: 0; border-radius: 8px; background: var(--ink); color: #fff; font-weight: 800; cursor: pointer; }
.community-upload-button span { margin-left: 7px; color: var(--yellow); font-size: 18px; }
.community-list { display: grid; gap: 8px; margin-top: 14px; }
.community-empty { padding: 28px 6px; color: var(--muted); font-size: 14px; text-align: center; }
.community-row { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; min-height: 62px; padding: 9px 11px; border: 1px solid #e1e5df; border-radius: 7px; background: #f7f6f0; }
.community-row-main { min-width: 0; }
.community-row-name { overflow: hidden; color: var(--ink); font-size: 15px; font-weight: 800; text-overflow: ellipsis; white-space: nowrap; }
.community-row-meta { margin-top: 5px; color: var(--muted); font-size: 11px; }
.community-row-actions { display: flex; gap: 6px; }
.community-row-actions button { min-height: 32px; padding: 0 9px; border: 1px solid var(--line); border-radius: 6px; background: #fffdf8; color: var(--ink); font-size: 12px; font-weight: 800; cursor: pointer; }
.community-row-actions .community-delete-button { color: var(--accent-dark); }
.community-editor-modal { width: min(100%, 390px); padding: 27px 22px 22px; border-radius: 12px; background: #fffdf8; box-shadow: 0 24px 70px rgba(23,31,27,.2); }
.community-editor-modal h2 { margin-top: 8px; font-size: 30px; line-height: 1; }
.community-name-input { width: 100%; min-height: 50px; margin-top: 8px; padding: 0 14px; border: 1px solid var(--line); border-radius: 8px; outline: 0; background: #f7f6f0; color: var(--ink); font: inherit; font-size: 18px; }
.community-name-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(235,109,76,.14); }
.community-file-button { margin-top: 8px; }
.community-file-note { min-height: 19px; margin-top: 8px; color: var(--muted); font-size: 12px; line-height: 1.45; }

.nickname-modal { width: min(100%, 390px); padding: 27px 22px 22px; border-radius: 12px; background: #fffdf8; box-shadow: 0 24px 70px rgba(23,31,27,.2); }
.nickname-modal h2 { margin-top: 8px; font-size: 34px; line-height: 1; letter-spacing: 0; }
.nickname-label { display: block; margin-top: 23px; color: var(--muted); font-size: 12px; font-weight: 800; letter-spacing: .08em; }
.nickname-modal input { width: 100%; min-height: 50px; margin-top: 8px; padding: 0 14px; border: 1px solid var(--line); border-radius: 8px; outline: 0; background: #f7f6f0; color: var(--ink); font: inherit; font-size: 18px; }
.nickname-modal input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(235,109,76,.14); }
.nickname-modal input::placeholder { color: #a1aaa4; }
.nickname-error { margin-top: 8px; color: var(--accent-dark); font-size: 12px; }
.nickname-actions { display: grid; grid-template-columns: 1fr 1.25fr; gap: 10px; margin-top: 18px; }
.nickname-actions .secondary-button, .nickname-actions .primary-button { margin-top: 0; }
.nickname-actions .primary-button:disabled { cursor: wait; opacity: .6; }

.app-shell {
  width: min(100%, 540px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(22px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
}

.topbar, .stats-row, .footer-row { flex: 0 0 auto; }

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 2px 1px 18px;
}

.header-actions { display: flex; gap: 8px; }

.eyebrow, .stat-label, .footer-note {
  margin: 0;
  font-size: 10px;
  letter-spacing: .14em;
  font-weight: 800;
  color: var(--muted);
}

h1, h2, p { margin: 0; }

h1 {
  margin-top: 5px;
  font-size: clamp(24px, 7vw, 34px);
  line-height: 1;
  letter-spacing: -.035em;
}

.icon-button {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgba(255,255,255,.55);
  color: var(--ink);
  font-size: 23px;
  line-height: 1;
  cursor: pointer;
  transition: transform .18s ease, background .18s ease;
}

.icon-button:active { transform: rotate(-20deg) scale(.94); }
.icon-button:hover { background: #fff; }

.stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 9px;
  align-items: stretch;
  margin-bottom: 12px;
}

.stat-block, .next-preview {
  min-height: 64px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.stat-block strong {
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
  font-size: 27px;
  line-height: 1;
  letter-spacing: -.04em;
}

.best-stat strong { color: var(--accent-dark); }

.next-preview {
  min-width: 79px;
  padding-left: 12px;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 7px;
}

.next-preview .stat-label { align-self: flex-start; padding-top: 14px; }
.next-ball { width: 35px; height: 35px; margin-right: 8px; border-radius: 50%; overflow: hidden; background: #eef0eb; border: 2px solid #fff; box-shadow: 0 2px 8px rgba(25,40,32,.14); }
.next-ball img { width: 100%; height: 100%; object-fit: cover; display: block; }

.game-frame {
  position: relative;
  width: 100%;
  aspect-ratio: .69;
  min-height: 430px;
  max-height: min(620px, calc(100dvh - 210px));
  overflow: hidden;
  border: 1px solid #dfe4de;
  border-radius: 12px;
  background: var(--board);
  box-shadow: 0 18px 45px rgba(53, 63, 53, .08);
  touch-action: none;
}

#gameCanvas { width: 100%; height: 100%; display: block; touch-action: none; }

.danger-line {
  position: absolute;
  left: 0;
  right: 0;
  top: 16%;
  border-top: 1px dashed rgba(235,109,76,.42);
  pointer-events: none;
}

.danger-line::after {
  content: "";
  position: absolute;
  right: 12px;
  top: -7px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
}

.drop-guide {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: linear-gradient(to bottom, rgba(235,109,76,.38), rgba(235,109,76,0));
  opacity: .45;
  pointer-events: none;
}

.toast {
  position: absolute;
  top: 17px;
  left: 50%;
  transform: translate(-50%, -8px);
  padding: 7px 12px;
  border-radius: 999px;
  background: rgba(22,33,29,.9);
  color: #fff;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  white-space: nowrap;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

.footer-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 1px 0;
}

.meter { height: 4px; flex: 1; overflow: hidden; border-radius: 99px; background: #e2e5df; }
.meter span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--accent); transition: width .25s ease; }
.footer-note { flex: 0 0 auto; font-size: 9px; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(22,33,29,.32);
  backdrop-filter: blur(5px);
}

.modal-backdrop[hidden] { display: none; }
#communityEditor { z-index: 30; }
.taibang-backdrop { z-index: 35; }

.taibang-modal { width: min(100%, 430px); max-height: min(720px, calc(100dvh - 40px)); overflow: auto; padding: 22px 20px 20px; border-radius: 12px; background: #fffdf8; box-shadow: 0 24px 70px rgba(23,31,27,.2); }
.taibang-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.taibang-heading h2 { margin-top: 7px; font-size: 34px; line-height: 1; letter-spacing: -.05em; }
.taibang-image { display: block; width: 100%; max-height: min(66dvh, 520px); margin-top: 18px; border-radius: 8px; object-fit: contain; background: #f7f6f0; }

.result-modal {
  width: min(100%, 360px);
  padding: 29px 25px 24px;
  border-radius: 12px;
  background: #fffdf8;
  box-shadow: 0 24px 70px rgba(23,31,27,.2);
}

.result-modal h2 { margin-top: 8px; font-size: 34px; letter-spacing: -.05em; }
.result-score { display: flex; align-items: baseline; justify-content: space-between; margin-top: 28px; padding-bottom: 14px; border-bottom: 1px solid var(--line); color: var(--muted); font-size: 13px; }
.result-score strong { color: var(--accent-dark); font-size: 40px; line-height: 1; letter-spacing: -.06em; }
.result-caption { min-height: 42px; margin-top: 15px; color: #5d6862; font-size: 14px; line-height: 1.55; }
.secondary-button { width: 100%; min-height: 42px; margin-top: 17px; border: 1px solid var(--line); border-radius: 8px; background: transparent; color: var(--ink); font-weight: 700; cursor: pointer; }
.primary-button { width: 100%; min-height: 48px; margin-top: 18px; padding: 0 17px; border: 0; border-radius: 8px; background: var(--ink); color: #fff; font-weight: 700; cursor: pointer; }
.primary-button span { margin-left: 7px; color: var(--yellow); }
.primary-button:active { transform: translateY(1px); }

.leaderboard-modal { width: min(100%, 390px); max-height: min(640px, calc(100dvh - 40px)); overflow: auto; padding: 24px 20px 20px; border-radius: 12px; background: #fffdf8; box-shadow: 0 24px 70px rgba(23,31,27,.2); }
.leaderboard-heading { display: flex; align-items: flex-start; justify-content: space-between; }
.leaderboard-heading h2 { margin-top: 7px; font-size: 34px; line-height: 1; letter-spacing: -.05em; }
.leaderboard-chain { margin: 18px 0 9px; color: var(--muted); font-size: 12px; }
.leaderboard-tabs { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; margin-bottom: 11px; }
.leaderboard-tab { min-height: 34px; overflow: hidden; padding: 0 4px; border: 1px solid var(--line); border-radius: 7px; background: #f7f6f0; color: var(--muted); font-size: 11px; font-weight: 800; white-space: nowrap; cursor: pointer; }
.leaderboard-tab[aria-selected="true"] { border-color: var(--ink); background: var(--ink); color: #fff; }
.community-leaderboard-picker { display: grid; gap: 6px; margin: 2px 0 12px; }
.community-leaderboard-picker label { color: var(--muted); font-size: 11px; font-weight: 800; }
.community-leaderboard-picker select { width: 100%; min-height: 38px; padding: 0 10px; border: 1px solid var(--line); border-radius: 7px; background: #f7f6f0; color: var(--ink); font: inherit; font-size: 13px; }
.leaderboard-list { display: grid; gap: 7px; }
.leaderboard-empty { padding: 25px 5px; color: var(--muted); font-size: 14px; text-align: center; }
.leaderboard-row { display: grid; grid-template-columns: 30px 1fr auto; align-items: center; gap: 10px; min-height: 48px; padding: 7px 9px; border: 1px solid #e1e5df; border-radius: 7px; background: #f7f6f0; }
.leaderboard-rank { color: var(--accent-dark); font-size: 14px; font-weight: 900; }
.leaderboard-name { min-width: 0; overflow: hidden; color: var(--ink); font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard-score { color: var(--ink); font-size: 17px; font-variant-numeric: tabular-nums; font-weight: 900; }

@media (max-height: 700px) and (min-width: 420px) {
  .app-shell { padding-top: 16px; }
  .topbar { padding-bottom: 12px; }
  .game-frame { max-height: calc(100dvh - 175px); }
}
