/* Mandolord — phone-first shell. Follows the house mobile-shell pattern (shared-patterns §32):
   the document never scrolls; bars are in-flow rows; shell height = the real visual viewport. */

:root {
  --bg: #f6f1e8;
  --surface: #fffdf9;
  --surface-2: #efe8db;
  --text: #221f26;
  --muted: #6d6671;
  --line: #e2d9ca;
  --accent: #b4291f;        /* vermilion */
  --accent-ink: #fff7f3;
  --gold: #c9961a;
  --again: #c0392b;
  --hard: #c97b1a;
  --good: #2e8b57;
  --easy: #2b74c4;
  --shadow: 0 1px 2px rgba(40, 30, 20, .08), 0 8px 24px rgba(40, 30, 20, .08);
  --radius: 16px;
  --hanzi-font: "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Noto Sans SC", "Source Han Sans SC", "Microsoft YaHei", sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15131a;
    --surface: #201d27;
    --surface-2: #2a2632;
    --text: #f1ecf4;
    --muted: #a49cae;
    --line: #35303f;
    --accent: #e2574a;
    --accent-ink: #1a0b09;
    --gold: #e0b24a;
    --again: #e05a4b;
    --hard: #e39a3c;
    --good: #4cb37a;
    --easy: #5a9ae6;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html, body, #app { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
button, [role="button"] { cursor: pointer; touch-action: manipulation; }
a { color: var(--accent); }
[lang="zh-Hans"] { font-family: var(--hanzi-font); }

/* Shell: header row · scroller · (optional) action row */
.shell {
  height: var(--app-vh, 100%);
  display: flex;
  flex-direction: column;
  max-width: 640px;
  margin: 0 auto;
}
.topbar {
  flex: none;
  display: grid;
  grid-template-columns: 56px 1fr 56px;
  align-items: center;
  height: calc(52px + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.topbar h1 { margin: 0; font-size: 17px; font-weight: 650; text-align: center; letter-spacing: .01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .brand { font-size: 20px; font-weight: 800; letter-spacing: -.01em; }
.topbar .brand span { color: var(--accent); }
.iconbtn {
  width: 44px; height: 44px; margin: 0 6px; border: 0; border-radius: 12px;
  background: transparent; display: inline-grid; place-items: center; font-size: 20px;
}
.iconbtn:active { background: var(--surface-2); }
.iconbtn[disabled] { opacity: 0; pointer-events: none; }

.scroller {
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 4px 16px calc(24px + env(safe-area-inset-bottom));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
}
.actionbar {
  flex: none;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
  border-top: 1px solid var(--line);
  background: var(--bg);
}

/* Home */
.hero { display: flex; align-items: baseline; justify-content: space-between; margin: 8px 2px 14px; }
.hero .due { font-size: 14px; color: var(--muted); }
.hero .due b { color: var(--text); font-weight: 700; }
.deck {
  display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 14px 14px 16px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.deck .info { min-width: 0; text-align: left; background: none; border: 0; padding: 0; }
.deck .name { font-weight: 700; font-size: 17px; display: flex; gap: 8px; align-items: center; }
.deck .name .zh { color: var(--muted); font-weight: 500; white-space: nowrap; }
.deck .desc { color: var(--muted); font-size: 13.5px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pills { display: flex; gap: 6px; margin-top: 10px; min-height: 22px; }
.pill { font-size: 12.5px; font-weight: 650; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-variant-numeric: tabular-nums; }
.pill.new { color: var(--easy); } .pill.learn { color: var(--hard); } .pill.rev { color: var(--good); }
.pill.mode { margin-left: auto; font-weight: 500; }
.study {
  width: 84px; height: 48px; border: 0; border-radius: 14px;
  background: var(--accent); color: var(--accent-ink); font-weight: 750; font-size: 15px;
}
.study[disabled] { background: var(--surface-2); color: var(--muted); }
.study:active { filter: brightness(.92); }

/* Study card: fixed footprint so flipping never shifts the layout */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow);
  min-height: min(420px, 58vh); padding: 20px 18px; margin-top: 8px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  position: relative; width: 100%; -webkit-user-select: none; user-select: none;
}
.card .hanzi { font-family: var(--hanzi-font); font-size: clamp(40px, 13vw, 64px); line-height: 1.2; font-weight: 500; letter-spacing: .02em; word-break: keep-all; overflow-wrap: anywhere; }
.card .hanzi.long { font-size: clamp(28px, 8.5vw, 44px); }
.card .pinyin { font-size: 22px; color: var(--muted); margin-top: 8px; font-weight: 500; }
.card .english { font-size: 24px; font-weight: 600; margin-top: 6px; line-height: 1.3; }
.card .english.prompt { font-size: 30px; }
.card .note { font-size: 14px; color: var(--muted); margin-top: 14px; max-width: 34ch; }
.card .divider { width: 48px; height: 2px; background: var(--line); border-radius: 2px; margin: 18px 0 14px; }
.card .hint { position: absolute; bottom: 14px; left: 0; right: 0; font-size: 13px; color: var(--muted); }
.card .speak {
  position: absolute; top: 10px; right: 10px; width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: transparent; font-size: 20px; display: grid; place-items: center;
}
.card .speak:active { background: var(--surface-2); }
.card .tag { position: absolute; top: 16px; left: 16px; font-size: 12px; font-weight: 650; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; }
.card .tag.new { color: var(--easy); } .card .tag.learning, .card .tag.relearning { color: var(--hard); } .card .tag.review { color: var(--good); }

.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; height: 60px; }
.grades button, .flip {
  border: 0; border-radius: 14px; display: grid; place-items: center; align-content: center; gap: 1px;
  color: #fff; font-weight: 700; font-size: 15px; line-height: 1.1;
}
.grades small { font-weight: 600; font-size: 12px; opacity: .9; font-variant-numeric: tabular-nums; }
.grades .again { background: var(--again); } .grades .hard { background: var(--hard); }
.grades .good { background: var(--good); } .grades .easy { background: var(--easy); }
.grades button:active { filter: brightness(.9); }
.flip { width: 100%; height: 60px; background: var(--accent); color: var(--accent-ink); font-size: 17px; }
.progress { height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden; margin: 0 2px 10px; }
.progress i { display: block; height: 100%; background: var(--accent); border-radius: 2px; transition: width .25s ease; }

.done { text-align: center; padding: 48px 12px; }
.done .big { font-size: 56px; font-family: var(--hanzi-font); }
.done h2 { margin: 12px 0 6px; }
.done p { color: var(--muted); margin: 4px 0; }
.btn {
  display: inline-block; margin-top: 18px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 12px; padding: 12px 18px; font-weight: 650; min-height: 44px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { color: var(--again); }
.btn:active { filter: brightness(.95); }

/* Deck browse / settings */
.section { margin: 14px 2px 8px; font-size: 13px; font-weight: 700; color: var(--muted); letter-spacing: .06em; text-transform: uppercase; }
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.row { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; padding: 12px 14px; border-top: 1px solid var(--line); min-height: 56px; }
.row:first-child { border-top: 0; }
.row .zh { font-family: var(--hanzi-font); font-size: 22px; }
.row .py { color: var(--muted); font-size: 14px; margin-left: 8px; }
.row .en { font-size: 14px; color: var(--muted); margin-top: 2px; }
.row .state { font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); white-space: nowrap; text-align: right; }
.row .state.new { color: var(--easy); } .row .state.learning, .row .state.relearning { color: var(--hard); } .row .state.review { color: var(--good); }
.row .state small { display: block; font-weight: 500; text-transform: none; letter-spacing: 0; }
.row label { font-weight: 600; }
.row .help { font-size: 13px; color: var(--muted); margin-top: 2px; }
.row input[type="number"] { width: 72px; height: 40px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); font: inherit; padding: 0 10px; text-align: right; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 12px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 8px 12px; border-radius: 10px; font-weight: 650; font-size: 14px; color: var(--muted); min-height: 38px; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.switch { width: 52px; height: 32px; border-radius: 16px; border: 0; background: var(--surface-2); position: relative; }
.switch::after { content: ""; position: absolute; top: 4px; left: 4px; width: 24px; height: 24px; border-radius: 12px; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .15s ease; }
.switch.on { background: var(--good); } .switch.on::after { transform: translateX(20px); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 4px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; text-align: center; }
.stat b { display: block; font-size: 24px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.fine { color: var(--muted); font-size: 13px; margin: 10px 2px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; font-size: 14px; font-weight: 600;
  box-shadow: var(--shadow); max-width: calc(100vw - 32px); border: 0; z-index: 10;
}
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

@media (min-width: 520px) {
  .card { min-height: 420px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
