:root {
  --bg: #0a0a0c;
  --surface: #16161a;
  --surface-hover: #1f1f24;
  --text: #f5f5f7;
  --text-secondary: #8a8a92;
  --text-muted: #5a5a62;
  --accent: #ff5c39;
  --accent-soft: #ff8a5c;
  --accent-dim: rgba(255, 92, 57, 0.12);
  --border: #2a2a30;
  --border-strong: #3a3a42;
  --font-sans: 'Geist', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  --font-mono: 'Geist Mono', 'SF Mono', Monaco, monospace;
  --font-serif: 'Newsreader', Georgia, 'Iowan Old Style', serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  font-feature-settings: 'ss01', 'cv11';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  user-select: none;
  -webkit-user-select: none;
  transition: background 240ms ease, color 240ms ease;
}

button { font-family: inherit; color: inherit; }
input, textarea { font-family: inherit; }

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 240ms ease;
}
.screen.active { opacity: 1; pointer-events: auto; }

/* ============ Upload Screen ============ */
.upload-screen {
  padding: max(env(safe-area-inset-top), 24px) 24px max(env(safe-area-inset-bottom), 24px);
  overflow-y: auto;
}

.app-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 28px 0 36px;
}

.app-logo {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-soft) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 18px;
  box-shadow: 0 12px 32px rgba(255, 92, 57, 0.3), inset 0 1px 0 rgba(255,255,255,0.2);
  position: relative;
}

.app-logo svg { width: 30px; height: 30px; color: white; }

.app-title {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.025em;
  margin-bottom: 4px;
}

.app-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 400;
  letter-spacing: 0.02em;
}

.upload-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 520px;
  width: 100%;
  margin: 0 auto;
}

.drop-zone {
  flex: 0 0 auto;
  min-height: 168px;
  border: 1.5px dashed var(--border-strong);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  cursor: pointer;
  transition: all 200ms ease;
  background: var(--surface);
  text-align: center;
}

.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.drop-zone-icon {
  width: 40px;
  height: 40px;
  margin-bottom: 14px;
  color: var(--text-secondary);
  transition: color 200ms ease;
}

.drop-zone.dragover .drop-zone-icon { color: var(--accent); }

.drop-zone-title {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 6px;
  letter-spacing: -0.01em;
}

.drop-zone-subtitle {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
  letter-spacing: 0.02em;
}

.divider {
  display: flex;
  align-items: center;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  gap: 12px;
  margin: 4px 0;
}
.divider::before, .divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.paste-textarea {
  width: 100%;
  min-height: 110px;
  max-height: 180px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  resize: vertical;
  transition: border-color 200ms ease;
  user-select: text;
  -webkit-user-select: text;
}
.paste-textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.paste-textarea::placeholder { color: var(--text-muted); }

.btn-primary {
  width: 100%;
  padding: 14px 20px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 180ms ease;
  letter-spacing: -0.01em;
}
.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 10px 28px rgba(255, 92, 57, 0.35);
}
.btn-primary:active:not(:disabled) { transform: translateY(0); }
.btn-primary:disabled { opacity: 0.35; cursor: not-allowed; }

.recent-section {
  margin-top: 12px;
  padding-bottom: 24px;
}
.recent-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-bottom: 12px;
  padding-left: 4px;
}

.recent-item {
  display: flex;
  align-items: center;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: background 180ms ease, border-color 180ms ease;
  gap: 12px;
}
.recent-item:hover { background: var(--surface-hover); border-color: var(--border-strong); }

.recent-item-icon {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  background: var(--accent-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  flex-shrink: 0;
}

.recent-item-info { flex: 1; min-width: 0; }
.recent-item-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.005em;
}
.recent-item-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
  font-family: var(--font-mono);
  font-feature-settings: 'tnum';
}

.recent-item-progress {
  width: 36px;
  height: 36px;
  position: relative;
  flex-shrink: 0;
}
.recent-item-progress svg { transform: rotate(-90deg); }
.recent-item-progress circle { fill: none; stroke-width: 2.5; }
.recent-item-progress .bg-circle { stroke: var(--border); }
.recent-item-progress .fg-circle { stroke: var(--accent); transition: stroke-dasharray 300ms ease; }
.recent-item-progress-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-family: var(--font-mono);
  color: var(--text-secondary);
  font-feature-settings: 'tnum';
}

.recent-item-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  display: flex;
}
.recent-item-delete:hover { color: var(--text); background: var(--surface-hover); }

/* ============ Reader Screen ============ */
.reader-screen { background: var(--bg); }

.reader-top {
  display: flex;
  align-items: center;
  padding: max(env(safe-area-inset-top), 12px) 12px 8px;
  gap: 4px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: none;
  background: transparent;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 150ms ease;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface); }
.icon-btn:active { background: var(--surface-hover); transform: scale(0.96); }
.icon-btn svg { width: 22px; height: 22px; }

.reader-title {
  flex: 1;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  letter-spacing: -0.005em;
  padding: 0 4px;
}

.reader-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 24px 16px;
  cursor: pointer;
  min-height: 0;
}

.focus-rail {
  position: relative;
  width: 100%;
  max-width: 760px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.focus-guide {
  width: 1px;
  height: 16px;
  background: var(--border-strong);
  position: relative;
}
.focus-guide::before {
  content: '';
  position: absolute;
  width: 10px;
  height: 1px;
  background: var(--border-strong);
  left: -4.5px;
}
.focus-guide.top::before { bottom: 0; }
.focus-guide.bottom::before { top: 0; }

.word-display {
  width: 100%;
  display: flex;
  justify-content: center;
  font-size: clamp(36px, 8vw, 56px);
  font-weight: 500;
  letter-spacing: -0.025em;
  font-feature-settings: 'ss01', 'cv11';
  height: 1.25em;
  line-height: 1.25;
  overflow: hidden;
  transition: font-size 200ms ease, font-family 200ms ease;
}

.word-rail {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: baseline;
  width: 100%;
  white-space: nowrap;
}
.word-pre { justify-self: end; color: var(--text); }
.word-orp { color: var(--accent); }
.word-post { justify-self: start; color: var(--text); }

.context-line {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 400;
  margin-top: 8px;
  text-align: center;
  max-width: 92%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-feature-settings: 'ss01';
  letter-spacing: 0.005em;
  height: 1.4em;
}

/* ============ Reader Bottom ============ */
.reader-bottom {
  padding: 12px 18px max(env(safe-area-inset-bottom), 16px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.progress-section { display: flex; flex-direction: column; gap: 6px; }

.progress-bar {
  width: 100%;
  height: 4px;
  background: var(--surface);
  border-radius: 2px;
  position: relative;
  overflow: hidden;
  cursor: pointer;
}
.progress-bar::before {
  content: '';
  position: absolute;
  inset: -10px 0;
}
.progress-fill {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  background: linear-gradient(90deg, var(--accent), var(--accent-soft));
  border-radius: 2px;
  transition: width 100ms linear;
  width: 0%;
}

.progress-meta {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-feature-settings: 'tnum';
  letter-spacing: 0.02em;
}

.controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 380px;
  margin: 0 auto;
  width: 100%;
}

.control-btn {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  border: none;
  background: transparent;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 150ms ease;
}
.control-btn:hover { background: var(--surface); }
.control-btn:active { transform: scale(0.92); }
.control-btn svg { width: 22px; height: 22px; }
.control-btn.disabled { opacity: 0.3; pointer-events: none; }

.play-btn {
  width: 60px;
  height: 60px;
  background: var(--accent);
  border-radius: 50%;
  color: white;
  box-shadow: 0 8px 24px rgba(255, 92, 57, 0.35), inset 0 1px 0 rgba(255,255,255,0.2);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 150ms ease;
}
.play-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(255, 92, 57, 0.45), inset 0 1px 0 rgba(255,255,255,0.2);
}
.play-btn:active { transform: scale(0.96); }
.play-btn svg { width: 26px; height: 26px; fill: currentColor; }

.speed-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding-top: 4px;
}

.speed-display {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-family: var(--font-mono);
}
.speed-value {
  font-size: 20px;
  font-weight: 500;
  color: var(--text);
  font-feature-settings: 'tnum';
  letter-spacing: -0.02em;
}
.speed-unit {
  font-size: 10px;
  color: var(--text-secondary);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-weight: 500;
}

.speed-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  max-width: 480px;
  height: 32px;
  background: transparent;
  cursor: pointer;
  margin: 0;
  padding: 0;
}
.speed-slider:focus { outline: none; }

.speed-slider::-webkit-slider-runnable-track {
  height: 4px;
  background: var(--surface);
  border-radius: 2px;
  border: 1px solid var(--border);
}
.speed-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  background: var(--text);
  border-radius: 50%;
  margin-top: -10px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4), 0 0 0 1px var(--border);
  transition: transform 100ms ease;
  cursor: grab;
}
.speed-slider::-webkit-slider-thumb:hover { transform: scale(1.1); }
.speed-slider::-webkit-slider-thumb:active { transform: scale(0.95); cursor: grabbing; background: var(--accent); }

.speed-slider::-moz-range-track {
  height: 4px;
  background: var(--surface);
  border-radius: 2px;
  border: 1px solid var(--border);
}
.speed-slider::-moz-range-thumb {
  width: 22px;
  height: 22px;
  background: var(--text);
  border-radius: 50%;
  border: none;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  cursor: grab;
}

/* ============ Settings Panel ============ */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
  z-index: 100;
}
.settings-overlay.active { opacity: 1; pointer-events: auto; }

.settings-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top-left-radius: 24px;
  border-top-right-radius: 24px;
  padding: 12px 22px max(env(safe-area-inset-bottom), 24px);
  transform: translateY(100%);
  transition: transform 320ms cubic-bezier(0.32, 0.72, 0, 1);
  z-index: 101;
  max-height: 86vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-bottom: none;
}
.settings-panel.active { transform: translateY(0); }

.settings-handle {
  width: 36px;
  height: 4px;
  background: var(--border-strong);
  border-radius: 2px;
  margin: 0 auto 18px;
}

.settings-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 22px;
  letter-spacing: -0.02em;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 22px;
}

.setting-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.setting-row:last-child { border-bottom: none; }
.setting-row-name { font-size: 14px; color: var(--text); flex: 1; }

.toggle-bar {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--bg);
  border-radius: 10px;
  padding: 3px;
  border: 1px solid var(--border);
  gap: 0;
}

.toggle-option {
  padding: 8px 10px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  text-align: center;
  cursor: pointer;
  background: none;
  border: none;
  color: var(--text-secondary);
  transition: all 180ms ease;
  white-space: nowrap;
}
.toggle-option.active {
  background: var(--accent);
  color: white;
  box-shadow: 0 2px 8px rgba(255, 92, 57, 0.3);
}

.mini-toggle {
  display: inline-grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(48px, auto);
  background: var(--bg);
  border-radius: 8px;
  padding: 2px;
  border: 1px solid var(--border);
}
.mini-toggle .toggle-option { font-size: 12px; padding: 6px 10px; border-radius: 6px; }

.kbd-hint {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}
.kbd-hint kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
}

/* ============ Misc ============ */
.hidden { display: none !important; }

.spinner {
  width: 26px;
  height: 26px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 12, 0.92);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
}
.loading-overlay.active { opacity: 1; pointer-events: auto; }
.loading-text { font-size: 13px; color: var(--text-secondary); letter-spacing: 0.02em; }

.toast {
  position: fixed;
  bottom: max(env(safe-area-inset-bottom), 24px);
  left: 50%;
  transform: translateX(-50%) translateY(150%);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 13px;
  z-index: 300;
  transition: transform 320ms cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 12px 32px rgba(0,0,0,0.5);
  white-space: nowrap;
}
.toast.active { transform: translateX(-50%) translateY(0); }

/* ============ Chapters Panel ============ */
.chapters-panel { max-height: 78vh; }
.chapters-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-bottom: 8px;
}
.chapter-item {
  display: flex;
  align-items: baseline;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: none;
  background: none;
  color: var(--text);
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  transition: background 150ms ease;
}
.chapter-item:hover { background: var(--surface-hover); }
.chapter-item.active { background: var(--accent-dim); }
.chapter-num {
  flex-shrink: 0;
  min-width: 26px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-muted);
  font-feature-settings: 'tnum';
}
.chapter-item.active .chapter-num { color: var(--accent); }
.chapter-name {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chapter-item.active .chapter-name { color: var(--accent); font-weight: 500; }

@media (max-height: 600px) {
  .word-display { font-size: clamp(28px, 6vw, 44px); }
  .focus-rail { gap: 14px; }
  .focus-guide { height: 10px; }
  .reader-bottom { gap: 10px; padding-top: 8px; }
  .controls-row { max-width: 320px; }
}
