/* Sprint Library — offline speed reader */
:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #efefec;
  --ink: #1d1d1f;
  --ink-2: #55555c;
  --ink-3: #8a8a92;
  --line: #dcdcd8;
  --accent: #2563d9;
  --accent-ink: #ffffff;
  --accent-soft: rgba(37, 99, 217, 0.12);
  --orp: #d92b2b;
  --danger: #b42318;
  --grid: #e7e7e3;
  --radius: 10px;
  --reader-font: Georgia, "Times New Roman", serif;
  --reader-size: 48px;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #f3ead8;
  --surface: #fbf4e4;
  --surface-2: #ebe0c8;
  --ink: #3b2f22;
  --ink-2: #6b5a45;
  --ink-3: #9a8870;
  --line: #d8caae;
  --accent: #9a5b13;
  --accent-soft: rgba(154, 91, 19, 0.14);
  --orp: #c0392b;
  --grid: #e4d7bd;
}
:root[data-theme="dark"] {
  --bg: #121214;
  --surface: #1c1c1f;
  --surface-2: #26262a;
  --ink: #ececef;
  --ink-2: #b0b0b8;
  --ink-3: #7c7c85;
  --line: #34343a;
  --accent: #6ea2ff;
  --accent-ink: #0d1b33;
  --accent-soft: rgba(110, 162, 255, 0.16);
  --orp: #ff6b6b;
  --danger: #ff7b6e;
  --grid: #2a2a2f;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #121214;
    --surface: #1c1c1f;
    --surface-2: #26262a;
    --ink: #ececef;
    --ink-2: #b0b0b8;
    --ink-3: #7c7c85;
    --line: #34343a;
    --accent: #6ea2ff;
    --accent-ink: #0d1b33;
    --accent-soft: rgba(110, 162, 255, 0.16);
    --orp: #ff6b6b;
    --danger: #ff7b6e;
    --grid: #2a2a2f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select { font: inherit; color: inherit; }
button {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { border-color: var(--ink-3); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.icon { padding: 6px 10px; }
input[type="number"], input[type="text"], select {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 8px;
}
input[type="number"] { width: 90px; }
.muted { color: var(--ink-3); }
.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  padding-top: calc(10px + env(safe-area-inset-top));
  padding-left: calc(20px + env(safe-area-inset-left));
  padding-right: calc(20px + env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-weight: 700; letter-spacing: 0.2px; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a {
  color: var(--ink-2);
  text-decoration: none;
  padding: 5px 12px;
  border-radius: 8px;
}
.topbar nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.topbar .spacer { flex: 1; }

main { max-width: 1100px; margin: 0 auto; padding: 24px 16px 60px; }
main {
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
  padding-bottom: calc(60px + env(safe-area-inset-bottom));
}
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 17px; margin: 28px 0 12px; }

/* ---------- Library ---------- */
.lib-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin-bottom: 16px; }
.lib-head h1 { margin: 0; flex: 1; }
.continue {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 18px;
  background: var(--accent-soft);
  border-radius: var(--radius);
  margin-bottom: 18px;
}
.continue .t { flex: 1; min-width: 0; }
.continue .t b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dropzone {
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
  color: var(--ink-2);
  margin-bottom: 20px;
  transition: border-color .15s, background .15s;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dropzone label, .linkish {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
}
.paste {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: -8px 0 20px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.settings .paste { flex-basis: 100%; margin: 4px 0 0; }
textarea {
  width: 100%;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  resize: vertical;
}
.books { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); grid-gap: 14px; gap: 14px; }
.book {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.book .title { font-weight: 650; line-height: 1.3; }
.book .author { color: var(--ink-2); font-size: 13px; }
.book .meta { color: var(--ink-3); font-size: 12.5px; display: flex; gap: 8px; flex-wrap: wrap; }
.badge {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--ink-2);
}
.bar { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.book .actions { display: flex; gap: 6px; margin-top: auto; padding-top: 4px; }
.book .actions .primary { flex: 1; }
.empty { text-align: center; color: var(--ink-3); padding: 40px 0; }

/* ---------- Reader ---------- */
body.reading main { max-width: none; padding: 0; }
/* Reader fills the screen under the top bar, whatever the bar's height. */
body.reading { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
@supports (height: 100dvh) {
  body.reading { height: 100dvh; }
}
body.reading main { flex: 1; min-height: 0; display: flex; flex-direction: column; padding-bottom: 0; }
.reader { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.reader-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  flex-wrap: wrap;
}
.reader-head .book-title { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reader-head select { max-width: 260px; }
.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  min-height: 220px;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.focus {
  position: relative;
  width: 96vw;
  max-width: 900px;
  padding: 26px 0;
}
.focus::before, .focus::after {
  content: "";
  position: absolute;
  left: 50%;
  width: 2px;
  height: 14px;
  background: var(--ink-3);
  opacity: .55;
}
.focus::before { top: 0; }
.focus::after { bottom: 0; }
.focus .guide { position: absolute; left: 8%; right: 8%; height: 1px; background: var(--line); }
.focus .guide.top { top: 0; }
.focus .guide.bottom { bottom: 0; }
.word {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  font-family: var(--reader-font);
  font-size: var(--reader-size);
  font-size: min(var(--reader-size), 11vw); /* keep long words on screen on phones */
  line-height: 1.2;
  white-space: pre;
  min-height: 1.2em;
}
.word .pre { text-align: right; }
.word .piv { color: var(--orp); }
.word .post { text-align: left; }
.word.chunk { display: block; text-align: center; }
.word.chunk .piv { color: inherit; }
.stage .hint { position: absolute; bottom: 14px; color: var(--ink-3); font-size: 13px; }
.overlay {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: var(--bg);
  z-index: 2;
}
.overlay .count { font-size: 72px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.overlay .lead { max-width: 640px; text-align: center; color: var(--ink-3); font-family: var(--reader-font); font-size: 18px; padding: 0 16px; }
.context {
  max-height: 34vh;
  overflow-y: auto;
  margin: 0 auto 8px;
  width: 94vw;
  max-width: 820px;
  -webkit-overflow-scrolling: touch;
  padding: 14px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-family: var(--reader-font);
  font-size: 17px;
  line-height: 1.65;
}
.context p { margin: 0 0 .8em; }
.context p.cur { color: var(--ink); }
.context p:not(.cur) { color: var(--ink-3); }
.context span { cursor: pointer; border-radius: 3px; }
.context span:hover { background: var(--accent-soft); }
.context span.here { background: var(--accent); color: var(--accent-ink); padding: 0 2px; }
.context .tip { font-family: system-ui, sans-serif; font-size: 12px; color: var(--ink-3); margin-bottom: 8px; }
.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 16px;
  flex-wrap: wrap;
}
.controls .wpm { font-variant-numeric: tabular-nums; min-width: 150px; text-align: center; }
.controls .wpm b { font-size: 18px; }
.progress { padding: 8px 20px 14px; }
.progress {
  padding-bottom: calc(14px + env(safe-area-inset-bottom));
  padding-left: calc(20px + env(safe-area-inset-left));
  padding-right: calc(20px + env(safe-area-inset-right));
}
.progress .track {
  height: 10px;
  background: var(--surface-2);
  border-radius: 5px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.progress .fill { height: 100%; background: var(--accent); border-radius: 5px; width: 0; }
.progress .ticks i { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--bg); opacity: .8; }
.progress .labels { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; color: var(--ink-2); margin-top: 6px; flex-wrap: wrap; font-variant-numeric: tabular-nums; }

/* ---------- Stats ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); grid-gap: 12px; gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.tile .v { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile .k { color: var(--ink-2); font-size: 13px; }
.chart { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .legend { display: flex; gap: 16px; font-size: 12.5px; color: var(--ink-2); margin-bottom: 6px; }
.chart .legend i { display: inline-block; width: 14px; height: 2px; background: var(--accent); vertical-align: middle; margin-right: 6px; }
.chart .legend i.dash { background: none; border-top: 2px dashed var(--ink-3); height: 0; }
.chart .tt {
  position: absolute;
  pointer-events: none;
  background: var(--ink);
  color: var(--bg);
  font-size: 12.5px;
  padding: 6px 9px;
  border-radius: 6px;
  white-space: nowrap;
  transform: translate(-50%, -110%);
}
.chart text { fill: var(--ink-3); font-size: 14px; }
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); grid-gap: 14px; gap: 14px; }
table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--ink-2); font-weight: 600; background: var(--surface-2); }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

/* ---------- Settings ---------- */
.settings fieldset {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 8px 18px 14px;
  margin: 0 0 16px;
}
.settings legend { font-weight: 650; padding: 0 6px; }
.row { display: flex; align-items: center; gap: 12px; padding: 8px 0; flex-wrap: wrap; }
.row > label:first-child { flex: 0 0 260px; }
.row .help { flex-basis: 100%; color: var(--ink-3); font-size: 13px; margin-top: -4px; }
.row input[type="range"] { width: 220px; accent-color: var(--accent); }
.row input[type="checkbox"] { accent-color: var(--accent); width: 18px; height: 18px; }
.sub { padding-left: 18px; border-left: 2px solid var(--line); margin-left: 4px; }
.sub.disabled { opacity: .45; pointer-events: none; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 8px;
  z-index: 50;
  max-width: 90vw;
  margin-bottom: env(safe-area-inset-bottom);
}

/* Shown only with a mouse / only on touch screens */
.touch-only { display: none; }
@media (hover: none) and (pointer: coarse) {
  .pointer-only { display: none; }
  .touch-only { display: inline; }
}

@media (max-width: 640px) {
  .topbar { padding: 8px 12px; gap: 8px; }
  .topbar { padding-top: calc(8px + env(safe-area-inset-top)); }
  .brand { display: none; }
  .row > label:first-child { flex-basis: 100%; }
  .row input[type="range"] { width: 100%; max-width: 100%; }
  .charts { grid-template-columns: 1fr; }
  .reader-head { padding: 8px 12px; }
  .reader-head select { max-width: 100%; flex-basis: 100%; }
  .controls { padding: 6px 8px; }
  .controls .wpm { min-width: 0; }
}

/* ---------- Touch: 44px tap targets, 16px inputs (stops iOS zoom-on-focus) ---------- */
html { -webkit-text-size-adjust: 100%; }
button, .topbar nav a, select { touch-action: manipulation; }
@media (pointer: coarse) {
  button, select, input[type="text"], input[type="number"], .topbar nav a {
    min-height: 44px;
  }
  button { padding: 8px 14px; min-width: 44px; }
  input, select, textarea { font-size: 16px; }
  .topbar nav a { display: inline-flex; align-items: center; }
  .dropzone label, .linkish { display: inline-block; min-height: 44px; line-height: 44px; }
  .row input[type="checkbox"] { width: 26px; height: 26px; }
  .row input[type="range"] { height: 44px; }
  .progress .track { height: 18px; border-radius: 9px; }
  .context { font-size: 18px; line-height: 1.9; }
}

/* ---------- iOS 10 Safari: no flexbox `gap`, so space children with margins ---------- */
.no-flexgap .topbar > * + * { margin-left: 16px; }
.no-flexgap .topbar nav > * + * { margin-left: 4px; }
.no-flexgap .lib-head > * + *,
.no-flexgap .reader-head > * + *,
.no-flexgap .row > * + * { margin-left: 12px; }
.no-flexgap .row > .help, .no-flexgap .row > .paste { margin-left: 0; }
.no-flexgap .continue > * + * { margin-left: 16px; }
.no-flexgap .book > * + *,
.no-flexgap .paste > * + * { margin-top: 8px; }
.no-flexgap .book .meta > * + *,
.no-flexgap .book .actions > * + * { margin-left: 6px; }
.no-flexgap .overlay > * + * { margin-top: 18px; }
.no-flexgap .controls > * { margin: 4px; }
.no-flexgap .progress .labels > * + *,
.no-flexgap .chart .legend > * + * { margin-left: 12px; }
