/* ── reset + base ─────────────────────────────────────── */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #0a0a0f;
  --bg-card: #111118;
  --bg-hover: #1a1a24;
  --border: #1e1e2e;
  --border-glow: #2a2a4a;
  --text: #e8e8f0;
  --text-dim: #6e6e8a;
  --text-muted: #4a4a62;
  --accent: #8b7cf6;
  --accent-glow: #a78bfa;
  --accent-dim: rgba(139, 124, 246, 0.15);
  --warm: #f0c674;
  --warm-dim: rgba(240, 198, 116, 0.15);
  --danger: #f06292;
  --radius: 12px;
  --radius-sm: 8px;
  --font-serif: 'Crimson Pro', Georgia, serif;
  --font-mono: 'JetBrains Mono', monospace;
}

html { font-size: 16px; }
body {
  font-family: var(--font-mono);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ── stars canvas ─────────────────────────────────────── */
#stars {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 0;
  pointer-events: none;
}

/* ── app container ────────────────────────────────────── */
#app {
  position: relative;
  z-index: 1;
  max-width: 560px;
  margin: 0 auto;
  padding: 20px 16px 80px;
}

/* ── header ───────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0 32px;
}

.logo {
  font-family: var(--font-serif);
  font-size: 1.6rem;
  font-weight: 300;
  letter-spacing: 0.08em;
  color: var(--text);
  cursor: pointer;
  background: linear-gradient(135deg, var(--text) 0%, var(--accent-glow) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

nav { display: flex; gap: 4px; }

.nav-btn {
  background: none;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  padding: 6px 14px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.2s;
  letter-spacing: 0.05em;
}
.nav-btn:hover { color: var(--text); border-color: var(--border); }
.nav-btn.active { color: var(--text); background: var(--accent-dim); border-color: var(--border-glow); }

/* ── views ────────────────────────────────────────────── */
.view { display: none; }
.view.active { display: block; animation: fadeIn 0.3s ease; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ── create card ──────────────────────────────────────── */
.create-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
}

.subtitle {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  font-weight: 300;
  font-style: italic;
  color: var(--text-dim);
  margin-bottom: 28px;
}

/* ── form ─────────────────────────────────────────────── */
.form-group { margin-bottom: 20px; }
.form-row { display: flex; gap: 12px; }
.form-group.half { flex: 1; }

label {
  display: block;
  font-size: 0.7rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 0.82rem;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color 0.2s;
}
.input:focus { border-color: var(--accent); }
.input::placeholder { color: var(--text-muted); }
select.input { cursor: pointer; }

/* ── theme grid ───────────────────────────────────────── */
.theme-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.theme-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 0.7rem;
  padding: 10px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  letter-spacing: 0.02em;
}
.theme-btn:hover { border-color: var(--border-glow); color: var(--text); }
.theme-btn.selected {
  border-color: var(--accent);
  background: var(--accent-dim);
  color: var(--accent-glow);
}

/* ── buttons ──────────────────────────────────────────── */
.btn-primary {
  width: 100%;
  background: linear-gradient(135deg, var(--accent) 0%, #6c5ce7 100%);
  border: none;
  color: white;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  padding: 14px 24px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.3s;
  letter-spacing: 0.05em;
  margin-top: 8px;
  position: relative;
  overflow: hidden;
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 8px 30px rgba(139, 124, 246, 0.3); }
.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.icon-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent-glow); }
.icon-btn.active { color: var(--warm); border-color: var(--warm); }

/* ── story result card ────────────────────────────────── */
.story-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  margin-top: 20px;
  animation: fadeIn 0.5s ease;
}

.story-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 20px;
}

.story-header h2 {
  font-family: var(--font-serif);
  font-size: 1.4rem;
  font-weight: 400;
  line-height: 1.3;
  color: var(--text);
  flex: 1;
  margin-right: 12px;
}

.story-actions { display: flex; gap: 8px; flex-shrink: 0; }

/* ── audio player ─────────────────────────────────────── */
.player {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  margin-bottom: 24px;
}

.play-btn {
  background: var(--accent);
  border: none;
  color: white;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}
.play-btn:hover { background: var(--accent-glow); transform: scale(1.05); }
.play-btn svg { margin-left: 2px; }

.player-track { flex: 1; }

.progress-bar {
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  cursor: pointer;
  position: relative;
  margin-bottom: 8px;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-glow));
  border-radius: 2px;
  width: 0%;
  transition: width 0.1s linear;
  position: relative;
}
.progress-fill::after {
  content: '';
  position: absolute;
  right: -4px;
  top: -4px;
  width: 12px;
  height: 12px;
  background: var(--accent-glow);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.2s;
}
.progress-bar:hover .progress-fill::after { opacity: 1; }

.time-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.65rem;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

/* ── story text ───────────────────────────────────────── */
.story-text {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  line-height: 1.8;
  color: var(--text);
  opacity: 0.85;
  padding-right: 8px;
}
.story-text::-webkit-scrollbar { width: 3px; }
.story-text::-webkit-scrollbar-track { background: transparent; }
.story-text::-webkit-scrollbar-thumb { background: var(--border-glow); border-radius: 3px; }

.story-text p { margin-bottom: 1em; }
.story-text p:last-child { margin-bottom: 0; }

/* ── library ──────────────────────────────────────────── */
.library-filters {
  display: flex;
  gap: 6px;
  margin-bottom: 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 2px;
}
.library-filters::-webkit-scrollbar { display: none; }

.filter-pill {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 6px 14px;
  border-radius: 20px;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}
.filter-pill:hover { border-color: var(--border-glow); color: var(--text-dim); }
.filter-pill.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent-glow);
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-dim);
  font-size: 0.85rem;
}
.empty-state .hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 8px;
}

.library-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.library-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 14px;
}
.library-item:hover { border-color: var(--border-glow); transform: translateY(-1px); }

.library-play {
  background: var(--accent-dim);
  border: 1px solid var(--border-glow);
  color: var(--accent-glow);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.library-info { flex: 1; min-width: 0; }

.library-title {
  font-family: var(--font-serif);
  font-size: 1rem;
  font-weight: 400;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.library-meta {
  font-size: 0.65rem;
  color: var(--text-muted);
  display: flex;
  gap: 8px;
  letter-spacing: 0.03em;
  flex-wrap: wrap;
}

.library-fav {
  color: var(--text-muted);
  flex-shrink: 0;
  font-size: 0.8rem;
}
.library-fav.is-fav { color: var(--warm); }

.library-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.library-share,
.library-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.library-share:hover, .library-share:active { color: var(--accent); background: var(--accent-dim); }
.library-delete:hover, .library-delete:active { color: var(--danger); background: rgba(240,98,146,0.1); }

/* ── generating overlay ───────────────────────────────── */
.generating-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 15, 0.85);
  backdrop-filter: blur(8px);
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  animation: fadeIn 0.3s ease;
}

.generating-moon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, var(--warm) 0%, #c4a35a 50%, transparent 70%);
  box-shadow: 0 0 40px rgba(240, 198, 116, 0.3), 0 0 80px rgba(240, 198, 116, 0.1);
  animation: moonPulse 3s ease-in-out infinite;
}

@keyframes moonPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 40px rgba(240, 198, 116, 0.3); }
  50% { transform: scale(1.08); box-shadow: 0 0 60px rgba(240, 198, 116, 0.5); }
}

.generating-text {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  font-style: italic;
  color: var(--text-dim);
  animation: textFade 2s ease-in-out infinite;
}

@keyframes textFade {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

.generating-progress {
  width: 200px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.generating-bar {
  width: 100%;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.generating-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--warm));
  border-radius: 2px;
  transition: width 0.5s ease;
}

.generating-timer {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

/* ── toast ────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(30, 30, 45, 0.95);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 20px;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 200;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── responsive ───────────────────────────────────────── */
@media (max-width: 480px) {
  #app { padding: 12px 12px 60px; }
  .create-card { padding: 20px 16px; }
  .story-card { padding: 20px 16px; }
  .theme-grid { grid-template-columns: repeat(2, 1fr); }
  .form-row { flex-direction: column; gap: 0; }
  .form-group.half { flex: none; }
  .story-header h2 { font-size: 1.2rem; }
  .library-item { padding: 14px 12px; gap: 10px; }
  .library-play { width: 36px; height: 36px; }
  .library-title { font-size: 0.9rem; }
}

/* touch devices — always show action buttons */
@media (hover: none) {
  .library-share, .library-delete { opacity: 1; }
}

/* ── sleep timer ──────────────────────────────────────── */
#sleep-btn.active {
  color: var(--warm);
  border-color: var(--warm);
  background: var(--warm-dim);
}

.sleep-countdown {
  font-family: var(--font-mono);
  font-size: 0.6rem;
  color: var(--warm);
  text-align: right;
  letter-spacing: 0.08em;
  margin-top: 2px;
  opacity: 0.8;
}

/* ── illustrations ────────────────────────────────────── */
.story-page {
  margin-bottom: 2rem;
}

.page-illustration {
  width: 100%;
  max-width: 100%;
  margin: 0 auto 1rem;
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 1;
  background: rgba(255,255,255,.03);
  border: 1px solid var(--border);
}

.page-illustration img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.page-illustration.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 8px;
  animation: illPulse 2s ease-in-out infinite;
}

.page-illustration.loading::after {
  content: 'painting...';
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  letter-spacing: 0.1em;
}

@keyframes illPulse {
  0%, 100% { opacity: 0.3; border-color: var(--border); }
  50% { opacity: 0.6; border-color: var(--border-glow); }
}

.page-text {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  line-height: 1.8;
  color: var(--text);
  opacity: 0.85;
}

.page-text p {
  margin-bottom: 0.8rem;
}

.page-text p:last-child {
  margin-bottom: 0;
}

.illustration-status {
  text-align: center;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
  letter-spacing: 0.05em;
  padding: 8px;
  background: var(--accent-dim);
  border-radius: var(--radius-sm);
}

.illustration-status .ill-progress {
  display: inline-block;
  width: 60px;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  margin-left: 8px;
  vertical-align: middle;
}

.illustration-status .ill-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.5s;
}

.library-item.has-illustrations .library-play {
  background: var(--warm-dim);
  border-color: rgba(240, 198, 116, 0.3);
  color: var(--warm);
}
