:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1c1c1c;
  --muted: #6b6b6b;
  --line: #e4e4e0;
  --accent: #1d4ed8;
  --accent-soft: #dbe6ff;
  --new: #c2410c;
  --warn: #b45309;
  --current: #fff4c2;
  --prt: #2563eb;
  --pred: #15803d;
  --aux: #9333ea;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --surface: #1d1d1d;
    --text: #ececec;
    --muted: #9a9a9a;
    --line: #2e2e2e;
    --accent: #8ab4ff;
    --accent-soft: #1f2d4d;
    --new: #fb923c;
    --warn: #fbbf24;
    --current: #463d12;
    --prt: #93c5fd;
    --pred: #4ade80;
    --aux: #d8b4fe;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans JP", "Noto Sans TC", "Hiragino Sans", "Yu Gothic UI", system-ui, sans-serif;
  font-size: 17px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

/* ---------- top bar & menu ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.topbar h1 { flex: 1; margin: 0; font-size: 18px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { border: 0; background: none; font-size: 24px; line-height: 1; min-width: 40px; min-height: 40px; border-radius: 8px; cursor: pointer; }
.icon-btn:active { background: var(--line); }
.menu {
  position: fixed; right: 12px; top: 56px; z-index: 30;
  display: flex; flex-direction: column; min-width: 200px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgb(0 0 0 / .18); overflow: hidden;
}
.menu[hidden], .toast[hidden], .dialog[hidden] { display: none; }
.menu button { text-align: left; padding: 12px 16px; border: 0; background: none; cursor: pointer; }
.menu button:active { background: var(--line); }
.menu .danger { color: #dc2626; }

main { padding: 12px 16px 96px; max-width: 760px; margin: 0 auto; }

/* ---------- empty state & song list ---------- */
.empty { text-align: center; padding: 48px 8px; color: var(--muted); }
.empty h2 { color: var(--text); font-size: 20px; }
.primary {
  display: inline-block; border: 0; border-radius: 10px; padding: 12px 20px;
  background: var(--accent); color: #fff; font-weight: 600; cursor: pointer;
}
@media (prefers-color-scheme: dark) { .primary { color: #0b1220; } }

.home-actions { margin: 16px 0 8px; text-align: center; }
.secondary {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 10px 16px;
  cursor: pointer; font-size: 15px;
}
.secondary:active { background: var(--line); }
.songs { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.song {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  cursor: pointer; text-decoration: none; color: inherit;
}
.song:active { background: var(--line); }
.song-title { flex: 1; font-weight: 600; }
.song-sub { font-size: 13px; color: var(--muted); font-weight: 400; }
.meter { width: 64px; text-align: right; font-size: 13px; color: var(--muted); }
.meter-bar { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; margin-top: 4px; }
.meter-bar span { display: block; height: 100%; background: var(--accent); }
.section-title { font-size: 13px; color: var(--muted); margin: 20px 4px 8px; letter-spacing: .04em; }

/* ---------- song view ---------- */
.stick {
  position: sticky; top: calc(57px + max(0px, env(safe-area-inset-top) - 8px)); z-index: 10;
  margin: -12px -16px 0;
}
.player-wrap { background: #000; }
.player { position: relative; width: 100%; aspect-ratio: 16 / 9; max-height: 32vh; margin: 0 auto; }
.player iframe, .player > div { position: absolute; inset: 0; width: 100%; height: 100%; }
.offline-note { color: #ccc; font-size: 14px; padding: 12px 16px; text-align: center; }

.toolbar {
  display: flex; gap: 6px; overflow-x: auto;
  padding: 8px 16px; background: var(--bg); border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.lines { list-style: none; margin: 8px 0 0; padding: 0; }
.line { padding: 10px 8px; border-radius: 10px; }
.line.current { background: var(--current); box-shadow: inset 4px 0 0 var(--accent); }
.line-jp { font-size: 22px; line-height: 2.1; word-break: keep-all; overflow-wrap: anywhere; }
.line-jp ruby rt { font-size: 11px; color: var(--muted); }
.hide-ruby .line-jp rt { visibility: hidden; }
.hide-ruby .line-jp { line-height: 1.7; }
.line-romaji { font-size: 14px; color: var(--muted); }
.line-tr { font-size: 15px; }
.hide-romaji .line-romaji, .hide-tr .line-tr { display: none; }
.line-side { float: right; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; margin-left: 8px; }
.line-time { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.ask-btn { border: 1px solid var(--line); background: var(--surface); color: var(--muted); border-radius: 999px;
  font-size: 13px; padding: 5px 12px; min-height: 30px; cursor: pointer; }
.ask-btn:active { background: var(--accent-soft); color: var(--accent); }
.line-ex { display: none; margin-top: 4px; font-size: 14px; }
.show-ex .line-ex { display: block; }
.line-ex summary { color: var(--accent); cursor: pointer; font-size: 13px; }
.line-ex p { margin: 4px 0 2px; padding: 8px 10px; background: var(--surface); border-left: 3px solid var(--accent);
  border-radius: 6px; line-height: 1.6; }
.rate-chip { touch-action: none; user-select: none; -webkit-user-select: none; min-width: 72px; }
.sheet .sheet-ask { width: 100%; margin-top: 8px; }
.ask-options { display: grid; gap: 8px; }
.ask-option { display: flex; gap: 8px; align-items: flex-start; padding: 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.ask-text { width: 100%; min-height: 160px; font-size: 13px; }

.w { cursor: pointer; border-radius: 4px; }
.w.new .wt { text-decoration: underline; text-decoration-color: var(--new); text-decoration-thickness: 2px; text-underline-offset: 6px; }
.hide-ruby .w.new .wt { text-underline-offset: 4px; }

/* word-by-word mode: a short meaning under each word */
.gl { display: none; }
.show-gloss .line-jp { line-height: 1.5; }
.show-gloss .w { display: inline-flex; flex-direction: column; align-items: center; vertical-align: top; margin: 2px 3px 6px; }
.wr { display: none; }
.show-gloss:not(.hide-romaji) .wr { display: block; font-size: 11px; line-height: 1.2; color: var(--muted); margin-top: 1px; }
.show-gloss .line-romaji.has-wr { display: none; }
.show-gloss .gl { display: block; font-size: 11px; line-height: 1.25; color: var(--muted); white-space: nowrap; margin-top: 2px; }
.show-gloss .line-jp ruby rt { font-size: 10px; }

/* structure colours */
.show-struct .role-prt .wt { color: var(--prt); }
.show-struct .role-pred .wt { color: var(--pred); font-weight: 600; }
.show-struct .role-aux .wt { color: var(--aux); }
.show-struct .role-prt .gl { color: var(--prt); }
.show-struct .role-aux .gl { color: var(--aux); }
.legend { display: none; gap: 10px; flex-wrap: wrap; padding: 4px 16px 8px; background: var(--bg); font-size: 12px; border-bottom: 1px solid var(--line); }
.show-struct .legend { display: flex; }
.legend .role-prt { color: var(--prt); }
.legend .role-pred { color: var(--pred); font-weight: 600; }
.legend .role-aux { color: var(--aux); }
.legend-note { color: var(--muted); }
.w.grammar { color: inherit; }
.w:active, .w.sel { background: var(--accent-soft); }

/* ---------- word sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 40; background: rgb(0 0 0 / .35); display: flex; align-items: flex-end; }
.sheet[hidden] { display: none; }
.sheet-card {
  width: 100%; max-width: 760px; margin: 0 auto; background: var(--surface);
  border-radius: 16px 16px 0 0; padding: 16px 20px; padding-bottom: max(20px, env(safe-area-inset-bottom));
  box-shadow: 0 -8px 24px rgb(0 0 0 / .2);
}
.sheet-head { display: flex; justify-content: space-between; align-items: flex-start; }
.sheet-lemma { font-size: 30px; font-weight: 600; }
.sheet-reading { color: var(--muted); }
.sheet-meaning { font-size: 19px; margin: 10px 0 4px; }
.sheet-ff { color: var(--warn); margin: 6px 0; }
.sheet-en { font-size: 14px; color: var(--muted); }
.sheet-meta { font-size: 13px; color: var(--muted); margin: 10px 0 14px; }
.sheet .primary { width: 100%; }
.sheet .primary.is-known { background: var(--line); color: var(--text); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px);
}

/* datasets on the home page */
.dataset + .dataset { margin-top: 28px; }
.dataset-head { display: flex; align-items: center; justify-content: space-between; margin: 8px 4px 0; }
.dataset-head h2 { margin: 0; font-size: 18px; }
.icon-btn.small { font-size: 16px; min-width: 32px; min-height: 32px; color: var(--muted); }
.hint { font-size: 13px; color: var(--muted); }
.home-actions .hint { margin-top: 8px; }

/* local media */
.local-video { display: block; width: 100%; max-height: 32vh; background: #000; }
.local-audio { display: block; width: 100%; padding: 6px 8px; background: var(--surface); }
.media-bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 13px; color: var(--muted); padding: 8px 4px 0; }
.link-btn { border: 0; background: none; color: var(--accent); padding: 4px 0; cursor: pointer; font-size: 13px; }

/* dialog */
.dialog { position: fixed; inset: 0; z-index: 60; background: rgb(0 0 0 / .4); display: flex; align-items: center; justify-content: center; padding: 16px; }
.dialog-card { width: 100%; max-width: 480px; max-height: 85vh; overflow: auto; background: var(--surface); border-radius: 16px; padding: 18px 20px; }
.dialog-card h2 { margin: 0 0 8px; font-size: 19px; }
.dialog-card .primary { width: 100%; margin-top: 12px; }
.dialog-card .secondary { width: 100%; margin-top: 12px; }
.problems { padding-left: 20px; font-size: 14px; }
.problems li { margin: 4px 0; }
.rate-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 8px; }
.rate-option { display: flex; align-items: center; gap: 4px; padding: 8px 6px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; }
.w.plain { cursor: default; }

/* two toolbar rows: play controls, then display toggles (foldable) */
.toolbar.play-row { border-bottom: 0; padding-bottom: 4px; }
.toolbar.display-row { padding-top: 4px; }
.toolbar.display-row.collapsed { display: none; }
.toggle-chip { margin-left: auto; color: var(--muted); }

/* learn these first */
.lesson { margin: 10px 0 4px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.lesson summary { cursor: pointer; font-weight: 600; }
.lesson-sub { font-weight: 400; font-size: 13px; color: var(--muted); }
.lesson-words { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.lesson-word, .often-word { display: inline-flex; align-items: baseline; gap: 6px; border: 1px solid var(--line);
  background: var(--bg); border-radius: 10px; padding: 6px 10px; cursor: pointer; }
.lesson-word.known, .often-word.known { opacity: .45; text-decoration: line-through; }
.lw-lemma { font-size: 17px; }
.lw-reading { font-size: 12px; color: var(--muted); }
.lw-gloss { font-size: 13px; }
.lw-n { font-size: 11px; color: var(--muted); }
.often { margin-bottom: 20px; }

.home-actions .secondary + .secondary { margin-left: 8px; }
@media (max-width: 420px) { .home-actions .secondary { display: block; width: 100%; margin: 8px 0 0 !important; } }

/* your own song order */
.small-chip { padding: 4px 12px; font-size: 13px; margin-left: auto; margin-right: 4px; }
.layout-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 4px 0 10px; }
.layout-actions .secondary { padding: 8px 12px; font-size: 14px; }
.layout-actions .hint { flex-basis: 100%; margin: 0; }
.song-sep { display: flex; align-items: center; gap: 8px; margin: 10px 2px 2px; font-size: 13px; color: var(--muted); }
.song-sep::after { content: ""; flex: 1; border-top: 1px solid var(--line); order: 1; }
.song-sep .order-controls { order: 2; }
.sep-label:empty { display: none; }
.song-row { display: flex; align-items: center; gap: 6px; }
.song-row .song { flex: 1; min-width: 0; }
.song-row div.song { cursor: default; }
.order-controls { display: inline-flex; gap: 2px; }
.order-controls .icon-btn:disabled { opacity: .25; }
.sheet-origin { font-size: 13px; color: var(--accent); margin: 2px 0; }

/* lyrics written down by speech recognition */
.transcribed-note { font-size: 13px; color: var(--muted); border-left: 3px solid var(--accent); padding: 4px 8px; margin: 6px 0; }

/* space-separated languages: punctuation and clitics (, 's n't) stay attached to the word before */
.show-gloss .w.tight { margin-left: -3px; }

/* home: tabs 全部 | 最愛, album groups, favourites */
.tabs { display: flex; gap: 4px; margin: 6px 0 8px; border-bottom: 1px solid var(--line); }
.tab { background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 14px; font: inherit; color: var(--muted); cursor: pointer; }
.tab[aria-selected="true"] { color: var(--text, inherit); border-bottom-color: var(--accent); font-weight: 600; }
.sort-row, .album-jump { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 4px 0 8px; }
.sort-row .small-chip, .album-jump .small-chip { margin: 0; }
.songs.album { scroll-margin-top: 64px; }
.album-head { margin-top: 14px; }
.album-toggle { display: flex; align-items: center; gap: 8px; width: 100%; background: none; border: 0; padding: 4px 0; font: inherit; color: inherit; cursor: pointer; }
.album-toggle .sep-label { font-weight: 600; color: var(--text, inherit); }
.album-toggle::after { content: ""; flex: 1; border-top: 1px solid var(--line); order: 1; }
.album-count { order: 2; font-size: 12px; color: var(--muted); white-space: nowrap; }
.star { color: var(--muted); font-size: 18px; }
.star.on { color: #e0a800; }
.fav-empty { margin: 12px 2px; }
