*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --surface: #1a1d26;
  --surface2: #22263a;
  --border: #2e3248;
  --text: #e2e8f0;
  --text-muted: #64748b;
  --text-dim: #334155;
  --accent: #F59E0B;
  --accent-dim: rgba(245,158,11,0.15);
  --guide: rgba(56,189,248,0.35);
  --guide-line: rgba(56,189,248,0.6);
  --focus-zone: rgba(245,158,11,0.04);
  --radius: 8px;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;
  --font-ui: 'Inter', system-ui, sans-serif;
}

body.light {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-dim: #cbd5e1;
  --accent: #d97706;
  --accent-dim: rgba(217,119,6,0.1);
  --guide: rgba(14,165,233,0.3);
  --guide-line: rgba(14,165,233,0.7);
  --focus-zone: rgba(217,119,6,0.04);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  transition: background 0.2s, color 0.2s;
}

/* ── LAYOUT ────────────────────────────────────── */
#app {
  display: grid;
  grid-template-columns: 360px 1fr;
  grid-template-rows: 100vh;
  height: 100vh;
  overflow: hidden;
}

#panel {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 20px;
  gap: 16px;
}

#viewport {
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

/* ── PANEL HEADER ──────────────────────────────── */
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  font-family: var(--font-mono);
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.02em;
}

.theme-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 4px 10px;
  transition: color 0.15s, background 0.15s;
}
.theme-btn:hover { color: var(--text); background: var(--border); }

/* ── SECTIONS ──────────────────────────────────── */
.section-label {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 8px;
}

/* ── TEXTAREA ──────────────────────────────────── */
#input-area {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

#text-input {
  width: 100%;
  flex: 1;
  min-height: 180px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: 1.6;
  padding: 12px;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}
#text-input:focus { border-color: var(--accent); }
#text-input::placeholder { color: var(--text-muted); }

.text-stats {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* ── MODE TABS ─────────────────────────────────── */
.mode-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}

.mode-tab {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 500;
  padding: 8px 10px;
  text-align: center;
  transition: all 0.15s;
}
.mode-tab:hover { color: var(--text); border-color: var(--accent); }
.mode-tab.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

/* ── CONTROLS ──────────────────────────────────── */
.control-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.control-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.control-label {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.control-value {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--accent);
  font-weight: 700;
}

input[type=range] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  transition: transform 0.1s;
}
input[type=range]::-webkit-slider-thumb:hover { transform: scale(1.2); }

.hidden { display: none !important; }

/* ── BUTTONS ───────────────────────────────────── */
.btn-row {
  display: flex;
  gap: 8px;
}

.btn {
  flex: 1;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: var(--font-ui);
  font-size: 0.82rem;
  font-weight: 600;
  transition: all 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: #000;
}
.btn-primary:hover { filter: brightness(1.1); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-secondary {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-secondary:hover { background: var(--border); }
.btn-secondary:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── PROGRESS BAR ──────────────────────────────── */
.progress-wrap {
  background: var(--surface2);
  border-radius: 2px;
  height: 4px;
  overflow: hidden;
  width: 100%;
}
.progress-bar {
  background: var(--accent);
  height: 100%;
  width: 0%;
  transition: width 0.1s linear;
}

/* ── TIME REMAINING ────────────────────────────── */
.time-remaining {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-muted);
  text-align: right;
  margin-top: 5px;
  min-height: 1em;
}

kbd {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  color: var(--text-muted);
  vertical-align: middle;
  line-height: 1.6;
}

/* ── STEP BUTTONS ──────────────────────────────── */
.btn-icon {
  flex: none;
  width: 40px;
  padding: 10px 0;
  font-size: 0.9rem;
}

/* ── STATS ROW ─────────────────────────────────── */
.stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.stat-box {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  text-align: center;
}
.stat-val {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--accent);
}
.stat-lbl {
  font-size: 0.65rem;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ── VIEWPORT ──────────────────────────────────── */
#vp-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

#vp-topbar .vp-title {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

#vp-topbar .vp-mode-badge {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--accent);
  background: var(--accent-dim);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 1px 6px;
}

.vp-progress-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.vp-time-remaining {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--text-muted);
  text-align: right;
  min-height: 1em;
}

#reading-area {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Margin guide lines */
.margin-guide-left,
.margin-guide-right {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--guide-line);
  pointer-events: none;
  z-index: 10;
  transition: opacity 0.2s;
}
.margin-guide-left { left: 10%; }
.margin-guide-right { right: 10%; }

.margin-guide-area-left,
.margin-guide-area-right {
  position: absolute;
  top: 0;
  bottom: 0;
  background: var(--guide);
  pointer-events: none;
  z-index: 9;
}
.margin-guide-area-left { left: 0; width: 10%; }
.margin-guide-area-right { right: 0; width: 10%; }

/* Focus zone band */
.focus-zone {
  position: absolute;
  left: 10%;
  right: 10%;
  height: 80px;
  top: 50%;
  transform: translateY(-50%);
  background: var(--focus-zone);
  border-top: 1px solid rgba(245,158,11,0.12);
  border-bottom: 1px solid rgba(245,158,11,0.12);
  border-radius: 4px;
  pointer-events: none;
  z-index: 8;
}

/* ── RSVP DISPLAY ──────────────────────────────── */
#rsvp-display {
  position: relative;
  z-index: 20;
  text-align: center;
  font-family: var(--font-mono);
  padding: 20px;
  user-select: none;
}

#rsvp-word {
  font-size: 3.5rem;
  font-weight: 500;
  line-height: 1.1;
  letter-spacing: -0.01em;
  color: var(--text);
  min-height: 1.2em;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
}

#rsvp-word .orp {
  color: var(--accent);
  font-weight: 700;
}

/* ── CHUNK DISPLAY ─────────────────────────────── */
#chunk-display {
  position: relative;
  z-index: 20;
  text-align: center;
  font-family: var(--font-mono);
  padding: 20px 12%;
  user-select: none;
}

#chunk-words {
  font-size: 2.2rem;
  font-weight: 500;
  line-height: 1.4;
  letter-spacing: -0.01em;
  color: var(--text);
  opacity: 1;
  transition: opacity 0.12s ease;
}

#chunk-words .orp {
  color: var(--accent);
  font-weight: 700;
}

#chunk-words.fade {
  opacity: 0;
}

/* ── LINE MODE DISPLAY ─────────────────────────── */
#line-display {
  position: relative;
  z-index: 20;
  width: 80%;
  max-width: 800px;
  height: 100%;
  overflow: hidden;
  padding: 20px 0;
}

#line-container {
  position: relative;
  font-family: var(--font-mono);
  font-size: 1rem;
  line-height: 1.9;
  color: var(--text);
}

.line-item {
  padding: 2px 12px;
  border-radius: 4px;
  transition: background 0.15s, color 0.15s;
  color: var(--text-dim);
  position: relative;
}

.line-item.active {
  background: rgba(245,158,11,0.08);
  color: var(--text);
}

.line-item.nearby {
  color: var(--text-muted);
}

/* ── BIONIC DISPLAY ────────────────────────────── */
#bionic-display {
  position: relative;
  z-index: 20;
  width: 80%;
  max-width: 800px;
  height: 100%;
  overflow-y: auto;
  padding: 40px 0;
  font-family: var(--font-mono);
  font-size: 1.05rem;
  line-height: 2;
  color: var(--text);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#bionic-display::-webkit-scrollbar { width: 6px; }
#bionic-display::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.bionic-word b {
  font-weight: 700;
  color: var(--text);
}

.bionic-word.long-word {
  background: rgba(245,158,11,0.07);
  border-radius: 2px;
  padding: 0 1px;
}

/* ── COMPLETE SCREEN ───────────────────────────── */
#complete-screen {
  position: absolute;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  z-index: 100;
}

.complete-title {
  font-family: var(--font-mono);
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--accent);
}

.complete-stats {
  display: grid;
  grid-template-columns: repeat(3, 140px);
  gap: 16px;
  text-align: center;
}

.complete-stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.complete-stat-val {
  font-family: var(--font-mono);
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  display: block;
}

.complete-stat-lbl {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* ── PLACEHOLDER ───────────────────────────────── */
#vp-placeholder {
  z-index: 20;
  text-align: center;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 2;
  user-select: none;
}

.placeholder-icon {
  font-size: 2.5rem;
  margin-bottom: 12px;
  opacity: 0.4;
}

/* ── MOBILE ────────────────────────────────────── */
@media (max-width: 768px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
    min-height: 100vh;
  }
  #panel {
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  #viewport {
    height: 70vh;
  }
  #rsvp-word { font-size: 2.2rem; }
  #chunk-words { font-size: 1.5rem; }
  #line-display, #bionic-display { width: 90%; }
}

/* ── DIVIDER ───────────────────────────────────── */
.divider {
  border: none;
  border-top: 1px solid var(--border);
}

#rsvp-display, #chunk-display {
  font-size-adjust: 0.5;
}
