:root {
  --bg: #0e1116;
  --surface: #171b23;
  --surface-2: #202633;
  --line: #2c3444;
  --text: #eef1f6;
  --muted: #8f9aad;
  --accent: #ff5a5f;
  --accent-ink: #ffffff;
  --good: #35d49a;
  --learn: #ffb547;
  --bad: #ff6b6b;
  --radius: 16px;
  --tabs-h: 64px;
  color-scheme: dark;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", "Meiryo", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans JP", "Noto Sans KR", sans-serif;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f5f8;
    --surface: #ffffff;
    --surface-2: #eceef3;
    --line: #dde1e8;
    --text: #151922;
    --muted: #5f6878;
    --accent: #e8434a;
    --good: #12a36f;
    --learn: #c9820c;
    --bad: #d93b3b;
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eceef3;
  --line: #dde1e8;
  --text: #151922;
  --muted: #5f6878;
  --accent: #e8434a;
  --good: #12a36f;
  --learn: #c9820c;
  --bad: #d93b3b;
  color-scheme: light;
}
:lang(ja) { font-family: "Hiragino Sans", "Yu Gothic UI", "Meiryo", "Noto Sans JP", system-ui, sans-serif; }
:lang(ko) { font-family: "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif; }

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.brand > div { min-width: 0; }
.brand img { border-radius: 8px; flex: none; }
.brand h1 { font-size: 1.05rem; letter-spacing: .02em; }
.brand p { margin: 0; font-size: .72rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: 6px; flex: none; }
.pill {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 5px 12px; font-size: .78rem; white-space: nowrap;
}

.view { max-width: 720px; margin: 0 auto; padding: 16px; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface-2);
  border-radius: 12px; padding: 10px 18px; font-weight: 600;
  transition: transform .08s ease, background .15s ease;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 12px; font-size: .85rem; }
.btn.big { width: 100%; padding: 14px; font-size: 1.05rem; margin-top: 16px; }
.btn.danger { width: 100%; background: transparent; color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, transparent); }
.btn.again { flex: 1; background: color-mix(in srgb, var(--bad) 16%, var(--surface)); border-color: color-mix(in srgb, var(--bad) 50%, transparent); }
.btn.good { flex: 1; background: color-mix(in srgb, var(--good) 18%, var(--surface)); border-color: color-mix(in srgb, var(--good) 55%, transparent); }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid transparent; background: transparent;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.speak { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.icon-btn.lg { width: 52px; height: 52px; }
.icon-btn.xl { width: 88px; height: 88px; border-radius: 50%; margin: 8px auto 12px; }
.icon-btn.xl svg { width: 40px; height: 40px; }
.icon-btn.fav { font-size: 1.3rem; color: var(--muted); }
.icon-btn.fav.on { color: var(--learn); }

/* ---------- chips & search ---------- */
.search input {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
}
.chips {
  display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 4px;
  scrollbar-width: none; margin: 0 -16px; padding-left: 16px; padding-right: 16px;
}
.chips::-webkit-scrollbar { display: none; }
.panel .chips { flex-wrap: wrap; overflow: visible; margin: 0; padding: 12px 0 0; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface);
  border-radius: 999px; padding: 6px 12px; font-size: .85rem; white-space: nowrap;
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip-n { opacity: .6; font-size: .75rem; }
.count { margin: 4px 0 8px; font-size: .78rem; color: var(--muted); }

/* ---------- phrase list ---------- */
.phrases { display: grid; gap: 10px; }
.phrase {
  display: flex; gap: 8px; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 10px 14px 16px; cursor: pointer;
}
.phrase:active { background: var(--surface-2); }
.ph-body { flex: 1; min-width: 0; }
.ph-target { font-size: 1.2rem; font-weight: 700; display: flex; align-items: center; gap: 8px; overflow-wrap: anywhere; }
.ph-read { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: .85rem; color: var(--accent); margin-top: 2px; }
.ph-read span + span { color: var(--muted); }
.ph-native { color: var(--muted); margin-top: 4px; font-size: .92rem; }
.ph-note {
  margin: 8px 0 0; font-size: .8rem; color: var(--muted);
  background: var(--surface-2); border-radius: 10px; padding: 6px 10px;
}
.ph-actions { display: flex; flex-direction: column; gap: 4px; flex: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--line); }
.dot-learn { background: var(--learn); }
.dot-done { background: var(--good); }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------- panels ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; margin-bottom: 12px;
}
.panel > p { margin: 6px 0 0; }
.center { text-align: center; }
.center .btn { margin: 8px 4px 0; }
.big-emoji { font-size: 3rem; line-height: 1; margin-bottom: 8px; }
.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 16px 0; }
.stat-row.three { grid-template-columns: repeat(3, 1fr); }
.stat { background: var(--surface-2); border-radius: 12px; padding: 12px; text-align: center; }
.stat b { display: block; font-size: 1.6rem; line-height: 1.2; }
.stat span { font-size: .78rem; color: var(--muted); }
.c-done { color: var(--good); }
.c-learn { color: var(--learn); }
.switch { display: flex; align-items: center; gap: 10px; margin-top: 12px; font-size: .92rem; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }

/* ---------- flashcards ---------- */
.progress { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress div { height: 100%; background: var(--accent); transition: width .3s ease; }
.fc-top { display: flex; justify-content: space-between; align-items: center; margin: 12px 0; font-size: .85rem; }
.tag { background: var(--surface-2); border-radius: 999px; padding: 3px 10px; }
.flashcard {
  display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 6px;
  width: 100%; min-height: 280px; padding: 28px 20px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px;
  text-align: center;
}
.flashcard hr { width: 60%; border: 0; border-top: 1px dashed var(--line); margin: 14px 0; }
.flashcard .ph-read { justify-content: center; font-size: 1rem; }
.fc-big { font-size: 1.6rem; font-weight: 700; overflow-wrap: anywhere; }
.fc-ans { font-size: 1.9rem; font-weight: 800; color: var(--text); overflow-wrap: anywhere; }
.hint { color: var(--muted); font-size: .85rem; margin-top: 18px; }
.fc-actions { display: flex; gap: 10px; align-items: center; margin-top: 14px; }
.fc-actions .btn { padding: 14px; font-size: 1rem; }

/* ---------- quiz ---------- */
.segs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 14px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.seg { border: 0; background: transparent; padding: 9px 4px; border-radius: 9px; font-size: .82rem; font-weight: 600; color: var(--muted); }
.seg.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .25); }
.quiz-q .q-label { color: var(--muted); font-size: .85rem; margin: 0 0 10px; }
.quiz-q .ph-read { justify-content: center; }
.opts { display: grid; gap: 8px; }
.opt {
  text-align: left; padding: 14px 16px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--surface); font-size: 1.02rem;
}
.opt small { display: block; color: var(--muted); font-size: .8rem; }
.opt:disabled { cursor: default; opacity: .6; }
.opt.right { border-color: var(--good); background: color-mix(in srgb, var(--good) 16%, var(--surface)); opacity: 1; }
.opt.bad { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 14%, var(--surface)); opacity: 1; }
.feedback {
  position: sticky; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 8px);
  display: flex; align-items: center; gap: 10px; margin-top: 12px;
  padding: 10px 10px 10px 16px; border-radius: 14px; background: var(--surface-2); border: 1px solid var(--line);
}
.feedback b { flex: 1; }
.feedback.ok b { color: var(--good); }
.feedback.ng b { color: var(--bad); }
.miss { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.miss li { display: flex; flex-direction: column; border-bottom: 1px solid var(--line); padding-bottom: 8px; }
.miss li:last-child { border: 0; }
.row-btns { display: flex; gap: 8px; }
.row-btns .btn { flex: 1; padding: 13px; }

/* ---------- stats ---------- */
.bar { display: flex; height: 10px; border-radius: 99px; overflow: hidden; background: var(--surface-2); }
.b-done { background: var(--good); }
.b-learn { background: var(--learn); }
.streak { margin-top: 14px; font-size: .95rem; }
.cat-stats { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.cat-line { display: flex; justify-content: space-between; font-size: .9rem; margin-bottom: 4px; }
.cat-stats .bar { height: 6px; }

/* ---------- tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabs button {
  border: 0; background: transparent; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: .72rem; font-weight: 600;
}
.tabs button .tab-ico { font-size: 1.25rem; filter: grayscale(1); opacity: .7; }
.tabs button.on { color: var(--text); }
.tabs button.on .tab-ico { filter: none; opacity: 1; }

/* ---------- dialog ---------- */
dialog {
  width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 40px);
  border: 1px solid var(--line); border-radius: 20px; padding: 20px;
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .55); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.field { display: grid; gap: 6px; margin-bottom: 14px; font-size: .9rem; }
.field select, .field input[type="range"] { width: 100%; }
.field select { padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); }
.field input[type="range"] { accent-color: var(--accent); }
#settings-body > .btn { margin-top: 14px; }
.ok { color: var(--good); font-size: .85rem; }
.warn { color: var(--learn); font-size: .85rem; }

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

@media (min-width: 720px) {
  .chips { flex-wrap: wrap; overflow: visible; margin: 0; padding-left: 0; padding-right: 0; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
