:root {
  --bg: #0b0d12;
  --panel: rgba(20, 23, 31, 0.86);
  --panel-solid: #161a23;
  --line: rgba(255, 255, 255, 0.12);
  --text: #f3f4f7;
  --muted: #9aa1b2;
  --accent: #ff5a5f;
  --accent-2: #ffd166;
  --ok: #6ee7b7;
  --jp: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Noto Sans CJK JP", "Yu Gothic", "Meiryo", sans-serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --rw: 0.82;
  --rh: 0.16;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 16px/1.45 var(--ui); }
body.cam { overflow: hidden; overscroll-behavior: none; touch-action: manipulation; position: fixed; inset: 0; }
button { font: inherit; color: inherit; cursor: pointer; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- Camera ---------- */
#video { position: fixed; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }

#reticle {
  position: fixed; left: 50%; top: 38%;
  width: calc(var(--rw) * 100vw); height: calc(var(--rh) * 100vh);
  transform: translate(-50%, -50%);
  border-radius: 14px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.42);
  transition: box-shadow .25s;
  touch-action: none;
}
#reticle .c { position: absolute; width: 26px; height: 26px; border: 3px solid #fff; pointer-events: none; }
#reticle .tl { left: -2px; top: -2px; border-right: 0; border-bottom: 0; border-radius: 14px 0 0 0; }
#reticle .tr { right: -2px; top: -2px; border-left: 0; border-bottom: 0; border-radius: 0 14px 0 0; }
#reticle .bl { left: -2px; bottom: -2px; border-right: 0; border-top: 0; border-radius: 0 0 0 14px; }
#reticle .br { right: -2px; bottom: -2px; border-left: 0; border-top: 0; border-radius: 0 0 14px 0; }
#handle {
  position: absolute; right: -18px; bottom: -18px; width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center; touch-action: none;
}
#handle::after { content: ""; width: 14px; height: 14px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 4px rgba(255,255,255,.25); }
#freeze { position: absolute; inset: 0; width: 100%; height: 100%; border-radius: 12px; opacity: 0; transition: opacity .2s; pointer-events: none; }
#reticle.frozen #freeze { opacity: 1; }
#reticle.frozen .c { border-color: var(--accent-2); }
.scanline { position: absolute; left: 6px; right: 6px; top: 0; height: 2px; opacity: 0; pointer-events: none;
  background: linear-gradient(90deg, transparent, var(--accent-2), transparent); box-shadow: 0 0 12px var(--accent-2); }
#reticle.busy .scanline { opacity: 1; animation: sweep 1.1s ease-in-out infinite alternate; }
#reticle.vertical .scanline { top: 6px; bottom: 6px; left: 0; width: 2px; height: auto;
  background: linear-gradient(180deg, transparent, var(--accent-2), transparent); }
#reticle.vertical.busy .scanline { animation-name: sweep-x; }
@keyframes sweep { from { top: 4px; } to { top: calc(100% - 6px); } }
@keyframes sweep-x { from { left: 4px; } to { left: calc(100% - 6px); } }
#reticle.flash { animation: flash .25s; }
@keyframes flash { 0% { box-shadow: 0 0 0 100vmax rgba(255,255,255,.55); } 100% { box-shadow: 0 0 0 100vmax rgba(0,0,0,.42); } }

/* ---------- Result overlay (anchored under reticle) ---------- */
#result {
  position: fixed; left: 50%; transform: translateX(-50%);
  width: min(94vw, 640px); max-height: 34vh; overflow: auto;
  background: var(--panel); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px 8px;
  animation: rise .22s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }
#result-text, .scan-text { font: 500 24px/2.1 var(--jp); word-break: break-all; white-space: pre-line; }
#result-text.empty, .scan-text.empty { font: 15px/1.4 var(--ui); color: var(--muted); }
.result-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
#result-meta { flex: 1; font-size: 12px; color: var(--muted); }
.chip { border: 1px solid var(--line); background: rgba(255,255,255,.06); border-radius: 999px; padding: 4px 12px; font-size: 13px; }

.word, .kchar {
  border: 0; background: none; padding: 0 1px; margin: 0; font: inherit; color: inherit;
  border-bottom: 2px solid rgba(255, 209, 102, .55); border-radius: 2px;
}
.word:active, .kchar:active, .word:focus-visible, .kchar:focus-visible { background: rgba(255, 209, 102, .2); outline: none; }
.kchar { border-bottom-style: dotted; border-bottom-color: rgba(255,255,255,.35); }
ruby rt { font-size: .5em; color: var(--accent-2); letter-spacing: .02em; }

/* ---------- Top bar & controls ---------- */
#topbar {
  position: fixed; left: 0; right: 0; top: 0; display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-t) + 10px) 12px 10px;
  background: linear-gradient(rgba(0,0,0,.55), transparent);
}
.spacer { flex: 1; }
.icon-btn { width: 44px; height: 44px; border: 0; border-radius: 50%; background: rgba(0,0,0,.35); display: grid; place-items: center; }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.icon-btn.on { background: var(--accent-2); color: #111; }
.pill { height: 36px; min-width: 52px; padding: 0 12px; border: 1px solid var(--line); border-radius: 999px; background: rgba(0,0,0,.35); font: 600 15px var(--jp); }
.pill.sharp { background: var(--accent-2); color: #111; border-color: transparent; }
#zoom-wrap { display: flex; align-items: center; }
#zoom { width: 110px; accent-color: #fff; }

#controls {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; flex-direction: column; align-items: center;
  padding: 12px 12px calc(var(--safe-b) + 14px);
  background: linear-gradient(transparent, rgba(0,0,0,.55));
}
#shutter { width: 78px; height: 78px; border-radius: 50%; border: 4px solid #fff; background: transparent; padding: 5px; }
#shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: #fff; transition: transform .12s, background .2s; }
#shutter:active span { transform: scale(.88); }
#shutter.busy span { background: var(--accent-2); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(.8); } }
#hint { margin: 8px 0 0; font-size: 13px; color: rgba(255,255,255,.8); text-shadow: 0 1px 3px #000; }

/* ---------- Bottom sheet ---------- */
#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.25); }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-height: 82vh; display: flex; flex-direction: column;
  background: var(--panel); backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--line); border-radius: 20px 20px 0 0;
  padding: 6px 16px calc(var(--safe-b) + 16px);
  animation: up .24s cubic-bezier(.2,.8,.2,1);
  max-width: 720px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(100%); } to { transform: none; } }
.grab { width: 40px; height: 5px; border-radius: 3px; background: rgba(255,255,255,.3); margin: 4px auto 6px; touch-action: none; }
.sheet-head { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.sheet-head h2 { flex: 1; margin: 0; font: 600 17px var(--ui); }
.sheet-head .icon-btn { background: rgba(255,255,255,.06); }
#sheet-body { overflow: auto; overscroll-behavior: contain; padding-bottom: 6px; }

.word-head { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; margin: 4px 0 10px; }
.word-big { font: 600 40px/1.2 var(--jp); }
.word-reading { font: 500 20px var(--jp); color: var(--accent-2); }
.word-base { font-size: 13px; color: var(--muted); width: 100%; }
.word-base b { font-family: var(--jp); color: var(--text); }
.glosses { margin: 0 0 14px; padding-left: 22px; }
.glosses li { margin: 3px 0; }
.section-label { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 14px 0 8px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.tile { border: 1px solid var(--line); background: rgba(255,255,255,.04); border-radius: 14px; padding: 10px 8px; text-align: center; }
.tile .k { display: block; font: 500 38px/1.1 var(--jp); }
.tile .m { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .r { display: block; font: 12px var(--jp); color: var(--accent-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.kanji-view { display: grid; grid-template-columns: minmax(150px, 220px) 1fr; gap: 16px; align-items: start; }
@media (max-width: 460px) { .kanji-view { grid-template-columns: 1fr; } .stroke-box { max-width: 240px; margin: 0 auto; } }
.stroke-box { background: #fbfaf6; border-radius: 14px; aspect-ratio: 1; position: relative; }
.stroke-box svg { width: 100%; height: 100%; display: block; }
.stroke-box .guide { stroke: #d9d4c7; stroke-width: .5; stroke-dasharray: 2 2; fill: none; }
.stroke-box .ghost { stroke: #e3ded2; stroke-width: 3; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.stroke-box .ink { stroke: #1b1b1f; stroke-width: 3.2; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.stroke-box .ink.current { stroke: var(--accent); }
.stroke-box .num { font: 600 6px var(--ui); fill: var(--accent); }
.stroke-box .nostroke { position: absolute; inset: 0; display: grid; place-items: center; font: 110px var(--jp); color: #1b1b1f; }
.stroke-ctl { display: flex; gap: 6px; justify-content: center; margin-top: 8px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0; }
.facts dt { color: var(--muted); font-size: 13px; padding-top: 2px; }
.facts dd { margin: 0; font-family: var(--jp); }
.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.badge { font-size: 12px; border-radius: 999px; padding: 2px 10px; background: rgba(255,255,255,.08); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { border-bottom: 1px solid var(--line); }
.list button { width: 100%; text-align: left; border: 0; background: none; padding: 10px 2px; display: flex; gap: 12px; align-items: center; }
.list img { width: 64px; height: 40px; object-fit: cover; border-radius: 6px; background: #222; flex: none; }
.list .t { font: 17px var(--jp); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.list .d { font-size: 12px; color: var(--muted); }
.scan-view .scan-text { font-size: 22px; }
.explain-src { margin: 0; font: 20px/1.6 var(--jp); white-space: pre-line; }
.explain-tr { margin: 0; font-size: 18px; font-weight: 600; }
.explain-lit { margin: 0; color: var(--muted); font-style: italic; }
.notes { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 0; }
.notes dt { font: 600 17px var(--jp); color: var(--accent-2); }
.notes dd { margin: 0; }
.explain-meta { margin: 6px 0 0; font-size: 12px; color: var(--muted); }
.scan-thumb { width: 100%; max-height: 180px; object-fit: contain; border-radius: 12px; background: #000; }

.btn { border: 0; border-radius: 12px; padding: 12px 18px; background: var(--accent); color: #fff; font-weight: 600; }
.btn.ghost { background: rgba(255,255,255,.08); color: var(--text); }
.stack { display: grid; gap: 10px; }
.stat { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
.stat b { font-variant-numeric: tabular-nums; }
.seg { display: flex; background: rgba(255,255,255,.06); border-radius: 12px; padding: 3px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 9px; }
.seg button[aria-pressed="true"] { background: rgba(255,255,255,.16); font-weight: 600; }

#camera-error { position: fixed; inset: 0; display: grid; place-content: center; text-align: center; padding: 24px; background: var(--bg); gap: 6px; }
#camera-error h2 { margin: 0; }
#camera-error .btn { justify-self: center; margin-top: 10px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 130px); transform: translateX(-50%);
  background: rgba(20,23,31,.95); border: 1px solid var(--line); padding: 8px 14px; border-radius: 999px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; }

/* ---------- Login / about pages ---------- */
body.page { display: grid; place-items: center; min-height: 100%; padding: 24px 16px; }
.card { width: min(380px, 100%); background: var(--panel-solid); border: 1px solid var(--line); border-radius: 20px; padding: 28px 22px; }
.card h1 { margin: 0 0 4px; font: 700 26px var(--jp); }
.card h1 span { color: var(--accent); }
.card p { margin: 0 0 18px; color: var(--muted); }
.card input { width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line); background: #0f1218; color: var(--text); font-size: 17px; }
.card input:focus { outline: 2px solid var(--accent-2); border-color: transparent; }
.card .btn { width: 100%; margin-top: 12px; font-size: 16px; }
.card .btn:disabled { opacity: .5; }
.err { min-height: 22px; margin-top: 10px; color: #ff8a8f; font-size: 14px; }
.prose { max-width: 680px; }
.prose a { color: var(--accent-2); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
}
