:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-soft: #dbe6fd;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --ng: #dc2626;
  --ng-soft: #fee2e2;
  --perfect: #7c3aed;
  --known: #16a34a;
  --learning: #f59e0b;
  --weak: #ef4444;
  --new: #cbd5e1;
  --mark: #fde68a;
  --shadow: 0 1px 2px rgb(15 23 42 / 6%), 0 8px 24px rgb(15 23 42 / 8%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --surface: #151b2e;
    --surface-2: #1e263d;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --line: #263049;
    --accent: #60a5fa;
    --accent-soft: #1e3355;
    --ok: #22c55e;
    --ok-soft: #12351f;
    --ng: #f87171;
    --ng-soft: #3d1717;
    --perfect: #a78bfa;
    --known: #22c55e;
    --learning: #fbbf24;
    --weak: #f87171;
    --new: #334155;
    --mark: #6b5310;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 30%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", Meiryo, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: .45; cursor: default; }
input, select, textarea { font: inherit; color: inherit; }

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}

.screen { display: flex; flex-direction: column; gap: 14px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.loading { padding: 40px 0; text-align: center; color: var(--muted); }

/* ---------- top bars ---------- */
.topbar { display: grid; grid-template-columns: 40px 1fr 40px; align-items: center; gap: 8px; }
.topbar h1 { margin: 0; font-size: 1.15rem; text-align: center; letter-spacing: .02em; }
#home .topbar { grid-template-columns: 40px 1fr 40px; }
#home .topbar::before { content: ""; }

.icon-btn {
  width: 40px; height: 40px;
  border: 0; border-radius: 12px;
  background: transparent;
  font-size: 1.2rem;
  display: grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- generic ---------- */
.panel {
  background: var(--surface);
  border-radius: 16px;
  padding: 14px;
  box-shadow: var(--shadow);
}
.panel h2 { margin: 0 0 10px; font-size: .95rem; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }

.btn {
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  background: var(--surface-2);
  font-weight: 600;
  min-height: 48px;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.danger { background: var(--ng-soft); color: var(--ng); }
.btn.wide { width: 100%; }

.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  flex: 1;
  border: 0; border-radius: 8px;
  background: transparent;
  padding: 7px 8px;
  font-size: .85rem;
  white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / 12%); font-weight: 700; }

.label { font-size: .78rem; color: var(--muted); margin-bottom: 4px; }

/* ---------- home ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface); border-radius: 14px; padding: 10px 12px; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 1.35rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat span { font-size: .75rem; color: var(--muted); }

.stack { display: flex; height: 10px; border-radius: 999px; overflow: hidden; background: var(--new); }
.stack i { display: block; height: 100%; }
.stack .k { background: var(--known); }
.stack .l { background: var(--learning); }
.stack .w { background: var(--weak); }

.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: .78rem; color: var(--muted); margin-top: 8px; }
.legend span::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; background: var(--c); }

.quick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.quick button {
  text-align: left;
  border: 0;
  border-radius: 16px;
  padding: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 2px;
  min-height: 88px;
}
.quick button b { font-size: 1.02rem; }
.quick button .big { font-size: 1.6rem; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.1; }
.quick .review .big { color: var(--weak); }
.quick .fresh .big { color: var(--accent); }

.controls { display: grid; gap: 10px; margin-bottom: 12px; }

.stage-tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 10px; }
.stage-tabs button {
  border: 1px solid var(--line); background: transparent; border-radius: 10px;
  padding: 6px 2px; font-size: .8rem; line-height: 1.25;
}
.stage-tabs button[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); font-weight: 700; }
.stage-tabs small { display: block; font-size: .7rem; color: var(--muted); font-weight: 400; }

.ranges { display: grid; gap: 6px; }
.range {
  display: grid;
  grid-template-columns: 84px 1fr auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: 0;
  border-radius: 12px;
  background: var(--surface-2);
  padding: 12px;
  text-align: left;
}
.range:hover { outline: 2px solid var(--accent-soft); }
.range .name { font-weight: 700; font-variant-numeric: tabular-nums; }
.range .num { font-size: .78rem; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.range.all { background: transparent; border: 1px dashed var(--line); }

.links { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- study ---------- */
#study { min-height: calc(100dvh - 28px); gap: 10px; }
.study-top { display: grid; grid-template-columns: 40px 1fr 40px; align-items: center; gap: 8px; }
.study-progress { display: grid; gap: 4px; }
.bar { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
#bar-fill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
#study-count { text-align: center; font-variant-numeric: tabular-nums; }

.prev {
  min-height: 40px;
  display: flex; align-items: center; gap: 8px;
  padding: 6px 6px 6px 12px;
  border-radius: 12px;
  background: var(--surface);
  font-size: .88rem;
  box-shadow: var(--shadow);
}
.prev:empty { visibility: hidden; }
.prev .mark { font-weight: 800; width: 1.2em; text-align: center; }
.prev .mark.o { color: var(--ok); }
.prev .mark.x { color: var(--ng); }
.prev .mark.k { color: var(--perfect); }
.prev .pw { font-weight: 700; }
.prev .pm { flex: 1; color: var(--muted); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.prev.x { box-shadow: inset 4px 0 0 var(--ng), var(--shadow); }
.prev.x .pm { color: var(--text); font-weight: 600; }
.prev button { border: 0; background: var(--surface-2); border-radius: 8px; padding: 4px 10px; font-size: .8rem; }

.card {
  flex: 1;
  position: relative;
  min-height: 300px;
  background: var(--surface);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 22px 20px;
  display: flex; flex-direction: column; justify-content: center; gap: 18px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  outline: none;
  cursor: pointer;
  overflow: hidden;
}
.card.enter { animation: enter .14s ease-out; }
@keyframes enter { from { opacity: .2; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.card.state-wrong { box-shadow: inset 0 0 0 3px var(--ng), var(--shadow); }
.card.state-checked { box-shadow: inset 0 0 0 3px var(--ok), var(--shadow); }

.card .meta { position: absolute; top: 12px; left: 16px; right: 16px; display: flex; justify-content: space-between; font-size: .75rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .72rem; background: var(--surface-2); color: var(--muted); }
.tag.weak { background: var(--ng-soft); color: var(--ng); }
.tag.known { background: var(--ok-soft); color: var(--ok); }

.q { text-align: center; display: grid; gap: 8px; justify-items: center; }
.pos { font-size: .78rem; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; }
.word { font-size: clamp(2.1rem, 10vw, 3.2rem); font-weight: 800; letter-spacing: .01em; line-height: 1.15; word-break: break-word; }
.meaning-big { font-size: clamp(1.4rem, 6.5vw, 2rem); font-weight: 800; line-height: 1.35; }
.speak { border: 0; background: var(--surface-2); border-radius: 999px; width: 36px; height: 36px; font-size: 1rem; }
.word-row { display: flex; align-items: center; gap: 10px; justify-content: center; flex-wrap: wrap; }

.a { display: grid; gap: 10px; border-top: 1px dashed var(--line); padding-top: 16px; }
.a .meaning { font-size: 1.35rem; font-weight: 800; text-align: center; line-height: 1.4; }
.ex { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; display: grid; gap: 4px; }
.ex .en { font-size: 1.02rem; line-height: 1.45; }
.ex .ja { font-size: .9rem; color: var(--muted); }
.ex .en button { border: 0; background: transparent; padding: 0 0 0 4px; font-size: .9rem; vertical-align: baseline; }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 2px; font-weight: 700; }
.q .ex { width: 100%; text-align: left; }

.hint { text-align: center; font-size: .78rem; color: var(--muted); }

.stamp {
  position: absolute; top: 50%; left: 50%;
  translate: -50% -50%;
  font-size: 6rem; font-weight: 900; line-height: 1;
  pointer-events: none;
  opacity: 0;
}
.stamp.o { color: var(--ok); }
.stamp.x { color: var(--ng); }
.stamp.k { color: var(--perfect); }

.actions {
  display: grid;
  grid-template-columns: 1fr 64px 1fr;
  gap: 10px;
  padding-bottom: 2px;
}
.actions.single { grid-template-columns: 1fr; }
.actions button {
  border: 0;
  border-radius: 16px;
  min-height: 64px;
  font-size: 1.05rem;
  font-weight: 800;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.2;
}
.actions button small { font-size: .68rem; font-weight: 500; opacity: .75; }
.act-x { background: var(--ng-soft); color: var(--ng); }
.act-o { background: var(--ok-soft); color: var(--ok); }
.act-k { background: var(--surface); color: var(--perfect); box-shadow: var(--shadow); }
.act-next { background: var(--accent); color: #fff; }
@media (hover: none) { .actions small.kb { display: none; } }

/* ---------- result ---------- */
.result-head { text-align: center; padding: 12px 0 4px; }
.result-head .big { font-size: 2.6rem; font-weight: 900; line-height: 1.1; }
.result-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.result-actions { display: grid; gap: 10px; }
.miss-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.miss-list li { display: flex; gap: 10px; align-items: baseline; padding: 8px 4px; border-bottom: 1px solid var(--line); }
.miss-list li:last-child { border-bottom: 0; }
.miss-list b { min-width: 7em; }

/* ---------- list ---------- */
.list-tools { display: grid; gap: 8px; }
#list-q {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 11px 14px;
}
.word-list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }
.word-list li { border-bottom: 1px solid var(--line); }
.word-list li:last-child { border-bottom: 0; }
.word-list .head {
  width: 100%; border: 0; background: transparent; text-align: left;
  display: grid; grid-template-columns: 40px 10px minmax(6em, auto) 1fr; gap: 10px; align-items: center;
  padding: 10px 12px;
}
.word-list .n { font-size: .72rem; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }
.word-list .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--new); }
.dot.known { background: var(--known); }
.dot.learning { background: var(--learning); }
.dot.weak { background: var(--weak); }
.word-list .w { font-weight: 700; }
.word-list .m { color: var(--muted); font-size: .88rem; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.word-list .detail { padding: 0 12px 12px 72px; display: grid; gap: 8px; }
.word-list .detail .row { flex-wrap: wrap; }
.word-list .detail .seg { max-width: 360px; }

/* ---------- settings ---------- */
.setting { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting:last-child { border-bottom: 0; }
.setting .desc { font-size: .78rem; color: var(--muted); }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--new); transition: background .15s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgb(0 0 0 / 25%); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.kbd-table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.kbd-table td { padding: 6px 0; border-bottom: 1px solid var(--line); }
kbd { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8rem; background: var(--surface-2); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 6px; padding: 1px 6px; }
textarea { width: 100%; min-height: 90px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); padding: 8px; font-size: .8rem; }

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

@media (max-width: 380px) {
  .range { grid-template-columns: 70px 1fr auto; gap: 8px; }
  .stats .stat b { font-size: 1.15rem; }
}
