/* ── Variables ─────────────────────────────────────────────────────────────── */
:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface2: #242424;
  --surface3: #2e2e2e;
  --accent: #6c63ff;
  --accent-dim: rgba(108, 99, 255, 0.15);
  --text: #e8e8e8;
  --text2: #aaaaaa;
  --text3: #555555;
  --radius: 20px;
  --radius-sm: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html {
  height: -webkit-fill-available;
  scroll-behavior: smooth;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'PingFang SC',
    'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: -webkit-fill-available;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

/* When onboarding is visible, lock body to prevent iOS page scroll/bounce */
body.onboarding-active {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
}

/* ── Loading screen ─────────────────────────────────────────────────────────── */
#loading {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px; z-index: 100;
}
#loading .spinner {
  width: 44px; height: 44px; border-radius: 50%;
  border: 3px solid var(--surface3);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loading p { color: var(--text2); font-size: 15px; }

/* ── App shell ───────────────────────────────────────────────────────────────── */
#app {
  display: none;
  max-width: 480px;
  margin: 0 auto;
  padding-bottom: calc(32px + var(--safe-bottom));
}

/* ── Header ─────────────────────────────────────────────────────────────────── */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(16px + var(--safe-top)) 20px 12px;
}
header h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.5px; }
.date-badge {
  font-size: 12px; color: var(--text2);
  background: var(--surface2);
  padding: 5px 12px; border-radius: 20px;
  white-space: nowrap;
}

/* ── Player card ─────────────────────────────────────────────────────────────── */
#player-card {
  margin: 0 14px 18px;
  background: linear-gradient(145deg, #1a1a2e 0%, #16213e 60%, #0f3460 100%);
  border-radius: var(--radius);
  padding: 22px 20px 20px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

#artwork {
  width: 72px; height: 72px; border-radius: 16px;
  background: var(--accent-dim);
  border: 1.5px solid rgba(108, 99, 255, 0.4);
  display: flex; align-items: center; justify-content: center;
  font-size: 34px; margin-bottom: 14px;
  flex-shrink: 0;
}

#player-info { margin-bottom: 18px; }
#player-title {
  font-size: 18px; font-weight: 700;
  margin-bottom: 5px; line-height: 1.3;
}
#player-meta { font-size: 13px; color: var(--text2); }

/* Progress */
#progress-wrap {
  margin-bottom: 10px; cursor: pointer;
  padding: 10px 0; touch-action: none;
}
#progress-track {
  width: 100%; height: 4px; background: rgba(255,255,255,0.12);
  border-radius: 2px; position: relative; overflow: visible;
}
#progress-fill {
  height: 100%; background: var(--accent); border-radius: 2px;
  width: 0%; position: relative; transition: width 0.25s linear;
}
#progress-fill::after {
  content: '';
  position: absolute; right: -7px; top: -5px;
  width: 14px; height: 14px;
  background: #fff; border-radius: 50%;
  box-shadow: 0 0 0 3px var(--accent);
  opacity: 0; transition: opacity 0.15s;
}
#progress-wrap:hover #progress-fill::after,
#progress-wrap:active #progress-fill::after { opacity: 1; }
#time-row {
  display: flex; justify-content: space-between;
  font-size: 12px; color: rgba(255,255,255,0.4);
  margin-top: 6px;
}

/* Controls */
#controls {
  display: flex; align-items: center; justify-content: center;
  gap: 28px; margin: 16px 0 14px;
}
.ctrl-btn {
  background: none; border: none; color: rgba(255,255,255,0.8);
  cursor: pointer; padding: 8px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s, transform 0.1s;
}
.ctrl-btn:active { opacity: 0.7; transform: scale(0.92); }
.ctrl-btn svg { width: 26px; height: 26px; fill: currentColor; }

.ctrl-btn.play-btn {
  width: 68px; height: 68px;
  background: var(--accent); border-radius: 50%;
  color: #fff; font-size: 26px;
  box-shadow: 0 4px 20px rgba(108, 99, 255, 0.5);
}
.ctrl-btn.play-btn:active { transform: scale(0.94); }

/* Speed */
#speed-row {
  display: flex; justify-content: center; gap: 8px;
}
.speed-btn {
  background: rgba(255,255,255,0.08);
  border: 1.5px solid transparent;
  color: rgba(255,255,255,0.5);
  border-radius: 20px; padding: 5px 14px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.15s;
}
.speed-btn.active {
  border-color: var(--accent); color: var(--accent);
  background: var(--accent-dim);
}

/* ── Section wrapper ─────────────────────────────────────────────────────────── */
.section { margin: 0 14px 18px; }
.section-title {
  font-size: 12px; font-weight: 600; color: var(--text3);
  text-transform: uppercase; letter-spacing: 1px;
  margin-bottom: 10px;
}

/* ── Generate button ─────────────────────────────────────────────────────────── */
#btn-generate {
  width: 100%; padding: 16px;
  background: var(--accent); color: #fff;
  border: none; border-radius: var(--radius-sm);
  font-size: 16px; font-weight: 600; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s, transform 0.1s;
  letter-spacing: 0.3px;
}
#btn-generate:active { opacity: 0.85; transform: scale(0.98); }
#btn-generate:disabled {
  background: var(--surface3); color: var(--text3); cursor: default;
  transform: none;
}
#generate-status {
  text-align: center; font-size: 13px; color: var(--text2);
  margin-top: 10px; min-height: 20px;
}

/* ── Transcript ───────────────────────────────────────────────────────────────── */
#transcript-box {
  background: var(--surface); border-radius: var(--radius-sm);
  max-height: 300px; overflow-y: auto;
  padding: 14px 16px;
  -webkit-overflow-scrolling: touch;
}
/* Custom scrollbar */
#transcript-box::-webkit-scrollbar { width: 3px; }
#transcript-box::-webkit-scrollbar-track { background: transparent; }
#transcript-box::-webkit-scrollbar-thumb { background: var(--surface3); border-radius: 2px; }

.segment {
  padding: 10px 0; border-bottom: 1px solid var(--surface3);
  transition: background 0.2s;
}
.segment:last-child { border-bottom: none; }
.segment-title {
  font-size: 14px; font-weight: 700; color: var(--text);
  margin-bottom: 6px; line-height: 1.4;
}
.segment-body { font-size: 14px; line-height: 1.75; color: var(--text2); }

.segment.active {
  background: var(--accent-dim);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 4px -12px;
  border-bottom: none;
}
.segment.active .segment-title { color: var(--accent); }
.segment.active .segment-body { color: var(--text); }

/* ── History list ────────────────────────────────────────────────────────────── */
#history-list { display: flex; flex-direction: column; gap: 10px; }
.episode-item {
  background: var(--surface); border-radius: var(--radius-sm);
  padding: 14px 16px; display: flex; align-items: center;
  gap: 14px; cursor: pointer; transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
  border: 1.5px solid transparent;
}
.episode-item:active { background: var(--surface2); }
.episode-item.current { border-color: var(--accent); }
.ep-icon {
  width: 44px; height: 44px;
  background: var(--accent-dim);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; flex-shrink: 0;
}
.ep-info { flex: 1; min-width: 0; }
.ep-date { font-size: 15px; font-weight: 600; }
.ep-meta { font-size: 12px; color: var(--text3); margin-top: 3px; }
.ep-play {
  color: var(--text3); font-size: 20px; flex-shrink: 0;
}
.episode-item.current .ep-play { color: var(--accent); }

/* ── Empty state ─────────────────────────────────────────────────────────────── */
.empty { color: var(--text3); font-size: 14px; padding: 8px 0; }

/* ── Toast ───────────────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: calc(28px + var(--safe-bottom));
  left: 50%; transform: translateX(-50%) translateY(80px);
  background: var(--surface3); color: var(--text);
  padding: 11px 22px; border-radius: 24px;
  font-size: 14px; font-weight: 500;
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 200; white-space: nowrap;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}
#toast.show { transform: translateX(-50%) translateY(0); }

/* ── Onboarding ─────────────────────────────────────────────────────────────── */
#onboarding {
  max-width: 480px;
  margin: 0 auto;
  padding-bottom: calc(32px + var(--safe-bottom));
  overflow: hidden;           /* prevent iOS scroll bounce */
  touch-action: pan-x pan-y;  /* allow sphere to override */
}
.onboard-content {
  padding: 0 20px;
  text-align: center;
  touch-action: manipulation;
}
.onboard-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 6px;
}
.onboard-sub {
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 20px;
  line-height: 1.5;
}

/* ── 3D Sphere container ──────────────────────────────────────────────────── */
#sphere-container {
  width: 320px;
  height: 320px;
  margin: 0 auto 20px;
  perspective: 800px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  position: relative;
  cursor: grab;
}
#sphere-container:active { cursor: grabbing; }

#sphere {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
}

.sphere-tag {
  position: absolute;
  left: 50%;
  top: 50%;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  white-space: nowrap;
  padding: 7px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.25s, color 0.25s, box-shadow 0.25s, border-color 0.25s;
  background: var(--surface2);
  color: var(--text2);
  border: 1.5px solid var(--surface3);
  pointer-events: auto;
}
.sphere-tag:active { transform: scale(0.95); }

.sphere-tag.selected {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  box-shadow: 0 0 14px rgba(108, 99, 255, 0.5), 0 0 30px rgba(108, 99, 255, 0.2);
}

.sphere-tag.expanding {
  animation: tagPop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes tagPop {
  0% { transform: scale(0); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.sphere-tag.removing {
  animation: tagShrink 0.25s ease-in forwards;
}
@keyframes tagShrink {
  to { transform: scale(0); opacity: 0; }
}

/* ── Selected tags row ────────────────────────────────────────────────────── */
#selected-tags-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-bottom: 20px;
  min-height: 36px;
}
.selected-pill {
  background: var(--accent-dim);
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 5px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  animation: tagPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ── Onboard done button ─────────────────────────────────────────────────── */
#btn-onboard-done {
  width: 100%;
  padding: 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s, transform 0.1s;
  letter-spacing: 0.3px;
}
#btn-onboard-done:active { opacity: 0.85; transform: scale(0.98); }
#btn-onboard-done:disabled {
  background: var(--surface3);
  color: var(--text3);
  cursor: default;
  transform: none;
}
