/* ===== Theme variables ===== */
:root {
  --scale: 1;
  --bg: #FFF6EE;
  --page: #F3E6DA;
  --surface: #FFFFFF;
  --surface-2: #FBEFE6;
  --text: #2B2230;
  --muted: #7A6E7E;
  --line: #EADBD0;
  --accent: #FF7A50;
  --accent-2: #FFB547;
  --accent-text: #FFFFFF;
  --ok: #24A866;
  --bad: #E5484D;
  --shadow: 0 6px 18px rgba(80, 40, 20, .10);
  --radius: 18px;
  --bw: 2px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #1B1720; --page: #0F0D12; --surface: #26212D; --surface-2: #2E2836;
    --text: #F5EEF7; --muted: #A99EAE; --line: #3A3343;
    --accent: #FF8A5C; --accent-2: #FFB547; --accent-text: #1B1720;
    --ok: #3CCB7F; --bad: #FF6B6F; --shadow: 0 6px 18px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1B1720; --page: #0F0D12; --surface: #26212D; --surface-2: #2E2836;
  --text: #F5EEF7; --muted: #A99EAE; --line: #3A3343;
  --accent: #FF8A5C; --accent-2: #FFB547; --accent-text: #1B1720;
  --ok: #3CCB7F; --bad: #FF6B6F; --shadow: 0 6px 18px rgba(0,0,0,.35);
  color-scheme: dark;
}
:root[data-theme="contrast"] {
  --bg: #000000; --page: #000000; --surface: #000000; --surface-2: #111111;
  --text: #FFFFFF; --muted: #FFFFFF; --line: #FFFFFF;
  --accent: #FFE600; --accent-2: #FFE600; --accent-text: #000000;
  --ok: #00FF7F; --bad: #FF4040; --shadow: none; --bw: 3px;
  color-scheme: dark;
}

/* ===== Base ===== */
* { box-sizing: border-box; }
html { font-size: calc(16px * var(--scale)); -webkit-text-size-adjust: 100%; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--page);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans CJK SC", "Noto Sans Devanagari", sans-serif;
  display: flex; align-items: center; justify-content: center;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .9rem; }
.center { text-align: center; }

/* ===== App frame: phone-shaped on desktop ===== */
.app {
  position: relative;
  width: 100%;
  max-width: 460px;
  height: 100dvh;
  background: var(--bg);
  overflow: hidden;
  container-type: inline-size;
}
@media (min-width: 520px) and (min-height: 640px) {
  .app {
    height: min(100dvh - 48px, 900px);
    border-radius: 32px;
    box-shadow: 0 20px 60px rgba(0,0,0,.18);
  }
  :root[data-theme="contrast"] .app { outline: 3px solid #fff; }
}

.screen {
  position: absolute; inset: 0;
  display: none; flex-direction: column;
  padding: max(12px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom));
  overflow-y: auto;
}
.screen.active { display: flex; animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 52px; gap: 8px; }
.topbar h2 { margin: 0; font-size: 1.4rem; }
.spacer { flex: 1; }
.icon-btn {
  width: 52px; height: 52px; border-radius: 50%;
  border: none; background: transparent; color: var(--text);
  display: grid; place-items: center; flex: none;
}
.icon-btn svg { width: calc(28px * var(--scale)); height: calc(28px * var(--scale)); max-width: 44px; max-height: 44px; }
.icon-btn:active { background: var(--surface-2); }

/* ===== Buttons ===== */
.btn-primary, .btn-ghost, .btn-danger {
  border-radius: var(--radius); padding: .8rem 1.2rem; font-weight: 700;
  border: var(--bw) solid transparent; transition: transform .08s ease;
}
.btn-primary { background: linear-gradient(135deg, var(--accent-2), var(--accent)); color: var(--accent-text); box-shadow: var(--shadow); }
:root[data-theme="contrast"] .btn-primary { background: var(--accent); }
.btn-ghost { background: var(--surface); border-color: var(--line); }
.btn-danger { background: transparent; border-color: var(--bad); color: var(--bad); width: 100%; margin-top: .6rem; }
.btn-big { font-size: min(1.6rem, 9cqi); padding: 1.1rem 1.4rem; width: 100%; }
.btn-primary:active, .btn-ghost:active, .btn-danger:active { transform: scale(.97); }

/* ===== Home ===== */
.hero { text-align: center; margin-top: 4px; }
.logo { width: calc(80px * var(--scale)); height: auto; max-width: 110px; border-radius: 24%; box-shadow: var(--shadow); }
.hero h1 { font-size: min(1.8rem, 8.5cqi); margin: .6rem 0 .2rem; }
.tip { margin: 0 auto; max-width: 30ch; color: var(--muted); font-size: min(1rem, 5cqi); min-height: 2.6em; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 16px 0 12px; }
.stat {
  background: var(--surface); border: var(--bw) solid var(--line); border-radius: var(--radius);
  padding: .7rem .3rem; display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 0;
}
.stat-val { font-size: min(1.7rem, 9cqi); font-weight: 800; line-height: 1.1; }
.stat-lbl { font-size: clamp(11px, .8rem, 4cqi); color: var(--muted); text-align: center; line-height: 1.2; hyphens: auto; }

.card { background: var(--surface); border: var(--bw) solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.chart-card { flex: 1 1 auto; min-height: 150px; display: flex; flex-direction: column; }
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.chart-head h2 { font-size: min(1rem, 5cqi); margin: 0 0 6px; }
#chart { width: 100%; flex: 1; min-height: 110px; display: block; }
#noData { margin: auto; padding: 0 8px; }
#configLabel { font-size: min(.9rem, 4cqi); }

.home-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.home-actions #btnInstall { align-self: center; }

/* ===== Game ===== */
.game-top { display: flex; align-items: center; gap: 10px; min-height: 56px; }
.timer { flex: 1; display: flex; align-items: center; gap: 12px; }
.timer-bar { flex: 1; height: 14px; background: var(--surface-2); border-radius: 99px; overflow: hidden; border: var(--bw) solid var(--line); }
.timer-fill { height: 100%; width: 100%; background: linear-gradient(90deg, var(--accent-2), var(--accent)); border-radius: 99px; transform-origin: left; }
.timer-text { font-size: 1.6rem; font-weight: 800; min-width: 2.2ch; text-align: right; font-variant-numeric: tabular-nums; }

.play { flex: 1; position: relative; display: flex; flex-direction: column; justify-content: flex-end; }
/* the task block sits in the lower part, problem directly above the answers */
.task { display: flex; flex-direction: column; gap: 18px; padding-bottom: 6px; }
.problem {
  font-size: min(4.2rem, 19cqi);
  font-weight: 800; line-height: 1.1; white-space: nowrap;
  font-variant-numeric: tabular-nums; letter-spacing: .02em;
  text-align: right; padding: 0 4px;
  min-height: 1.2em;
}
.answers { display: flex; flex-direction: column; gap: 12px; width: min(100%, max(62%, 11rem)); margin-left: auto; }
.hand-left .answers { margin-left: 0; margin-right: auto; }
.hand-left .problem { text-align: left; }
.ans {
  min-height: 4.4rem;
  font-size: min(2.4rem, 13cqi); font-weight: 800; font-variant-numeric: tabular-nums;
  background: var(--surface); color: var(--text);
  border: var(--bw) solid var(--line); border-radius: 20px; box-shadow: var(--shadow);
  transition: transform .06s ease, background-color .12s ease;
}
.ans:active { transform: scale(.97); }
.ans.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.ans.bad { background: var(--bad); border-color: var(--bad); color: #fff; opacity: .55; animation: shake .3s; }
:root[data-theme="contrast"] .ans.ok, :root[data-theme="contrast"] .ans.bad { color: #000; opacity: 1; }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
.problem.pop { animation: pop .18s ease; }
@keyframes pop { from { transform: scale(.94); opacity: .4; } to { transform: none; opacity: 1; } }

.countdown {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  font-size: min(6rem, 30cqi); font-weight: 800; color: var(--accent); background: var(--bg); z-index: 2;
}
.countdown small { display: block; font-size: 1.2rem; color: var(--muted); font-weight: 600; }

/* ===== Result ===== */
.result { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 6px; }
.result-badge img { width: calc(80px * var(--scale)); max-width: 104px; height: auto; border-radius: 24%; box-shadow: var(--shadow); }
.result-badge.record img { animation: glow 1.6s ease-in-out infinite; }
@keyframes glow { 0%,100% { box-shadow: 0 0 0 0 rgba(255,181,71,.0); transform: scale(1); } 50% { box-shadow: 0 0 0 14px rgba(255,181,71,.35); transform: scale(1.05); } }
.result-title { font-size: min(1.6rem, 8cqi); margin: .6rem 0 0; }
.result-score { font-size: min(6.5rem, 32cqi); font-weight: 900; line-height: 1; color: var(--accent); font-variant-numeric: tabular-nums; }
.result-stats { width: 100%; }
.result-actions { display: flex; flex-direction: column; gap: 10px; }
.hand-right .result-actions { align-items: stretch; }

/* ===== Settings ===== */
.settings {
  position: absolute; inset: 0; z-index: 30; overflow-y: auto;
  background: var(--bg); color: var(--text); animation: fade .2s ease;
}
.settings-inner { padding: max(12px, env(safe-area-inset-top)) 18px max(24px, env(safe-area-inset-bottom)); }
.set-group { background: var(--surface); border: var(--bw) solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 12px 0; }
.set-label { display: block; font-weight: 700; margin-bottom: 8px; }
.select { width: 100%; padding: .7rem; border-radius: 12px; border: var(--bw) solid var(--line); background: var(--surface-2); font-size: 1.1rem; }
.seg { display: flex; gap: 6px; }
.seg-wrap { flex-wrap: wrap; }
.seg-col { flex-direction: column; }
.seg button {
  flex: 1 1 0; min-height: 48px; padding: .5rem .4rem; border-radius: 12px;
  border: var(--bw) solid var(--line); background: var(--surface-2); font-weight: 600;
}
.seg-wrap button { flex: 1 1 40%; }
.seg button[aria-checked="true"], .seg button[aria-pressed="true"] {
  background: var(--accent); color: var(--accent-text); border-color: var(--accent);
}
.seg-size button { font-weight: 800; padding: .2rem; }
.seg-ops button { font-size: 1.6rem; font-weight: 800; }
.size-preview { margin-top: 10px; text-align: center; font-size: 2.2rem; font-weight: 800; }
.switch-row { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; font-weight: 600; gap: 12px; }
.switch { appearance: none; width: 54px; height: 32px; border-radius: 99px; background: var(--line); position: relative; flex: none; transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 4px; left: 4px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(22px); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > button { flex: 1 1 40%; }
.about { margin: 16px 4px 6px; line-height: 1.45; }

/* ===== Toast & confetti ===== */
.toast {
  position: absolute; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: .7rem 1.1rem; border-radius: 14px; z-index: 50;
  box-shadow: var(--shadow); max-width: 90%; text-align: center;
}
.confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 40; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
