/* ========================================================================
   睡前故事 — 睡眠友好主题
   深色优先、金色点缀、柔和过渡
   ======================================================================== */

:root {
  --bg: #0b1020;
  --bg-2: #131a35;
  --bg-3: #1c2347;
  --text: #e8e6f0;
  --text-dim: #a0a3b8;
  --text-mute: #6a6d85;
  --gold: #d4a574;
  --gold-2: #e8c89a;
  --rose: #ff7a8a;
  --line: #2a2f4a;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --shadow-1: 0 4px 16px rgba(0,0,0,0.3);
  --shadow-2: 0 8px 32px rgba(0,0,0,0.5);
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;
  --r-xl: 24px;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(ellipse at top, #1a2347 0%, var(--bg) 50%),
    var(--bg);
}

a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }

.hidden { display: none !important; }

/* ============ Header ============ */

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 12px) 18px 12px;
  background: linear-gradient(to bottom, rgba(11,16,32,0.95) 60%, rgba(11,16,32,0));
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 1px;
}
.brand-moon {
  font-size: 22px;
  filter: drop-shadow(0 0 8px rgba(212, 165, 116, 0.6));
}
.brand-name {
  background: linear-gradient(120deg, var(--gold-2), var(--gold));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.app-nav {
  display: flex;
  gap: 18px;
  font-size: 14px;
  color: var(--text-dim);
}
.app-nav a.active {
  color: var(--gold);
}
.app-nav a:hover { color: var(--gold-2); }

/* ============ Page root ============ */

.page-root {
  padding: 16px 18px calc(var(--safe-bottom) + 100px);
  max-width: 1100px;
  margin: 0 auto;
}

.loading {
  text-align: center;
  padding: 60px 0;
  color: var(--text-mute);
}

/* ============ Library page ============ */

.search-bar {
  position: relative;
  margin: 0 0 18px;
}
.search-bar input {
  width: 100%;
  padding: 12px 16px 12px 40px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}
.search-bar input:focus {
  border-color: var(--gold);
}
.search-bar::before {
  content: "🔍";
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0.6;
  font-size: 14px;
}

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 18px;
  overflow-x: auto;
  scrollbar-width: none;
}
.filter-chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.2s;
}
.chip.active {
  background: var(--gold);
  border-color: var(--gold);
  color: var(--bg);
}

.story-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}
@media (min-width: 600px) {
  .story-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 900px) {
  .story-grid { grid-template-columns: repeat(4, 1fr); }
}
@media (min-width: 1200px) {
  .story-grid { grid-template-columns: repeat(5, 1fr); }
}

.story-card {
  display: block;
  background: var(--bg-2);
  border-radius: var(--r-lg);
  overflow: hidden;
  border: 1px solid transparent;
  transition: transform 0.2s, border-color 0.2s, box-shadow 0.2s;
  cursor: pointer;
}
.story-card:hover {
  transform: translateY(-2px);
  border-color: var(--line);
  box-shadow: var(--shadow-1);
}
.story-card:active {
  transform: translateY(0);
}
.story-card-cover {
  width: 100%;
  aspect-ratio: 3/4;
  background: var(--bg-3);
  display: block;
  object-fit: cover;
}
.story-card-body {
  padding: 10px 12px 14px;
}
.story-card-title {
  font-size: 15px;
  font-weight: 500;
  margin: 0 0 4px;
  color: var(--text);
}
.story-card-en {
  font-size: 11px;
  color: var(--text-mute);
  margin: 0 0 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.story-card-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-dim);
}
.story-card-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--text-mute);
}
.story-card-dot.ready {
  background: var(--gold);
  box-shadow: 0 0 6px var(--gold);
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-mute);
}
.empty-state .emoji { font-size: 48px; margin-bottom: 12px; }

/* ============ Player page ============ */

.player-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 10px;
}
.player-cover {
  width: min(320px, 75vw);
  aspect-ratio: 3/4;
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-2);
  background: var(--bg-2);
  overflow: hidden;
  margin: 0 auto 24px;
}
.player-cover img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.player-title {
  font-size: 24px;
  font-weight: 500;
  text-align: center;
  margin: 0 0 4px;
  color: var(--text);
}
.player-en {
  text-align: center;
  font-size: 13px;
  color: var(--text-mute);
  margin: 0 0 20px;
}
.player-author {
  text-align: center;
  font-size: 12px;
  color: var(--text-mute);
  margin: 0 0 20px;
}

/* Player controls */
.player-controls {
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 0 4px;
}
.progress {
  margin: 0 0 8px;
}
.progress-bar {
  width: 100%;
  height: 4px;
  background: var(--line);
  border-radius: 2px;
  position: relative;
  cursor: pointer;
  overflow: hidden;
}
.progress-fill {
  position: absolute;
  top: 0; left: 0; bottom: 0;
  background: var(--gold);
  border-radius: 2px;
  width: 0;
  transition: width 0.1s linear;
}
.progress-buffer {
  position: absolute;
  top: 0; left: 0; bottom: 0;
  background: var(--bg-3);
  border-radius: 2px;
  width: 0;
}
.progress-time {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-mute);
  margin: 0 0 18px;
}

.player-buttons {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  margin: 0 0 18px;
}
.btn-circle {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--bg-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text);
  transition: background 0.2s;
}
.btn-circle:hover { background: var(--bg-3); }
.btn-circle.btn-play {
  width: 72px; height: 72px;
  font-size: 28px;
  background: var(--gold);
  color: var(--bg);
  box-shadow: 0 0 24px rgba(212, 165, 116, 0.4);
}
.btn-circle.btn-play:hover { background: var(--gold-2); }
.btn-circle.active {
  background: var(--gold);
  color: var(--bg);
}

.player-extras {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin: 0 0 18px;
}
.btn-pill {
  padding: 8px 14px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-dim);
  transition: all 0.2s;
}
.btn-pill:hover { color: var(--text); border-color: var(--text-mute); }
.btn-pill.active {
  background: var(--gold);
  color: var(--bg);
  border-color: var(--gold);
}

.text-viewer {
  width: 100%;
  max-width: 640px;
  margin: 24px auto 0;
  padding: 18px 18px 40px;
  background: var(--bg-2);
  border-radius: var(--r-lg);
  max-height: 60vh;
  overflow-y: auto;
  font-size: 16px;
  line-height: 1.9;
  white-space: pre-wrap;
  color: var(--text-dim);
  scroll-behavior: smooth;
}
.text-viewer p { margin: 0 0 1em; }
.text-viewer .current {
  color: var(--text);
}

/* Sleep timer */
.sleep-timer-panel {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 80px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 12px;
  box-shadow: var(--shadow-2);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 240px;
}
.sleep-timer-panel .title {
  font-size: 13px;
  color: var(--text-dim);
  text-align: center;
  margin: 0 0 4px;
}
.sleep-timer-panel .row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}
.sleep-timer-panel .remaining {
  text-align: center;
  font-size: 12px;
  color: var(--gold);
  margin: 4px 0 0;
}

/* ============ Admin page ============ */

.admin-section {
  background: var(--bg-2);
  border-radius: var(--r-lg);
  padding: 18px;
  margin: 0 0 16px;
}
.admin-section h2 {
  font-size: 16px;
  font-weight: 500;
  color: var(--gold-2);
  margin: 0 0 12px;
}

.upload-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.upload-form input[type="text"],
.upload-form textarea,
.upload-form select {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: 14px;
  outline: none;
}
.upload-form input[type="file"] {
  font-size: 13px;
  color: var(--text-dim);
}
.upload-form label {
  font-size: 12px;
  color: var(--text-dim);
  display: block;
  margin-bottom: 4px;
}
.btn-primary {
  padding: 12px 18px;
  background: var(--gold);
  color: var(--bg);
  border-radius: var(--r-md);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-primary:hover { background: var(--gold-2); }
.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-ghost {
  padding: 10px 16px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text-dim);
  border-radius: var(--r-md);
  font-size: 13px;
  cursor: pointer;
}
.btn-ghost:hover { color: var(--text); border-color: var(--text-mute); }

.alert {
  padding: 10px 12px;
  border-radius: var(--r-sm);
  font-size: 13px;
  margin: 0 0 12px;
}
.alert-warn { background: rgba(212, 165, 116, 0.15); color: var(--gold-2); }
.alert-ok { background: rgba(120, 200, 120, 0.15); color: #9ad99a; }
.alert-err { background: rgba(255, 100, 100, 0.15); color: #ff8a8a; }

.voice-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.voice-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: var(--r-sm);
}
.voice-item .name { font-size: 14px; }
.voice-item .meta { font-size: 11px; color: var(--text-mute); }
.voice-item .actions { display: flex; gap: 6px; }

.gen-task {
  padding: 10px 12px;
  background: var(--bg);
  border-radius: var(--r-sm);
  margin: 0 0 8px;
  font-size: 13px;
}
.gen-task .top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}
.gen-task .status {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-3);
  color: var(--text-dim);
}
.gen-task .status.running { background: rgba(212,165,116,0.2); color: var(--gold-2); }
.gen-task .status.completed { background: rgba(120,200,120,0.2); color: #9ad99a; }
.gen-task .status.failed { background: rgba(255,100,100,0.2); color: #ff8a8a; }
.gen-task .bar {
  height: 3px;
  background: var(--line);
  border-radius: 2px;
  overflow: hidden;
}
.gen-task .bar > div {
  height: 100%;
  background: var(--gold);
  transition: width 0.3s;
}

.progress-summary {
  margin: 0 0 14px;
}
.progress-summary-inner {
  padding: 12px 14px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
}
.progress-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  margin-bottom: 10px;
}
.stat-item {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}
.stat-num {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}
.stat-label {
  font-size: 12px;
  color: var(--text-dim);
}
.stat-done .stat-num { color: #6ec47a; }
.stat-running .stat-num { color: var(--gold); }
.stat-failed .stat-num { color: #e07a7a; }
.stat-progress .stat-num { color: var(--gold); font-size: 20px; }
.progress-meta {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
}
.sse-dot {
  color: #6ec47a;
  font-size: 10px;
  display: inline-block;
  animation: pulse 1.5s infinite;
  margin-right: 4px;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}
.bar.total-bar {
  height: 6px;
  background: var(--bg-3);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 8px;
}
.bar.total-bar > div {
  height: 100%;
  background: linear-gradient(90deg, var(--gold), #f0c98a);
  transition: width 0.5s ease;
}
.queue-empty {
  text-align: center;
  padding: 32px 20px;
  background: var(--bg-2);
  border: 1px dashed var(--line);
  border-radius: var(--r-md);
  margin-top: 8px;
}
.queue-empty-icon {
  font-size: 36px;
  margin-bottom: 8px;
}
.queue-empty-text {
  font-size: 15px;
  color: var(--text);
  font-weight: 500;
}
.task-slug {
  font-size: 13px;
  font-weight: 500;
}

/* ============ Player bar (persistent) ============ */

.player-bar {
  position: fixed;
  left: 8px; right: 8px;
  bottom: calc(var(--safe-bottom) + 8px);
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 8px 8px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-2);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.player-bar-cover {
  width: 40px; height: 40px;
  border-radius: var(--r-sm);
  background: var(--bg-3);
  background-size: cover;
  background-position: center;
  flex-shrink: 0;
}
.player-bar-info {
  flex: 1;
  min-width: 0;
}
.player-bar-title {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.player-bar-time {
  font-size: 10px;
  color: var(--text-mute);
}
.player-bar-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--gold);
  color: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}

/* ============ Utility ============ */

.center { text-align: center; }
.muted { color: var(--text-mute); }
.row-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
hr.div {
  border: none;
  border-top: 1px solid var(--line);
  margin: 16px 0;
}
