:root {
  color-scheme: light dark;
  --primary: #b8452f; --on-primary: #fff; --primary-container: #fadcd2; --on-primary-container: #3f0e04;
  --secondary: #2b6c66; --secondary-container: #d2eae5; --on-secondary-container: #0a2e2a;
  --tertiary: #8a6418;
  --bg: #f7f2e9; --on-bg: #211e1a; --surface: #fffcf6; --surface-high: #f2ece0;
  --muted: #6a6256; --outline: #a89d8b; --outline-soft: #e0d7c6; --error: #b3261e;
  --paper: #fffefa; --paper-ink: #1f1c18; --paper-muted: #7a7164;
  --again: #a5301f; --again-bg: #fadcd2; --good: #1f5c56; --good-bg: #d2eae5; --easy: #74530e; --easy-bg: #f6e3b8;
  --serif: Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --gutter: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --primary: #ffb4a1; --on-primary: #5c1708; --primary-container: #8f2f1b; --on-primary-container: #ffdad1;
    --secondary: #9ad2ca; --secondary-container: #1d4f4a; --on-secondary-container: #b6eee6;
    --tertiary: #e9c46a;
    --bg: #171411; --on-bg: #ede6da; --surface: #211d19; --surface-high: #2b2722;
    --muted: #b9afa0; --outline: #857b6d; --outline-soft: #3f3931; --error: #ffb4ab;
    --paper: #2a2621; --paper-ink: #f1eade; --paper-muted: #a89e8f;
    --again: #ffb4a1; --again-bg: #5e2114; --good: #a6ddd5; --good-bg: #1d4f4a; --easy: #f0ce7a; --easy-bg: #4f3b05;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100dvh; background: var(--bg); color: var(--on-bg);
  font-family: var(--sans); font-size: 16px; line-height: 1.4; overflow-x: hidden;
}
button, input { font: inherit; color: inherit; }
button { border: 0; background: none; padding: 0; cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
h1, h2 { font-family: var(--serif); margin: 0; }
h2 { font-size: 20px; margin-top: 10px; }
p { margin: 0; }

#app { max-width: 680px; margin: 0 auto; padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
#app.fixed { height: 100dvh; overflow: hidden; }
.loading { padding-top: 40dvh; text-align: center; font: 700 26px var(--serif); color: var(--primary); }

.icon { display: inline-flex; width: 24px; height: 24px; flex: none; }
.icon svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon svg .fill { fill: currentColor; }
.icon-btn { width: 44px; height: 44px; flex: none; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; color: var(--muted); }
.icon-btn.primary { color: var(--primary); }
.icon-btn:active { background: var(--surface-high); }

.home-head, .topbar { display: flex; align-items: center; padding: calc(env(safe-area-inset-top) + 10px) var(--gutter) 8px; gap: 4px; }
.home-head { justify-content: space-between; }
.home-head h1 { font-size: 30px; line-height: 1.1; }
.greeting { color: var(--muted); font-size: 15px; }
.actions { display: flex; }
.topbar { padding-left: 6px; position: sticky; top: 0; z-index: 5; background: var(--bg); }
.topbar h1 { flex: 1; min-width: 0; font-size: 21px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.page { display: flex; flex-direction: column; gap: 14px; padding: 6px var(--gutter) calc(env(safe-area-inset-bottom) + 92px); }

.banner { display: flex; gap: 8px; align-items: flex-start; padding: 12px 6px 12px 16px; border-radius: 16px; background: var(--secondary-container); color: var(--on-secondary-container); }
.banner > div { flex: 1; }
.banner p { font-size: 14px; margin-top: 2px; }
.banner .icon-btn { color: inherit; margin-top: -6px; }

.today { padding: 20px; border-radius: 22px; background: var(--primary); color: var(--on-primary); }
.eyebrow { font-size: 12px; font-weight: 700; letter-spacing: 1.4px; opacity: .85; }
.today-count { display: flex; align-items: baseline; gap: 9px; margin-top: 4px; }
.today-count b { font: 700 46px/1.1 var(--serif); }
.today-done { font: 700 26px/1.2 var(--serif); margin-top: 6px; }
.today p { opacity: .9; font-size: 15px; margin-top: 2px; }

.btn { display: inline-flex; min-height: 48px; align-items: center; justify-content: center; gap: 8px; padding: 0 22px; border-radius: 24px; font-weight: 600; }
.btn.wide { width: 100%; }
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.on-primary { width: 100%; margin-top: 14px; background: var(--on-primary); color: var(--primary); }
.btn.ghost { border: 1px solid var(--outline); color: var(--primary); }
.btn.danger { color: var(--error); }
.btn:active { opacity: .85; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { display: flex; flex-direction: column; min-width: 0; padding: 12px; border-radius: 16px; background: var(--surface); border: 1px solid var(--outline-soft); }
.stat b { font: 700 24px var(--serif); }
.stat span { color: var(--muted); font-size: 12px; }

.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; margin: 0 calc(var(--gutter) * -1); padding: 2px var(--gutter); }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; min-height: 38px; padding: 0 15px; border-radius: 19px; border: 1px solid var(--outline); color: var(--muted); font-size: 14px; font-weight: 600; }
.chip.on { background: var(--secondary-container); border-color: var(--secondary-container); color: var(--on-secondary-container); }

.list { display: flex; flex-direction: column; gap: 8px; }
.row, .option { width: 100%; display: flex; align-items: center; gap: 10px; padding: 12px 14px; text-align: left; border-radius: 16px; background: var(--surface); border: 1px solid var(--outline-soft); }
.row:active, .option:active { background: var(--surface-high); }
.row > .icon { color: var(--outline); }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.row-title { font-family: var(--serif); font-size: 17px; font-weight: 700; overflow-wrap: anywhere; }
.row-sub { color: var(--muted); font-size: 14px; overflow-wrap: anywhere; }
.row-sub em { color: var(--primary); font-style: normal; font-weight: 600; }
.row-no { flex: none; align-self: flex-start; color: var(--muted); font-size: 12px; }
.bar { display: flex; height: 5px; margin-top: 6px; border-radius: 3px; overflow: hidden; background: var(--surface-high); }
.bar .mastered { background: var(--secondary); }
.bar .learning { background: var(--primary); opacity: .65; }
.empty { padding: 28px 0; text-align: center; color: var(--muted); }

.group { display: flex; flex-direction: column; gap: 8px; }
.group-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.group-head h2 { margin-top: 0; font-size: 18px; min-width: 0; }

.fab { position: fixed; z-index: 6; right: max(var(--gutter), calc((100vw - 680px) / 2 + var(--gutter))); bottom: calc(env(safe-area-inset-bottom) + 16px); display: inline-flex; align-items: center; gap: 6px; height: 52px; padding: 0 20px 0 15px; border-radius: 18px; background: var(--primary-container); color: var(--on-primary-container); font-weight: 600; box-shadow: 0 3px 10px rgb(0 0 0 / .25); }

.dialect { display: inline-flex; align-items: baseline; gap: 7px; margin-top: 2px; color: var(--secondary); font-family: var(--serif); font-style: italic; font-size: 17px; font-weight: 400; }
.dialect .tag { flex: none; padding: 1px 6px; border-radius: 6px; background: var(--secondary-container); color: var(--on-secondary-container); font: 700 10px var(--sans); }
.dialect.plain { color: inherit; font-style: normal; font-size: inherit; font-weight: inherit; }
.row .dialect { font-size: 14px; }

.study { height: 100dvh; display: flex; flex-direction: column; }
.study-head { display: flex; align-items: center; gap: 4px; padding: calc(env(safe-area-inset-top) + 6px) 6px 4px; }
.study-bar { flex: 1; height: 6px; border-radius: 3px; overflow: hidden; background: var(--surface-high); }
.study-bar i { display: block; height: 100%; background: var(--primary); transition: width .2s; }
.study-count { flex: none; padding-left: 6px; color: var(--muted); font-size: 13px; }
.study .topbar { position: static; }
.card-area { flex: 1; min-height: 0; padding: 6px var(--gutter); }
.study-foot { flex: none; display: flex; align-items: center; justify-content: center; min-height: 76px; padding: 4px var(--gutter) calc(env(safe-area-inset-bottom) + 10px); }
.tap-hint { text-align: center; color: var(--muted); font-size: 14px; }

.flip { width: 100%; height: 100%; perspective: 1400px; cursor: pointer; }
.flip-inner { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; -webkit-transform-style: preserve-3d; transition: transform .42s ease-in-out; }
.flip.flipped .flip-inner { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; display: flex; flex-direction: column; padding: 20px 18px 6px 22px; border-radius: 22px; background: var(--paper); color: var(--paper-ink); border: 1px solid var(--outline-soft); box-shadow: 0 3px 12px rgb(0 0 0 / .14); backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.face.back { transform: rotateY(180deg); }
/* The faces swap at 90 degrees, halfway through the turn, even if backface-visibility is ignored. */
.face { transition: visibility 0s linear .21s; }
.flip:not(.flipped) .back, .flip.flipped .front { visibility: hidden; pointer-events: none; }
.face-head { flex: none; display: flex; justify-content: space-between; gap: 12px; color: var(--paper-muted); font-size: 14px; }
.face-topic { display: flex; flex-direction: column; min-width: 0; }
.face-no { flex: none; font-size: 15px; }
.lines { flex: 1; min-height: 0; margin: 18px 0 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.lines li { display: flex; flex: none; font: 400 20px/28px var(--serif); }
.lines li.headword { font-weight: 700; font-size: 23px; }
.line-no { flex: none; width: 30px; color: var(--paper-muted); font: 400 16px/28px var(--sans); }
.line-body { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; overflow-wrap: anywhere; }
.lines .dialect.plain { line-height: 28px; }
.face-foot { flex: none; display: flex; align-items: center; justify-content: space-between; min-height: 44px; color: var(--paper-muted); font-size: 11px; font-weight: 500; letter-spacing: 1.4px; text-transform: uppercase; }

.rates { width: 100%; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.rate { min-height: 60px; display: flex; flex-direction: column; align-items: center; justify-content: center; border-radius: 16px; }
.rate b { font-size: 16px; }
.rate span { font-size: 12px; opacity: .85; }
.rate.again { background: var(--again-bg); color: var(--again); }
.rate.good { background: var(--good-bg); color: var(--good); }
.rate.easy { background: var(--easy-bg); color: var(--easy); }
.rate:active { opacity: .8; }

.done { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: var(--gutter); text-align: center; }
.done h1 { font-size: 32px; }
.done .btn { margin-top: 16px; }
.done-icon { display: inline-flex; padding: 16px; border-radius: 50%; background: var(--secondary-container); color: var(--on-secondary-container); }

.search-input, .field input { width: 100%; height: 48px; padding: 0 14px; border-radius: 14px; border: 1px solid var(--outline); background: var(--surface); font-size: 16px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { color: var(--muted); font-size: 13px; }
.help { color: var(--muted); font-size: 14px; margin-top: -8px; }
.line-edit { display: flex; flex-direction: column; gap: 10px; padding: 12px; border-radius: 16px; background: var(--surface); border: 1px solid var(--outline-soft); }
.line-edit .field input { background: var(--bg); }
.line-edit-head { display: flex; align-items: center; justify-content: space-between; }
.line-edit-head .chips { margin: 0; padding: 2px 0; }

.option .radio { flex: none; width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--outline); }
.option.on .radio { border: 6px solid var(--primary); }
.option .switch { position: relative; flex: none; width: 46px; height: 28px; border-radius: 14px; background: var(--outline); transition: background .15s; }
.option .switch::after { content: ""; position: absolute; left: 3px; top: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; }
.option.on .switch { background: var(--primary); }
.option.on .switch::after { transform: translateX(18px); }
.option.danger .row-title { color: var(--error); }

.toast { position: fixed; z-index: 20; left: var(--gutter); right: var(--gutter); bottom: calc(env(safe-area-inset-bottom) + 84px); max-width: 480px; margin: 0 auto; padding: 12px 16px; border-radius: 12px; background: var(--on-bg); color: var(--bg); font-size: 14px; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  .flip-inner, .face, .study-bar i { transition: none; }
}
