:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece4;
  --text: #1f2328;
  --muted: #6b7078;
  --border: #e1ddd2;
  --accent: #d64545;
  --accent-ink: #ffffff;
  --user: #1f2937;
  --user-ink: #f9fafb;
  --ok: #2f9e6b;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.06);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f25;
    --surface-2: #262a32;
    --text: #e8eaed;
    --muted: #9aa0aa;
    --border: #2d323b;
    --accent: #ef5b5b;
    --user: #3a4256;
    --user-ink: #f3f4f6;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Yu Gothic", sans-serif;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
kbd {
  font: 600 .8em/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: .25em .55em;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface);
}

/* Header */
header {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.brand { font-weight: 700; font-size: 1.15rem; letter-spacing: .02em; margin-right: auto; display: flex; align-items: baseline; gap: 8px; }
.brand small { color: var(--muted); font-weight: 500; font-size: .8rem; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); font-size: .85rem; color: var(--muted);
  text-decoration: none;
  transition: background .15s, color .15s, border-color .15s;
}
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip:hover { border-color: var(--muted); }
.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface); color: var(--muted);
}
.icon-btn:hover { color: var(--text); border-color: var(--muted); }
.icon-btn svg { width: 18px; height: 18px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Conversation */
main {
  flex: 1; min-height: 0;
  overflow-y: auto;
  padding: 20px 16px 8px;
  scroll-behavior: smooth;
}
.thread { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; flex-direction: column; max-width: 88%; animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.bubble {
  padding: 10px 14px; border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}
.user .bubble { background: var(--user); color: var(--user-ink); border-bottom-right-radius: 4px; }
.assistant .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; font-size: 1.05rem; line-height: 2; }
.bubble.error { border-color: var(--accent); color: var(--accent); }
ruby { ruby-position: over; }
rt { font-size: .5em; color: var(--muted); font-weight: 500; user-select: none; }
body.nofuri rt { display: none; }
.meta { display: flex; gap: 6px; margin-top: 4px; min-height: 28px; align-items: center; color: var(--muted); font-size: .78rem; }
.mini {
  display: inline-flex; align-items: center; gap: 4px; height: 26px; padding: 0 9px;
  border: 1px solid var(--border); border-radius: 999px; background: transparent; color: var(--muted); font-size: .78rem;
}
.mini:hover { color: var(--text); border-color: var(--muted); }
.mini svg { width: 13px; height: 13px; }
.mini.playing { color: var(--accent); border-color: var(--accent); }
.dots span { display: inline-block; width: 6px; height: 6px; margin: 0 2px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.empty { text-align: center; color: var(--muted); padding: 8vh 12px 0; }
.empty h1 { font-size: 2.4rem; margin: 0 0 4px; color: var(--text); letter-spacing: .06em; }
.empty p { margin: 6px auto; max-width: 420px; }
.empty .start { margin-top: 18px; display: inline-flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

/* Dock */
footer {
  padding: 10px 16px;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.dock { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.status { min-height: 1.4em; font-size: .9rem; color: var(--muted); text-align: center; }
.status b { color: var(--text); font-weight: 600; }
.ptt-wrap { position: relative; width: 104px; height: 104px; }
.ptt {
  position: absolute; inset: 0; margin: auto; width: 88px; height: 88px; border-radius: 50%;
  border: 0; background: var(--text); color: var(--bg);
  display: grid; place-items: center;
  box-shadow: var(--shadow);
  transition: transform .12s, background .15s;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.ptt svg { width: 34px; height: 34px; }
.ptt:hover { transform: scale(1.03); }
.ptt::before { /* level ring */
  content: ""; position: absolute; inset: -6px; border-radius: 50%;
  border: 3px solid var(--accent); opacity: 0;
  transform: scale(calc(1 + var(--level, 0) * .28));
  transition: opacity .15s;
}
.ptt[data-state="recording"] { background: var(--accent); color: var(--accent-ink); transform: scale(1.06); }
.ptt[data-state="recording"]::before { opacity: .55; }
.ptt[data-state="transcribing"], .ptt[data-state="thinking"] { background: var(--surface-2); color: var(--muted); }
.ptt[data-state="speaking"] { background: var(--ok); color: #fff; }
.ptt[data-state="transcribing"]::after, .ptt[data-state="thinking"]::after {
  content: ""; position: absolute; inset: -6px; border-radius: 50%;
  border: 3px solid transparent; border-top-color: var(--muted); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.row { display: flex; gap: 8px; width: 100%; max-width: 520px; }
.row input[type="text"] {
  flex: 1; min-width: 0; height: 40px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
}
.row input[type="text"]::placeholder { color: var(--muted); }
.send { height: 40px; padding: 0 16px; border: 0; border-radius: 999px; background: var(--text); color: var(--bg); font-weight: 600; }
.send:disabled { opacity: .4; cursor: default; }
.cost { font-size: .76rem; color: var(--muted); text-align: center; font-variant-numeric: tabular-nums; display: flex; gap: 4px 12px; flex-wrap: wrap; justify-content: center; }
.cost b { color: var(--text); font-weight: 600; }
.cost .sep { opacity: .5; }
.hint { font-size: .78rem; color: var(--muted); text-align: center; }

/* Settings */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  padding: 0; border: 1px solid var(--border); border-radius: 20px;
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
dialog::backdrop { background: rgba(0,0,0,.45); backdrop-filter: blur(2px); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 8px; position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dlg-head h2 { margin: 0; font-size: 1.1rem; }
.dlg-body { padding: 4px 20px 20px; display: grid; gap: 16px; overflow-y: auto; max-height: calc(100dvh - 120px); }
fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 12px; }
legend { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding: 0; margin-bottom: 8px; font-weight: 700; }
label.f { display: grid; gap: 4px; font-size: .88rem; font-weight: 600; }
label.f small { font-weight: 400; color: var(--muted); }
.f input[type="text"], .f input[type="password"], .f select, .f textarea {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); font-weight: 400;
}
.f textarea { min-height: 170px; resize: vertical; line-height: 1.45; font-size: .9rem; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 460px) { .two { grid-template-columns: 1fr; } }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: .9rem; }
.check input { margin-top: 4px; width: 16px; height: 16px; accent-color: var(--accent); }
.check small { display: block; color: var(--muted); }
.inline { display: flex; gap: 8px; }
.inline > :first-child { flex: 1; }
.btn {
  height: 38px; padding: 0 14px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); font-size: .85rem;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  color: inherit; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.danger { color: var(--accent); }
.btn.primary { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 600; }
.range { display: flex; align-items: center; gap: 10px; }
.range input { flex: 1; accent-color: var(--accent); }
.range output { width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.note { font-size: .78rem; color: var(--muted); margin: 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(190px + env(safe-area-inset-bottom)); transform: translate(-50%, 8px);
  max-width: min(520px, calc(100vw - 32px)); padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: .88rem; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 10; box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--accent); color: #fff; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; scroll-behavior: auto !important; } }
@media (hover: none) { .hint .kbd-only { display: none; } }

/* ---------- App shell ---------- */
.brand { flex-shrink: 0; }
nav.pages {
  display: flex; gap: 4px; align-items: center;
  margin-right: auto; min-width: 0; overflow-x: auto; scrollbar-width: none;
}
nav.pages::-webkit-scrollbar { display: none; }
nav.pages a {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 12px; border-radius: 999px;
  color: var(--muted); text-decoration: none; font-size: .85rem; white-space: nowrap;
  border: 1px solid transparent;
}
nav.pages a:hover { color: var(--text); border-color: var(--border); }
nav.pages a[aria-current="page"] { background: var(--text); color: var(--bg); }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px;
  display: inline-grid; place-items: center;
  background: var(--accent); color: #fff; font-size: .7rem; font-weight: 700;
}
nav.pages a[aria-current="page"] .badge { background: var(--bg); color: var(--text); }
header form { display: contents; }

.page { width: 100%; max-width: 860px; margin: 0 auto; padding: 18px 4px 48px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { font-size: 1.4rem; margin: 0; letter-spacing: .01em; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px;
}
.card + .card { margin-top: 12px; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.stack { display: grid; gap: 12px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  height: 26px; padding: 0 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: .76rem; color: var(--muted); font-variant-numeric: tabular-nums;
}
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--border)); }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat b { display: block; font-size: 1.5rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat small { color: var(--muted); font-size: .78rem; }

/* ---------- Forms ---------- */
.field { display: grid; gap: 5px; margin-bottom: 12px; font-size: .88rem; font-weight: 600; }
.field small { font-weight: 400; color: var(--muted); }
.field input, .field select, .field textarea, .control {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); font-weight: 400; font-size: .95rem;
}
.form-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }

/* ---------- Review ---------- */
.review-card { max-width: 620px; margin: 0 auto; }
.review-meta { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.review-front {
  font-size: 1.9rem; line-height: 2; text-align: center;
  padding: 26px 6px; min-height: 120px;
  display: grid; place-items: center; gap: 6px;
}
.review-front .hint-line { font-size: .95rem; color: var(--muted); line-height: 1.7; }
.review-answer { border-top: 1px dashed var(--border); margin-top: 8px; padding-top: 16px; }
.review-answer h3 { margin: 0 0 4px; font-size: 1.4rem; }
.review-answer .reading { color: var(--muted); font-size: .95rem; margin-bottom: 8px; }
.review-answer .example { margin-top: 12px; line-height: 2; }
.grade-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 18px; }
.grade {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 10px 4px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface-2); font-weight: 700; font-size: .9rem;
}
.grade small { font-weight: 500; font-size: .72rem; color: var(--muted); }
.grade:hover { border-color: var(--muted); }
.grade[data-rating="1"] { color: #c23b3b; }
.grade[data-rating="2"] { color: #b7791f; }
.grade[data-rating="3"] { color: var(--ok); }
.grade[data-rating="4"] { color: #2b6cb0; }
.grade kbd { font-size: .65rem; }

/* ---------- Items ---------- */
.item-list { display: grid; gap: 8px; }
.item-row {
  display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center;
  padding: 10px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
}
.item-term { font-size: 1.05rem; font-weight: 600; }
.item-term rt { font-size: .55em; }
.item-sub { font-size: .8rem; color: var(--muted); }
.item-actions { display: flex; gap: 6px; }
.item-row.inactive { opacity: .55; }
.empty-note { text-align: center; color: var(--muted); padding: 28px 12px; }

/* ---------- Stats ---------- */
.bars { display: flex; align-items: flex-end; gap: 3px; height: 90px; margin-top: 10px; }
.bars span { flex: 1; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; opacity: .8; }
.bars span[data-zero="1"] { opacity: .15; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .78rem; color: var(--muted); }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 4px; }

/* ---------- Auth ---------- */
body.auth-page { display: block; }
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth .card { width: min(430px, 100%); }
.auth h1 { margin: 0 0 4px; font-size: 1.5rem; letter-spacing: .06em; }
.auth .lead { margin: 0 0 18px; color: var(--muted); font-size: .9rem; }
.alert { padding: 10px 12px; border-radius: 10px; font-size: .88rem; margin-bottom: 14px;
  background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.alert.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }
.auth .btn.primary { width: 100%; height: 42px; }

/* ---------- Chat extras ---------- */
.chat-toolbar { max-width: 760px; margin: 0 auto 10px; display: flex; gap: 8px; flex-wrap: wrap; }
.chat-toolbar .icon-btn { margin-left: auto; }
.saved-note {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; margin-top: 4px;
  border-radius: 999px; background: color-mix(in srgb, var(--ok) 14%, transparent);
  color: var(--ok); font-size: .76rem; font-weight: 600;
}
@media (max-width: 640px) {
  nav.pages a span.nav-label { display: none; }
  .grade-row { grid-template-columns: repeat(2, 1fr); }
  .review-front { font-size: 1.5rem; }
}

