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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --border: #2e3147;
  --accent: #5b6cff;
  --accent-hover: #7080ff;
  --danger: #ff5b5b;
  --text: #e8eaf0;
  --text-muted: #7a7f9a;
  --success: #4caf82;
  --warning: #f0a050;
  --radius: 8px;
  --font-mono: 'Fira Code', 'Cascadia Code', 'Courier New', monospace;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem 1rem;
}

header {
  width: 100%;
  max-width: 860px;
  margin-bottom: 2rem;
}

header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

header h1 span {
  color: var(--accent);
}

header p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 0.3rem;
}

main {
  width: 100%;
  max-width: 860px;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.card-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

/* Voice selector row */
.voice-row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

select {
  flex: 1;
  min-width: 200px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.8rem;
  font-size: 0.9rem;
  outline: none;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%237a7f9a' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  padding-right: 2.25rem;
  transition: border-color 0.15s;
}

select:focus {
  border-color: var(--accent);
}

.provider-badge {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border-radius: 99px;
  background: rgba(91, 108, 255, 0.15);
  color: var(--accent);
  border: 1px solid rgba(91, 108, 255, 0.3);
  white-space: nowrap;
}

/* Card header row (label + tabs side by side) */
.card-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.card-header-row .card-label {
  margin-bottom: 0;
}

/* Editor mode tab toggle */
.tab-toggle {
  display: flex;
  gap: 2px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px;
}

.tab-btn {
  padding: 0.28rem 0.8rem;
  font-size: 0.78rem;
  font-weight: 500;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: calc(var(--radius) - 2px);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.tab-btn:hover:not(.active) { color: var(--text); }

.tab-btn.active {
  background: var(--accent);
  color: #fff;
}

/* Read-along word display */
#read-along-view {
  min-height: 220px;
  max-height: 420px;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  font-size: 1.1rem;
  line-height: 2;
  color: var(--text);
}

#word-display {
  display: inline;
}

.word {
  border-radius: 3px;
  padding: 0.05em 0.1em;
  transition: background 0.08s, color 0.08s;
}

.word.spoken { color: var(--text-muted); }

.word.active {
  background: rgba(91, 108, 255, 0.4);
  color: #fff;
  font-weight: 600;
}

/* SSML textarea */
textarea {
  width: 100%;
  min-height: 220px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.6;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}

textarea:focus {
  border-color: var(--accent);
}

textarea.error {
  border-color: var(--danger);
}

/* Sliders */
.sliders {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

@media (max-width: 500px) {
  .sliders { grid-template-columns: 1fr; }
}

.slider-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.slider-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.slider-label span:last-child {
  color: var(--text);
  font-weight: 500;
}

input[type=range] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Controls */
.controls {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

button {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}

button:active { transform: scale(0.97); }
button:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-play {
  background: var(--accent);
  color: #fff;
}

.btn-play:hover:not(:disabled) { background: var(--accent-hover); }

.btn-stop {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-stop:hover:not(:disabled) { border-color: var(--danger); color: var(--danger); }

/* Status bar */
.status-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  padding: 0.6rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 2.4rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
  transition: background 0.2s;
}

.status-dot.playing { background: var(--success); animation: pulse 1.2s infinite; }
.status-dot.error   { background: var(--danger); }
.status-dot.warning { background: var(--warning); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}

.status-text { flex: 1; }
.status-text.error   { color: var(--danger); }
.status-text.warning { color: var(--warning); }

/* Provider toggle */
.provider-toggle {
  display: flex;
  gap: 0.5rem;
}

.btn-provider {
  padding: 0.5rem 1rem;
  background: var(--bg);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.btn-provider:hover { color: var(--text); border-color: var(--accent); }

.btn-provider.active {
  background: rgba(91, 108, 255, 0.12);
  color: var(--accent);
  border-color: var(--accent);
}

/* ElevenLabs badge variant */
.provider-badge.elevenlabs {
  background: rgba(76, 175, 130, 0.15);
  color: var(--success);
  border-color: rgba(76, 175, 130, 0.3);
}

/* Google TTS badge variant */
.provider-badge.google {
  background: rgba(66, 133, 244, 0.15);
  color: #4285f4;
  border-color: rgba(66, 133, 244, 0.3);
}

/* API key section */
.apikey-row {
  display: flex;
  gap: 0.6rem;
  align-items: center;
}

.apikey-row input {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.8rem;
  font-size: 0.9rem;
  font-family: var(--font-mono);
  outline: none;
  transition: border-color 0.15s;
}

.apikey-row input:focus { border-color: var(--accent); }

.btn-save-key {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.55rem 1rem;
  font-size: 0.85rem;
  font-weight: 600;
}

.btn-save-key:hover { background: var(--accent-hover); }

.btn-clear-key {
  background: var(--surface);
  color: var(--text-muted);
  border: 1px solid var(--border);
  padding: 0.55rem 0.8rem;
  font-size: 0.85rem;
}

.btn-clear-key:hover { border-color: var(--danger); color: var(--danger); }

.apikey-hint {
  margin-top: 0.6rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.apikey-hint a { color: var(--accent); text-decoration: none; }
.apikey-hint a:hover { text-decoration: underline; }

/* ElevenLabs controls note */
.slider-note {
  margin-top: 0.85rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.slider-note code {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  background: var(--bg);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  border: 1px solid var(--border);
}

/* Media player */
#media-player-section audio {
  width: 100%;
  border-radius: var(--radius);
  margin-bottom: 0.85rem;
}

.media-player-actions {
  display: flex;
  justify-content: flex-end;
}

.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
  transition: background 0.15s, color 0.15s;
  cursor: pointer;
}

.btn-download:hover {
  background: rgba(91, 108, 255, 0.12);
}

footer {
  margin-top: 2.5rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  text-align: center;
}

footer a { color: var(--accent); text-decoration: none; }
footer a:hover { text-decoration: underline; }
