:root, [data-theme="light"] {
  --bg: #f6f1e7;
  --paper: #fffdf8;
  --fg: #241f18;
  --fg-dim: color-mix(in srgb, var(--fg) 58%, transparent);
  --surface-alt: #efe7d8;
  --border-fade: color-mix(in srgb, var(--fg) 14%, transparent);
  --border-strong: color-mix(in srgb, var(--fg) 30%, transparent);
  --deck-a: #0f766e;
  --deck-a-soft: #d9efe9;
  --deck-b: #9d174d;
  --deck-b-soft: #fbe3ea;
  --danger: #9a2f22;
  --font-display: "Fraunces", "Iowan Old Style", serif;
  --font-mono: "Space Mono", ui-monospace, monospace;
  --font-body: "Inter", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17140f;
    --paper: #221d16;
    --fg: #f3ead9;
    --fg-dim: color-mix(in srgb, var(--fg) 55%, transparent);
    --surface-alt: #2b241a;
    --border-fade: color-mix(in srgb, var(--fg) 12%, transparent);
    --border-strong: color-mix(in srgb, var(--fg) 26%, transparent);
    --deck-a: #2dd4bf;
    --deck-a-soft: #123832;
    --deck-b: #fb7185;
    --deck-b-soft: #3a1620;
  }
}
:root[data-theme="dark"] {
  --bg: #17140f;
  --paper: #221d16;
  --fg: #f3ead9;
  --fg-dim: color-mix(in srgb, var(--fg) 55%, transparent);
  --surface-alt: #2b241a;
  --border-fade: color-mix(in srgb, var(--fg) 12%, transparent);
  --border-strong: color-mix(in srgb, var(--fg) 26%, transparent);
  --deck-a: #2dd4bf;
  --deck-a-soft: #123832;
  --deck-b: #fb7185;
  --deck-b-soft: #3a1620;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
}
h1, h2 { margin: 0; }
button { font-family: inherit; cursor: pointer; }
a { color: var(--deck-a); }
.mono { font-family: var(--font-mono); }
.hint { color: var(--fg-dim); font-size: 12.5px; line-height: 1.6; padding: 10px 8px; }

a:focus-visible, button:focus-visible {
  outline: 2px solid var(--deck-a);
  outline-offset: 3px;
  border-radius: 2px;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* accounts for mobile browser chrome */
  overflow: hidden;
}

/* ---------------------------------------------------------------- topbar */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 16px;
  padding: 12px 24px;
  background: var(--paper);
  box-shadow: 0 1px 0 var(--border-fade);
  z-index: 10;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.imprint {
  font-size: 12px;
  text-decoration: none;
  color: var(--fg-dim);
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.imprint:hover { color: var(--fg); }
.wordmark-sm {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 600;
  font-size: 17px;
  white-space: nowrap;
  background: linear-gradient(90deg, var(--deck-a), var(--deck-b), var(--deck-a));
  background-size: 200% auto;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: wordmarkShift 8s ease-in-out infinite;
}
@keyframes wordmarkShift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.search-wrap { position: relative; justify-self: center; width: 100%; max-width: 420px; }
.search-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-alt);
  border-radius: 999px;
  padding: 6px 6px 6px 16px;
}
.search-pill input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--fg);
  font-size: 13.5px;
  padding: 6px 0;
}
.search-pill input::placeholder { color: var(--fg-dim); }
.search-pill input:focus { outline: none; }
.search-pill button[type="submit"] {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: none;
  background: var(--fg);
  color: var(--bg);
  display: grid; place-items: center;
  font-size: 14px;
  flex-shrink: 0;
}
.link-icon {
  background: none;
  border: none;
  color: var(--fg-dim);
  font-size: 15px;
  text-decoration: none;
  white-space: nowrap;
  flex-shrink: 0;
}
.link-icon:hover { color: var(--fg); }
.header-actions { display: flex; align-items: center; justify-content: flex-end; gap: 18px; min-width: 0; }
.header-actions .link-icon { font-size: 11.5px; }
.theme-switch {
  background: none;
  border: none;
  font-size: 16px;
  color: var(--fg-dim);
  line-height: 1;
  padding: 2px;
}
.theme-switch:hover { color: var(--fg); }

.lookup-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0; right: 0;
  background: var(--paper);
  border: 1px solid var(--border-fade);
  border-radius: 12px;
  padding: 10px;
  display: flex;
  gap: 8px;
  box-shadow: 0 14px 28px -10px rgba(0,0,0,0.3);
  z-index: 20;
}
.lookup-dropdown[hidden] { display: none; }
.lookup-dropdown input {
  flex: 1; min-width: 0;
  border: none;
  border-bottom: 1px solid var(--border-strong);
  background: transparent;
  color: var(--fg);
  font-size: 13px;
  padding: 6px 2px;
}
.lookup-dropdown input:focus { outline: none; }
.lookup-dropdown button {
  background: none; border: none;
  font-family: var(--font-mono);
  font-size: 12px; font-weight: 700;
  color: var(--fg);
  white-space: nowrap;
}

.status {
  min-height: 16px;
  font-size: 11px;
  letter-spacing: 0.02em;
  color: var(--fg-dim);
  text-align: center;
  padding: 8px 0 0;
}

.link-btn {
  background: none;
  border: none;
  color: var(--fg-dim);
  font-size: 11px;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.link-btn:hover { color: var(--fg); }

/* ------------------------------------------------------------- workspace */
.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr) 270px;
  grid-template-rows: minmax(0, 1fr);
  gap: 26px;
  padding: 16px 24px 12px;
}

.pane { min-height: 0; display: flex; flex-direction: column; }
.pane[hidden] { display: none; }
.pane-crate, .pane-similar {
  background: var(--paper);
  border-radius: 12px;
  padding: 14px 16px;
}
.pane h2 {
  font-family: var(--font-mono);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-dim);
  margin: 0 0 8px;
  flex-shrink: 0;
}

.pane-crate ol, .pane-results ol, .pane-similar ol {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

@keyframes cratePulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--deck-a) 45%, transparent); }
  100% { box-shadow: 0 0 0 14px color-mix(in srgb, var(--deck-a) 0%, transparent); }
}
.pane-crate.pulse { animation: cratePulse 0.6s ease-out; }

/* -------------------------------------------------------- track listing */
.track-listing {
  list-style: none;
  margin: 0;
  padding: 0;
  counter-reset: row;
}
.track-row {
  counter-increment: row;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 8px;
  border-radius: 8px;
  animation: rowIn 0.28s cubic-bezier(.2,.7,.3,1) both;
}
.track-row:hover { background: var(--surface-alt); }
.track-row::before {
  content: counter(row, decimal-leading-zero);
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--fg-dim);
  padding-top: 3px;
  width: 18px;
  flex-shrink: 0;
}
@keyframes rowIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.tr-main { flex: 1; min-width: 0; }
.tr-track {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 14.5px;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tr-artist { font-size: 12px; color: var(--fg-dim); margin: 2px 0 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tr-score { font-size: 10px; color: var(--deck-a); margin: 3px 0 0; }
.tr-actions {
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  flex-shrink: 0;
  align-self: center;
}
.tr-actions button {
  background: var(--surface-alt);
  border: none;
  border-radius: 999px;
  padding: 7px 12px;
  min-width: 32px;
  min-height: 32px;
  font-size: 13.5px;
  font-weight: 700;
  color: var(--fg-dim);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tr-actions button:hover { background: var(--border-fade); color: var(--fg); }
.tr-actions button[data-act="uncrate"] { color: var(--danger); }

/* A/B are the primary, most-used actions — give them a permanent colored
   pill (matching the dock below) instead of relying on hover to reveal
   what they do, since hover doesn't exist on touch. */
.tr-actions button[data-act="a"] { background: var(--deck-a-soft); color: var(--deck-a); }
.tr-actions button[data-act="b"] { background: var(--deck-b-soft); color: var(--deck-b); }
.tr-actions button[data-act="a"]:hover, .tr-actions button[data-act="b"]:hover { filter: brightness(1.08); }

/* Compact lists (crate + similar) are too narrow to fit title/artist next
   to 4 pill buttons on one line — stack instead: title, artist, then a
   full-width button row so nothing gets squeezed or cut off. */
.track-listing.compact .track-row {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 7px;
  padding: 10px 8px;
}
.track-listing.compact .track-row::before { content: none; }
.track-listing.compact .tr-main { flex: 0 0 auto; }
.track-listing.compact .tr-track {
  font-size: 13px;
  white-space: normal;
  overflow-wrap: break-word;
}
.track-listing.compact .tr-track::before {
  content: counter(row, decimal-leading-zero) "  ";
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 400;
  color: var(--fg-dim);
}
.track-listing.compact .tr-artist { font-size: 11px; white-space: normal; }
.track-listing.compact .tr-actions { gap: 7px; align-self: stretch; width: 100%; }
.track-listing.compact .tr-actions button { flex: 1; padding: 8px 6px; min-width: 0; min-height: 32px; font-size: 13px; }

.pane-crate #crate-export { margin-top: 8px; flex-shrink: 0; }

/* ------------------------------------------------------------- skeleton */
.skeleton-row {
  display: flex;
  gap: 12px;
  padding: 10px 8px;
  animation: rowIn 0.3s ease both;
}
.skeleton-bar {
  height: 10px;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--border-fade) 25%, var(--border-strong) 50%, var(--border-fade) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* --------------------------------------------------------- player dock */
.player-dock {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 28px;
  padding: 16px 28px;
  background: var(--paper);
  box-shadow: 0 -1px 0 var(--border-fade), 0 -10px 22px -14px rgba(0,0,0,0.18);
}
.dock-deck { display: flex; align-items: center; gap: 16px; min-width: 0; }
.dock-deck[data-deck="a"] { justify-self: end; }
.dock-deck[data-deck="b"] { justify-self: start; flex-direction: row-reverse; }
[data-deck="a"] { --glow: var(--deck-a); }
[data-deck="b"] { --glow: var(--deck-b); }

.md-screen {
  width: 148px; height: 84px;
  border-radius: 8px;
  overflow: hidden;
  background: #000;
  flex-shrink: 0;
  box-shadow: 0 0 0 1px var(--border-strong);
}
.dock-deck[data-playing="true"] .md-screen { animation: glowPulse 2.4s ease-in-out infinite; }
@keyframes glowPulse {
  0%, 100% { box-shadow: 0 0 0 1px var(--border-strong), 0 0 0 0 color-mix(in srgb, var(--glow) 0%, transparent); }
  50% { box-shadow: 0 0 0 1px var(--border-strong), 0 0 18px 4px color-mix(in srgb, var(--glow) 55%, transparent); }
}
.md-meta { min-width: 0; max-width: 220px; }
.dock-deck[data-deck="a"] .md-meta { text-align: right; }
.dock-deck[data-deck="b"] .md-meta { text-align: left; }
.md-track { margin: 0; font-size: 15px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.md-artist { margin: 3px 0 0; font-size: 12.5px; color: var(--fg-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.md-play {
  width: 44px; height: 44px;
  border-radius: 50%;
  border: none;
  flex-shrink: 0;
  display: grid; place-items: center;
  font-size: 16px;
  transition: transform 0.15s ease, box-shadow 0.25s ease;
}
[data-deck="a"] .md-play { background: var(--deck-a); color: #04211c; }
[data-deck="b"] .md-play { background: var(--deck-b); color: #2b0414; }
.md-play:disabled { opacity: 0.35; cursor: not-allowed; }
.md-play:active:not(:disabled) { transform: scale(0.9); }
.dock-deck[data-playing="true"] .md-play { box-shadow: 0 0 0 5px color-mix(in srgb, var(--glow) 32%, transparent); }

.dock-center { display: flex; flex-direction: column; align-items: center; gap: 8px; justify-self: center; }
#scene {
  width: 380px; height: 150px;
  display: block;
  border-radius: 10px;
  background: radial-gradient(120% 160% at 50% 0%, #2c2116, #14100b);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.35);
}
.fader-row { display: flex; align-items: center; gap: 8px; }
.fader-row .mono { font-size: 10px; color: var(--fg-dim); }
#crossfader { width: 220px; accent-color: var(--fg); }

/* --------------------------------------------------------------- mobile */
/* Still single-viewport: #app stays a 100dvh flex column, so the dock is
   pinned by flow (no position:fixed needed) and only the active pane
   scrolls internally — never the page itself. */
@media (max-width: 900px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .search-wrap { max-width: none; }

  .workspace {
    display: flex;
    flex-direction: column;
    padding: 12px 16px 10px;
    gap: 10px;
  }
  .pane-crate {
    flex: 0 0 auto;
    max-height: 84px;
  }
  .pane-results { flex: 1.3; min-height: 0; }
  .pane-similar { flex: 1; min-height: 0; }

  .player-dock { padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px)); gap: 14px; }
  #scene { width: 170px; height: 66px; }
  .md-meta { max-width: 110px; }
  .md-track { font-size: 11.5px; }
  .md-artist { font-size: 10px; }
  .md-screen { width: 40px; height: 23px; }
  .md-play { width: 28px; height: 28px; font-size: 11px; }
  #crossfader { width: 90px; }

  #toast-root { bottom: calc(18px + env(safe-area-inset-bottom, 0px)); }
}

@media (max-width: 560px) {
  .brand .imprint { display: none; }
  .header-actions .link-icon:not(.theme-switch) { display: none; }
  #scene { width: 130px; height: 52px; }
  #crossfader { width: 60px; }
  .md-meta { max-width: 76px; }
}

/* ---------------------------------------------------------------- toast */
#toast-root {
  position: fixed;
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
}
.toast {
  background: var(--fg);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 6px;
  font-size: 12.5px;
  font-family: var(--font-mono);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.err { background: var(--danger); color: #fff; }
