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

:root {
  --bg: #111111;
  --bg2: #1a1a1a;
  --bg3: #222222;
  --border: #2e2e2e;
  --text: #e8e8e8;
  --text-muted: #777;
  --accent: #d4a853;
  --accent-dim: rgba(212, 168, 83, 0.15);
  --radius: 8px;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-serif: 'Georgia', 'Times New Roman', serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}

#app {
  height: 100%;
  position: relative;
  overflow: hidden;
}

/* Screens */
.screen {
  display: none;
  flex-direction: column;
  height: 100%;
  position: absolute;
  inset: 0;
}
.screen.active {
  display: flex;
}

/* Top bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 56px;
}

.app-title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 55vw;
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 8px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  transition: color 0.15s, background 0.15s;
}
.icon-btn:hover, .icon-btn:active { color: var(--text); background: var(--bg3); }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.saved { color: var(--accent); }

/* Input screen */
.input-main {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px calc(var(--safe-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 0;
}

.input-group { display: flex; flex-direction: column; gap: 8px; }

.input-group label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.url-row { display: flex; gap: 8px; }

input[type="url"], textarea {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 16px;
  padding: 12px 14px;
  width: 100%;
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
}
input[type="url"]:focus, textarea:focus {
  border-color: var(--accent);
}
textarea { resize: vertical; min-height: 140px; line-height: 1.5; }

.primary-btn {
  background: var(--accent);
  color: #111;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  padding: 12px 20px;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s;
  -webkit-appearance: none;
}
.primary-btn:active { opacity: 0.8; }
.primary-btn.full-width { width: 100%; }

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-muted);
  font-size: 13px;
  margin: 28px 0;
}
.divider::before, .divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* Player bar */
#player-bar {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.player-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.play-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--accent);
  color: #111;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s, transform 0.1s;
}
.play-btn:active { opacity: 0.8; transform: scale(0.95); }
.play-btn svg { width: 22px; height: 22px; }

.ctrl-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg3);
  color: var(--text-muted);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
}
.ctrl-btn:hover { color: var(--text); background: var(--border); }
.ctrl-btn svg { width: 16px; height: 16px; }

#read-time {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  white-space: nowrap;
  margin-left: 4px;
}

.player-settings {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.speed-group, .voice-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.speed-group label, .voice-group label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
  width: 42px;
  flex-shrink: 0;
}

input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

#speed-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  width: 34px;
  text-align: right;
  flex-shrink: 0;
}

select {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  padding: 6px 10px;
  outline: none;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

/* Article area */
#article-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#article-content {
  max-width: 680px;
  margin: 0 auto;
  padding: 28px 20px calc(var(--safe-bottom) + 48px);
}

#article-title {
  font-family: var(--font-serif);
  font-size: clamp(22px, 5vw, 32px);
  font-weight: 700;
  line-height: 1.25;
  color: var(--text);
  margin-bottom: 12px;
}

#article-meta {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 28px;
  line-height: 1.4;
}

#article-body {
  font-family: var(--font-serif);
  font-size: clamp(17px, 4vw, 20px);
  line-height: 1.75;
  color: #d4d4d4;
}

#article-body p { margin-bottom: 1.2em; }
#article-body h1, #article-body h2, #article-body h3,
#article-body h4, #article-body h5, #article-body h6 {
  font-family: var(--font-sans);
  color: var(--text);
  margin: 1.6em 0 0.5em;
  line-height: 1.3;
}
#article-body h2 { font-size: 1.25em; }
#article-body h3 { font-size: 1.1em; }
#article-body a { color: var(--accent); text-decoration: none; }
#article-body img { max-width: 100%; border-radius: var(--radius); margin: 1em 0; }
#article-body blockquote {
  border-left: 3px solid var(--accent);
  margin: 1.2em 0;
  padding: 0.2em 1em;
  color: var(--text-muted);
  font-style: italic;
}
#article-body ul, #article-body ol {
  margin: 0.8em 0 1.2em 1.5em;
}
#article-body li { margin-bottom: 0.4em; }
#article-body pre, #article-body code {
  font-family: 'Menlo', 'Monaco', monospace;
  font-size: 0.85em;
  background: var(--bg3);
  border-radius: 4px;
}
#article-body code { padding: 0.1em 0.35em; }
#article-body pre { padding: 1em; overflow-x: auto; }
#article-body pre code { background: none; padding: 0; }

/* Tappable paragraphs */
#article-body p,
#article-body h1, #article-body h2, #article-body h3, #article-body h4,
#article-body li, #article-body blockquote {
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.15s;
  margin-left: -4px;
  margin-right: -4px;
  padding-left: 4px;
  padding-right: 4px;
}
#article-body p:hover,
#article-body h1:hover, #article-body h2:hover,
#article-body h3:hover, #article-body h4:hover,
#article-body li:hover, #article-body blockquote:hover {
  background: rgba(255,255,255,0.04);
}
#article-body p:active,
#article-body h1:active, #article-body h2:active,
#article-body h3:active, #article-body h4:active,
#article-body li:active, #article-body blockquote:active {
  background: var(--accent-dim);
}

/* Highlight spoken word */
.speaking-highlight {
  background: var(--accent-dim);
  border-radius: 3px;
  color: var(--accent);
  transition: background 0.1s;
}

/* Saved list */
#saved-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0 calc(var(--safe-bottom) + 8px);
}

.saved-item {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  cursor: pointer;
  transition: background 0.1s;
}
.saved-item:active { background: var(--bg2); }

.saved-item-text {
  flex: 1;
  overflow: hidden;
}
.saved-item-title {
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.saved-item-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 3px;
}

.saved-item-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  transition: color 0.15s;
  flex-shrink: 0;
}
.saved-item-delete:hover { color: #e05050; }
.saved-item-delete svg { width: 16px; height: 16px; }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  font-size: 15px;
  padding: 60px 20px;
}

/* Toast */
#toast {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 20px);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 20px;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  white-space: nowrap;
  z-index: 100;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Loading overlay */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(17,17,17,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Error dialog */
#error-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

#error-dialog {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#error-title {
  font-size: 15px;
  font-weight: 700;
  color: #e05050;
}

#error-msg {
  font-size: 14px;
  color: var(--text);
  line-height: 1.6;
  word-break: break-word;
  white-space: pre-wrap;
  max-height: 50vh;
  overflow-y: auto;
}

#error-close {
  align-self: flex-end;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  padding: 8px 20px;
  cursor: pointer;
  margin-top: 4px;
}
#error-close:active { opacity: 0.7; }

/* Desktop tweaks */
@media (min-width: 640px) {
  #player-bar {
    flex-direction: row;
    align-items: center;
    gap: 24px;
    padding: 14px 24px;
  }
  .player-settings {
    flex-direction: row;
    flex: 1;
    gap: 20px;
  }
  .speed-group, .voice-group { flex: 1; }
  #article-content { padding: 40px 32px 80px; }
}
