:root {
  --void-0: #070b16;
  --void-1: #0d1426;
  --panel: rgba(16, 24, 43, 0.72);
  --panel-solid: #111a2e;
  --line: rgba(120, 140, 180, 0.18);
  --ink: #e7ecf6;
  --ink-dim: #9aa8c4;
  --ink-faint: #6b7898;
  --amber: #e8a13a;
  --amber-soft: rgba(232, 161, 58, 0.15);
  --cyan: #5ad0d8;
  --danger: #e8746a;
  --display: "Chakra Petch", ui-sans-serif, system-ui, sans-serif;
  --serif: "Spectral", Georgia, serif;
  --body: ui-sans-serif, system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

/* iOS full-bleed fix (hard-won in tts-web-v2):
   - Paint html+body with the bg so no white bands show at the status bar / home area.
   - Use 100dvh (dynamic viewport) not 100vh — 100vh is wrong on iOS Safari/WebView.
   - max-width:100% + overflow-x:hidden + overscroll-behavior-x:none kills the sideways
     scroll caused by any long unbreakable string (book titles, urls, tokens). */
html, body {
  margin: 0;
  min-height: 100%;
  max-width: 100%;
  overflow-x: hidden;
  overscroll-behavior: none;
  overscroll-behavior-x: none;
  background: var(--void-0);
  color: var(--ink);
  font-family: var(--body);
  -webkit-font-smoothing: antialiased;
}

/* Every full-height surface paints the bg + fills the dynamic viewport, so the native
   WebView never shows through as white above/below the content. */
.screen {
  min-height: 100dvh;
  background: var(--void-0);
  color: var(--ink);
}

/* Long unbreakable strings must wrap, or the page scrolls sideways on iOS. */
.shelf-title, .v-title, .v-author, .teaser-text, .incoming-name,
.jr-snippet, .chapter-item, h1, h2 { overflow-wrap: anywhere; }

[hidden] { display: none !important; }

button { font-family: var(--display); cursor: pointer; }
input, select { font-family: var(--body); }

/* ---------- buttons ---------- */
.btn-primary {
  background: var(--amber);
  color: #1a1206;
  border: none;
  border-radius: 8px;
  padding: 13px 22px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: .02em;
  transition: transform .12s ease, box-shadow .2s ease, opacity .2s;
  box-shadow: 0 6px 20px -8px rgba(232, 161, 58, 0.7);
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 10px 26px -8px rgba(232,161,58,0.8); }
.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled { opacity: .6; cursor: default; transform: none; }

.btn-ghost {
  background: transparent;
  color: var(--ink-dim);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 15px;
  font-size: 13px;
  transition: color .2s, border-color .2s, background .2s;
}
.btn-ghost:hover { color: var(--ink); border-color: rgba(120,140,180,0.4); background: rgba(255,255,255,0.03); }

.btn-link {
  background: none; border: none; color: var(--amber);
  font-family: var(--body); font-size: 13.5px; padding: 6px; text-decoration: underline;
  text-underline-offset: 3px; text-decoration-color: rgba(232,161,58,0.4);
}
.btn-link:hover { text-decoration-color: var(--amber); }

.form-error { color: var(--danger); font-size: 13.5px; min-height: 18px; margin: 0; }
.form-note { color: var(--cyan); font-size: 13px; min-height: 16px; margin: 0; }
.muted { color: var(--ink-faint); font-weight: 400; }

/* ---------- auth ---------- */
.auth-screen {
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background:
    radial-gradient(80% 60% at 50% 0%, rgba(232,161,58,0.08), transparent 60%),
    radial-gradient(90% 70% at 80% 100%, rgba(90,208,216,0.07), transparent 55%),
    linear-gradient(180deg, var(--void-0), var(--void-1));
}
.auth-card {
  width: 100%; max-width: 400px;
  animation: rise .6s cubic-bezier(.2,.8,.2,1) both;
}
.brand { text-align: center; margin-bottom: 32px; }
.brand-mark { font-size: 34px; color: var(--amber); display: block; margin-bottom: 8px;
  text-shadow: 0 0 24px rgba(232,161,58,0.6); }
.brand h1 {
  font-family: var(--display); font-weight: 700; font-size: 30px; margin: 0;
  letter-spacing: .04em;
}
.brand-tag { font-family: var(--serif); font-style: italic; color: var(--ink-dim); margin: 6px 0 0; }

.auth-form {
  background: var(--panel); backdrop-filter: blur(12px);
  border: 1px solid var(--line); border-radius: 16px; padding: 28px 26px;
  display: flex; flex-direction: column; gap: 15px;
}
.auth-form h2 { font-family: var(--display); font-weight: 500; font-size: 19px; margin: 0 0 4px; }
.auth-form label, .settings-form label, .book-form label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12.5px; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-faint);
}
.auth-form input, .settings-form input, .settings-form select,
.book-form input {
  background: rgba(7, 11, 22, 0.6); border: 1px solid var(--line); border-radius: 8px;
  padding: 12px 14px; color: var(--ink); font-size: 15px; text-transform: none; letter-spacing: 0;
  transition: border-color .2s, box-shadow .2s;
}
input:focus, select:focus {
  outline: none; border-color: var(--amber);
  box-shadow: 0 0 0 3px var(--amber-soft);
}
.auth-form .btn-primary { margin-top: 6px; }
.auth-form .btn-link { align-self: center; }

/* ---------- top bar ---------- */
.top-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 22px; border-bottom: 1px solid var(--line);
  padding-top: calc(16px + env(safe-area-inset-top));
}
.top-brand { font-family: var(--display); font-weight: 600; letter-spacing: .04em; color: var(--ink); }
.top-right { display: flex; align-items: center; gap: 14px; }
.whoami { font-size: 13px; color: var(--ink-dim); }

/* ---------- library ---------- */
.library-screen {
  background:
    radial-gradient(70% 50% at 20% 10%, rgba(90,208,216,0.06), transparent 60%),
    linear-gradient(180deg, var(--void-0), var(--void-1));
}
.library-body { display: flex; justify-content: center; padding: 8vh 24px 40px; }
.chart-panel {
  width: 100%; max-width: 520px;
  animation: rise .5s cubic-bezier(.2,.8,.2,1) both;
}
.eyebrow {
  font-family: var(--display); font-size: 12px; letter-spacing: .22em; text-transform: uppercase;
  color: var(--amber); margin: 0 0 10px;
}
.chart-panel h2 { font-family: var(--display); font-weight: 600; font-size: 30px; margin: 0 0 10px; }
.lede { font-family: var(--serif); font-size: 17px; color: var(--ink-dim); margin: 0 0 26px; line-height: 1.5; }
.book-form { display: flex; flex-direction: column; gap: 16px; }
.book-form .btn-primary { align-self: flex-start; margin-top: 4px; }

.wpm-hint { margin-top: 20px; font-size: 14px; color: var(--ink-dim); font-family: var(--serif); }

/* ---------- upload + shelf ---------- */
.or-divider {
  display: flex; align-items: center; gap: 12px; margin: 22px 0 16px;
  color: var(--ink-faint); font-family: var(--display); font-size: 12px;
  letter-spacing: .2em; text-transform: uppercase;
}
.or-divider::before, .or-divider::after {
  content: ""; flex: 1; height: 1px; background: var(--line);
}
.upload-drop {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 26px 20px; border: 1.5px dashed var(--line); border-radius: 12px;
  cursor: pointer; text-align: center; transition: border-color .2s, background .2s;
}
.upload-drop:hover, .upload-drop.drag {
  border-color: var(--amber); background: var(--amber-soft);
}
.upload-icon { font-size: 24px; color: var(--amber); }
.upload-text { font-family: var(--display); font-weight: 600; font-size: 15px; color: var(--ink); }
.upload-sub { font-size: 13px; color: var(--ink-faint); }

.library-body { gap: 40px; flex-wrap: wrap; align-items: flex-start; }
.shelf-panel { width: 100%; max-width: 340px; animation: rise .5s cubic-bezier(.2,.8,.2,1) both; }
.shelf-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.shelf-item {
  display: flex; flex-direction: column; gap: 5px; text-align: left;
  background: var(--surface, rgba(16,24,43,0.5)); border: 1px solid var(--line);
  border-radius: 10px; padding: 14px 16px; cursor: pointer; transition: border-color .2s, transform .1s;
}
.shelf-item:hover { border-color: rgba(120,140,180,0.45); transform: translateY(-1px); }
.shelf-title { font-family: var(--display); font-weight: 600; font-size: 15px; color: var(--ink); }
.shelf-meta { font-size: 12px; color: var(--ink-faint); }
.shelf-bar { height: 4px; border-radius: 3px; background: rgba(120,140,180,0.2); overflow: hidden; }
.shelf-bar span { display: block; height: 100%; background: var(--amber); }
.shelf-pct { font-size: 11px; color: var(--cyan); font-family: var(--display); letter-spacing: .04em; }

/* ---------- jump panel ---------- */
.jump-form { display: flex; flex-direction: column; gap: 12px; margin-bottom: 16px; }
.jump-form label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12.5px; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-faint);
}
.jump-form input {
  background: rgba(7,11,22,0.6); border: 1px solid var(--line); border-radius: 8px;
  padding: 12px 14px; color: var(--ink); font-size: 15px; text-transform: none; letter-spacing: 0;
}
.jump-results { display: flex; flex-direction: column; gap: 8px; max-height: 34vh; overflow-y: auto; }
.jump-result {
  display: flex; gap: 12px; align-items: flex-start; text-align: left;
  background: rgba(7,11,22,0.4); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; cursor: pointer; transition: border-color .2s;
}
.jump-result:hover { border-color: var(--amber); }
.jr-pct { font-family: var(--display); font-weight: 600; font-size: 13px; color: var(--amber); flex: none; }
.jr-snippet { font-family: var(--serif); font-size: 13.5px; color: var(--ink-dim); line-height: 1.4; }
.jump-chapters { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 16px; }
.chapter-list { display: flex; flex-direction: column; gap: 4px; max-height: 26vh; overflow-y: auto; }
.chapter-item {
  display: flex; justify-content: space-between; gap: 12px; text-align: left;
  background: transparent; border: none; border-bottom: 1px solid var(--line);
  padding: 10px 4px; cursor: pointer; color: var(--ink); font-family: var(--serif); font-size: 14.5px;
}
.chapter-item:hover { color: var(--amber); }
.chapter-pct { font-family: var(--display); font-size: 12px; color: var(--ink-faint); }

/* ---------- wpm test screen ---------- */
.wpm-screen {
  background:
    radial-gradient(70% 50% at 80% 10%, rgba(232,161,58,0.06), transparent 60%),
    linear-gradient(180deg, var(--void-0), var(--void-1));
  display: flex; justify-content: center;
}
.wpm-body { width: 100%; max-width: 620px; padding: 24px; padding-top: calc(24px + env(safe-area-inset-top)); }
.wpm-head { display: flex; align-items: center; gap: 16px; margin-bottom: 28px; }
.wpm-stage { animation: rise .4s cubic-bezier(.2,.8,.2,1) both; }
.wpm-stage h2 { font-family: var(--display); font-weight: 600; font-size: 27px; margin: 0 0 10px; }
.wpm-lang { margin: 20px 0 8px; }
.wpm-lang label, .wpm-custom label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12.5px; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-faint);
}
.wpm-lang select, .wpm-custom textarea {
  background: rgba(7,11,22,0.6); border: 1px solid var(--line); border-radius: 8px;
  padding: 12px 14px; color: var(--ink); font-size: 15px; text-transform: none; letter-spacing: 0;
}
.wpm-custom { margin: 6px 0 14px; }
.wpm-custom textarea { width: 100%; font-family: var(--serif); resize: vertical; line-height: 1.5; }
.wpm-count { font-size: 12px; color: var(--cyan); font-family: var(--display); }
.wpm-passage {
  font-family: var(--serif); font-size: 19px; line-height: 1.7; color: var(--ink);
  margin: 0 0 26px; max-width: 60ch;
}
.wpm-number { font-family: var(--display); margin: 4px 0 8px; }
.wpm-number #wpmValue { font-size: 64px; font-weight: 700; color: var(--amber); }
.wpm-unit { font-size: 22px; color: var(--ink-dim); }
.wpm-actions { display: flex; align-items: center; gap: 18px; margin-top: 18px; }

/* ---------- settings sheet ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(4, 7, 14, 0.6); backdrop-filter: blur(4px);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .2s ease;
}
.sheet-card {
  width: 100%; max-width: 520px; background: var(--panel-solid);
  border: 1px solid var(--line); border-bottom: none;
  border-radius: 18px 18px 0 0; padding: 22px 24px calc(28px + env(safe-area-inset-bottom));
  animation: slideUp .3s cubic-bezier(.2,.8,.2,1);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.sheet-head h3 { font-family: var(--display); margin: 0; font-size: 20px; }
.settings-form { display: flex; flex-direction: column; gap: 16px; }
.key-row { display: flex; flex-direction: column; gap: 10px; }
.key-status { font-size: 13px; color: var(--ink-dim); font-family: var(--display); letter-spacing: .04em; margin: 0; }
.settings-actions { display: flex; align-items: center; gap: 16px; margin-top: 4px; }

/* ---------- voyage ---------- */
.voyage-screen { position: fixed; inset: 0; overflow: hidden; }
#voyageCanvas, #cockpitCanvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.view-toggle { margin-left: auto; }

/* cockpit: incoming milestone indicator (top center) */
.incoming {
  position: absolute; top: calc(64px + env(safe-area-inset-top)); left: 50%;
  transform: translateX(-50%); z-index: 9;
  display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center;
  pointer-events: none;
}
.incoming-label {
  font-family: var(--display); font-size: 10px; letter-spacing: .22em; text-transform: uppercase;
  color: var(--ink-faint);
}
.incoming-name { font-family: var(--display); font-weight: 600; font-size: 15px; color: var(--ink-dim); }
.incoming-eta { font-family: var(--display); font-size: 12px; color: var(--cyan); }

/* cockpit: vertical fuel/progress gauge (left side) */
.fuel-gauge {
  position: absolute; left: 18px; top: 0; bottom: 0; z-index: 9;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  pointer-events: none;
}
.fuel-track {
  position: relative; width: 6px; height: 44vh; border-radius: 4px;
  background: rgba(120,140,180,0.18); overflow: visible;
}
.fuel-fill {
  position: absolute; left: 0; right: 0; bottom: 0; height: 0%;
  background: linear-gradient(180deg, var(--amber), rgba(232,161,58,0.4));
  border-radius: 4px; box-shadow: 0 0 12px rgba(232,161,58,0.6);
}
.fuel-ship {
  position: absolute; left: 50%; transform: translate(-50%, 50%); bottom: 0;
  width: 12px; height: 12px; border-radius: 50%; background: var(--ship, #ffd9a0);
  box-shadow: 0 0 10px var(--amber);
}
.fuel-marks { position: absolute; inset: 0; }
.fuel-mark {
  position: absolute; left: 50%; transform: translate(-50%, 50%);
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--void-1); border: 1.5px solid var(--waypointAhead, #5a6a8a);
}
.fuel-mark.reached { border-color: var(--cyan); background: var(--cyan); box-shadow: 0 0 8px var(--cyan); }
.fuel-label {
  font-family: var(--display); font-size: 9px; letter-spacing: .2em; text-transform: uppercase;
  color: var(--ink-faint); writing-mode: vertical-rl;
}

.voyage-head {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 16px 20px; padding-top: calc(16px + env(safe-area-inset-top));
}
.voyage-title { display: flex; flex-direction: column; line-height: 1.15; }
.v-title { font-family: var(--display); font-weight: 600; font-size: 17px; }
.v-author { font-family: var(--serif); font-style: italic; font-size: 13px; color: var(--ink-dim); }

.teaser-banner {
  position: absolute; top: 34%; left: 0; right: 0; z-index: 8;
  display: flex; justify-content: center; padding: 0 24px; pointer-events: none;
}
.teaser-text {
  font-family: var(--serif); font-style: italic; font-size: clamp(19px, 4vw, 27px);
  color: var(--ink); text-align: center; max-width: 20ch; line-height: 1.35;
  text-shadow: 0 2px 30px rgba(7,11,22,0.9);
  transition: opacity .4s;
}

/* waypoint labels over the canvas */
.waypoint-layer { position: absolute; inset: 0; z-index: 6; pointer-events: none; }
.waypoint-label {
  position: absolute; transform: translate(-50%, -100%);
  font-family: var(--display); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--ink-faint); white-space: nowrap; transition: color .3s;
  padding: 3px 7px; border-radius: 5px;
  background: rgba(7, 11, 22, 0.55); backdrop-filter: blur(2px);
}
.waypoint-label.reached { color: var(--cyan); }

/* HUD */
.hud {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 10;
  background: linear-gradient(180deg, transparent, rgba(7,11,22,0.85) 40%);
  padding: 40px 20px calc(20px + env(safe-area-inset-bottom));
}
.hud-stats {
  display: flex; justify-content: space-between; gap: 16px; max-width: 640px; margin: 0 auto 16px;
}
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat:nth-child(2) { align-items: center; }
.stat:last-child { align-items: flex-end; text-align: right; }
.stat-label {
  font-family: var(--display); font-size: 10px; letter-spacing: .16em; text-transform: uppercase;
  color: var(--ink-faint);
}
.stat-value { font-family: var(--display); font-weight: 600; font-size: 16px; color: var(--ink);
  font-variant-numeric: tabular-nums; }
.stat-value.big { font-size: 28px; color: var(--amber); line-height: 1; }
.stat-sub { font-size: 12px; color: var(--cyan); font-family: var(--display); }

.hud-controls { max-width: 640px; margin: 0 auto; }
.progress-slider { width: 100%; margin: 0 0 14px; }
.control-row { display: flex; align-items: center; gap: 14px; }
.wpm-control { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.wpm-readout { font-family: var(--display); font-size: 12.5px; color: var(--ink-dim); min-width: 62px; }

/* range inputs — themed */
input[type="range"] {
  -webkit-appearance: none; appearance: none; height: 4px; border-radius: 4px;
  background: rgba(120,140,180,0.25); outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%;
  background: var(--amber); cursor: pointer; box-shadow: 0 0 12px rgba(232,161,58,0.8);
  border: 2px solid #1a1206;
}
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%; background: var(--amber);
  cursor: pointer; border: 2px solid #1a1206;
}
.wpm-control input[type="range"] { width: 120px; }

/* ---------- animations ---------- */
@keyframes rise { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(30px); } to { transform: none; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ---------- responsive (iPad / wider) ---------- */
@media (min-width: 820px) {
  .teaser-text { max-width: 24ch; }
  .stat-value.big { font-size: 36px; }
}
@media (max-width: 420px) {
  .hud-stats { font-size: 13px; }
  .control-row { flex-wrap: wrap; }
  .wpm-control { margin-left: 0; width: 100%; }
  .wpm-control input[type="range"] { flex: 1; width: auto; }
}
