:root {
  --paper: #f3eddf;
  --paper-deep: #e7ddca;
  --ink: #173b36;
  --ink-soft: #4b625d;
  --red: #df553f;
  --red-dark: #be3e2d;
  --jade: #b8d1bd;
  --white: #fffdf7;
  --line: rgba(23, 59, 54, 0.14);
  --shadow: 0 26px 60px rgba(37, 45, 34, 0.16), 0 5px 14px rgba(37, 45, 34, 0.08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; overscroll-behavior: none; }
body {
  min-height: 100dvh;
  background: #cfc9bd;
  color: var(--ink);
  font-family: "Manrope", system-ui, sans-serif;
}
button { font: inherit; color: inherit; touch-action: manipulation; }
button, img { -webkit-user-drag: none; }
button { user-select: none; }

.app-shell {
  position: relative;
  width: min(100%, 460px);
  min-height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  background:
    radial-gradient(circle at 86% 4%, rgba(223, 85, 63, .11), transparent 30%),
    linear-gradient(155deg, #f8f3e9 0%, var(--paper) 65%, #eee5d5 100%);
}

[hidden] { display: none !important; }
.menu-screen {
  height: 100dvh;
  overflow-y: auto;
  padding: max(18px, env(safe-area-inset-top)) 20px max(28px, env(safe-area-inset-bottom));
  overscroll-behavior: contain;
}
.menu-header, .menu-player, .random-panel__top, .section-heading, .all-cards-button, .all-cards-count {
  display: flex;
  align-items: center;
}
.menu-header { justify-content: space-between; }
.brand-mark {
  display: grid;
  width: 45px;
  height: 45px;
  place-items: center;
  border-radius: 50% 50% 50% 18%;
  background: var(--red);
  color: white;
  font-family: "Noto Serif SC", serif;
  font-size: 24px;
  font-weight: 700;
  transform: rotate(-4deg);
}
.menu-player { gap: 7px; font-size: 12px; font-weight: 800; }
.menu-player .avatar { border-color: rgba(255,255,255,.65); }
.menu-intro { margin: clamp(28px, 6dvh, 54px) 2px 24px; }
.menu-intro h1 { max-width: 340px; font-size: clamp(36px, 10vw, 48px); line-height: 1.02; }
.menu-intro > p:last-child { max-width: 330px; margin: 13px 0 0; color: var(--ink-soft); font-size: 14px; line-height: 1.55; }
.random-panel {
  position: relative;
  overflow: hidden;
  padding: 20px;
  border-radius: 26px 26px 52px 26px;
  background: var(--ink);
  color: white;
  box-shadow: 0 18px 44px rgba(23,59,54,.16);
}
.random-panel::after {
  content: "";
  position: absolute;
  right: -42px;
  bottom: -65px;
  width: 150px;
  height: 150px;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 50%;
  box-shadow: 0 0 0 28px rgba(255,255,255,.025), 0 0 0 56px rgba(255,255,255,.018);
}
.random-panel__top { position: relative; z-index: 1; justify-content: space-between; }
.menu-kicker { color: #e7a89b; font-size: 9px; font-weight: 800; letter-spacing: .15em; }
.random-panel h2 { margin-top: 3px; color: white; font-size: 24px; }
.random-glyph { font-family: "Noto Serif SC", serif; font-size: 50px; line-height: 1; opacity: .2; }
.deck-size { position: relative; z-index: 1; display: flex; gap: 8px; margin: 20px 0 12px; }
.deck-size button {
  min-width: 58px;
  min-height: 44px;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 13px;
  background: rgba(255,255,255,.07);
  color: white;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.deck-size button.is-selected { border-color: #f2b0a2; background: var(--red); }
.menu-primary {
  position: relative;
  z-index: 1;
  display: flex;
  width: 100%;
  min-height: 52px;
  align-items: center;
  justify-content: space-between;
  padding: 0 17px;
  border: 0;
  border-radius: 15px;
  background: var(--white);
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.menu-primary span { font-size: 20px; }
.topics-section { margin-top: 28px; }
.section-heading { justify-content: space-between; margin: 0 2px 13px; }
.section-heading h2 { font-size: 21px; }
.section-heading span { color: var(--ink-soft); font-size: 11px; font-weight: 700; }
.topic-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; }
.topic-button {
  min-width: 0;
  min-height: 118px;
  padding: 12px 9px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: rgba(255,253,247,.67);
  text-align: left;
  cursor: pointer;
}
.topic-glyph {
  display: grid;
  width: 42px;
  height: 42px;
  margin-bottom: 10px;
  place-items: center;
  border-radius: 50% 50% 16px 50%;
  font-family: "Noto Serif SC", serif;
  font-size: 22px;
  font-weight: 700;
}
.topic-glyph--coral { background: #f2c7bb; color: #9f3e2f; }
.topic-glyph--gold { background: #efdbac; color: #805b17; }
.topic-glyph--green { background: #c9ddca; color: #2f6659; }
.topic-glyph--blue { background: #c5dbe1; color: #326576; }
.topic-glyph--violet { background: #dad0e2; color: #634f75; }
.topic-glyph--ink { background: #cbd3cc; color: #294c46; }
.topic-glyph--rose { background: #e6c6cf; color: #8f4a5e; }
.topic-glyph--clay { background: #ddc9ac; color: #7a5230; }
.topic-button strong, .topic-button small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topic-button strong { font-size: 12px; }
.topic-button small { margin-top: 3px; color: var(--ink-soft); font-size: 10px; }
.all-cards-button {
  width: 100%;
  min-height: 70px;
  justify-content: space-between;
  margin-top: 10px;
  padding: 0 17px;
  border: 0;
  border-radius: 18px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.all-cards-copy strong, .all-cards-copy small { display: block; }
.all-cards-copy strong { font-size: 15px; }
.all-cards-copy small { margin-top: 3px; color: var(--ink-soft); font-size: 11px; }
.all-cards-count { gap: 12px; }
.all-cards-count b { font-size: 13px; }
.all-cards-count > span { display: grid; width: 36px; height: 36px; place-items: center; border-radius: 50%; background: var(--red); color: white; font-size: 18px; }
.all-cards-section { margin-top: 10px; padding-bottom: 8px; }
.all-cards-section .all-cards-button { margin-top: 0; }

.toggle-row {
  display: flex;
  width: 100%;
  min-height: 46px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 2px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: none;
  font-size: 13px;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}
.toggle-row:last-of-type { border-bottom: 0; }
.switch { position: relative; flex: 0 0 auto; width: 46px; height: 28px; border-radius: 999px; background: rgba(23,59,54,.22); transition: background .2s ease; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s ease; }
.toggle-row[aria-checked="true"] .switch { background: var(--red); }
.toggle-row[aria-checked="true"] .switch::after { transform: translateX(18px); }
.toggle-arrow { color: var(--red-dark); font-size: 18px; font-weight: 800; }

.game-screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(16px, env(safe-area-inset-top)) 20px max(14px, env(safe-area-inset-bottom));
}

.topbar, .progress-row, .answer-buttons, .score-strip, .sheet-header {
  display: flex;
  align-items: center;
}
.topbar { justify-content: space-between; min-height: 44px; }
.topbar__left { display: flex; align-items: center; gap: 5px; }
.back-button { font-size: 25px; }
.avatar {
  display: inline-grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 2px solid var(--paper);
  border-radius: 50%;
  color: white;
  font-size: 10px;
  font-weight: 800;
}
.avatar--red { background: var(--red); }
.avatar--blue { background: #467d8b; }
.avatar--gold { background: #c5953d; }
.icon-button, .close-button {
  display: grid;
  width: 44px;
  height: 44px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}
.icon-button svg { width: 23px; height: 23px; }

.eyebrow { margin: 0 0 4px; color: var(--red-dark); font-size: 11px; font-weight: 800; letter-spacing: .14em; }
h1, h2 { margin: 0; letter-spacing: -.04em; }
h1 { font-size: clamp(26px, 7vw, 32px); line-height: 1.1; }
h2 { font-size: 25px; }
.timer { position: relative; display: grid; width: 52px; height: 52px; place-items: center; }
.timer svg { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.timer circle { fill: none; stroke-width: 3; }
.timer__track { stroke: rgba(23,59,54,.12); }
.timer__progress { stroke: var(--red); stroke-linecap: round; stroke-dasharray: 113.1; stroke-dashoffset: 0; transition: stroke-dashoffset .3s linear; }
.timer span { font-size: 11px; font-weight: 800; font-variant-numeric: tabular-nums; }
.timer.is-hidden { display: none; }

.progress-row { gap: 10px; margin: 14px 2px 14px; }
.progress-bar { flex: 1; height: 4px; overflow: hidden; border-radius: 99px; background: rgba(23,59,54,.11); }
.progress-bar span { display: block; width: 100%; height: 100%; border-radius: inherit; background: var(--red); transition: transform .35s ease-out; transform-origin: left; }
.progress-row > span { min-width: 40px; color: var(--ink-soft); font-size: 11px; font-weight: 800; }
.progress-row .timer { flex: 0 0 auto; width: 38px; height: 38px; }
.progress-row .timer span { font-size: 9px; }

.card-stage { position: relative; flex: 1; min-height: 0; perspective: 1300px; }
.card-stack { position: absolute; z-index: 0; inset: 0; pointer-events: none; }
.stack-card {
  position: absolute;
  inset: 0;
  display: block;
  border: 1px solid rgba(255,255,255,.78);
  border-radius: 30px 30px 68px 30px;
  background: rgba(255,253,247,.72);
  box-shadow: 0 14px 36px rgba(37,45,34,.08);
  transform-origin: 50% 85%;
  transition: opacity .28s ease, transform .4s cubic-bezier(.2,.75,.25,1);
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
}
.stack-card--3 { opacity: .38; transform: translate(4px, -17px) rotate(2.6deg) scale(.962); }
.stack-card--2 { opacity: .56; transform: translate(-5px, -11px) rotate(-1.8deg) scale(.976); }
.stack-card--1 { opacity: .82; transform: translate(3px, -5px) rotate(.8deg) scale(.99); }
.stack-card.is-hidden { opacity: 0; transform: translateY(0) scale(.94); }
.flashcard { position: absolute; z-index: 1; inset: 0; width: 100%; height: auto; min-height: 0; padding: 0; border: 0; background: none; cursor: pointer; touch-action: none; transition: transform .35s cubic-bezier(.2,.75,.25,1), opacity .35s ease; will-change: transform; }
.flashcard.is-dragging { transition: none; }
.flashcard.is-reset,
.flashcard.is-reset .flashcard__inner,
.flashcard.is-reset .flashcard__face,
.flashcard.no-flip .flashcard__inner,
.flashcard.no-flip .flashcard__face { transition: none !important; }
.swipe-cue {
  position: absolute;
  z-index: 3;
  top: 22px;
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: .02em;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s linear;
}
.swipe-cue span { font-size: 18px; }
.swipe-cue--miss { left: 18px; background: rgba(229,222,209,.94); color: #6f5d56; border: 2px solid #c8bda9; transform: rotate(-6deg); }
.swipe-cue--hit { right: 18px; background: rgba(23,59,54,.94); color: white; border: 2px solid #2f7061; transform: rotate(6deg); }
.swipe-hint { position: absolute; z-index: 4; inset: 0; display: flex; align-items: center; justify-content: space-between; padding: 0 10px; opacity: 0; transition: opacity .25s ease; pointer-events: none; }
.swipe-hint.is-visible { opacity: 1; }
.swipe-hint__side { display: flex; flex-direction: column; align-items: center; gap: 4px; max-width: 96px; color: var(--ink); }
.swipe-hint__side--miss { color: #7a655c; }
.swipe-hint__side--hit { color: var(--ink); }
.swipe-hint__arrow { font-size: 46px; font-weight: 800; line-height: 1; animation: nudge 1.1s ease-in-out infinite; }
.swipe-hint__side--miss .swipe-hint__arrow { animation-name: nudge-left; }
.swipe-hint__side--hit .swipe-hint__arrow { animation-name: nudge-right; }
.swipe-hint__label { font-size: 12px; font-weight: 800; text-align: center; }
@keyframes nudge-left { 0%, 100% { transform: translateX(0); } 50% { transform: translateX(-8px); } }
@keyframes nudge-right { 0%, 100% { transform: translateX(0); } 50% { transform: translateX(8px); } }
.flashcard__inner { position: relative; display: block; width: 100%; height: 100%; transform-style: preserve-3d; -webkit-transform-style: preserve-3d; transition: transform .62s cubic-bezier(.2,.75,.25,1); }
.flashcard.is-flipped .flashcard__inner { transform: rotateY(180deg); }
.flashcard__face {
  position: absolute;
  inset: 0;
  display: flex;
  overflow: hidden;
  border-radius: 30px 30px 68px 30px;
  background: var(--white);
  box-shadow: var(--shadow);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transition: visibility 0s .31s;
}
.flashcard__front { flex-direction: column; align-items: center; justify-content: center; padding: 28px; transform: rotateY(0deg) translateZ(1px); visibility: visible; }
.flashcard__front::before {
  content: "";
  position: absolute;
  width: 180px;
  height: 180px;
  border: 1px solid rgba(223,85,63,.13);
  border-radius: 50%;
  box-shadow: 0 0 0 38px rgba(223,85,63,.04), 0 0 0 76px rgba(223,85,63,.025);
}
.card-label { z-index: 1; color: var(--red-dark); font-size: 10px; font-weight: 800; letter-spacing: .16em; }
.hanzi { z-index: 1; display: flex; align-items: center; justify-content: center; margin: -4px 0 6px; font-family: "Noto Serif SC", serif; font-size: clamp(64px, min(34vw, 24dvh), 160px); font-weight: 600; line-height: 1; }
.hanzi[data-length="2"] { font-size: clamp(52px, min(26vw, 18dvh), 120px); }
.hanzi[data-length="3"] { font-size: clamp(40px, min(20vw, 14dvh), 92px); }
.hanzi-char { color: var(--radical-color, var(--ink)); }
.pinyin { z-index: 1; font-size: 26px; font-weight: 600; }
.flip-hint { position: absolute; bottom: 24px; color: #768680; font-size: 12px; font-weight: 600; }
.flashcard__back { flex-direction: column; transform: rotateY(180deg) translateZ(1px); visibility: hidden; }
.flashcard.is-flipped .flashcard__front { visibility: hidden; }
.flashcard.is-flipped .flashcard__back { visibility: visible; }
.association { display: block; width: 100%; height: 54%; flex: 0 0 54%; min-height: 0; object-fit: contain; object-position: center; background: #eee5d5; }
.back-copy { display: flex; flex: 1 1 auto; min-height: 0; flex-direction: column; align-items: center; justify-content: center; padding: 14px 18px; overflow: hidden; }
.translation { margin: 4px 0 2px; max-width: 100%; font-size: clamp(20px, 5.6vw, 32px); font-weight: 800; letter-spacing: -.03em; line-height: 1.12; text-align: center; overflow-wrap: anywhere; }
.back-pinyin { color: var(--ink-soft); font-size: 15px; font-weight: 600; text-align: center; }
.back-hanzi { font-family: "Noto Serif SC", serif; font-weight: 700; }

.answer-area { height: 93px; padding-top: 13px; opacity: 0; pointer-events: none; transform: translateY(8px); transition: opacity .2s ease, transform .2s ease; }
.answer-area.is-visible { opacity: 1; pointer-events: auto; transform: none; }
.answer-area p { margin: 0 0 8px; color: var(--ink-soft); text-align: center; font-size: 11px; font-weight: 700; }
.answer-buttons { gap: 10px; }
.answer-button { flex: 1; min-height: 48px; border: 0; border-radius: 16px; font-size: 14px; font-weight: 800; cursor: pointer; }
.answer-button span { margin-right: 4px; font-size: 20px; vertical-align: -1px; }
.answer-button--miss { background: #e5ded1; color: #6f5d56; }
.answer-button--hit { background: var(--ink); color: white; }

.score-strip { justify-content: center; gap: 16px; margin-top: 10px; padding-top: 12px; border-top: 1px solid var(--line); }
.score-player { display: grid; grid-template-columns: 28px auto auto; column-gap: 8px; align-items: center; }
.score-player .avatar { grid-row: 1; }
.score-player span:not(.avatar) { font-size: 12px; font-weight: 700; }
.score-player strong { font-size: 18px; line-height: 1; }

.result-screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(22px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
}
.result-heading h1 { font-size: clamp(30px, 8vw, 38px); }
.result-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 20px 0 16px; }
.result-stat { padding: 15px 17px; border: 1px solid var(--line); border-radius: 20px; background: rgba(255,253,247,.72); }
.result-stat b { display: block; font-size: 34px; line-height: 1; letter-spacing: -.04em; }
.result-stat span { display: block; margin-top: 5px; color: var(--ink-soft); font-size: 10px; font-weight: 800; letter-spacing: .1em; }
.result-stat--hit b { color: #2f6659; }
.result-stat--miss b { color: var(--red-dark); }
.result-list { flex: 1; min-height: 0; margin: 0; padding: 0; list-style: none; overflow-y: auto; overscroll-behavior: contain; border-top: 1px solid var(--line); }
.result-item { display: flex; gap: 10px; padding: 12px 4px; border-bottom: 1px solid var(--line); font-size: 14px; }
.result-mark { flex: 0 0 auto; width: 18px; font-weight: 800; text-align: center; }
.result-item.is-hit .result-mark { color: #2f6659; }
.result-item.is-miss .result-mark { color: var(--red); }
.result-word { min-width: 0; overflow-wrap: anywhere; }
.result-hanzi { font-family: "Noto Serif SC", serif; font-weight: 700; }
.result-pinyin { margin-left: 4px; color: var(--ink-soft); }
.result-translation { color: var(--ink-soft); }
.result-home { display: flex; width: 100%; min-height: 56px; align-items: center; justify-content: center; gap: 10px; margin-top: 16px; border: 0; border-radius: 18px; background: var(--ink); color: white; font-size: 15px; font-weight: 800; cursor: pointer; }
.result-home span { font-size: 20px; }

.sheet-backdrop { position: absolute; z-index: 5; inset: 0; background: rgba(20,33,30,.33); backdrop-filter: blur(3px); }
.settings-sheet {
  position: absolute;
  z-index: 6;
  right: 0;
  bottom: 0;
  left: 0;
  max-height: 92dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px 20px max(18px, env(safe-area-inset-bottom));
  border-radius: 30px 30px 0 0;
  background: #fffdf7;
  box-shadow: 0 -20px 60px rgba(20,33,30,.18);
  transform: translateY(105%);
  transition: transform .38s cubic-bezier(.2,.8,.2,1);
}
.settings-sheet.is-open { transform: none; }
.sheet-handle { width: 38px; height: 4px; margin: 0 auto 17px; border-radius: 99px; background: #d5cdbf; }
.sheet-header { justify-content: space-between; margin-bottom: 22px; }
.close-button { background: #eee8dd; font-size: 28px; font-weight: 400; }
fieldset { margin: 0 0 20px; padding: 0; border: 0; }
legend { margin-bottom: 10px; font-size: 13px; font-weight: 800; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.segmented button { min-height: 92px; padding: 12px; border: 1px solid var(--line); border-radius: 18px; background: #f5f0e7; cursor: pointer; }
.segmented button strong { display: block; font-family: "Noto Serif SC", serif; font-size: 28px; }
.segmented button span { display: block; margin-top: 2px; font-size: 11px; font-weight: 700; }
.segmented button.is-selected { border-color: var(--red); background: #fbe9e4; box-shadow: inset 0 0 0 1px var(--red); }
.segmented--sm { grid-template-columns: 1fr 1fr; }
.segmented--sm button { min-height: 58px; }
.segmented--sm button strong { font-size: 22px; }
.timer-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.timer-options button { min-height: 44px; border: 1px solid var(--line); border-radius: 14px; background: transparent; font-size: 12px; font-weight: 700; cursor: pointer; }
.timer-options button.is-selected { border-color: var(--ink); background: var(--ink); color: white; }
.start-button { display: flex; width: 100%; min-height: 56px; align-items: center; justify-content: space-between; padding: 0 20px; border: 0; border-radius: 18px; background: var(--red); color: white; font-weight: 800; cursor: pointer; }
.start-button span { font-size: 22px; }
.toast { position: absolute; z-index: 9; bottom: 24px; left: 50%; padding: 10px 14px; border-radius: 12px; background: var(--ink); color: white; font-size: 12px; font-weight: 700; opacity: 0; transform: translate(-50%, 10px); transition: .2s ease; pointer-events: none; }
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

button:focus-visible { outline: 3px solid rgba(223,85,63,.38); outline-offset: 3px; }
button:active { transform: scale(.97); transition: transform .08s ease; }
.flashcard:active { transform: none; }

@media (max-height: 740px) {
  .menu-intro { margin-block: 20px 18px; }
  .topic-button { min-height: 105px; }
  .progress-row { margin-block: 10px; }
  .score-strip { margin-top: 4px; }
}
@media (min-width: 461px) {
  body { padding-block: 20px; }
  .app-shell { min-height: calc(100dvh - 40px); border-radius: 36px; box-shadow: 0 30px 80px rgba(35,40,35,.25); }
  .game-screen { height: calc(100dvh - 40px); }
  .result-screen { height: calc(100dvh - 40px); }
  .menu-screen { height: calc(100dvh - 40px); }
  .settings-sheet { border-radius: 30px; bottom: 10px; left: 10px; right: 10px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
@media (hover: hover) and (pointer: fine) {
  .icon-button:hover, .close-button:hover { background: rgba(23,59,54,.08); }
  .answer-button--hit:hover { background: #24564e; }
  .answer-button--miss:hover { background: #dad1c2; }
  .start-button:hover { background: var(--red-dark); }
}
