/* ── Reset & Base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0b0c10;
  --surface:   #13141a;
  --surface2:  #1c1d26;
  --border:    #2a2b38;
  --accent:    #7c6ef9;
  --accent2:   #a78bfa;
  --heading-c: #fbbf24;
  --text:      #f0ecff;
  --muted:     #6b6e8a;
  --red:       #f87171;
  --green:     #34d399;
  --radius:    14px;
}

html, body {
  height: 100%;
  height: 100dvh;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  /* Prevent pull-to-refresh & bounce on iOS */
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

/* ── Screens ──────────────────────────────────────────────────── */
.screen { display: none; width: 100%; height: 100%; height: 100dvh; }
.screen.active { display: flex; flex-direction: column; align-items: center; justify-content: center; }

/* ─── Drop Screen ─────────────────────────────────────────────── */
#drop-screen {
  gap: 28px;
  padding: env(safe-area-inset-top, 20px) 24px env(safe-area-inset-bottom, 20px);
  background: radial-gradient(ellipse at 50% 0%, #1a1040 0%, var(--bg) 70%);
}

.app-title {
  font-size: clamp(1.7rem, 8vw, 2.4rem);
  font-weight: 800;
  letter-spacing: -1px;
  background: linear-gradient(135deg, var(--accent2), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-align: center;
}

.app-subtitle {
  color: var(--muted);
  font-size: clamp(.85rem, 3.5vw, 1rem);
  text-align: center;
  max-width: min(360px, 90vw);
  padding: 0 8px;
}

.drop-zone {
  width: min(360px, calc(100vw - 48px));
  height: clamp(160px, 38vw, 220px);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  cursor: pointer;
  transition: border-color .25s, background .25s;
  background: var(--surface);
  position: relative;
  -webkit-tap-highlight-color: transparent;
}
.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent);
  background: #1a1840;
}
.drop-zone input[type="file"] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer;
}
.drop-icon  { font-size: clamp(2.2rem, 10vw, 3rem); line-height: 1; }
.drop-label { font-size: clamp(.85rem, 3.5vw, .95rem); color: var(--muted); }
.drop-hint  { font-size: clamp(.72rem, 3vw, .8rem); color: var(--border); }

/* ─── Loading Screen ──────────────────────────────────────────── */
#load-screen { gap: 20px; }
.spinner {
  width: 48px; height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.load-msg { color: var(--muted); font-size: 1rem; }

/* ─── Reader Screen ───────────────────────────────────────────── */
#reader-screen {
  display: none;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;
  width: 100%;
}
#reader-screen.active { display: flex; }

/* Top bar */
.top-bar {
  padding: env(safe-area-inset-top, 14px) 16px 0;
  padding-top: max(env(safe-area-inset-top), 14px);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
#chapter-label {
  font-size: .75rem;
  color: var(--muted);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
#back-btn {
  background: none; border: none; color: var(--muted);
  font-size: .8rem; cursor: pointer; padding: 8px 10px;
  border-radius: 8px;
  transition: color .2s, background .2s;
  flex-shrink: 0;
  min-height: 44px;
  display: flex; align-items: center;
  -webkit-tap-highlight-color: transparent;
}
#back-btn:hover { color: var(--text); background: var(--surface2); }

/* Progress */
.progress-track {
  height: 3px;
  background: var(--surface2);
  margin: 8px 16px 0;
  border-radius: 99px;
  overflow: hidden;
  flex-shrink: 0;
}
#progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 99px;
  transition: width .2s;
}
#pos-label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: .72rem;
  color: var(--muted);
  margin-top: 6px;
  flex-shrink: 0;
  white-space: nowrap;
  overflow: hidden;
  padding: 0 12px;
}
#pos-chapter-inline {
  max-width: 50vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--accent2);
  font-weight: 600;
}
.pos-sep {
  color: var(--border);
  flex-shrink: 0;
}

/* ── Word Stage ───────────────────────────────────────────────── */
.word-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 clamp(16px, 6vw, 40px);
  position: relative;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* Guide lines */
.guide-line {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 24px;
  border-radius: 2px;
  background: var(--accent);
  opacity: .35;
}
.guide-top    { top:    calc(50% - 90px); }
.guide-bottom { bottom: calc(50% - 90px); }

#word-display {
  text-align: center;
  max-width: 900px;
  line-height: 1.15;
  transition: opacity .12s ease;
}
#word-display.fade { opacity: 0; }

#word-display.type-word {
  font-size: clamp(56px, 14vw, 120px);
  font-weight: 700;
  letter-spacing: clamp(-1px, -0.02em, -2px);
  color: var(--text);
}
#word-display.type-heading {
  font-size: clamp(28px, 7vw, 64px);
  font-weight: 800;
  color: var(--heading-c);
  text-decoration: underline;
  text-decoration-color: rgba(251,191,36,.45);
  text-underline-offset: 8px;
  text-decoration-thickness: 3px;
  letter-spacing: -1px;
  padding: 0 12px;
}

/* ── Picture token ────────────────────────────────────────── */
#word-display.type-picture {
  font-size: clamp(22px, 5vw, 48px);
  font-weight: 500;
  color: var(--muted);
  font-style: italic;
  letter-spacing: .02em;
  border: 2px dashed var(--border);
  padding: 14px 24px;
  border-radius: var(--radius);
}

/* Pause overlay */
#pause-msg {
  position: absolute;
  font-size: .9rem;
  color: var(--muted);
  bottom: 16px;
  letter-spacing: .05em;
  pointer-events: none;
  opacity: 0;
  transition: opacity .3s;
  text-align: center;
  padding: 0 20px;
}
#pause-msg.visible { opacity: 1; }

/* Swipe hint */
#swipe-hint {
  position: absolute;
  bottom: 44px;
  font-size: .72rem;
  color: var(--border);
  pointer-events: none;
  letter-spacing: .04em;
}

/* ── Controls ─────────────────────────────────────────────────── */
.controls {
  flex-shrink: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 16px max(env(safe-area-inset-bottom), 12px);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.controls-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: nowrap;
}

/* Speed + Font combined row */
.speed-font-row {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

/* Speed row */
.speed-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}
.speed-label {
  font-size: .75rem;
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}
#wpm-slider {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  min-width: 80px;
  height: 6px;
  border-radius: 99px;
  background: var(--border);
  outline: none;
  cursor: pointer;
}
#wpm-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(124,110,249,.25);
  cursor: pointer;
}
#wpm-slider::-moz-range-thumb {
  width: 24px; height: 24px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  cursor: pointer;
}
#wpm-value {
  font-size: .8rem;
  font-weight: 700;
  color: var(--accent2);
  min-width: 58px;
  flex-shrink: 0;
}

/* Font size row */
.font-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.font-row span {
  font-size: .75rem;
  color: var(--muted);
}
.font-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0;
  width: 40px; height: 40px;
  border-radius: 10px;
  font-size: .95rem;
  cursor: pointer;
  transition: background .2s;
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.font-btn:active { background: var(--border); }

/* Playback buttons */
.ctrl-btn {
  display: flex; align-items: center; justify-content: center;
  width: 48px; height: 48px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  font-size: 1.1rem;
  transition: background .15s, border-color .15s, transform .1s;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.ctrl-btn:active { transform: scale(.9); background: var(--border); }

#play-btn {
  width: 62px; height: 62px;
  font-size: 1.5rem;
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 20px rgba(124,110,249,.4);
}
#play-btn:active { background: var(--accent2); }

.kbd {
  display: inline-block;
  font-size: .68rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  line-height: 1.5;
  font-family: monospace;
}

/* Hide keyboard shortcuts on touch devices */
.shortcuts-hint {
  text-align: center;
  font-size: .72rem;
  color: var(--muted);
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}
.shortcut-item { display: flex; align-items: center; gap: 5px; }
@media (pointer: coarse) {
  .shortcuts-hint { display: none; }
}

/* ── Mode toggle ──────────────────────────────────────────────── */
#mode-toggle {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 3px;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}
.mode-pill {
  padding: 6px 12px;
  border-radius: 99px;
  font-size: .75rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--muted);
  transition: background .2s, color .2s;
  white-space: nowrap;
  min-height: 36px;
  display: flex; align-items: center;
  touch-action: manipulation;
}
.mode-pill.active {
  background: var(--accent);
  color: #fff;
}

/* ── Display mode toggle ──────────────────────────────────────── */
#display-toggle {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 3px;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}
.display-pill {
  padding: 5px 11px;
  border-radius: 99px;
  font-size: .72rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--muted);
  transition: background .2s, color .2s;
  white-space: nowrap;
  min-height: 32px;
  display: flex; align-items: center;
  touch-action: manipulation;
}
.display-pill.active {
  background: var(--accent);
  color: #fff;
}

/* ── Sentence mode display ────────────────────────────────────── */
#word-display.type-sentence {
  font-size: clamp(28px, 7vw, 56px);
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(5px, 1.8vw, 14px);
  flex-wrap: nowrap;
  max-width: 98vw;
  line-height: 1.3;
}
.sent-past, .sent-future {
  font-size: .55em;
  font-weight: 500;
  color: var(--muted);
  opacity: .5;
  white-space: nowrap;
  /* Fixed width keeps the current word pixel-centred */
  min-width: clamp(56px, 11vw, 110px);
  max-width: clamp(56px, 11vw, 110px);
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  display: inline-block;
}
.sent-current {
  font-size: 1em;
  font-weight: 800;
  color: var(--accent2);
  background: rgba(124, 110, 249, .18);
  padding: 2px 10px;
  border-radius: 8px;
  white-space: nowrap;
  text-align: center;
  flex-shrink: 0;
}

/* ── Book view ───────────────────────────────────────────────── */
#book-view {
  display: none;
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px clamp(16px, 6vw, 80px) 32px;
  scroll-behavior: smooth;
}
#book-view::-webkit-scrollbar { width: 6px; }
#book-view::-webkit-scrollbar-track { background: transparent; }
#book-view::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }

.bv-heading {
  font-size: clamp(18px, 5vw, 32px);
  font-weight: 800;
  color: var(--heading-c);
  text-decoration: underline;
  text-decoration-color: rgba(251,191,36,.4);
  text-underline-offset: 6px;
  text-decoration-thickness: 2px;
  margin: 2em 0 .6em;
  line-height: 1.2;
}
.bv-heading:first-child { margin-top: 0; }

.bv-para {
  font-size: var(--book-fs, 18px);
  line-height: 1.85;
  color: var(--text);
  margin-bottom: 1.2em;
  max-width: 72ch;
  -webkit-tap-highlight-color: transparent;
}

.bv-word {
  cursor: pointer;
  border-radius: 3px;
  padding: 0 1px;
  transition: background .15s;
}
.bv-word:hover { background: rgba(124,110,249,.25); }
.bv-word.bv-current {
  background: var(--accent);
  color: #fff;
  border-radius: 3px;
}

.bv-picture {
  display: inline-block;
  font-size: var(--book-fs, 18px);
  color: var(--muted);
  font-style: italic;
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 6px 18px;
  margin: .5em 0;
  cursor: pointer;
  transition: background .15s;
}
.bv-picture:hover { background: var(--surface2); }
.bv-picture.bv-current { background: var(--accent); color: #fff; border-color: var(--accent); }

.book-hint {
  text-align: center;
  font-size: .75rem;
  color: var(--muted);
  padding: 8px 0 0;
}

/* hide speed-only elements in book mode */
.reader-speed-only { transition: opacity .2s; }
.book-mode .reader-speed-only { opacity: 0; pointer-events: none; }

/* ── iPhone-specific tweaks ─────────────────────────────────── */
@media (max-width: 430px) {
  .top-bar { gap: 6px; }
  #chapter-label { display: none; }

  .controls { gap: 8px; padding-left: 12px; padding-right: 12px; }

  .speed-font-row { flex-wrap: nowrap; }
  .speed-row { flex: 1; }

  .controls-row { gap: 8px; }
  .ctrl-btn { width: 44px; height: 44px; font-size: 1rem; }
  #play-btn  { width: 56px; height: 56px; font-size: 1.35rem; }

  .font-btn { width: 36px; height: 36px; font-size: .9rem; }

  #book-view { padding: 16px 16px 24px; }
}

/* Very small phones */
@media (max-width: 360px) {
  .ctrl-btn  { width: 38px; height: 38px; font-size: .9rem; }
  #play-btn  { width: 50px; height: 50px; }
  .controls-row { gap: 6px; }
}
