/* ===== Theme variables ===== */
:root {
  --bg: #f5f6f8;
  --bg-soft: #eef0f4;
  --card: #ffffff;
  --fg: #1f2430;
  --fg-soft: #5b6472;
  --fg-faint: #9aa3b2;
  --border: #e4e7ec;
  --accent: #4f6df5;
  --accent-soft: #e8edff;
  --danger: #e5484d;
  --danger-soft: #fdecec;
  --ok: #2f9e6e;
  --ok-soft: #e6f5ee;
  --shadow: 0 1px 3px rgba(20, 30, 60, .08);
  --radius: 14px;
}
html[data-theme="dark"] {
  --bg: #14161c;
  --bg-soft: #1c1f27;
  --card: #20242e;
  --fg: #e8eaf0;
  --fg-soft: #a6adbc;
  --fg-faint: #6c7484;
  --border: #2d323d;
  --accent: #6f8bff;
  --accent-soft: #26304d;
  --danger: #ff6b70;
  --danger-soft: #3a2426;
  --ok: #4cc38a;
  --ok-soft: #1f332b;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, "PingFang SC", "Hiragino Sans", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.6;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select { font-family: inherit; }
a { color: var(--accent); text-decoration: none; }

/* ===== Header ===== */
.app-header {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.header-title { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 34px; height: 34px; border-radius: 9px;
  display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 18px;
}
.app-header h1 { font-size: 17px; font-weight: 650; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px;
  display: grid; place-items: center; font-size: 18px;
  background: var(--bg-soft);
}

/* ===== View / main ===== */
.view {
  max-width: 680px; margin: 0 auto;
  padding: 16px 16px 88px;
  min-height: calc(100vh - 61px);
}

/* ===== Tabbar ===== */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex; justify-content: center;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tabbar .tab {
  flex: 1; max-width: 110px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 0; border-radius: 10px;
  font-size: 11px; color: var(--fg-soft);
  transition: color .15s;
}
.tabbar .tab .ico { font-size: 20px; line-height: 1; }
.tabbar .tab.active { color: var(--accent); font-weight: 600; }
.tabbar .tab.active .ico { transform: translateY(-1px); }

/* ===== Generic components ===== */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 14px;
}
.section-title { font-size: 15px; font-weight: 650; margin: 4px 0 12px; }
.muted { color: var(--fg-soft); }
.faint { color: var(--fg-faint); }
.small { font-size: 12px; }

.searchbar {
  display: flex; gap: 8px; margin-bottom: 14px;
}
.searchbar input {
  flex: 1; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--card); color: var(--fg);
  font-size: 15px; outline: none;
}
.searchbar input:focus { border-color: var(--accent); }
.searchbar select {
  padding: 10px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--card); color: var(--fg);
  font-size: 14px; outline: none; max-width: 120px;
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 18px; border-radius: 12px;
  background: var(--accent); color: #fff; font-size: 15px; font-weight: 600;
  transition: opacity .15s;
}
.btn:hover { opacity: .9; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.block { width: 100%; }
.btn.ghost { background: var(--bg-soft); color: var(--fg); }
.btn.danger { background: var(--danger); }
.btn.small { padding: 6px 12px; font-size: 13px; border-radius: 9px; }

.tag {
  display: inline-block; padding: 2px 8px; border-radius: 6px;
  font-size: 11px; background: var(--accent-soft); color: var(--accent);
  white-space: nowrap;
}
.tag.pos { background: var(--bg-soft); color: var(--fg-soft); }
.tag.ok { background: var(--ok-soft); color: var(--ok); }
.tag.danger { background: var(--danger-soft); color: var(--danger); }

/* ===== List rows ===== */
.list-row {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 14px; border-radius: 12px;
  background: var(--card); border: 1px solid var(--border);
  margin-bottom: 8px; cursor: pointer;
  transition: transform .08s, border-color .15s;
}
.list-row:active { transform: scale(.99); border-color: var(--accent); }
.list-row .main { flex: 1; min-width: 0; }
.list-row .title { font-size: 16px; font-weight: 600; }
.list-row .sub { font-size: 13px; color: var(--fg-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ===== Word card (flashcard) ===== */
.flashcard-wrap { perspective: 1000px; max-width: 420px; margin: 20px auto; }
.flashcard {
  position: relative; width: 100%; height: 260px;
  transform-style: preserve-3d; transition: transform .5s;
}
.flashcard.flipped { transform: rotateY(180deg); }
.flashcard .face {
  position: absolute; inset: 0; backface-visibility: hidden;
  border-radius: 18px; border: 1px solid var(--border);
  box-shadow: var(--shadow); background: var(--card);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px; text-align: center;
}
.flashcard .back { transform: rotateY(180deg); }
.flashcard .jp { font-size: 34px; font-weight: 700; }
.flashcard .jp.small { font-size: 24px; }
.flashcard .kana { font-size: 16px; color: var(--fg-soft); margin-top: 6px; }
.flashcard .pos-line { margin-top: 14px; }
.flashcard .meaning { font-size: 16px; margin-top: 10px; color: var(--fg); }
.flashcard .hint { margin-top: 16px; font-size: 12px; color: var(--fg-faint); }

/* ===== Quiz ===== */
.option {
  display: block; width: 100%; text-align: left;
  padding: 13px 16px; margin-bottom: 10px; border-radius: 12px;
  background: var(--card); border: 1px solid var(--border); color: var(--fg);
  font-size: 15px; transition: border-color .12s, background .12s;
}
.option:active { border-color: var(--accent); }
.option.correct { border-color: var(--ok); background: var(--ok-soft); }
.option.wrong { border-color: var(--danger); background: var(--danger-soft); }
.option:disabled { cursor: default; opacity: .85; }

.progress { height: 6px; border-radius: 3px; background: var(--bg-soft); overflow: hidden; margin-bottom: 16px; }
.progress > i { display: block; height: 100%; background: var(--accent); transition: width .3s; }

.stat-grid { display: flex; gap: 10px; margin-bottom: 14px; }
.stat-grid .stat {
  flex: 1; text-align: center; padding: 12px 8px; border-radius: 12px;
  background: var(--card); border: 1px solid var(--border);
}
.stat-grid .stat .num { font-size: 22px; font-weight: 700; }
.stat-grid .stat .lbl { font-size: 11px; color: var(--fg-soft); }

/* ===== AI explain box ===== */
.explain {
  background: var(--accent-soft); border: 1px solid var(--accent);
  border-radius: 12px; padding: 14px; margin-top: 16px; font-size: 14px;
}
.explain pre { white-space: pre-wrap; font-family: inherit; }

/* ===== Misc ===== */
.empty { text-align: center; color: var(--fg-faint); padding: 40px 16px; }
.spinner {
  width: 26px; height: 26px; border: 3px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite; margin: 20px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: var(--fg); color: var(--bg); padding: 9px 18px;
  border-radius: 20px; font-size: 14px; z-index: 99; opacity: 0;
  transition: opacity .25s; pointer-events: none; max-width: 80vw; text-align: center;
}
.toast.show { opacity: .95; }

.kanji-big {
  font-size: 64px; font-weight: 700; text-align: center; line-height: 1.2;
}
.freq-bar { height: 4px; border-radius: 2px; background: var(--accent); margin: 8px 0; }
