:root {
  /* surfaces */
  --bg: #100a1f;
  --bg-2: #0b0717;
  --surface: #1a1233;
  --surface-2: #221847;
  --surface-3: #2b2153;
  --line: #2b2150;
  --line-2: #3b2f66;

  /* text */
  --text: #f3ecff;
  --muted: #a89bcb;
  --muted-2: #766a9e;

  /* brand */
  --brand-from: #f472b6;
  --brand-to: #a855f7;
  --primary: #a855f7;
  --primary-2: #7c3aed;
  --brand-glow: rgba(168, 85, 247, .38);

  --danger: #fb5c8a;
  --success: #34d399;

  /* geometry */
  --r-card: 16px;
  --r-ctrl: 12px;
  --r-input: 10px;
  --r-sm: 8px;

  /* motion */
  --ease: cubic-bezier(.2, .7, .25, 1);
  --dur-fast: 120ms;
  --dur-base: 180ms;
  --dur-slow: 320ms;

  /* gradients */
  --grad-brand: linear-gradient(135deg, var(--brand-from), var(--brand-to));
  --grad-sunset: linear-gradient(90deg, #f472b6, #a855f7, #22d3ee);
  --grad-card: linear-gradient(180deg, var(--surface), var(--bg-2));

  /* fonts */
  --font-display: "Space Grotesk", sans-serif;
  --font-body: "Inter", sans-serif;
  --font-mono: "JetBrains Mono", monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

/* --- Galaxy background --- */

#galaxy {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 120% 80% at 50% 30%, #1a0f30 0%, var(--bg-2) 60%),
    var(--bg);
}

main, .toast { position: relative; z-index: 1; }

@media (prefers-reduced-motion: reduce) {
  #galaxy { opacity: .9; }
}

main {
  max-width: 760px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 5rem;
  background: rgba(16, 10, 31, 0.72); /* var(--bg) at ~72% opacity */
  backdrop-filter: blur(28px);
  -webkit-backdrop-filter: blur(28px); /* Safari */
  border-radius: var(--r-card);
  box-shadow: 0 0 60px -20px rgba(0, 0, 0, 0.6);
}

/* --- Site nav --- */

.site-nav {
  display: flex;
  gap: 6px;
  margin-bottom: 1.75rem;
}

.nav-link {
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 600;
  padding: 0.5rem 0.9rem;
  border-radius: var(--r-ctrl);
  color: var(--muted);
  text-decoration: none;
  border: 1px solid transparent;
  transition: background var(--dur-base) var(--ease), color var(--dur-base) var(--ease), border-color var(--dur-base) var(--ease);
}

.nav-link:hover { background: var(--surface-2); color: var(--text); }

.nav-link.active {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--line-2);
}

/* --- Convert page --- */

.convert-url-input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-ctrl);
  padding: 0.75rem 1rem;
  font-size: 15px;
  color: var(--text);
  transition: border-color var(--dur-base) var(--ease);
}

.convert-url-input::placeholder { color: var(--muted-2); }
.convert-url-input:focus { outline: none; border-color: var(--primary); }

.convert-preview {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 16px;
}

.convert-preview .clip-thumb {
  width: 72px;
  height: 52px;
}

.convert-meta { flex: 1; min-width: 0; }

.convert-submit {
  width: 100%;
  justify-content: center;
  padding: 0.75rem;
  margin-top: 20px;
}

button, input {
  font-family: inherit;
  color: inherit;
}

/* Focus ring */
:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* --- Header --- */

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 2rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.logo {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--grad-brand);
  box-shadow: 0 0 16px -2px var(--brand-glow);
  flex-shrink: 0;
}

.brand-text h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 14px;
}

/* --- Buttons --- */

.btn {
  font-family: var(--font-display);
  font-size: 14px;
  font-weight: 600;
  border-radius: var(--r-ctrl);
  border: 1px solid var(--line-2);
  padding: 0.6rem 1.1rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  transition: transform var(--dur-fast) var(--ease), box-shadow var(--dur-base) var(--ease),
    background var(--dur-base) var(--ease), border-color var(--dur-base) var(--ease), opacity var(--dur-base) var(--ease);
  background: var(--surface-2);
  color: var(--text);
}

.btn:hover { background: var(--surface-3); border-color: var(--line-2); }
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--grad-brand);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 0 16px -2px var(--brand-glow);
}

.btn-primary:hover { transform: scale(1.02); box-shadow: 0 0 22px -1px var(--brand-glow); }
.btn-primary:active { transform: scale(0.97); }
.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-ghost {
  background: transparent;
  border-color: var(--line);
}

.btn-ghost:hover { background: var(--surface-2); border-color: var(--line-2); }

.btn-icon {
  padding: 0.45rem;
  border-radius: var(--r-sm);
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}

.btn-icon:hover { background: var(--surface-3); color: var(--text); }
.btn-icon:active { transform: scale(0.9); }
.btn-icon.danger:hover { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }

/* --- Section head --- */

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.eyebrow {
  font-family: var(--font-mono);
  font-size: 11.5px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--muted-2);
}

/* --- Clip card --- */

#clips { display: flex; flex-direction: column; gap: 12px; margin-bottom: 12px; }

.clip {
  position: relative;
  background: var(--grad-card);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  padding: 14px 15px 15px;
  transition: opacity var(--dur-slow) var(--ease), transform var(--dur-slow) var(--ease), border-color var(--dur-base) var(--ease);
}

.clip.enter { opacity: 0; transform: translateY(8px); }
.clip.exit { opacity: 0; transform: translateY(8px); }
.clip.dragging { z-index: 5; border-color: var(--line-2); box-shadow: 0 12px 32px -8px rgba(0,0,0,.5); }
.clip.error { border-color: var(--danger); }

.clip-spine {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  border-radius: 0;
}

.clip-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.clip-grip {
  cursor: grab;
  color: var(--muted-2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  touch-action: none;
  /* Padding + negative margin enlarges the touch target without shifting layout. */
  padding: 11px;
  margin: -11px;
}

.clip-grip:active { cursor: grabbing; }

.clip-index {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--primary);
  width: 20px;
  flex-shrink: 0;
}

.clip-thumb {
  width: 52px;
  height: 38px;
  object-fit: cover;
  border-radius: var(--r-sm);
  background: var(--surface-2);
  flex-shrink: 0;
}

.clip-meta { flex: 1; min-width: 0; }

.clip-url-input {
  width: 100%;
  background: none;
  border: none;
  font-size: 14px;
  color: var(--text);
  padding: 0.3rem 0;
}

.clip-url-input::placeholder { color: var(--muted-2); }

.clip-title {
  font-size: 14px;
  font-weight: 600;
  outline: none;
  cursor: text;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.clip-title:empty::before {
  content: attr(data-placeholder);
  color: var(--muted-2);
  font-weight: 500;
}

.clip-source {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--muted-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.clip-status {
  font-size: 12px;
  color: var(--danger);
  margin-top: 2px;
}

.clip-status.hidden { display: none; }

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

.clip-actions .remove { padding: 0.6rem; }

/* --- Waveform + controls (indented under grip+index) --- */

.clip-body {
  padding-left: 34px;
  margin-top: 10px;
}

.clip-body.hidden { display: none; }

.video-toggle {
  font-size: 12px;
  padding: 0.4rem 0.7rem;
  margin-bottom: 8px;
}

.clip-video-wrap { margin-bottom: 10px; }

.clip-video-wrap.hidden { display: none; }

.clip-video {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: var(--bg-2);
  border-radius: var(--r-input);
  overflow: hidden;
}

.yt-mount, .yt-mount iframe {
  width: 100%;
  height: 100%;
  display: block;
  border: none;
}

.video-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 10px;
}

.video-actions .btn {
  border-color: var(--line-2);
  white-space: nowrap;
}

.waveform {
  position: relative;
  height: 62px;
  background: var(--bg-2);
  border-radius: var(--r-input);
  overflow: hidden;
}

.waveform.resolving {
  background: linear-gradient(90deg, var(--bg-2) 25%, var(--surface-2) 50%, var(--bg-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.waveform canvas { display: block; width: 100%; height: 100%; }

.wf-mask {
  position: absolute;
  top: 0;
  bottom: 0;
  background: rgba(11, 7, 23, 0.72);
  pointer-events: none;
}

.wf-mask.left { left: 0; }
.wf-mask.right { right: 0; }

.wf-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  /* Wider than the visual line so it's a workable touch target; centered
     on the trim position via translateX so the drag math (which sets
     `left` to the trim point) doesn't need to change. */
  width: 24px;
  transform: translateX(-50%);
  cursor: ew-resize;
  touch-action: none;
}

.wf-handle::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 3px;
  transform: translateX(-50%);
  background: var(--primary);
  box-shadow: 0 0 16px -2px var(--brand-glow);
}

.wf-handle::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 12px;
  height: 26px;
  border-radius: 6px;
  background: var(--primary);
  box-shadow: 0 0 16px -2px var(--brand-glow);
}

.clip-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.chip {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 0.3rem 0.55rem;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  color: var(--muted);
}

.chip .chip-label { color: var(--muted-2); font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.08em; }
.chip .chip-value { color: var(--text); }
.chip.length .chip-value { color: var(--primary); }

input.chip-input {
  width: 42px;
  background: none;
  border: none;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
  padding: 0;
}

input.chip-input:focus { color: var(--primary); }

.fade-control {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}

.fade-group {
  display: flex;
  align-items: center;
  gap: 5px;
}

.fade-label {
  font-family: var(--font-mono);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted-2);
  white-space: nowrap;
}

.fade-unit {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--muted-2);
  white-space: nowrap;
}

.fade-in-input, .fade-out-input {
  width: 34px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
  padding: 0.25rem 0.3rem;
  /* Number input with spinner arrows hidden — plain numeric entry only. */
  -moz-appearance: textfield;
  appearance: textfield;
}

.fade-in-input::-webkit-outer-spin-button,
.fade-in-input::-webkit-inner-spin-button,
.fade-out-input::-webkit-outer-spin-button,
.fade-out-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.vol-control {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.vol-label {
  font-family: var(--font-mono);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted-2);
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 110px;
  height: 4px;
  border-radius: 4px;
  background: var(--line-2);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 0 10px -1px var(--brand-glow);
  cursor: pointer;
  border: none;
}

input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 0 10px -1px var(--brand-glow);
  cursor: pointer;
  border: none;
}

.vol-value {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  width: 34px;
}

/* --- Add clip --- */

.add-row {
  width: 100%;
  justify-content: center;
  margin-bottom: 1.75rem;
  padding: 0.75rem;
}

/* --- Empty state --- */

.empty-state {
  border: 1.5px dashed var(--line-2);
  border-radius: var(--r-card);
  padding: 2.5rem 1.5rem;
  text-align: center;
  color: var(--muted);
  margin-bottom: 12px;
}

.empty-state .empty-icon {
  color: var(--muted-2);
  margin-bottom: 0.75rem;
}

.empty-state h2 {
  font-family: var(--font-display);
  font-size: 16px;
  margin: 0 0 0.3rem;
  color: var(--text);
}

.empty-state p { margin: 0; font-size: 13px; color: var(--muted-2); }

.hidden { display: none !important; }

/* --- Export preview --- */

.preview-section {
  border-top: 1px solid var(--line);
  padding-top: 1.25rem;
  margin-top: 1.5rem;
}

.preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.preview-total {
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--muted);
}

.preview-timeline {
  display: flex;
  height: 34px;
  border-radius: var(--r-input);
  overflow: hidden;
  background: var(--bg-2);
  position: relative;
}

.preview-empty {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted-2);
  display: flex;
  align-items: center;
  padding-left: 12px;
}

.preview-segment {
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 8px;
  font-size: 11px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.55);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity var(--dur-base) var(--ease);
  border-right: 1px solid rgba(0, 0, 0, 0.15);
}

.preview-footer {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 1rem;
}

.progress-track {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.progress-label {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted-2);
}

.progress-bar {
  height: 8px;
  border-radius: 4px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--grad-sunset);
  border-radius: 4px;
  transition: width var(--dur-base) linear;
}

.result-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  font-size: 13px;
  color: var(--muted);
}

.result-check {
  color: var(--success);
  flex-shrink: 0;
}

.result-meta { font-family: var(--font-mono); font-size: 12px; }

/* --- Toast --- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 12px);
  background: var(--surface-3);
  border: 1px solid var(--line-2);
  border-radius: var(--r-ctrl);
  padding: 0.65rem 1rem;
  font-size: 13px;
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-base) var(--ease), transform var(--dur-base) var(--ease);
  box-shadow: 0 12px 32px -8px rgba(0,0,0,.5);
  z-index: 50;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* --- Reduced motion --- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}

/* --- Responsive --- */

@media (max-width: 560px) {
  .clip-body { padding-left: 0; }
  .vol-control { margin-left: 0; width: 100%; }
  .clip-controls { flex-direction: column; align-items: stretch; }
  .app-header { flex-direction: column; }
  .btn-primary.mixdown-btn { width: 100%; justify-content: center; }
}
