:root {
  color-scheme: light;
  --bg: #F5EEDB;
  --surface: #E2C171;
  --surface-muted: #F1E4C4;
  --primary: #24324D;
  --primary-dark: #1B253A;
  --accent-red: #C24432;
  --accent-blue: #2B4F7F;
  --ink: #2B2623;
  --muted: #5B5047;
  --toolbar-icon: #F7F0DF;
  --control-highlight: rgba(194, 68, 50, .20);
  --seen: rgba(43, 38, 35, .10);
  --lookup: rgba(194, 68, 50, .18);
  --speech: rgba(36, 50, 77, .12);
  --speech-token: rgba(194, 68, 50, .28);
  --shadow: rgba(36, 50, 77, .24);
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Arial Narrow", "Roboto Condensed", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
}
.shell { min-height: 100vh; }
.hidden { display: none !important; }
.auth {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(280px, 420px) minmax(280px, 360px);
  align-items: center;
  justify-content: center;
  gap: 48px;
  padding: 32px;
}
.auth h1 { margin: 0 0 12px; font-size: 42px; letter-spacing: .05em; color: var(--primary); }
.auth .motto { margin: 0 0 10px; color: var(--accent-red); font-size: 21px; font-weight: 700; line-height: 1.35; }
.auth p { margin: 0; color: var(--muted); line-height: 1.5; }
form {
  display: grid;
  gap: 12px;
  padding: 24px;
  background: var(--surface-muted);
  border: 2px solid var(--primary);
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(36, 50, 77, .20);
}
input, select, button {
  font: inherit;
  min-height: 40px;
  border-radius: 6px;
  border: 2px solid var(--primary);
  background: var(--bg);
  color: var(--ink);
  padding: 0 12px;
}
button {
  cursor: pointer;
  background: var(--primary);
  border-color: var(--primary);
  color: var(--toolbar-icon);
  font-weight: 650;
}
button:disabled { opacity: .45; cursor: default; }
.auth-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#registerButton, #logoutButton, #prevPage, #nextPage, #speakPage, #closeSheet {
  background: transparent;
  color: var(--toolbar-icon);
}
#speakPage.playing {
  background: var(--accent-red);
  color: var(--toolbar-icon);
}
.message { min-height: 20px; font-size: 14px; color: var(--accent-red); }
.reader { min-height: 100vh; display: grid; grid-template-rows: auto 1fr; }
.toolbar {
  position: sticky;
  top: 0;
  z-index: 3;
  display: grid;
  grid-template-columns: auto minmax(180px, 1fr) 40px auto 40px 40px auto auto minmax(160px, auto);
  gap: 8px;
  align-items: center;
  min-height: 56px;
  padding: 6px 8px;
  background: var(--primary);
  box-shadow: 0 4px 10px rgba(27, 37, 58, .30);
}
.toolbar select {
  min-height: 40px;
  background: var(--surface-muted);
  border-color: var(--primary-dark);
  color: var(--ink);
}
.app-title {
  color: var(--toolbar-icon);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: .05em;
  line-height: 1;
  padding: 0 8px;
  white-space: nowrap;
}
.toolbar button {
  min-width: 40px;
  min-height: 40px;
  padding: 0 8px;
  border-color: transparent;
  background: transparent;
  color: var(--toolbar-icon);
}
.toolbar button:hover { background: var(--control-highlight); }
#pageStatus {
  min-width: 7em;
  color: var(--accent-red);
  font-weight: 700;
  letter-spacing: .05em;
  text-align: center;
  white-space: nowrap;
}
.speech-status { color: var(--toolbar-icon); font-size: 14px; min-width: 0; overflow-wrap: anywhere; }
.page {
  width: calc(100vw - 48px);
  max-width: 980px;
  min-height: calc(100vh - 56px - 56px);
  margin: 24px auto 88px;
  padding: 24px;
  background: var(--surface);
  border: 3px solid var(--primary);
  border-radius: 16px;
  box-shadow: 0 4px 12px var(--shadow);
  font-family: Georgia, "Times New Roman", "Noto Serif", serif;
  font-size: 22px;
  line-height: 1.55;
  letter-spacing: .01em;
  overflow-wrap: break-word;
  hyphens: none;
  text-rendering: optimizeLegibility;
  font-kerning: normal;
  text-align: justify;
}
.reader-paragraph {
  margin: 0;
  text-indent: 0;
}
.reader-paragraph:empty { min-height: 1em; }
.reader-line-break { display: block; height: 0; }
.page .token:first-child,
.reader-paragraph > .token:first-child { margin-left: 0; }
.reader-paragraph .token[data-punctuation="true"] { margin-left: 0; }
.reader-paragraph .token.dialogue-dash { margin-left: -.15em; }
.reader-paragraph .token.dialogue-dash + .token { margin-left: .08em; }
.page::selection,
.token::selection {
  background: var(--control-highlight);
}
.token {
  border-radius: 6px;
  padding: 1px 2px;
  transition: background .12s ease, box-shadow .12s ease;
}
.token:hover { background: var(--lookup); }
.token.visible { background: var(--seen); }
.token.speech-sentence { background: var(--speech); }
.token.speech-focus {
  background: var(--speech-token);
  color: var(--accent-red);
  box-shadow: inset 0 0 0 2px var(--accent-red);
}
.token-sheet {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: min(420px, calc(100vw - 32px));
  max-height: min(560px, calc(100vh - 32px));
  overflow: auto;
  padding: 18px;
  background: var(--surface-muted);
  border: 2px solid var(--primary);
  border-radius: 12px;
  box-shadow: 0 4px 14px var(--shadow);
  z-index: 4;
}
.token-sheet h2 { margin: 0 36px 12px 0; font-size: 26px; }
.token-sheet dl { display: grid; grid-template-columns: 96px 1fr; gap: 8px 12px; margin: 0 0 16px; }
.token-sheet dt { color: var(--muted); }
.token-sheet dd { margin: 0; }
#closeSheet { position: absolute; right: 10px; top: 10px; min-width: 34px; width: 34px; height: 34px; padding: 0; }
.stats-panel {
  position: fixed;
  inset: 64px 16px 16px;
  z-index: 5;
  display: grid;
  grid-template-rows: auto auto 1fr;
  width: min(1040px, calc(100vw - 32px));
  margin-left: auto;
  padding: 18px;
  background: var(--bg);
  border: 2px solid var(--primary);
  border-radius: 12px;
  box-shadow: 0 4px 14px var(--shadow);
}
.stats-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.stats-head h2 { margin: 0; font-size: 24px; color: var(--primary); letter-spacing: .05em; }
.stats-tabs { display: flex; gap: 8px; padding: 14px 0; }
.stats-tabs button { background: var(--surface-muted); color: var(--primary); border-color: var(--primary); }
.stats-tabs button.active { background: var(--primary); color: var(--toolbar-icon); }
.stats-content { overflow: auto; border-top: 2px solid var(--primary); }
.stat-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) 90px 90px 90px 130px;
  gap: 10px;
  align-items: center;
  min-height: 44px;
  border-bottom: 1px solid rgba(36, 50, 77, .22);
  font-size: 15px;
}
.stat-row.header { position: sticky; top: 0; background: var(--surface-muted); color: var(--muted); font-weight: 700; z-index: 1; }
.stat-row.clickable { cursor: pointer; }
.stat-row.clickable:hover { background: var(--surface-muted); }
.stat-main { min-width: 0; overflow-wrap: anywhere; }
.stat-sub { color: var(--muted); font-size: 13px; }
.timeline-panel {
  padding: 14px 0 18px;
  border-bottom: 2px solid var(--primary);
}
.timeline-title {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-bottom: 8px;
}
.timeline-title strong { font-size: 17px; }
.timeline-title button { background: var(--surface-muted); color: var(--primary); border-color: var(--primary); }
.timeline-axis {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}
.timeline-svg {
  width: 100%;
  height: 72px;
  display: block;
  background: var(--surface-muted);
  border: 2px solid var(--primary);
  border-radius: 12px;
}
.timeline-legend { display: flex; gap: 14px; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
.legend-dot { display: inline-block; width: 9px; height: 9px; border-radius: 99px; margin-right: 5px; vertical-align: baseline; }
@media (max-width: 760px) {
  .auth { grid-template-columns: 1fr; align-content: center; gap: 24px; }
  .toolbar { grid-template-columns: auto 1fr 40px auto 40px 40px; }
  .app-title { font-size: 16px; padding-right: 0; }
  #statsButton, #logoutButton, #speechStatus { grid-column: 1 / -1; }
  .page { width: calc(100vw - 48px); padding: 24px; font-size: 20px; line-height: 1.55; }
  .stat-row { grid-template-columns: 1fr 70px 70px; }
  .stat-row .wide-only { display: none; }
}
