:root {
  --serif: "ET Book", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --sans: "Lato", "Helvetica Neue", Arial, sans-serif;
  --brand: "Quicksand", "Avenir Next", sans-serif;
  --mono: "JetBrains Mono", Consolas, monospace;
  --jp: "Noto Serif CJK JP", "Yu Mincho", "Hiragino Mincho ProN", serif;
  --bg: #fbf6ec;
  --raised: #fffdf8;
  --sunk: #f4efe6;
  --ink: #322d27;
  --ink-2: #574f45;
  --ink-3: #786d61;
  --rule: #ded7cc;
  --rule-soft: #ece5da;
  --accent: #800000;
  --accent-ink: #5e0000;
  --accent-tint: #f3e3e3;
  --success: #3e6c57;
  --success-tint: #e4eee8;
  --shadow: 0 14px 44px rgba(61, 39, 24, 0.1);
  --focus: #a63d24;
}

:root[data-theme="dark"] {
  --bg: #1b1916;
  --raised: #24211d;
  --sunk: #17150f;
  --ink: #ece5d8;
  --ink-2: #c3baa9;
  --ink-3: #9d9485;
  --rule: #3b362e;
  --rule-soft: #2d2923;
  --accent: #e0897a;
  --accent-ink: #f0afa2;
  --accent-tint: #3b2420;
  --success: #83b39b;
  --success-tint: #21372d;
  --shadow: 0 18px 52px rgba(0, 0, 0, 0.34);
  --focus: #f0aa73;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="dark"]) {
    --bg: #1b1916;
    --raised: #24211d;
    --sunk: #17150f;
    --ink: #ece5d8;
    --ink-2: #c3baa9;
    --ink-3: #9d9485;
    --rule: #3b362e;
    --rule-soft: #2d2923;
    --accent: #e0897a;
    --accent-ink: #f0afa2;
    --accent-tint: #3b2420;
    --success: #83b39b;
    --success-tint: #21372d;
    --shadow: 0 18px 52px rgba(0, 0, 0, 0.34);
    --focus: #f0aa73;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { color-scheme: light dark; -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 18px;
  line-height: 1.55;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

button, select { font: inherit; color: inherit; cursor: pointer; }
button, select, a { -webkit-tap-highlight-color: transparent; }
a { color: var(--accent); }
svg { width: 1.15rem; height: 1.15rem; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
progress { appearance: none; border: 0; }
progress::-webkit-progress-bar { background: var(--rule-soft); border-radius: 99px; }
progress::-webkit-progress-value { background: var(--accent); border-radius: 99px; }
progress::-moz-progress-bar { background: var(--accent); border-radius: 99px; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
  border-radius: 4px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: 64px;
  display: flex;
  align-items: center;
  gap: clamp(0.65rem, 2vw, 1.3rem);
  padding: 0.65rem clamp(1rem, 4vw, 2.2rem);
  border-bottom: 1px solid var(--rule);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px) saturate(1.3);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.62rem;
  color: var(--ink);
  text-decoration: none;
  flex: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 9px;
  color: #fff;
  background: var(--accent);
  font-family: var(--jp);
  font-size: 1.22rem;
  line-height: 1;
  box-shadow: 0 1px 3px rgba(50, 20, 10, 0.16);
}

.brand-word { display: flex; flex-direction: column; line-height: 1.05; }
.brand-word strong { font-family: var(--brand); font-size: 1.02rem; }
.brand-word span { color: var(--ink-3); font-family: var(--sans); font-size: 0.59rem; letter-spacing: 0.17em; text-transform: uppercase; }

.dataset-control {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
  margin-left: auto;
  color: var(--ink-3);
  font-family: var(--sans);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.dataset-control select {
  max-width: min(30vw, 22rem);
  min-height: 38px;
  padding: 0.4rem 2rem 0.4rem 0.7rem;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--raised);
  color: var(--ink-2);
  font-family: var(--sans);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: none;
  letter-spacing: 0;
}

.top-actions { display: flex; gap: 0.4rem; }
.icon-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: transparent;
  color: var(--ink-2);
  transition: color 130ms ease, background 130ms ease, border-color 130ms ease;
}
.icon-button:hover, .icon-button[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); background: var(--accent-tint); }

main { min-height: calc(100vh - 65px); }
.loading, .error-state {
  min-height: calc(100vh - 65px);
  display: grid;
  place-content: center;
  padding: 2rem;
  text-align: center;
  color: var(--ink-3);
  font-family: var(--sans);
}
.loading-rule { display: block; width: 58px; height: 2px; margin: auto; background: var(--accent); animation: breathe 1.5s ease-in-out infinite; }

.reader {
  width: min(100%, 74rem);
  min-height: calc(100vh - 65px);
  margin: 0 auto;
  padding: clamp(1.1rem, 3vw, 2.25rem) clamp(1rem, 4vw, 2.2rem) clamp(1.4rem, 4vw, 3rem);
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: clamp(1rem, 2.5vw, 1.65rem);
}

.reader-head { display: grid; gap: 0.65rem; }
.context-row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.eyebrow, .counter, .micro-label {
  font-family: var(--sans);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.13em;
  text-transform: uppercase;
}
.eyebrow { color: var(--accent); }
.eyebrow.review { color: var(--ink-3); }
.counter { color: var(--ink-3); font-family: var(--mono); letter-spacing: 0.04em; }

.progress-track { width: 100%; height: 3px; overflow: hidden; border-radius: 9px; background: var(--rule-soft); }
.progress-fill { display: block; height: 100%; border-radius: inherit; background: var(--accent); transition: width 240ms ease; }

.sentence-card {
  align-self: stretch;
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(16rem, 0.75fr);
  border: 1px solid var(--rule);
  border-radius: 18px;
  background: var(--raised);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.sentence-primary {
  min-height: 29rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: clamp(1.35rem, 4vh, 2.5rem);
  padding: clamp(1.5rem, 5vw, 4.2rem);
  border-right: 1px solid var(--rule);
}

.target-line {
  margin: 0;
  color: var(--ink);
  font-family: var(--jp);
  font-size: clamp(2rem, 5vw, 4.2rem);
  font-weight: 400;
  line-height: 1.55;
  letter-spacing: 0.015em;
  text-align: center;
  text-wrap: balance;
}

rt {
  color: var(--accent);
  font-family: var(--jp);
  font-size: 0.34em;
  font-weight: 400;
  letter-spacing: 0.03em;
}
.readings-off rt { visibility: hidden; }

.reading-line {
  margin: -0.6rem 0 0;
  color: var(--ink-3);
  font-family: var(--jp);
  font-size: clamp(0.88rem, 1.4vw, 1.04rem);
  letter-spacing: 0.1em;
  text-align: center;
}
.readings-off .reading-line { visibility: hidden; }

.support-block { display: grid; gap: 0.7rem; text-align: center; }
.language-tabs { display: flex; justify-content: center; gap: 0.35rem; flex-wrap: wrap; }
.language-tab {
  min-width: 2.8rem;
  padding: 0.3rem 0.55rem;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--ink-3);
  font-family: var(--sans);
  font-size: 0.67rem;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}
.language-tab:hover { color: var(--ink); background: var(--sunk); }
.language-tab[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); background: var(--accent-tint); }
.support-text { margin: 0 auto; max-width: 42rem; color: var(--ink-2); font-size: clamp(1.05rem, 2.1vw, 1.45rem); font-style: italic; line-height: 1.45; text-wrap: balance; }

.target-group { display: grid; gap: 0.45rem; justify-items: center; }
.micro-label { color: var(--ink-3); }
.chip-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.42rem; }
.chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0.28rem 0.62rem;
  border: 1px solid color-mix(in srgb, var(--accent) 34%, var(--rule));
  border-radius: 999px;
  background: var(--accent-tint);
  color: var(--accent-ink);
  font-family: var(--sans);
  font-size: 0.73rem;
  font-weight: 700;
}
.chip.secondary { border-color: var(--rule); background: var(--sunk); color: var(--ink-2); font-weight: 400; }

.sentence-details {
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: color-mix(in srgb, var(--sunk) 72%, var(--raised));
}
.details-content { min-width: 0; display: grid; gap: 1.35rem; padding: clamp(1.25rem, 3vw, 2.1rem); }
.detail-section { min-width: 0; display: grid; gap: 0.62rem; }
.detail-heading { margin: 0; color: var(--ink); font-family: var(--sans); font-size: 0.79rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
.fact-list { min-width: 0; margin: 0; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.34rem 0.8rem; color: var(--ink-2); font-family: var(--sans); font-size: 0.76rem; }
.fact-list dt { color: var(--ink-3); }
.fact-list dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
.mono { font-family: var(--mono); font-size: 0.92em; }

.measure-list { min-width: 0; display: grid; gap: 0.58rem; }
.measure { min-width: 0; display: grid; grid-template-columns: minmax(5.2rem, 1fr) minmax(3rem, 2fr) auto; align-items: center; gap: 0.55rem; }
.measure-name { color: var(--ink-2); font-family: var(--sans); font-size: 0.7rem; }
.measure-track { width: 100%; height: 5px; overflow: hidden; border-radius: 99px; background: var(--rule); }
.measure-fill { display: block; height: 100%; background: var(--accent); border-radius: inherit; }
.measure-value { min-width: 2.2rem; color: var(--ink-3); font-family: var(--mono); font-size: 0.65rem; text-align: right; }

.details-disclosure { margin-top: auto; border-top: 1px solid var(--rule); }
.details-disclosure summary {
  padding: 0.85rem clamp(1.25rem, 3vw, 2.1rem);
  color: var(--ink-2);
  font-family: var(--sans);
  font-size: 0.75rem;
  font-weight: 700;
  cursor: pointer;
}
.limitations { margin: 0; padding: 0 clamp(1.25rem, 3vw, 2.1rem) 1.25rem 2.7rem; color: var(--ink-3); font-family: var(--sans); font-size: 0.7rem; }

.reader-controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 0.75rem; }
.control-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.48rem;
  padding: 0.55rem 0.9rem;
  border: 1px solid var(--rule);
  border-radius: 9px;
  background: var(--raised);
  color: var(--ink-2);
  font-family: var(--sans);
  font-size: 0.78rem;
  font-weight: 700;
  transition: color 130ms ease, background 130ms ease, border-color 130ms ease;
}
.control-button:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); background: var(--accent-tint); }
.control-button:disabled { opacity: 0.38; cursor: default; }
.control-button.next { justify-self: end; }
.control-button.complete { color: var(--success); border-color: color-mix(in srgb, var(--success) 45%, var(--rule)); }
.control-button.complete[aria-pressed="true"] { background: var(--success-tint); }
.completion-copy { color: var(--ink-3); font-family: var(--sans); font-size: 0.7rem; text-align: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  z-index: 30;
  transform: translate(-50%, 0.5rem);
  max-width: calc(100vw - 2rem);
  padding: 0.58rem 0.85rem;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--ink);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-family: var(--sans);
  font-size: 0.74rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.noscript { max-width: 40rem; margin: 4rem auto; padding: 1rem; }

@keyframes breathe { 0%, 100% { opacity: 0.3; transform: scaleX(0.55); } 50% { opacity: 1; transform: scaleX(1); } }

@media (max-width: 860px) {
  .sentence-card { grid-template-columns: 1fr; }
  .sentence-primary { min-height: 24rem; border-right: 0; border-bottom: 1px solid var(--rule); }
  .sentence-details { display: grid; grid-template-columns: 1fr; }
  .details-content { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 680px) {
  body { font-size: 16px; }
  .topbar { min-height: 58px; gap: 0.48rem; padding: 0.55rem 0.75rem; }
  .brand-mark { width: 34px; height: 34px; }
  .brand-word span { display: none; }
  .dataset-control { flex: 1; margin-left: 0; }
  .dataset-control > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .dataset-control select { width: 100%; max-width: none; min-width: 0; font-size: 0.72rem; }
  .top-actions { gap: 0.25rem; }
  .icon-button { width: 36px; height: 36px; }
  .reader { min-height: calc(100vh - 58px); padding: 0.9rem 0.75rem 1rem; }
  .sentence-card { border-radius: 13px; }
  .sentence-primary { min-height: 22rem; padding: 2.2rem 1rem; }
  .target-line { font-size: clamp(1.85rem, 9.6vw, 3rem); }
  .details-content { grid-template-columns: 1fr; padding: 1.15rem; }
  .reader-controls { grid-template-columns: 1fr 1fr; }
  .control-button { padding: 0.5rem 0.7rem; }
  .control-button.complete { grid-column: 1 / -1; grid-row: 1; }
  .completion-copy { display: none; }
  .control-button.prev { grid-column: 1; }
  .control-button.next { grid-column: 2; }
}

@media (max-width: 430px) {
  .brand-word { display: none; }
}

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