:root {
  color-scheme: light dark;
  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #faf7f0;
  --border: #d9d0bc;
  --ink: #16212e;
  --ink-muted: #556370;
  --primary: #0b4a6f;
  --primary-ink: #ffffff;
  --accent: #c66b1d;
  --success: #317850;
  --warn: #c58a1e;
  --danger: #c23b2e;
  --wave-1: #0b4a6f;
  --wave-2: #5a8fb0;
  --card-shadow: 0 4px 16px rgba(11, 40, 58, 0.14), 0 1px 2px rgba(11, 40, 58, 0.08);
  --radius: 14px;
  --fs-q: clamp(17px, 2vw + 8px, 23px);
  --fs-a: clamp(16px, 1.6vw + 8px, 21px);
  --font: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", "Noto Sans JP", system-ui, -apple-system, sans-serif;
}

:root:not([data-theme="light"]) {
  @media (prefers-color-scheme: dark) {
    --bg: #0d1620;
    --surface: #152431;
    --surface-2: #1a2d3d;
    --border: #2b4257;
    --ink: #e6ecf1;
    --ink-muted: #94a6b8;
    --primary: #4fa6d4;
    --primary-ink: #0b1821;
    --accent: #e39255;
    --success: #58b682;
    --warn: #dfae49;
    --danger: #e26558;
    --card-shadow: 0 4px 18px rgba(0,0,0,0.5), 0 1px 2px rgba(0,0,0,0.3);
  }
}
:root[data-theme="dark"] {
  --bg: #0d1620;
  --surface: #152431;
  --surface-2: #1a2d3d;
  --border: #2b4257;
  --ink: #e6ecf1;
  --ink-muted: #94a6b8;
  --primary: #4fa6d4;
  --primary-ink: #0b1821;
  --accent: #e39255;
  --success: #58b682;
  --warn: #dfae49;
  --danger: #e26558;
  --card-shadow: 0 4px 18px rgba(0,0,0,0.5), 0 1px 2px rgba(0,0,0,0.3);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 300px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "aside top" "aside main";
}

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  background: linear-gradient(180deg, var(--surface) 0%, var(--surface) 100%);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.anchor { font-size: 24px; }
.brand-text h1 { margin: 0; font-size: 1.05rem; letter-spacing: 0.02em; }
.brand-text small { color: var(--ink-muted); font-size: 12px; }
.top-actions { display: flex; gap: 6px; }
.icon-btn {
  background: transparent; border: 1px solid transparent; color: var(--ink);
  width: 38px; height: 38px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--border); }
.menu-btn { display: none; }

.sidebar {
  grid-area: aside;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 0;
  overflow-y: auto;
  height: 100dvh;
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
}
.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.side-head h2 { margin: 0; font-size: 1rem; }
.close-btn { display: none; }
.side-body { padding: 14px 16px 30px; overflow-y: auto; }
.field { margin-bottom: 18px; }
.field > label {
  display: block; font-size: 12px; font-weight: 600; color: var(--ink-muted);
  letter-spacing: 0.08em; text-transform: uppercase; margin-bottom: 6px;
}
.chipset { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: transparent; border: 1px solid var(--border); color: var(--ink);
  padding: 6px 12px; border-radius: 999px; cursor: pointer; font-size: 13px;
  transition: all .12s ease;
}
.chip[aria-pressed="true"], .chip.active {
  background: var(--primary); border-color: var(--primary); color: var(--primary-ink);
}
.chip:hover:not([aria-pressed="true"]) { background: var(--surface-2); }
#searchBox {
  width: 100%; padding: 8px 10px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; color: var(--ink); font-size: 14px;
  font-family: inherit;
}
#searchBox:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: var(--primary); }
.section-tree { max-height: 46vh; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.tree-top { padding: 6px 10px; cursor: pointer; display: flex; align-items: center; justify-content: space-between; user-select: none; }
.tree-top:hover { background: var(--bg); }
.tree-top .count { color: var(--ink-muted); font-size: 12px; }
.tree-children { padding: 2px 4px 8px 20px; display: none; }
.tree-children.open { display: block; }
.tree-leaf { display: flex; align-items: center; gap: 6px; padding: 3px 6px; font-size: 13px; cursor: pointer; border-radius: 6px; }
.tree-leaf:hover { background: var(--bg); }
.tree-leaf input { accent-color: var(--primary); }
.tree-leaf .lc { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-leaf .lcount { color: var(--ink-muted); font-size: 11px; }
.summary {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px;
}
.summary-row { display: flex; justify-content: space-between; font-size: 13px; padding: 2px 0; }
.summary-row strong { font-size: 14px; }
.danger-btn {
  width: 100%; padding: 9px; background: transparent; color: var(--danger);
  border: 1px solid var(--danger); border-radius: 8px; cursor: pointer;
  font-size: 13px;
}
.danger-btn:hover { background: var(--danger); color: var(--primary-ink); }

.scrim {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20;
}
.scrim.on { display: block; }

.main {
  grid-area: main;
  padding: 18px max(16px, 2vw);
  display: flex; flex-direction: column; gap: 18px;
  max-width: 1100px; width: 100%;
  margin: 0 auto;
}

.progressbar {
  height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden;
  border: 1px solid var(--border);
}
.progressbar-fill {
  height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent));
  width: 0%; transition: width 220ms ease;
}

.card-area {
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: stretch;
  min-height: 56vh;
}
.nav-btn {
  background: var(--surface); border: 1px solid var(--border); color: var(--ink);
  border-radius: 12px; cursor: pointer; padding: 0 10px;
  display: flex; align-items: center; justify-content: center;
}
.nav-btn:hover { background: var(--surface-2); }
.nav-btn:disabled { opacity: .4; cursor: not-allowed; }

.card-wrapper {
  position: relative; perspective: 2000px; min-height: 52vh;
}
.card {
  position: relative; width: 100%; height: 100%; min-height: 52vh;
  transform-style: preserve-3d; transition: transform 500ms cubic-bezier(.3,.9,.2,1);
  cursor: pointer; outline: none;
}
.card.flipped { transform: rotateY(180deg); }
.card:focus-visible .card-face { outline: 2px solid var(--primary); outline-offset: 2px; }

.card-face {
  position: absolute; inset: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 22px clamp(16px, 2vw, 32px);
  display: flex; flex-direction: column; gap: 14px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow-y: auto;
}
.card-face.back { transform: rotateY(180deg); }

.card-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 11px; }
.face-badge {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 10px;
  font-size: 18px; font-weight: 700; letter-spacing: 0;
  box-shadow: 0 2px 0 rgba(0,0,0,.08);
  margin-right: 4px;
}
.badge-q { background: var(--primary); color: var(--primary-ink); }
.badge-a { background: var(--accent); color: #fff; }

.tag {
  padding: 3px 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--ink-muted);
  font-size: 11px; letter-spacing: 0;
}
.tag-part { background: var(--primary); color: var(--primary-ink); border-color: transparent; font-weight: 600; letter-spacing: .04em; }
.tag-status[data-status="known"]  { background: var(--success); color: #fff; border-color: transparent; }
.tag-status[data-status="learning"]{ background: var(--warn); color: #221a06; border-color: transparent; }
.tag-status[data-status="unknown"] { background: var(--surface-2); color: var(--ink-muted); }

.q-header { font-size: 12px; color: var(--ink-muted); font-weight: 600; letter-spacing: .04em; margin-top: 4px; }
.q-body {
  font-size: var(--fs-q); line-height: 1.85; white-space: pre-wrap; word-break: break-word;
  font-feature-settings: "palt" 1;
}
.q-figure, .a-figure { margin-top: 4px; }
.card-face.front { gap: 12px; }
.card-face.front .q-body { flex: 1; }
.hint {
  text-align: center; color: var(--ink-muted); font-size: 12px; padding-top: 10px;
  border-top: 1px dashed var(--border); margin-top: auto;
}
.hint kbd {
  display: inline-block; padding: 1px 6px; border-radius: 4px;
  background: var(--surface-2); border: 1px solid var(--border);
  font-family: ui-monospace, monospace; font-size: 11px;
  color: var(--ink);
}

.answer-wrap { display: flex; flex-direction: column; gap: 12px; flex: 1; }
.a-body {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  border-left: 4px solid var(--accent);
  padding: 14px 18px; font-size: var(--fs-a); line-height: 1.85; white-space: pre-wrap; word-break: break-word;
  font-feature-settings: "palt" 1;
}
.exp { margin-top: 2px; }
.exp summary {
  cursor: pointer; font-size: 13px; color: var(--primary); user-select: none;
  padding: 6px 10px; border-radius: 8px; background: var(--surface-2);
  border: 1px dashed var(--border); list-style: none; display: inline-block;
}
.exp summary::-webkit-details-marker { display: none; }
.exp summary::before { content: "▸ "; display: inline; }
.exp[open] summary::before { content: "▾ "; }
.exp[open] summary { margin-bottom: 8px; }
.e-body {
  font-size: 13.5px; color: var(--ink-muted); line-height: 1.75; white-space: pre-wrap; word-break: break-word;
  padding: 10px 14px; background: var(--surface-2); border-left: 3px solid var(--border); border-radius: 0 8px 8px 0;
}
.src-link { font-size: 11px; color: var(--ink-muted); text-align: right; }

.figure {
  margin: 10px auto; max-width: 560px;
  padding: 14px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  text-align: center;
}
.figure svg { max-width: 100%; height: auto; display: block; margin: 0 auto; }
.figure .caption { font-size: 12px; color: var(--ink-muted); margin-top: 8px; font-weight: 500; }

.rating { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.rate {
  padding: 14px 10px; border-radius: 12px; font-size: 15px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  cursor: pointer; border: 2px solid transparent; transition: transform .1s ease;
}
.rate:hover { transform: translateY(-1px); }
.rate:active { transform: translateY(0); }
.rate-key { font-size: 11px; opacity: .75; }
.rate-x { background: rgba(194, 59, 46, .12); color: var(--danger); border-color: rgba(194,59,46,.35); }
.rate-tri { background: rgba(197, 138, 30, .14); color: var(--warn); border-color: rgba(197,138,30,.35); }
.rate-o { background: rgba(49, 120, 80, .14); color: var(--success); border-color: rgba(49,120,80,.35); }

.counter { text-align: center; color: var(--ink-muted); font-size: 13px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-size: 13px; z-index: 50; opacity: 0; pointer-events: none;
  transition: all .22s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Markup helpers for question content */
.q-body code, .a-body code, .e-body code {
  background: var(--surface); border: 1px solid var(--border); padding: 1px 6px; border-radius: 4px; font-family: ui-monospace, monospace; font-size: 0.95em;
}
.q-body .blank {
  display: inline-block; min-width: 2.5em; padding: 0 6px; margin: 0 2px;
  background: var(--surface-2); border-bottom: 2px solid var(--primary); border-radius: 4px 4px 0 0;
  text-align: center; color: var(--primary); font-weight: 600;
}
.ok-flash {
  animation: ok 420ms ease;
}
@keyframes ok {
  0% { box-shadow: 0 0 0 0 rgba(49,120,80,0.6); }
  100% { box-shadow: 0 0 0 20px rgba(49,120,80,0); }
}
.ng-flash { animation: ng 420ms ease; }
@keyframes ng {
  0% { box-shadow: 0 0 0 0 rgba(194,59,46,0.6); }
  100% { box-shadow: 0 0 0 20px rgba(194,59,46,0); }
}

/* Responsive */
@media (max-width: 860px) {
  body { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
  .menu-btn { display: inline-flex; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(85vw, 360px); z-index: 30;
    transform: translateX(-102%); transition: transform .25s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.2);
  }
  .sidebar.open { transform: translateX(0); }
  .close-btn { display: inline-flex; }
  .main { padding: 14px; }
  .card-area { grid-template-columns: 1fr; }
  .nav-btn { display: none; }
  .brand-text h1 { font-size: .95rem; }
  .rate { font-size: 13px; padding: 12px 6px; }
  .rate-label { font-size: 12px; }
}

@media (max-width: 420px) {
  .rate { padding: 10px 4px; }
  .rate-key { font-size: 10px; }
  .q-body { line-height: 1.6; }
}

/* KaTeX overrides */
.katex { font-size: 1em !important; }

/* Blank placeholder */
.marker { color: var(--accent); font-weight: 700; }
