:root {
  --bg: #14122b;
  --bg-2: #1f1c3d;
  --card: #2a2650;
  --text: #f4f2ff;
  --muted: #a39fc9;
  --accent: #ffb938;
  --accent-2: #ff7a59;
  --good: #5ee0a0;
  --bad: #ff5c7a;
  --key: #332f5e;
  --key-press: #474283;
  --alt: #eadbbd;     /* 交替顏色：單數組的數字（米色） */
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", sans-serif;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

body {
  background: radial-gradient(circle at 50% 0%, var(--bg-2), var(--bg) 70%);
}

button { font: inherit; color: inherit; border: 0; cursor: pointer; }

.screen {
  display: none;
  flex-direction: column;
  align-items: center;
  min-height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 24px) 20px calc(env(safe-area-inset-bottom) + 20px);
}
.screen.active { display: flex; }

/* ---------- 主選單 ---------- */
#screen-menu { justify-content: center; text-align: center; gap: 8px; }
.logo {
  font-size: 110px;
  line-height: 1;
  font-family: Georgia, "Times New Roman", serif;
  color: var(--accent);
  text-shadow: 0 8px 30px rgba(255, 185, 56, .35);
}
h1 { margin: 8px 0 0; font-size: 34px; letter-spacing: 2px; }
.subtitle { margin: 0; color: var(--muted); }
.best-box {
  margin: 20px 0 8px;
  padding: 10px 20px;
  background: var(--card);
  border-radius: 999px;
  color: var(--muted);
}
.best-box strong { color: var(--accent); font-size: 20px; margin: 0 4px; }
.menu-buttons {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 20px;
}
.menu-btn {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px 20px;
  border-radius: 18px;
  background: var(--card);
  text-align: left;
  box-shadow: 0 4px 0 rgba(0, 0, 0, .3);
  transition: transform .05s, background .1s;
}
.menu-btn:active { transform: translateY(2px); box-shadow: none; background: var(--key-press); }
.menu-icon { font-size: 30px; }
.menu-text { display: flex; flex-direction: column; gap: 4px; }
.menu-text b { font-size: 20px; }
.menu-text small { color: var(--muted); font-size: 13px; }

/* ---------- 設定畫面 ---------- */
.setup { gap: 10px; }
.setup h2 { margin-top: 4px; }
.setup-desc { margin: 0 0 4px; color: var(--muted); font-size: 14px; text-align: center; }
.setup-actions {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: auto;
  padding-top: 12px;
}

.toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg-2);
  border-radius: 18px;
  cursor: pointer;
}
.toggle-text { color: var(--muted); font-size: 14px; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  position: relative;
  flex: none;
  width: 48px;
  height: 28px;
  border-radius: 999px;
  background: var(--key);
  transition: background .2s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--text);
  transition: transform .2s;
}
.toggle input:checked + .switch { background: var(--accent); }
.toggle input:checked + .switch::after { transform: translateX(20px); }
.toggle input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }

.stepper {
  width: 100%;
  padding: 14px;
  background: var(--bg-2);
  border-radius: 18px;
}
.stepper-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.stepper-label { color: var(--muted); font-size: 14px; }
.stepper-reset {
  padding: 3px 10px;
  border-radius: 999px;
  background: transparent;
  border: 1px solid var(--key-press);
  color: var(--muted);
  font-size: 12px;
}
.stepper-reset:active { background: var(--key-press); }
.stepper-reset:disabled { opacity: .3; cursor: default; }
.stepper-value { margin-left: auto; font-size: 30px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.stepper-value small { font-size: 14px; color: var(--muted); font-weight: 400; margin-left: 4px; }
/* 左邊減號、中間分隔線、右邊加號 */
.stepper-row { display: grid; grid-template-columns: 1fr 1fr 1px 1fr 1fr; gap: 6px; align-items: center; }
.stepper-row.cols-1 { grid-template-columns: 1fr 1px 1fr; }
.stepper-row + .stepper-row { margin-top: 6px; }
.stepper-divider { align-self: stretch; background: var(--key-press); }
.stepper-row button {
  height: 40px;
  border-radius: 10px;
  background: var(--key);
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  transition: background .1s;
}
.stepper-row button:active { background: var(--key-press); }
.stepper-row button:disabled { opacity: .3; cursor: default; }
.stepper-row .minus { color: var(--muted); }

.btn-primary {
  width: 100%;
  max-width: 320px;
  padding: 16px;
  border-radius: 16px;
  font-size: 20px;
  font-weight: 700;
  color: #2a1a00;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 6px 20px rgba(255, 122, 89, .35);
  transition: transform .08s;
}
.btn-primary:active { transform: scale(.97); }
.btn-ghost {
  width: 100%;
  max-width: 320px;
  padding: 14px;
  border-radius: 16px;
  font-size: 17px;
  background: transparent;
  border: 1px solid var(--key-press);
  color: var(--muted);
}

/* ---------- 遊戲畫面 ---------- */
.hud {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}
.hearts { display: flex; gap: 4px; font-size: 26px; }
.heart { transition: transform .3s, opacity .3s, filter .3s; }
.heart.lost { opacity: .25; filter: grayscale(1); transform: scale(.8); }
.heart.breaking { animation: heart-break .5s ease; }
@keyframes heart-break {
  0% { transform: scale(1); }
  30% { transform: scale(1.4); }
  100% { transform: scale(.8); }
}

.training-tag {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--card);
  color: var(--good);
  font-size: 14px;
}
.training-tag[hidden] { display: none; }

.score { text-align: center; line-height: 1.1; }
.score-num { font-size: 36px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.score-total { font-size: 16px; color: var(--muted); font-variant-numeric: tabular-nums; }
.position { color: var(--muted); font-size: 12px; margin-top: 2px; }

.hud-right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.btn-quit {
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--key-press);
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}
.btn-quit:active { background: var(--key-press); }
.btn-quit[hidden] { display: none; }
.clock { font-size: 18px; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }

/* 遊戲畫面固定一個螢幕高，鍵盤吃掉剩下的空間 */
#screen-game.active { height: 100dvh; min-height: 0; }

/* 數字只顯示一行：輸入位置在正中間，左邊是已輸入的，右邊是還沒輸入的 */
.digits {
  flex: none;
  width: 100%;
  margin: 12px 0 0;
  padding: 8px 14px;
  background: var(--bg-2);
  border-radius: 18px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 80px;
  line-height: 1.15;
  letter-spacing: 1px;
  transition: background .2s;
}
/* 輸入位置：正中間再往右一格（1ch = 一個數字寬，再加上 letter-spacing） */
.digits-line { display: grid; grid-template-columns: calc(50% + 1ch + 1px) 1fr; align-items: center; }
.dl-past, .dl-future {
  min-width: 0;
  display: flex;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
}
.dl-past {
  justify-content: flex-end;
  -webkit-mask-image: linear-gradient(to right, transparent, #000 28px);
  mask-image: linear-gradient(to right, transparent, #000 28px);
}
.dl-future {
  justify-content: flex-start;
  -webkit-mask-image: linear-gradient(to left, transparent, #000 28px);
  mask-image: linear-gradient(to left, transparent, #000 28px);
}
.dl-past > span, .dl-future > span { flex: none; }
.digits .int { color: var(--accent); }
.digits .ctx { color: var(--muted); opacity: .6; }
/* 每 10 位交替深淺（開關打開時 .digits 會有 .alt） */
.digits.alt .d.odd { color: var(--alt); }
.digits .d.fresh, .digits.alt .d.fresh { animation: pop .25s ease; color: var(--accent); }
.digits .d.future { color: var(--muted); animation: pop .3s ease; }
.digits.alt .d.ctx.odd { color: var(--muted); opacity: .35; }
.digits.alt .d.future.odd { color: var(--muted); opacity: .5; }
.digits .cursor {
  display: inline-block;
  width: 5px;
  height: .9em;
  margin-left: 1px;
  background: var(--accent);
  animation: blink 1s steps(1) infinite;
}
.digits .d.miss, .digits.alt .d.miss {
  color: var(--bad);
  outline: 3px solid var(--bad);
  outline-offset: -3px; /* 畫在格子裡面，才不會被上下裁掉 */
  border-radius: 12px;
}
@keyframes pop { from { transform: translateY(6px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes blink { 50% { opacity: 0; } }

.digits.wrong { background: rgba(255, 92, 122, .18); animation: shake .35s; }
@keyframes shake {
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}

/* 思考時間倒數條 */
.countdown { width: 100%; display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.countdown[hidden] { display: none; }
.countdown-bar { flex: 1; height: 8px; border-radius: 999px; background: var(--bg-2); overflow: hidden; }
.countdown-fill { height: 100%; width: 100%; border-radius: inherit; background: var(--accent); transition: width .1s linear; }
.countdown-num { min-width: 36px; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.countdown.urgent .countdown-fill { background: var(--bad); }
.countdown.urgent .countdown-num { color: var(--bad); animation: pulse .5s ease infinite alternate; }
@keyframes pulse { to { transform: scale(1.15); } }

/* 數字鍵盤：填滿剩下的高度（約畫面 2/3） */
.keypad {
  flex: 1;
  min-height: 0;
  width: 100%;
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
}
.keypad button {
  min-height: 56px;
  border-radius: 18px;
  background: var(--key);
  font-size: 36px;
  font-weight: 600;
  box-shadow: 0 4px 0 rgba(0, 0, 0, .3);
  transition: background .1s, transform .05s;
}
.keypad button:active, .keypad button.pressed { background: var(--key-press); transform: translateY(2px); box-shadow: none; }
.keypad[hidden] { display: none; }

/* ---------- 結果區（遊戲結束後取代數字鍵盤） ---------- */
.result-panel {
  flex: 1;
  min-height: 0;
  width: 100%;
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
  overflow-y: auto;
  animation: panel-in .3s ease;
}
.result-panel[hidden] { display: none; }
@keyframes panel-in { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
h2 { margin: 0; font-size: 28px; }
.result-panel.success h2 { color: var(--good); }
.result-label { color: var(--muted); }
.result-label b { color: var(--accent); font-size: 22px; margin: 0 2px; }
.new-record { font-size: 18px; color: var(--good); font-weight: 700; }
.new-record[hidden] { display: none; }

.stats { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { padding: 14px; background: var(--bg-2); border-radius: 16px; }
.stat-num { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { margin-top: 4px; color: var(--muted); font-size: 13px; }

.over-actions { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 10px; }

/* ---------- 圓周率瀏覽（分頁，一頁 500 位） ---------- */
/* 固定一個螢幕高：上方一行、數字、翻頁都在同一頁，不用捲動；左右邊距縮小讓數字更大 */
#screen-digits { padding-left: 12px; padding-right: 12px; }
#screen-digits.active { height: 100dvh; min-height: 0; }
.viewer-head { width: 100%; display: flex; align-items: center; gap: 8px; }
.btn-back {
  flex: none;
  min-width: 72px;
  height: 40px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--key-press);
  background: transparent;
  color: var(--muted);
  font-size: 15px;
}
.btn-back:active { background: var(--key-press); }
.viewer-jump { flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; }
.viewer-jump label, .viewer-jump span { flex: none; }
.viewer-jump input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 10px;
  border-radius: 12px;
  border: 1px solid var(--key-press);
  background: var(--bg-2);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* 16px 以上，iPhone 點進去才不會自動放大 */
}
.viewer-jump input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.viewer-jump button {
  flex: none;
  height: 40px;
  padding: 0 14px;
  border-radius: 12px;
  background: var(--accent);
  color: #2a1a00;
  font-weight: 700;
}

/* 數字區：每行 25 位 + 4 個組距 ≈ 16.8em，字級依寬度算到剛好放滿（最大 26px） */
.viewer-list {
  flex: 1;
  min-height: 0;
  width: 100%;
  margin-top: 10px;
  padding: 8px 10px;
  background: var(--bg-2);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  overflow-y: auto; /* 很矮的手機才會用到 */
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: min(26px, calc((min(100vw, 480px) - 46px) / 16.8));
  line-height: 1.3;
}
.viewer-row { display: flex; white-space: nowrap; }
.viewer-digits { display: flex; gap: .45em; }
.viewer-int { color: var(--accent); font-weight: 700; }
.viewer-target {
  color: #2a1a00;
  background: var(--accent);
  border-radius: 4px;
  animation: target-flash 1.2s ease 2;
}
@keyframes target-flash { 50% { background: transparent; color: var(--accent); } }

/* 翻頁：上一頁｜頁碼｜下一頁 */
.viewer-pager { width: 100%; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; margin-top: 10px; }
.viewer-info { text-align: center; color: var(--muted); font-size: 15px; line-height: 1.3; }
.viewer-info b { color: var(--accent); font-size: 20px; }
.viewer-info small { display: block; font-size: 11px; }
.btn-page {
  padding: 14px 8px;
  border-radius: 16px;
  background: var(--card);
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 3px 0 rgba(0, 0, 0, .3);
}
.btn-page:active { background: var(--key-press); transform: translateY(2px); box-shadow: none; }
.btn-page:disabled { opacity: .3; cursor: default; }

/* ---------- 確認視窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(8, 7, 20, .72);
  animation: fade-in .15s ease;
}
.modal[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 340px;
  padding: 24px 20px 20px;
  border-radius: 22px;
  background: var(--card);
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .5);
  animation: pop-in .18s ease;
}
.modal-card h3 { margin: 0 0 8px; font-size: 21px; }
.modal-card p { margin: 0 0 20px; color: var(--muted); font-size: 15px; line-height: 1.5; }
.modal-card p b { color: var(--accent); }
.modal-actions { display: flex; flex-direction: column; align-items: center; gap: 10px; }
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { transform: scale(.92); opacity: 0; } }
