:root {
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --surface-2: #f1ebdf;
  --ink: #1f1e1c;
  --muted: #7a736a;
  --line: #e4dccd;
  --accent: #c4432b;
  --accent-soft: #f6dcd3;
  --yes: #3d7a58;
  --yes-soft: #dcebe1;
  --yes-strong: #3d7a58;
  --no: #c4432b;
  --no-soft: #f6dcd3;
  --new: #3a6aa0;
  --learn: #b0731c;
  --heat-0: #ebe4d6;
  --heat-1: #d9c7b0;
  --heat-2: #d49a7a;
  --heat-3: #c8664a;
  --heat-4: #a8371f;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 8px 28px rgba(60, 40, 20, .08);
  --radius: 18px;
  --jp: "Noto Sans JP", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", sans-serif; /* set from settings */
  --sans: "Noto Sans JP", system-ui, -apple-system, "Segoe UI", Roboto, "Hiragino Sans", "Yu Gothic UI", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15161a;
    --surface: #1e1f24;
    --surface-2: #26282e;
    --ink: #ebe7df;
    --muted: #9b958c;
    --line: #2f3138;
    --accent: #e2694f;
    --accent-soft: #3a2420;
    --yes: #63b287;
    --yes-soft: #1f3329;
    --yes-strong: #2e7651;
    --no: #e2694f;
    --no-soft: #3a2420;
    --new: #82aadb;
    --learn: #dba64a;
    --heat-0: #24262c;
    --heat-1: #3c3430;
    --heat-2: #6b3e31;
    --heat-3: #a54d36;
    --heat-4: #e2694f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 10px 30px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15161a;
  --surface: #1e1f24;
  --surface-2: #26282e;
  --ink: #ebe7df;
  --muted: #9b958c;
  --line: #2f3138;
  --accent: #e2694f;
  --accent-soft: #3a2420;
  --yes: #63b287;
  --yes-soft: #1f3329;
  --yes-strong: #2e7651;
  --no: #e2694f;
  --no-soft: #3a2420;
  --new: #82aadb;
  --learn: #dba64a;
  --heat-0: #24262c;
  --heat-1: #3c3430;
  --heat-2: #6b3e31;
  --heat-3: #a54d36;
  --heat-4: #e2694f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 10px 30px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
a { color: var(--accent); }

.app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top, 0) 16px env(safe-area-inset-bottom, 0);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-block: 14px 6px;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 32px; height: 32px;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", serif !important;
  display: grid; place-items: center;
  background: var(--accent);
  color: #fff;
  border-radius: 9px;
  font: 600 18px/1 var(--jp);
}
.brand-name { font-weight: 500; letter-spacing: .02em; }
.tabs { display: flex; gap: 2px; background: var(--surface-2); padding: 3px; border-radius: 12px; }
.tab {
  display: flex; align-items: center; gap: 6px;
  border: 0; background: transparent;
  padding: 7px 12px;
  border-radius: 9px;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
}
.tab svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
@media (max-width: 500px) {
  .tab span { display: none; }
  .tab { padding: 8px 11px; }
}

main { flex: 1; display: flex; flex-direction: column; }
.view { flex: 1; display: flex; flex-direction: column; padding-bottom: 24px; }

/* ---------- study ---------- */
.session-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding-block: 8px;
}
.counts { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.count b { font-weight: 600; font-variant-numeric: tabular-nums; }
.count-new b { color: var(--new); }
.count-learn b { color: var(--accent); }
.count-due b { color: var(--yes); }

.icon-btn {
  width: 36px; height: 36px;
  display: inline-grid; place-items: center;
  border: 0; background: transparent;
  border-radius: 10px;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--ink); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.trash:hover { color: var(--no); background: var(--no-soft); }

.nudge {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 8px;
}
.nudge span { flex: 1 1 auto; color: var(--muted); }

.stage { flex: 1; display: flex; flex-direction: column; justify-content: center; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 22px 26px;
  min-height: min(360px, 52dvh);
  display: flex; flex-direction: column;
  outline: none;
  cursor: pointer;
  touch-action: none; /* front: we handle all swipes (up = reveal) */
  user-select: none;
  transition: transform .18s ease, opacity .18s ease;
  will-change: transform;
}
.card.revealed { cursor: default; touch-action: pan-y; /* answer side may need scrolling */ }
.card.fly-left { transform: translateX(-28px) rotate(-1.5deg); opacity: 0; }
.card.fly-right { transform: translateX(28px) rotate(1.5deg); opacity: 0; }
.card.enter { animation: enter .22s ease; }
@keyframes enter { from { opacity: 0; transform: translateY(8px); } }

.card-top { display: flex; align-items: center; gap: 8px; min-height: 36px; }
.level-badge {
  font-size: 12px; font-weight: 600; letter-spacing: .04em;
  padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.card-tag { font-size: 12px; color: var(--muted); flex: 1; }
.card-tag.is-new { color: var(--new); }
.card-tag.is-again { color: var(--accent); }

.card-front {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  padding-block: 24px 8px;
}
.reading-front { font-size: 15px; color: var(--muted); min-height: 1.5em; letter-spacing: .05em; }
.word {
  font-family: var(--jp);
  font-weight: 600;
  font-size: clamp(48px, 14vw, 76px);
  line-height: 1.15;
  letter-spacing: .02em;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
.word.long { font-size: clamp(34px, 9vw, 52px); }
.sentence-front {
  margin: 18px 0 0;
  font-family: var(--jp);
  font-size: 18px;
  color: var(--muted);
}
mark {
  background: linear-gradient(transparent 60%, var(--accent-soft) 60%);
  color: inherit;
  padding: 0 1px;
}

.card-back {
  border-top: 1px solid var(--line);
  margin-top: 8px;
  padding-top: 16px;
  display: none;
  animation: enter .2s ease;
}
.card.revealed .card-back { display: block; }
.reading-row { display: flex; align-items: center; justify-content: center; gap: 4px; }
.reading { font-size: 22px; letter-spacing: .06em; }
.meaning { text-align: center; font-size: 18px; margin-top: 4px; }
.examples { list-style: none; padding: 0; margin: 18px 0 0; display: grid; gap: 12px; }
.examples li { background: var(--surface-2); border-radius: 12px; padding: 10px 14px; }
.examples .ja { font-family: var(--jp); font-size: 17px; }
.examples .en { color: var(--muted); font-size: 14px; }

.answer-bar {
  position: sticky; bottom: 0;
  padding-block: 14px calc(10px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(transparent, var(--bg) 30%);
}
.answer-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px;
  padding: 10px 18px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-weight: 500;
  cursor: pointer;
  text-align: center;
}
.btn:hover { border-color: var(--muted); }
.btn:active { transform: translateY(1px); }
.btn-reveal, .btn-yes, .btn-no { width: 100%; min-height: 56px; font-size: 16px; border-radius: 14px; }
.btn-reveal { background: var(--ink); color: var(--bg); border-color: transparent; }
.btn-yes { background: var(--yes-strong); color: #fff; border-color: transparent; }
.btn-no { background: var(--no-soft); color: var(--no); border-color: transparent; }
.btn-ghost { background: transparent; }
.btn-danger { color: var(--no); border-color: var(--no-soft); background: transparent; }
.btn-danger:hover { background: var(--no-soft); border-color: var(--no); }
kbd {
  font: 500 11px/1 var(--sans);
  padding: 3px 5px;
  border-radius: 5px;
  border: 1px solid currentColor;
  opacity: .55;
}
@media (hover: none), (max-width: 500px) { .btn kbd { display: none; } }

.done { text-align: center; padding: 24px; }
.done-mark {
  width: 84px; height: 84px; margin: 0 auto 12px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 2px solid var(--accent);
  color: var(--accent);
  font: 600 38px/1 var(--jp);
}
.done h2 { margin: 8px 0 4px; font-weight: 500; }
.done-actions { margin-top: 18px; display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }

/* ---------- panels ---------- */
.panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-top: 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.panel-title { margin: 0 0 10px; font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pill { font-size: 12px; font-weight: 500; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }

.stats-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.stat b { display: block; font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
@media (max-width: 500px) { .stats-row { grid-template-columns: repeat(2, 1fr); } }

.heatmap-wrap { overflow-x: auto; margin-top: 14px; }
.heatmap { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 11px); gap: 3px; width: max-content; }
.heatmap i { width: 11px; height: 11px; border-radius: 3px; background: var(--heat-0); }
.heatmap i[data-l="1"] { background: var(--heat-1); }
.heatmap i[data-l="2"] { background: var(--heat-2); }
.heatmap i[data-l="3"] { background: var(--heat-3); }
.heatmap i[data-l="4"] { background: var(--heat-4); }
.heatmap i.future { visibility: hidden; }

.levels { display: grid; gap: 8px; margin-top: 10px; }
.level {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  gap: 14px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
}
.level.off { opacity: .55; }
.level-name { font: 600 17px/1 var(--jp); }
.level-meta { font-size: 12px; color: var(--muted); margin-top: 6px; }
.bar { height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; display: flex; }
.bar i { display: block; height: 100%; }
.bar .b-known { background: var(--yes); }
.bar .b-learning { background: var(--learn); }

/* switch */
.switch {
  appearance: none;
  width: 44px; height: 26px;
  border-radius: 999px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background .15s;
  flex: none;
  margin: 0;
}
.switch::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 20px; height: 20px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,.2);
  transition: transform .15s;
}
.switch:checked { background: var(--yes); }
.switch:checked::after { transform: translateX(18px); }
.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding-block: 10px;
  border-bottom: 1px solid var(--line);
}
.field:last-child { border-bottom: 0; }
.input {
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 10px;
  padding: 8px 10px;
  min-height: 40px;
  max-width: 100%;
}
.input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.input-num { width: 90px; text-align: right; flex: none; }
.field-label { flex: 1 1 auto; min-width: 0; }
.field select.input { flex: 0 1 auto; min-width: 0; max-width: 60%; text-overflow: ellipsis; }
input[type="search"].input { width: 100%; }
select.input { padding-right: 28px; }

.word-list { list-style: none; padding: 0; margin: 10px 0 0; }
.word-list li {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}
.word-list li:last-child { border-bottom: 0; }
.wl-main { flex: 1; min-width: 0; }
.wl-word { font: 500 17px/1.3 var(--jp); }
.wl-reading { color: var(--muted); font-size: 13px; margin-left: 6px; }
.wl-meaning { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wl-status { font-size: 11px; padding: 2px 7px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.wl-status.known { color: var(--yes); background: var(--yes-soft); }
.wl-status.learning { color: var(--learn); }
.wl-more { color: var(--muted); font-size: 13px; justify-content: center; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 12px 0 4px; font-size: 14px; }
.kv span:nth-child(odd) { color: var(--muted); }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.btn-row.end { justify-content: flex-end; }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 500; cursor: pointer; padding: 4px 2px; }
.link-btn.subtle { color: var(--muted); }

.credits { text-align: center; margin: 22px 8px 8px; line-height: 1.9; }

/* dialog & toast */
.dialog {
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 0;
  width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(10, 8, 6, .45); }
.dialog-body { padding: 20px 22px; }
.dialog h3 { margin: 0 0 8px; font-weight: 600; }
#dialog-text p { margin: 6px 0; }
.dialog-ok-danger { background: var(--no); color: #fff; border-color: transparent; }

.toast {
  position: fixed;
  left: 50%; bottom: calc(96px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 10px 14px 10px 18px;
  border-radius: 12px;
  display: flex; align-items: center; gap: 14px;
  box-shadow: var(--shadow);
  font-size: 14px;
  max-width: calc(100vw - 32px);
  z-index: 10;
  animation: enter .2s ease;
}
.toast .link-btn { color: var(--accent-soft); }

/* furigana */
ruby { ruby-position: over; ruby-align: space-around; } /* standard: spread short readings across the kanji */
rt {
  font-size: .5em;
  font-weight: 400;
  color: var(--muted);
  letter-spacing: 0;
  user-select: none;
}
.word rt { font-size: .32em; padding-bottom: .1em; }
.examples .ja, .sentence-front { line-height: 1.95; }
.examples .ja rt, .sentence-front rt { font-size: .55em; }
mark rt { color: var(--accent); }

.field-label small.field-hint { display: block; font-size: 12px; color: var(--muted); }
.font-preview {
  font-family: var(--jp);
  font-size: 22px;
  line-height: 2.2;
  text-align: center;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 6px 12px;
  margin: 2px 0 6px;
}

/* short screens (small phones, landscape) */
@media (max-height: 640px) {
  .topbar { padding-block: 8px 2px; }
  .session-bar { padding-block: 4px; }
  .card { padding: 12px 18px 16px; }
  .card-front { padding-block: 8px 4px; }
  .word { font-size: clamp(38px, 11vh, 60px); }
  .sentence-front { margin-top: 8px; font-size: 16px; }
  .card-back { padding-top: 10px; }
  .examples { margin-top: 10px; gap: 8px; }
  .answer-bar { padding-block: 8px calc(8px + env(safe-area-inset-bottom, 0px)); }
  .btn-reveal, .btn-yes, .btn-no { min-height: 48px; }
}
@media (max-width: 360px) {
  .app { padding-inline: 12px; }
  .card { padding-inline: 16px; }
  .panel { padding: 16px; }
  .counts { gap: 10px; }
}
@media (hover: none), (max-width: 500px) { .keys-help { display: none; } }

/* mobile: same buttons, bigger tap targets */
@media (max-width: 500px) {
  .icon-btn { width: 48px; height: 48px; border-radius: 14px; }
  .icon-btn svg { width: 24px; height: 24px; }
  #btn-undo, .card-top .trash, .reading-row .speak { background: var(--surface-2); color: var(--ink); }
  #btn-undo:disabled { background: transparent; }
  .card-top .trash { color: var(--no); background: var(--no-soft); }
  .reading-row { gap: 10px; }
  .card { padding-top: 14px; }
  .tab { min-height: 44px; min-width: 48px; justify-content: center; }
  .nudge .link-btn, .toast .link-btn { padding: 12px 10px; min-height: 44px; }
}
/* voice */
.voice-controls { display: flex; gap: 8px; align-items: center; }
.voice-controls .btn { min-height: 40px; padding: 8px 14px; }
.voice-help { font-size: 13px; color: var(--muted); padding: 8px 0 10px; border-bottom: 1px solid var(--line); }
.voice-help summary { cursor: pointer; color: var(--accent); font-weight: 500; padding: 6px 0; min-height: 32px; }
.voice-help ul { margin: 6px 0 0; padding-left: 18px; display: grid; gap: 6px; }
.voice-help b { color: var(--ink); font-weight: 500; }
.examples li[data-say] { cursor: pointer; position: relative; }
.examples li[data-say]::after {
  content: "";
  position: absolute; top: 12px; right: 12px;
  width: 16px; height: 16px;
  opacity: .35;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 10v4h4l5 4V6L8 10H4z'/%3E%3Cpath d='M16.5 8.5a5 5 0 0 1 0 7'/%3E%3C/svg%3E") center / contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 10v4h4l5 4V6L8 10H4z'/%3E%3Cpath d='M16.5 8.5a5 5 0 0 1 0 7'/%3E%3C/svg%3E") center / contain no-repeat;
}
.examples li[data-say] { padding-right: 34px; }
.examples li[data-say]:active { background: var(--line); }

/* swipe feedback */
.card.drag-yes { box-shadow: 0 0 0 3px var(--yes), var(--shadow); }
.card.drag-no { box-shadow: 0 0 0 3px var(--no), var(--shadow); }

/* font readiness */
.card.font-wait { visibility: hidden; }

/* pitch accent */
.pa { display: inline-flex; }
.pa span { padding-top: 4px; border-top: 2px solid transparent; }
.pa .h { border-top-color: var(--accent); }
.pa .d { border-right: 2px solid var(--accent); margin-right: 1px; }
.r-note { color: var(--muted); font-size: .7em; margin-left: 6px; }
.pa-tag {
  font-size: 11px; font-weight: 600;
  margin-left: 8px; padding: 1px 6px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  vertical-align: middle;
}

/* picture */
.picture {
  text-align: center;
  font-size: 64px;
  line-height: 1.1;
  margin: 6px 0 2px;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
}
@media (max-height: 640px) { .picture { font-size: 48px; } }
