:root {
  --bg: #f3ede2;
  --surface: #fbf8f2;
  --surface-2: #ebe3d5;
  --ink: #1e1b17;
  --ink-2: #5d5549;
  --ink-3: #8f8676;
  --line: #ddd3c2;
  --accent: #b2382b;      /* Dancheong-Rot */
  --accent-ink: #fff8f0;
  --teal: #1d6b66;
  --again: #b2382b;
  --hard: #a8701c;
  --good: #1d6b66;
  --easy: #2f5d9a;
  --shadow: 0 1px 2px rgba(40, 30, 15, .06), 0 8px 24px rgba(40, 30, 15, .07);
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Segoe UI", Roboto, sans-serif;
  --font-ko: "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", var(--font);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b;
    --surface: #1e2126;
    --surface-2: #272b31;
    --ink: #efe9df;
    --ink-2: #b6ad9f;
    --ink-3: #847c70;
    --line: #2f343b;
    --accent: #e0624f;
    --accent-ink: #1a0f0c;
    --teal: #4fb3aa;
    --again: #e0624f;
    --hard: #d9a14a;
    --good: #4fb3aa;
    --easy: #7aa5e0;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
  }
}

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

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

main {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 20px) 16px calc(env(safe-area-inset-bottom) + 96px);
  min-height: 100%;
}

h1 { font-size: 30px; line-height: 1.15; letter-spacing: -0.02em; margin: 6px 0 4px; }
h2 { font-size: 13px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); margin: 28px 4px 10px; }
p { margin: 0; }
.sub { color: var(--ink-2); }
.ko { font-family: var(--font-ko); }

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

/* ---------- Bottom tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex; justify-content: center; gap: 4px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(16px) saturate(1.4);
  backdrop-filter: blur(16px) saturate(1.4);
  border-top: 1px solid var(--line);
  z-index: 10;
}
.tabs button {
  flex: 0 1 140px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  border: 0; background: none; padding: 4px 0;
  font-size: 11px; font-weight: 500; color: var(--ink-3);
}
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs button.active { color: var(--accent); }
body.studying .tabs { display: none; }

/* ---------- Home ---------- */
.hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.hero .seal {
  flex: none; width: 52px; height: 52px; border-radius: 14px;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
  font-family: var(--font-ko); font-size: 26px; font-weight: 700;
  transform: rotate(-4deg);
}

.stats {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 20px;
}
.stat {
  background: var(--surface); border-radius: var(--radius); padding: 14px 14px 12px;
  box-shadow: var(--shadow);
}
.stat b { display: block; font-size: 28px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.stat span { font-size: 13px; color: var(--ink-2); }
.stat.due b { color: var(--accent); }
.stat.new b { color: var(--teal); }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 52px; padding: 0 20px;
  border: 0; border-radius: 16px;
  background: var(--ink); color: var(--bg);
  font-size: 17px; font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); margin-top: 16px; }
.btn.ghost { background: var(--surface-2); color: var(--ink); }
.btn.small { min-height: 40px; font-size: 15px; border-radius: 12px; width: auto; }
.btn:disabled { opacity: .45; }
.btn:active { transform: scale(.985); }

.done-note {
  margin-top: 16px; padding: 16px; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); text-align: center; color: var(--ink-2);
}

.decks { display: flex; flex-direction: column; gap: 8px; }
.deck {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border-radius: 14px; padding: 12px 14px;
  border: 1px solid transparent; text-align: left; width: 100%;
}
.deck .check {
  flex: none; width: 22px; height: 22px; border-radius: 7px;
  border: 1.5px solid var(--ink-3); display: grid; place-items: center;
}
.deck.on .check { background: var(--teal); border-color: var(--teal); }
.deck.on .check::after { content: ""; width: 10px; height: 6px; border: 2px solid var(--surface); border-top: 0; border-right: 0; transform: translateY(-1px) rotate(-45deg); }
.deck .name { flex: 1; font-weight: 500; }
.deck .meta { font-size: 13px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.deck:not(.on) .name { color: var(--ink-3); }
.bar { height: 3px; border-radius: 2px; background: var(--surface-2); margin-top: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--teal); }

/* ---------- Study ---------- */
.study-top { display: flex; align-items: center; gap: 12px; }
.icon-btn {
  flex: none; width: 40px; height: 40px; border-radius: 12px; border: 0;
  background: var(--surface-2); display: grid; place-items: center;
}
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.progress { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--accent); transition: width .25s ease; }
.count { font-size: 13px; color: var(--ink-3); font-variant-numeric: tabular-nums; min-width: 32px; text-align: right; }

.card {
  position: relative;
  margin-top: 18px; min-height: 52vh;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  background: var(--surface); border-radius: 24px; box-shadow: var(--shadow);
  padding: 32px 20px;
}
.card .tag {
  position: absolute; top: 14px; left: 16px;
  font-size: 12px; color: var(--ink-3); letter-spacing: .02em;
}
.card .badge {
  position: absolute; top: 12px; right: 14px;
  font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 99px;
  background: color-mix(in srgb, var(--teal) 15%, transparent); color: var(--teal);
}
.card .badge.relearn { background: color-mix(in srgb, var(--again) 15%, transparent); color: var(--again); }
.card .prompt { font-size: 22px; font-weight: 600; line-height: 1.3; }
.card .prompt.ko { font-size: clamp(40px, 13vw, 64px); font-weight: 600; line-height: 1.15; letter-spacing: .01em; }
.card .prompt.ko.long { font-size: clamp(30px, 9vw, 44px); }
.card hr { width: 56px; border: 0; border-top: 2px solid var(--line); margin: 22px auto; }
.card .answer { font-size: 22px; font-weight: 600; }
.card .answer.ko { font-size: clamp(34px, 11vw, 52px); }
.card .rom { margin-top: 6px; color: var(--ink-2); font-size: 16px; letter-spacing: .02em; }
.card .speak { margin-top: 18px; }

.reveal { margin-top: 16px; }
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 16px; }
.grade {
  border: 0; border-radius: 14px; padding: 10px 4px 9px; min-height: 60px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: var(--surface); box-shadow: var(--shadow);
  font-weight: 600; font-size: 15px;
}
.grade small { font-size: 12px; font-weight: 500; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.grade.g1 { color: var(--again); }
.grade.g2 { color: var(--hard); }
.grade.g3 { color: var(--good); }
.grade.g4 { color: var(--easy); }
.grade:active { transform: scale(.97); }

.finish { text-align: center; padding-top: 12vh; }
.finish .big { font-family: var(--font-ko); font-size: 64px; color: var(--accent); line-height: 1; }
.finish h1 { margin-top: 16px; }
.finish p { margin: 8px 0 24px; }

/* ---------- Cards list ---------- */
.search {
  width: 100%; min-height: 46px; padding: 0 14px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-size: 16px;
}
.list { display: flex; flex-direction: column; margin-top: 12px; background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.row { display: flex; align-items: center; gap: 12px; padding: 11px 12px 11px 16px; border-top: 1px solid var(--line); }
.row:first-child { border-top: 0; }
.row .w { flex: 1; min-width: 0; }
.row .w .k { font-family: var(--font-ko); font-size: 19px; font-weight: 600; }
.row .w .r { font-size: 13px; color: var(--ink-3); margin-left: 6px; }
.row .w .d { font-size: 14px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .st { font-size: 12px; color: var(--ink-3); text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row .st.due { color: var(--accent); }
.row .icon-btn { width: 34px; height: 34px; border-radius: 10px; background: transparent; color: var(--ink-3); }
.more { padding: 12px 16px; text-align: center; color: var(--ink-3); font-size: 14px; border-top: 1px solid var(--line); }

/* ---------- Forms / settings ---------- */
.group { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--line); min-height: 52px; }
.field:first-child { border-top: 0; }
.field label { flex: 1; }
.field label small { display: block; color: var(--ink-3); font-size: 13px; }
.field input[type=text], .field input[type=number], .field select {
  font: inherit; font-size: 16px; color: var(--ink);
  background: var(--surface-2); border: 0; border-radius: 10px; padding: 8px 10px;
}
.field input[type=number] { width: 72px; text-align: center; }
.field.stack { flex-direction: column; align-items: stretch; gap: 6px; }
.field.stack input { width: 100%; }
.field.stack label { font-size: 13px; color: var(--ink-2); }

.switch { position: relative; flex: none; width: 50px; height: 30px; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--surface-2); transition: background .2s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.switch input:checked + span { background: var(--teal); }
.switch input:checked + span::after { transform: translateX(20px); }

.actions { display: flex; flex-direction: column; gap: 8px; }
.danger { color: var(--again) !important; }
.hint { font-size: 13px; color: var(--ink-3); margin: 8px 4px 0; }
.hint code { font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 86px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; font-weight: 500; max-width: calc(100% - 32px);
  transition: opacity .2s, transform .2s; z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 700px) {
  main { padding-top: 40px; }
}

/* ============ Bibliothek ============ */
.chips { display: flex; gap: 8px; margin: 18px -16px 0; padding: 0 16px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); color: var(--ink-2);
  border-radius: 99px; padding: 7px 14px; font-size: 14px; font-weight: 500;
}
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }

details.level { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 10px; overflow: hidden; }
details.level summary {
  list-style: none; display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center;
  padding: 14px 16px; cursor: pointer;
}
details.level summary::-webkit-details-marker { display: none; }
details.level summary .lv { font-size: 18px; font-weight: 700; }
details.level summary .lvmeta { font-size: 13px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
details.level summary .bar { grid-column: 1 / -1; margin-top: 8px; }
details.level .hint { margin: 0 16px 10px; }
details.level > .btn { margin: 0 16px 12px; width: calc(100% - 32px); }
.btn.primary-soft { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }

.lessons { border-top: 1px solid var(--line); }
.lesson { display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: 6px; padding: 8px 10px 8px 12px; border-top: 1px solid var(--line); }
.lesson:first-child { border-top: 0; }
.lesson .ln { font-weight: 500; font-size: 14px; white-space: nowrap; }
.lesson.done .ln { color: var(--ink-3); text-decoration: line-through; text-decoration-color: var(--line); }
.lesson.next { background: color-mix(in srgb, var(--accent) 6%, transparent); }
.lesson .tick {
  width: 24px; height: 24px; border-radius: 50%; border: 1.5px solid var(--ink-3); background: none; padding: 0;
  display: grid; place-items: center;
}
.lesson.done .tick { background: var(--teal); border-color: var(--teal); }
.lesson.done .tick::after { content: ""; width: 10px; height: 6px; border: 2px solid var(--surface); border-top: 0; border-right: 0; transform: translateY(-1px) rotate(-45deg); }
.lesson .pg {
  border: 0; border-radius: 10px; padding: 6px 8px; background: var(--surface-2);
  font-size: 12.5px; font-weight: 600; color: var(--ink); white-space: nowrap;
}
.lesson .pg small { font-weight: 400; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.lesson .pg:disabled { opacity: .35; }

.row.book .t { font-weight: 600; }
.row.book .d { white-space: normal; font-size: 13px; color: var(--ink-3); }
.row.book .bar { margin-top: 6px; }
.row.book.missing .t { color: var(--ink-3); }

.busy { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 70%, transparent); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
.busy > div { background: var(--surface); box-shadow: var(--shadow); border-radius: 18px; padding: 20px 24px; max-width: 300px; text-align: center; }
.busy p { margin-top: 10px; color: var(--ink-2); font-size: 15px; }
.spinner { display: inline-block; width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--surface-2); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ Leser ============ */
body.reading main { max-width: none; padding: 0; }
.reader { position: fixed; inset: 0; display: flex; flex-direction: column; background: var(--bg); z-index: 5; }
.r-top {
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.r-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.r-title b { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-title span { font-size: 12px; color: var(--accent); font-weight: 600; min-height: 15px; }
.r-pageno { border: 0; background: var(--surface-2); border-radius: 10px; padding: 7px 10px; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }

.r-panes { flex: 1; min-height: 0; display: flex; }
.r-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.r-pane + .r-pane { border-left: 1px solid var(--line); }
.r-phead { display: none; align-items: center; gap: 4px; padding: 4px 8px 4px 14px; background: var(--bg); border-bottom: 1px solid var(--line); }
.reader.split .r-phead { display: flex; }
.r-ptitle { flex: 1; min-width: 0; font-size: 13px; font-weight: 600; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-pane.active .r-phead { box-shadow: inset 0 -2px 0 var(--accent); }
.r-pane.active .r-ptitle { color: var(--accent); }
.r-pnav { border: 0; background: none; width: 32px; height: 32px; display: grid; place-items: center; border-radius: 8px; color: var(--ink-2); }
.r-pnav svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.r-pno { padding: 5px 8px; font-size: 12px; }

.r-stage { flex: 1; min-height: 0; overflow: auto; -webkit-overflow-scrolling: touch; padding: 8px; background: var(--surface-2); }
.r-page { position: relative; margin: 0 auto; box-shadow: 0 1px 4px rgba(0,0,0,.15); background: #fff; }
.r-page canvas { position: absolute; inset: 0; display: block; }
.c-ink { pointer-events: none; }
.reader.pen .c-ink { pointer-events: auto; touch-action: none; cursor: crosshair; }

.r-split {
  display: flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 10px; padding: 6px 10px; font-size: 13px; font-weight: 600; color: var(--ink-2);
}
.r-split svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.r-split.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }

.r-pentools { display: none; align-items: center; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--line); background: var(--bg); overflow-x: auto; }
.reader.pen .r-pentools { display: flex; }
.swatch { flex: none; width: 30px; height: 30px; border-radius: 50%; border: 3px solid var(--bg); background: var(--c); box-shadow: 0 0 0 1px var(--line); padding: 0; }
.swatch.on { box-shadow: 0 0 0 2px var(--ink); }
.tool { flex: none; border: 0; border-radius: 10px; padding: 7px 11px; background: var(--surface-2); font-size: 13px; font-weight: 600; }
.tool.on { background: var(--ink); color: var(--bg); }

.r-notes { display: none; flex-direction: column; gap: 6px; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--bg); }
.reader.notes .r-notes { display: flex; }
.r-notes label { font-size: 12px; color: var(--ink-3); font-weight: 600; }
.r-notes textarea {
  font: inherit; font-family: var(--font-ko); font-size: 16px; color: var(--ink);
  min-height: 92px; max-height: 30vh; resize: vertical;
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--surface);
}

.r-bottom {
  display: flex; align-items: center; justify-content: space-between; gap: 4px;
  padding: 6px 10px calc(env(safe-area-inset-bottom) + 6px);
  border-top: 1px solid var(--line); background: var(--bg);
}
.r-tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px; border: 0; background: none;
  font-size: 11px; font-weight: 500; color: var(--ink-3); padding: 4px 0; position: relative;
}
.r-tab svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.r-tab.on { color: var(--accent); }
.r-tab.has::after { content: ""; position: absolute; top: 3px; right: calc(50% - 16px); width: 7px; height: 7px; border-radius: 50%; background: var(--teal); }
.r-tab:disabled { opacity: .35; }

[hidden] { display: none !important; }

@media (min-width: 900px) {
  main { max-width: 760px; }
  .r-bottom { justify-content: center; gap: 24px; }
  .r-tab { flex: 0 0 96px; }
}

/* ============ Wort → Karte ============ */
.reader.word .r-page { cursor: pointer; }
.word-mark {
  position: absolute; border-radius: 6px; pointer-events: none;
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  box-shadow: 0 0 0 2px var(--accent);
  animation: markfade 1.6s ease forwards;
}
@keyframes markfade { 0%, 60% { opacity: 1; } 100% { opacity: 0; } }

.r-sheet {
  position: fixed; inset: 0; z-index: 25;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(20, 15, 10, .28);
}
.sheet-card {
  width: 100%; max-width: 520px;
  display: flex; flex-direction: column; gap: 10px;
  background: var(--bg); border-radius: 22px 22px 0 0; box-shadow: var(--shadow);
  padding: 14px 16px calc(env(safe-area-inset-bottom) + 16px);
  animation: sheetup .2s ease;
}
@keyframes sheetup { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (min-width: 700px) {
  .r-sheet { align-items: center; }
  .sheet-card { border-radius: 22px; padding-bottom: 18px; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head b { font-size: 17px; }
.sheet-ko { display: flex; align-items: center; gap: 8px; }
.sheet-card input, .sheet-card select {
  font: inherit; font-size: 16px; color: var(--ink);
  width: 100%; min-height: 46px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
}
.sheet-card #w-ko { font-family: var(--font-ko); font-size: 26px; font-weight: 600; min-height: 56px; }
.sheet-card .hint { margin: 0 2px; color: var(--hard); }
.sheet-deck { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--ink-2); }
.sheet-deck select { flex: 1; min-height: 40px; }
.sheet-card .btn.primary { margin-top: 4px; }
