:root {
  --bg: #f6f6f3;
  --surface: #ffffff;
  --surface-2: #ececE7;
  --text: #1b1d21;
  --muted: #6c7078;
  --line: #e2e2dc;
  --accent: #2f6fdf;
  --accent-soft: #e4edfc;
  --accent-text: #ffffff;
  --shadow: 0 -6px 24px rgba(0, 0, 0, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1d2026;
    --surface-2: #292d35;
    --text: #eceef2;
    --muted: #9399a4;
    --line: #2c3038;
    --accent: #6b9cff;
    --accent-soft: #233150;
    --accent-text: #0e1420;
    --shadow: 0 -6px 24px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(190px + env(safe-area-inset-bottom));
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
svg .fill { fill: currentColor; stroke: none; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(10px + env(safe-area-inset-top)) 16px 8px;
  border-bottom: 1px solid var(--line);
}
.top-row { display: flex; align-items: center; gap: 8px; max-width: 720px; margin: 0 auto; }
.book-select {
  flex: 1; min-width: 0;
  font-family: inherit; font-size: 17px; font-weight: 700; line-height: 1.3;
  color: var(--text); background: transparent;
  border: 0; padding: 6px 0; appearance: none;
  text-overflow: ellipsis;
}
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 10px; font-size: 22px; }
.icon-btn:active { background: var(--surface-2); }

.search {
  position: relative; max-width: 720px; margin: 6px auto 0;
  display: flex; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.search > svg { position: absolute; left: 11px; width: 18px; height: 18px; color: var(--muted); pointer-events: none; }
.search input {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 16px; padding: 11px 8px 11px 36px; outline: none;
}
.search input::-webkit-search-cancel-button { display: none; }
.search:focus-within { border-color: var(--accent); }
.clear { width: 40px; height: 40px; color: var(--muted); font-size: 20px; }

.chips {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  max-width: 720px; margin: 8px auto 0; padding-bottom: 2px;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 6px 12px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line);
  font-size: 13px; font-weight: 600; color: var(--muted);
}
.chip[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip-sep { flex: none; width: 1px; background: var(--line); margin: 4px 2px; }

/* ---------- list ---------- */
main { max-width: 720px; margin: 0 auto; }
.status { margin: 0; padding: 10px 16px 0; color: var(--muted); font-size: 13px; }
.status:empty { display: none; }
.list { list-style: none; margin: 0; padding: 6px 8px; }
.item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 10px; border-radius: 12px; cursor: pointer;
}
.item:active { background: var(--surface-2); }
.item .num {
  flex: none; width: 34px; text-align: center;
  font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.item .txt { min-width: 0; flex: 1; }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 12px; color: var(--muted); }
.item mark { background: var(--accent-soft); color: inherit; border-radius: 3px; padding: 0 1px; }
.item.current { background: var(--accent-soft); }
.item.current .num { color: var(--accent); }
.eq { display: inline-flex; gap: 2px; align-items: flex-end; height: 14px; }
.eq i { width: 3px; background: var(--accent); border-radius: 1px; height: 40%; }
.playing .eq i { animation: eq .9s ease-in-out infinite; }
.eq i:nth-child(2) { animation-delay: -.3s !important; height: 80%; }
.eq i:nth-child(3) { animation-delay: -.6s !important; height: 60%; }
@keyframes eq { 0%, 100% { height: 25%; } 50% { height: 100%; } }
@media (prefers-reduced-motion: reduce) { .playing .eq i { animation: none; } }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.jump-now {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(196px + env(safe-area-inset-bottom)); z-index: 6;
  padding: 8px 14px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--text); color: var(--bg); box-shadow: 0 4px 14px rgba(0,0,0,.2);
}

/* ---------- player ---------- */
.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--surface); box-shadow: var(--shadow);
  border-radius: 18px 18px 0 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
}
.player > * { max-width: 560px; margin-left: auto; margin-right: auto; }
.np { display: flex; align-items: center; gap: 6px; }
.np-text { flex: 1; min-width: 0; }
.np-title { font-weight: 700; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.np-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill {
  flex: none; min-width: 48px; padding: 6px 10px; border-radius: 999px;
  background: var(--surface-2); font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.pill.on { background: var(--accent-soft); color: var(--accent); }

.seek { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.time { flex: none; width: 42px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.time:last-child { text-align: right; }
#seek {
  flex: 1; min-width: 0; height: 32px; margin: 0; background: transparent;
  -webkit-appearance: none; appearance: none; touch-action: none;
  --p: 0%;
}
#seek::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(var(--accent), var(--accent)) 0 / var(--p) 100% no-repeat, var(--surface-2);
}
#seek::-moz-range-track { height: 6px; border-radius: 3px; background: var(--surface-2); }
#seek::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--accent); }
#seek::-webkit-slider-thumb {
  -webkit-appearance: none; width: 18px; height: 18px; margin-top: -6px;
  border-radius: 50%; background: var(--accent); border: 3px solid var(--surface);
  box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
#seek::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 3px solid var(--surface); }

.controls { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; }
.ctrl { width: 56px; height: 56px; display: grid; place-items: center; border-radius: 50%; }
.ctrl:active { background: var(--surface-2); }
.ctrl svg { width: 28px; height: 28px; }
.ctrl text { font: 700 7px system-ui, sans-serif; fill: currentColor; stroke: none; text-anchor: middle; }
.ctrl.play { width: 68px; height: 68px; background: var(--accent); color: var(--accent-text); }
.ctrl.play:active { filter: brightness(.92); background: var(--accent); }
.ctrl.play svg { width: 32px; height: 32px; }
.i-pause { display: none; }
.is-playing .i-play { display: none; }
.is-playing .i-pause { display: block; }
.ctrl:disabled { opacity: .35; }

/* ---------- menu sheet ---------- */
.sheet {
  width: min(560px, 100%); max-height: 88vh; margin: auto auto 0;
  border: 0; border-radius: 18px 18px 0 0; padding: 0 16px calc(20px + env(safe-area-inset-bottom));
  background: var(--surface); color: var(--text);
}
@media (min-width: 640px) { .sheet { margin: auto; border-radius: 18px; } }
.sheet::backdrop { background: rgba(0, 0, 0, .45); }
.sheet-head { position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between; background: var(--surface); padding-top: 10px; }
.sheet h2 { font-size: 18px; margin: 0; }
.sheet h3 { font-size: 14px; margin: 18px 0 6px; }
.note { font-size: 13px; color: var(--muted); margin: 4px 0; }
.note:empty { display: none; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.btn {
  display: inline-flex; align-items: center; padding: 10px 14px; border-radius: 10px;
  background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 14px; cursor: pointer;
}
.btn.ghost { background: var(--surface-2); color: var(--text); }
.local-books { list-style: none; padding: 0; margin: 8px 0; }
.local-books li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px; }
.local-books li span { flex: 1; min-width: 0; }
.local-books button { color: #d64545; font-size: 13px; font-weight: 600; padding: 6px 8px; }
.qr { display: flex; justify-content: center; padding: 8px 0; }
.qr img, .qr canvas { background: #fff; padding: 10px; border-radius: 10px; }
.urls { padding-left: 18px; font-size: 14px; word-break: break-all; }
.tips { padding-left: 18px; font-size: 13px; color: var(--muted); margin: 4px 0; }
.tips b { color: var(--text); }

/* ---------- word book ---------- */
.views {
  display: flex; gap: 4px; max-width: 720px; margin: 4px auto 0; padding: 3px;
  background: var(--surface-2); border-radius: 10px;
}
.views button {
  flex: 1; padding: 7px 0; border-radius: 8px; font-size: 14px; font-weight: 700; color: var(--muted);
}
.views button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.chip.track { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.wbtn {
  flex: none; padding: 5px 9px; border-radius: 999px; font-size: 12px; font-weight: 700;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.wbtn:active { background: var(--accent-soft); color: var(--accent); }

.study-bar { display: flex; gap: 8px; padding: 12px 16px 0; }
.study-bar #studyBtn { flex: 1; justify-content: center; }
.study-bar .btn:disabled { opacity: .4; }
.item.word .title { font-size: 16px; }
.item.word .sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.star { flex: none; width: 40px; height: 40px; font-size: 22px; color: var(--muted); border-radius: 50%; }
.star[aria-pressed="true"] { color: #e8a317; }
.star.big { width: 48px; height: 48px; font-size: 28px; }

/* word details (sheet and card back) */
.detail { --pos-line: var(--line); }
.d-head { margin: 4px 0 12px; }
.d-no { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.d-word { font-size: 30px; font-weight: 800; line-height: 1.2; overflow-wrap: anywhere; }
.d-pron { color: var(--muted); font-size: 15px; }
.detail h4 { font-size: 12px; color: var(--muted); margin: 16px 0 6px; letter-spacing: .04em; }
.detail ul { list-style: none; margin: 0; padding: 0; }
.d-senses li, .d-rel li { display: flex; align-items: baseline; gap: 8px; padding: 5px 0; flex-wrap: wrap; }
.d-senses .m { font-size: 17px; font-weight: 600; }
.pos {
  flex: none; display: inline-grid; place-items: center; min-width: 22px; height: 22px; padding: 0 3px;
  border: 1.5px solid currentColor; border-radius: 4px; font-size: 12px; font-weight: 700; color: var(--accent);
  transform: translateY(-1px);
}
.d-rel b, .d-refs b { font-weight: 700; }
.d-rel span, .d-refs span { color: var(--muted); }
.d-refs { display: flex; flex-wrap: wrap; gap: 6px; }
.d-refs li { padding: 5px 10px; border-radius: 8px; background: var(--surface-2); font-size: 14px; }
.d-ex li { padding: 8px 0; border-top: 1px solid var(--line); }
.d-ex li:first-child { border-top: 0; }
.d-ex .en { font-size: 15px; }
.d-ex .en b { color: var(--accent); }
.d-ex .ja { font-size: 13px; color: var(--muted); margin-top: 2px; }
.d-note { white-space: pre-wrap; font-size: 14px; margin: 0; }
.d-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.d-actions .star { margin-right: auto; }
.word-sheet { padding-bottom: calc(28px + env(safe-area-inset-bottom)); }

/* flashcards */
.cards {
  width: 100%; max-width: 100%; height: 100%; max-height: 100%; margin: 0; padding: 0; border: 0;
  background: var(--bg); color: var(--text);
}
.cards[open] {
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
.cards::backdrop { background: var(--bg); }
.cards-head, .cards-foot {
  display: flex; align-items: center; gap: 8px; width: 100%; max-width: 560px; margin: 0 auto; padding: 10px 16px;
}
.card-progress { flex: 1; font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.cards-foot { justify-content: space-between; padding-bottom: 16px; }
.cards-foot .btn { min-width: 76px; justify-content: center; }
.cards-foot .btn:disabled { opacity: .4; }
.card-stage {
  flex: 1; min-height: 0; width: 100%; max-width: 560px; margin: 0 auto; padding: 4px 16px;
  perspective: 1400px; touch-action: pan-y; cursor: pointer; user-select: none;
}
.card {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d; transition: transform .45s cubic-bezier(.3,.7,.3,1);
}
.card.flipped { transform: rotateY(180deg); }
.card.instant { transition: none; }
@media (prefers-reduced-motion: reduce) { .card { transition: none; } }
.face {
  position: absolute; inset: 0; border-radius: 20px; background: var(--surface);
  box-shadow: 0 6px 24px rgba(0,0,0,.12); border: 1px solid var(--line);
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
}
.front { display: flex; flex-direction: column; padding: 18px; }
.back { transform: rotateY(180deg); overflow-y: auto; padding: 18px 20px 24px; user-select: text; }
.c-no { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.c-center { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 8px; min-height: 0; }
.c-word { font-size: clamp(32px, 10vw, 48px); font-weight: 800; line-height: 1.15; overflow-wrap: anywhere; }
.c-ja { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; text-align: left; }
.c-ja li { display: flex; align-items: baseline; gap: 8px; font-size: 20px; font-weight: 700; }
.c-hint { text-align: center; font-size: 12px; color: var(--muted); }
.c-backword { font-size: 26px; font-weight: 800; margin-bottom: 6px; }

.pill.install { background: var(--accent); color: var(--accent-text); }
