:root {
  --bg: #f6f3ec;
  --surface: #fffdf8;
  --surface-2: #efe9dd;
  --text: #1f2330;
  --muted: #6b6f7b;
  --line: #e2dccf;
  --accent: #34478f;
  --accent-soft: #e6e9f5;
  --accent-text: #fffdf8;
  --ok: #2e7d4f;
  --ok-soft: #dff0e5;
  --star: #b7801a;
  --star-soft: #f8edd5;
  --mark: #f3e3a8;
  --shadow: 0 1px 2px rgba(40, 35, 20, .05), 0 3px 10px rgba(40, 35, 20, .05);
  --radius: 14px;
  --side: 380px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161c; --surface: #1c1f27; --surface-2: #262a35; --text: #e8e6e1; --muted: #9a9ca6; --line: #2f3340;
    --accent: #93a5ee; --accent-soft: #262d4a; --accent-text: #14161c; --ok: #74cf98; --ok-soft: #1a3326;
    --star: #e6b75a; --star-soft: #3a2f17; --mark: #4a3f1c; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14161c; --surface: #1c1f27; --surface-2: #262a35; --text: #e8e6e1; --muted: #9a9ca6; --line: #2f3340;
  --accent: #93a5ee; --accent-soft: #262d4a; --accent-text: #14161c; --ok: #74cf98; --ok-soft: #1a3326;
  --star: #e6b75a; --star-soft: #3a2f17; --mark: #4a3f1c; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans", "Noto Sans CJK SC", sans-serif;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:disabled { opacity: .4; cursor: default; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg { display: block; }
.ja { font-family: "Hiragino Mincho ProN", "Noto Serif CJK JP", "Yu Mincho", serif; }
.muted { color: var(--muted); }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }

/* ---------- shell: two panes on wide screens, one view at a time on phones ---------- */
.shell { min-height: 100dvh; }
#side { background: var(--bg); }
#main { min-width: 0; }
@media (min-width: 900px) {
  .shell { display: grid; grid-template-columns: var(--side) 1fr; }
  #side { position: sticky; top: 0; height: 100dvh; overflow-y: auto; border-right: 1px solid var(--line); }
  .only-narrow { display: none !important; }
}
@media (max-width: 899px) {
  body.view-list #main, body:not(.view-list) #side { display: none; }
}

/* ---------- side: library ---------- */
.side-head { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: calc(14px + var(--safe-t)) 16px 10px; }
.brand { display: flex; align-items: baseline; gap: 10px; margin-bottom: 12px; }
.brand h1 { margin: 0; font-size: 24px; letter-spacing: .04em; }
.brand .muted { font-size: 13px; }
.menu-wrap { margin-left: auto; align-self: center; position: relative; }
.avatar-btn { width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-size: 16px; font-weight: 600; }
.avatar-btn svg { width: 20px; height: 20px; }
.menu { position: absolute; right: 0; top: calc(100% + 8px); z-index: 20; min-width: 220px; padding: 6px; display: grid; }
.menu[hidden] { display: none; }
.menu a, .menu > button { padding: 10px 12px; border-radius: 10px; text-decoration: none; color: var(--text); font-size: 15px; text-align: left; }
.menu a:hover, .menu > button:hover { background: var(--surface-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; width: auto; }
.menu-user { padding: 8px 12px 6px; display: grid; }
.menu-user .muted { font-size: 12px; }
.menu-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 6px 6px 12px; font-size: 15px; }
.menu-row .seg button { font-size: 13px; padding: 3px 10px; }
.search { position: relative; }
.search svg { position: absolute; left: 12px; top: 50%; translate: 0 -50%; width: 18px; height: 18px; color: var(--muted); pointer-events: none; }
.search input {
  width: 100%; height: 42px; padding: 0 12px 0 38px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit; font-size: 16px; -webkit-appearance: none;
}
.levels { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 12px; }
.level { padding: 7px 0 6px; border-radius: 10px; text-align: center; background: var(--surface); border: 1px solid var(--line); }
.level b { display: block; font-size: 15px; line-height: 1.2; }
.level .meter { display: block; height: 3px; margin: 5px 8px 0; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.level .meter i { display: block; height: 100%; background: var(--ok); }
.level.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.level.on .meter { background: color-mix(in srgb, var(--accent-text) 30%, transparent); }
.level.on .meter i { background: var(--accent-text); }
.level-stat { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; gap: 8px; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { padding: 4px 10px; border-radius: 8px; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.review-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 12px; border-radius: 10px;
  background: var(--accent-soft); color: var(--accent); font-size: 14px; font-weight: 600; text-decoration: none;
}
.review-btn svg { width: 16px; height: 16px; }

.list { padding: 0 16px calc(24px + var(--safe-b)); }
.lesson { margin-top: 14px; }
.lesson-title { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); font-weight: 600; margin: 0 4px 6px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; text-decoration: none; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row:active, .row.on { background: var(--accent-soft); }
.row .dot { width: 22px; height: 22px; border-radius: 50%; flex: none; display: grid; place-items: center; font-size: 11px; color: var(--muted); border: 1.5px solid var(--line); font-variant-numeric: tabular-nums; }
.row .dot.ok { background: var(--ok); border-color: var(--ok); color: var(--surface); }
.row .dot svg { width: 13px; height: 13px; }
.row .txt { flex: 1; min-width: 0; }
.row .t { font-size: 18px; line-height: 1.35; }
.row .m { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .lv { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); flex: none; }
.row .star { width: 16px; height: 16px; color: var(--star); flex: none; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

/* ---------- main: detail ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-t)) 12px 8px; background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--line);
}
.topbar .crumb { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; flex: none; text-decoration: none; color: inherit; }
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn.star-on { color: var(--star); }

.doc { max-width: 720px; margin: 0 auto; padding: 14px 16px calc(100px + var(--safe-b)); }
.hero { padding: 0 4px 8px; }
.hero .g { font-size: clamp(30px, 8vw, 44px); line-height: 1.2; font-weight: 600; letter-spacing: .02em; }
.hero .meaning { font-size: 17px; margin-top: 4px; }
.hero .nuance { color: var(--muted); font-size: 14px; margin-top: 2px; line-height: 1.5; }
.status-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px; border-radius: 99px;
  border: 1.5px solid var(--line); background: var(--surface); font-size: 14px; font-weight: 600; color: var(--muted);
}
.pill svg { width: 16px; height: 16px; }
.pill.ok { border-color: var(--ok); background: var(--ok-soft); color: var(--ok); }
.pill.star { border-color: var(--star); background: var(--star-soft); color: var(--star); }

.sec { margin-top: 22px; }
.hero + .sec { margin-top: 12px; }
.sec-head { display: flex; justify-content: space-between; align-items: center; margin: 0 4px 8px; }
.sec-head h2 { margin: 0; font-size: 14px; color: var(--muted); font-weight: 600; letter-spacing: .05em; }
.formula { padding: 10px 14px; font-size: 15px; line-height: 1.7; }
.formula div + div { border-top: 1px dashed var(--line); margin-top: 4px; padding-top: 4px; }
.caution { margin-top: 10px; padding: 10px 14px; border-radius: var(--radius); background: var(--star-soft); color: var(--star); font-size: 15px; display: flex; gap: 10px; }
.caution svg { width: 20px; height: 20px; flex: none; margin-top: 2px; }
.ex { display: block; width: 100%; text-align: left; padding: 13px 16px; border-bottom: 1px solid var(--line); }
.ex:last-child { border-bottom: 0; }
.ex .ja { font-size: 19px; line-height: 1.75; }
.ex .tr { font-size: 14px; color: var(--muted); margin-top: 2px; }
.ex .tr.hidden { color: transparent; background: var(--surface-2); border-radius: 6px; user-select: none; }
.toggle { font-size: 13px; color: var(--accent); font-weight: 600; }
.about { padding: 16px; font-size: 16px; line-height: 1.8; }
.about p { margin: 0 0 12px; }
.about p:last-child { margin: 0; }
.source { text-align: center; font-size: 12px; color: var(--muted); margin-top: 20px; }
.source a { color: var(--muted); }

.foot {
  position: fixed; inset: auto 0 0 0; z-index: 6; padding: 8px 12px calc(8px + var(--safe-b));
  background: color-mix(in srgb, var(--surface) 94%, transparent); backdrop-filter: blur(12px); border-top: 1px solid var(--line);
}
@media (min-width: 900px) { .foot { left: var(--side); } }
.foot .in { max-width: 720px; margin: 0 auto; display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; align-items: center; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 46px; padding: 0 16px;
  border-radius: 12px; font-weight: 600; background: var(--surface-2); text-decoration: none; color: var(--text); font-size: 15px;
}
.btn svg { width: 18px; height: 18px; flex: none; }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.good { background: var(--ok); color: var(--surface); }
.btn.block { width: 100%; }
.btn[aria-disabled="true"] { opacity: .4; pointer-events: none; }
.btn .sub { font-weight: 400; font-size: 12px; opacity: .8; }
.pos { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; text-align: center; min-width: 64px; }

.placeholder { display: grid; place-items: center; min-height: 100dvh; color: var(--muted); text-align: center; padding: 24px; }
.placeholder .big { font-size: 64px; color: var(--line); }

/* ---------- review (flashcards) ---------- */
.review { max-width: 560px; margin: 0 auto; padding: 16px 16px calc(120px + var(--safe-b)); }
.progress { height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; margin: 4px 0 16px; }
.progress i { display: block; height: 100%; background: var(--accent); transition: width .2s; }
.flash { width: 100%; min-height: 52dvh; padding: 28px 20px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 10px; }
.flash .g { font-size: clamp(40px, 12vw, 60px); font-weight: 600; line-height: 1.25; }
.flash .hint { font-size: 14px; color: var(--muted); }
.flash .meaning { font-size: 19px; }
.flash .formula { padding: 0; font-size: 16px; color: var(--muted); }
.flash .ex { border: 0; text-align: center; padding: 6px 0; }
.flash hr { width: 60%; border: 0; border-top: 1px dashed var(--line); margin: 6px 0; }
.review .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.done { text-align: center; padding: 64px 16px; }
.done .big { font-size: 44px; font-weight: 700; }

.toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); z-index: 40; background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; }
.sync-warn { position: fixed; top: calc(6px + var(--safe-t)); right: 8px; z-index: 40; font-size: 12px; background: var(--star-soft); color: var(--star); padding: 3px 8px; border-radius: 8px; }

/* Japanese text + readings */
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag { font-size: 12px; padding: 2px 9px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.sec-head .seg button { font-size: 13px; padding: 3px 10px; }
.note { font-size: 13px; margin: 0 4px 8px; }
.about.ja-text { font-family: "Hiragino Mincho ProN", "Noto Serif CJK JP", "Yu Mincho", serif; font-size: 17px; line-height: 2.1; }
rt { font-size: .5em; color: var(--muted); font-family: -apple-system, "Hiragino Sans", sans-serif; }

/* top bar: the point's own title appears once the hero title scrolls away */
.topbar .point-title { color: var(--text); font-size: 18px; font-weight: 600; opacity: 0; translate: 0 6px; transition: opacity .15s, translate .15s; }
.topbar.scrolled .point-title { opacity: 1; translate: 0 0; }

/* ---------- login (public deployment only) ---------- */
.login { width: min(360px, calc(100% - 32px)); margin: 18vh auto 0; padding: 24px 20px; display: grid; gap: 14px; }
.login h1 { margin: 0 0 4px; font-size: 24px; }
.login label, .form label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.login input, .form input:not([type=checkbox]) { font: inherit; font-size: 16px;  /* under 16px iOS zooms in on focus and stays zoomed */ color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.login input:focus, .form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login-err, .form-err { color: var(--star); font-size: 14px; min-height: 1em; }
.form { padding: 16px; display: grid; gap: 12px; }
.form .check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.page { max-width: 560px; margin: 0 auto; padding: 16px 16px calc(40px + var(--safe-b)); }
.profile { display: flex; align-items: center; gap: 14px; padding: 16px; }
.avatar { width: 48px; height: 48px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-size: 22px; font-weight: 600; flex: none; }
.pname { font-size: 18px; font-weight: 600; }
.profile .muted, .user-row .muted { font-size: 13px; }
.stats { padding: 14px 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; }
.stats b { font-size: 26px; display: block; line-height: 1.2; }
.stats span { font-size: 13px; color: var(--muted); }
.stats .lvrow { grid-column: 1 / -1; display: grid; grid-template-columns: 48px 1fr 64px; align-items: center; gap: 10px; }
.stats .lvrow .meter { display: block; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.stats .lvrow .meter i { display: block; height: 100%; background: var(--ok); }
.stats .lvrow span:last-child { text-align: right; }
.page-actions { display: grid; gap: 10px; margin-top: 22px; }
.user-row { display: flex; align-items: center; gap: 10px; padding: 12px 16px; }
.user-row + .user-row { border-top: 1px solid var(--line); }
.u-main { flex: 1; min-width: 0; }
.u-acts { display: flex; gap: 12px; flex: none; }
.toggle.danger { color: var(--star); }
