/* tapedeck — mobile-first, iPhone-primary.
   Audio playback + karaoke transcript are the design centerpiece.
   Cassette aesthetic kept tasteful here; Sprint 2 dials it up.
*/

:root {
  --bg: #16110c;
  --bg-card: #221a13;
  --bg-card-2: #2c2118;
  --bg-elev: #2f2519;
  --cream: #f1e3c2;
  --cream-soft: #ddc999;
  --cream-dim: #b9a880;
  --ink: #1a130a;
  --amber: #d4a056;
  --amber-bright: #f0c074;
  --rust: #c0492f;
  --line: #3a2e22;
  --line-soft: #2d2418;
  --muted: #8b7a62;
  --font-serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;

  --shadow-card: 0 1px 0 rgba(255, 220, 170, 0.05) inset, 0 12px 28px rgba(0, 0, 0, 0.4);
  --shadow-pin: 0 -8px 24px rgba(0, 0, 0, 0.5);

  /* iPhone safe-area helpers */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);

  --tap: 44px;
}

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

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--cream);
  font-family: var(--font-serif);
  font-size: 17px;
  line-height: 1.55;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

body { overflow-x: hidden; }

a { color: var(--amber-bright); text-decoration: none; }
a:active { opacity: 0.7; }
a:hover { text-decoration: underline; }

button {
  font-family: inherit;
  cursor: pointer;
  min-height: var(--tap);
}

/* ─────────────── Topbar with Hamburger ─────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--safe-t)) calc(16px + var(--safe-r)) 10px calc(16px + var(--safe-l));
  background: linear-gradient(180deg, #1d160f 0%, #16110c 100%);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 50;
}

.hamburger {
  width: var(--tap);
  height: var(--tap);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--cream);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  line-height: 1;
}
.hamburger:active { background: var(--bg-card); }

.brand {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  color: var(--cream);
  margin-left: 4px;
}
.brand:hover { text-decoration: none; }
.brand-mark { color: var(--amber); font-size: 22px; line-height: 1; }
.brand-name {
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: lowercase;
  font-family: var(--font-sans);
  font-size: 17px;
}

.topbar-spacer { flex: 1; }

.topbar .who-mini {
  font-size: 13px;
  color: var(--cream-dim);
  font-family: var(--font-sans);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ─────────────── Drawer ─────────────── */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 90;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: min(82vw, 320px);
  background: var(--bg-card);
  border-right: 1px solid var(--line);
  box-shadow: 8px 0 24px rgba(0, 0, 0, 0.4);
  padding: calc(20px + var(--safe-t)) 20px calc(20px + var(--safe-b)) calc(20px + var(--safe-l));
  z-index: 100;
  transform: translateX(-100%);
  transition: transform 220ms cubic-bezier(0.2, 0.7, 0.2, 1);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.drawer.open { transform: translateX(0); }

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}
.drawer-title {
  font-family: var(--font-sans);
  font-size: 13px;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--muted);
}
.drawer-close {
  width: var(--tap); height: var(--tap);
  background: transparent; border: none; color: var(--cream-dim);
  font-size: 22px;
}

.drawer a, .drawer button.drawer-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 12px;
  border-radius: 8px;
  color: var(--cream);
  font-size: 17px;
  text-decoration: none;
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
}
.drawer a:active, .drawer button.drawer-item:active { background: var(--bg-elev); }
.drawer a.current { background: var(--bg-elev); color: var(--amber-bright); }
.drawer-divider { height: 1px; background: var(--line); margin: 12px 0; }
.drawer-foot { margin-top: auto; padding-top: 16px; border-top: 1px solid var(--line); }
.drawer-foot .who { font-family: var(--font-sans); color: var(--cream-dim); font-size: 14px; padding: 4px 12px; }

.icon { font-size: 18px; width: 24px; text-align: center; color: var(--amber); }

/* ─────────────── Page wrapper ─────────────── */
.page {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px calc(16px + var(--safe-r)) calc(80px + var(--safe-b)) calc(16px + var(--safe-l));
}

.footer {
  text-align: center;
  padding: 16px 0 calc(20px + var(--safe-b));
  color: var(--muted);
  font-style: italic;
  font-size: 13px;
}

/* ─────────────── Auth ─────────────── */
.auth-card {
  max-width: 380px;
  margin: 32px auto;
  padding: 28px 24px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow-card);
}
.auth-card h1 { margin: 0 0 18px; font-weight: 500; letter-spacing: 0.02em; font-size: 26px; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--cream-soft); font-family: var(--font-sans); }
.auth-card input {
  padding: 14px 14px;
  min-height: var(--tap);
  background: #0f0b07;
  color: var(--cream);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-family: inherit;
  font-size: 17px; /* iOS won't zoom on focus when font-size >= 16px */
}
.auth-card input:focus { outline: none; border-color: var(--amber); }
.auth-card .muted { color: var(--muted); font-size: 13px; }

button.primary, button.secondary, .btn-primary, .btn-secondary {
  padding: 14px 20px;
  min-height: var(--tap);
  border: none;
  border-radius: 6px;
  font-family: inherit;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.04em;
  transition: background 120ms ease, transform 80ms ease;
}
button.primary, .btn-primary { background: var(--amber); color: var(--ink); }
button.primary:active, .btn-primary:active { transform: scale(0.98); background: var(--amber-bright); }
button.secondary, .btn-secondary { background: transparent; color: var(--cream); border: 1px solid var(--line); }
button.secondary:active, .btn-secondary:active { background: var(--bg-card); }

.flash.error {
  background: rgba(192, 73, 47, 0.15);
  border: 1px solid var(--rust);
  color: #f6c5b8;
  padding: 12px 14px;
  border-radius: 6px;
  font-size: 14px;
  font-family: var(--font-sans);
}

/* ─────────────── Library shelf ─────────────── */
.library-header { margin-bottom: 20px; }
.library-header h1 { margin: 0 0 4px; font-weight: 500; font-size: 28px; letter-spacing: 0.01em; }
.muted { color: var(--muted); font-size: 14px; font-family: var(--font-sans); }

.cassette-shelf {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.cassette { display: block; }
.cassette-face {
  display: grid;
  grid-template-rows: 1fr auto;
  background: linear-gradient(180deg, #2f2519 0%, #1f180f 100%);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  box-shadow: var(--shadow-card);
  min-height: 150px;
  transition: transform 120ms ease, border-color 160ms ease;
}
.cassette-face:active { transform: scale(0.985); border-color: var(--amber); }
.cassette-face:hover { text-decoration: none; }

.cassette-label {
  background: linear-gradient(180deg, var(--cream) 0%, #e6d3a8 100%);
  color: var(--ink);
  padding: 14px 14px 12px;
  border-radius: 4px;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.18) inset;
  /* Subtle bandtype stripe — TDK-style */
  border-top: 4px solid var(--amber);
}
.label-title {
  font-weight: 600;
  font-size: 16px;
  line-height: 1.25;
  color: var(--ink);
  /* Multi-line truncate */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.label-channel { font-size: 13px; color: #6a5c43; font-style: italic; font-family: var(--font-sans); }
.label-meta {
  margin-top: auto;
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 12px;
  color: #6a5c43;
  font-family: var(--font-sans);
}
.chip {
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  letter-spacing: 0.04em;
}
.chip.done { background: var(--amber); color: var(--ink); font-weight: 600; }
.chip.wip { background: rgba(26, 19, 10, 0.15); color: var(--ink); }

.cassette-reels {
  margin-top: 12px;
  display: grid;
  grid-template-columns: 1fr 42px 1fr;
  align-items: center;
}
.reel {
  height: 32px;
  aspect-ratio: 1;
  background: radial-gradient(circle, #0f0a05 0%, #0f0a05 28%, #2a2117 32%, #1a130b 100%);
  border-radius: 50%;
  border: 2px solid #0a0704;
  justify-self: center;
  position: relative;
}
.reel::before { content: ""; position: absolute; inset: 28%; border-radius: 50%; background: var(--amber); opacity: 0.45; }
.reel::after { content: ""; position: absolute; inset: 44%; border-radius: 50%; background: #0f0a05; }
.reel-bridge { height: 2px; background: var(--line); align-self: center; }

.cassette-face:active .reel { animation: reel-spin 2s linear infinite; }
@keyframes reel-spin { to { transform: rotate(360deg); } }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty code {
  background: var(--bg-card);
  padding: 2px 8px;
  border-radius: 3px;
  color: var(--cream);
  font-family: var(--font-mono);
  font-size: 13px;
}

/* ─────────────── Tape detail ─────────────── */
.tape-detail h1 {
  font-size: 22px;
  font-weight: 500;
  letter-spacing: 0.01em;
  margin: 0 0 4px;
  line-height: 1.25;
}
.tape-detail .tape-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  font-family: var(--font-sans);
  font-size: 13px;
  color: var(--cream-dim);
  margin-bottom: 16px;
}
.tape-detail .source-link { color: var(--amber-bright); }

/* Player — sticky right under the topbar */
.player-pin {
  position: sticky;
  top: calc(var(--tap) + 22px + var(--safe-t)); /* under the topbar */
  z-index: 30;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  box-shadow: var(--shadow-card);
  margin-bottom: 18px;
}
.player-pin audio {
  width: 100%;
  height: 44px;
}

/* Chapter chips strip */
.chapter-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0 12px;
  margin: -4px -16px 16px;
  padding-left: 16px;
  padding-right: 16px;
  scroll-snap-type: x proximity;
}
.chapter-strip::-webkit-scrollbar { display: none; }
.chapter-chip {
  flex: 0 0 auto;
  scroll-snap-align: start;
  padding: 10px 14px;
  min-height: var(--tap);
  border-radius: 999px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  color: var(--cream);
  font-size: 14px;
  font-family: var(--font-sans);
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.chapter-chip:active { background: var(--bg-elev); }
.chapter-chip.active { background: var(--amber); color: var(--ink); border-color: var(--amber); font-weight: 600; }
.chapter-chip .ch-no { font-family: var(--font-mono); font-size: 12px; opacity: 0.7; }

/* Karaoke transcript */
.kar-wrap {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 18px;
}

.kar-chapter { margin: 0 0 24px; }
.kar-chapter:last-child { margin-bottom: 0; }
.kar-chapter-title {
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 10px;
  padding: 6px 8px 8px;
  border-bottom: 1px dashed var(--line);
  cursor: pointer;
  border-radius: 4px 4px 0 0;
  transition: background 180ms ease, color 180ms ease, border-color 180ms ease;
}
.kar-chapter-title .ch-no { color: var(--amber); margin-right: 8px; }
.kar-chapter-title.active {
  background: var(--amber);
  color: var(--ink);
  border-bottom-color: var(--ink);
}
.kar-chapter-title.active .ch-no { color: var(--ink); }

.kar-body {
  font-family: var(--font-serif);
  font-size: 19px;
  line-height: 1.65;
  color: var(--cream-soft);
}
/* Sentence ohne Word-Children (Title + Legacy-Fallback) = klickbares Karaoke-Ziel */
.kar-sentence {
  border-radius: 4px;
  padding: 1px 2px;
  margin: 0 -2px;
  transition: background 180ms ease, color 180ms ease;
}
.kar-sentence:has(.kar-word) { cursor: default; }
.kar-sentence:not(:has(.kar-word)) { cursor: pointer; }
.kar-sentence:not(:has(.kar-word)):active { background: var(--bg-elev); }

/* Title-Segment + Legacy-Fallback: kompletter Satz wird amber unterlegt */
.kar-sentence:not(:has(.kar-word)).active {
  background: var(--amber);
  color: var(--ink);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.06) inset;
}

/* Sentence MIT Wort-Children: nur subtiler Kontext-Tint, kein Vollflächen-Highlight.
   Damit hebt sich das Wort-Highlight sauber ab. */
.kar-sentence:has(.kar-word).active {
  background: rgba(212, 160, 86, 0.10);
  box-shadow: inset 0 0 0 1px rgba(212, 160, 86, 0.20);
}

/* Wort-Level — der echte Karaoke-Cursor */
.kar-word {
  cursor: pointer;
  border-radius: 3px;
  padding: 1px 2px;
  margin: 0 -2px;
  transition: background 90ms ease, color 90ms ease;
}
.kar-word:active { background: var(--bg-elev); }
.kar-word.active-word {
  background: var(--amber);
  color: var(--ink);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08) inset;
}

/* Chat — collapses to a button on mobile, opens to a full-page overlay */
.chat-trigger-row {
  position: sticky;
  bottom: calc(8px + var(--safe-b));
  z-index: 20;
  display: flex;
  justify-content: flex-end;
  pointer-events: none;
  margin-top: 24px;
}
.chat-trigger {
  pointer-events: auto;
  background: var(--rust);
  color: var(--cream);
  border: none;
  border-radius: 999px;
  padding: 14px 22px;
  font-weight: 600;
  font-family: var(--font-sans);
  font-size: 15px;
  letter-spacing: 0.04em;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.5), 0 1px 0 rgba(255, 220, 170, 0.1) inset;
}
.chat-trigger:active { transform: scale(0.97); }
.chat-trigger .dot {
  display: inline-block;
  width: 6px; height: 6px;
  background: var(--cream);
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: middle;
}

.chat-sheet {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 120;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 240ms cubic-bezier(0.2, 0.7, 0.2, 1);
}
.chat-sheet.open { transform: translateY(0); }
.chat-sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(12px + var(--safe-t)) 16px 12px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #1d160f 0%, #16110c 100%);
}
.chat-sheet-head h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 500;
  font-family: var(--font-sans);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-sheet-close {
  width: var(--tap); height: var(--tap);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--cream);
  font-size: 22px;
}
.chat-sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}
.chat-sheet-footer {
  padding: 12px 16px calc(16px + var(--safe-b));
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.chat-form { display: flex; gap: 8px; align-items: flex-end; }
.chat-form textarea {
  flex: 1;
  background: #0f0b07;
  color: var(--cream);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  font-family: inherit;
  font-size: 17px;
  resize: none;
  min-height: var(--tap);
  max-height: 30vh;
}
.chat-form textarea:focus { outline: none; border-color: var(--amber); }
.chat-form button { padding: 12px 16px; }

.chat-empty {
  text-align: center;
  color: var(--muted);
  font-style: italic;
  padding: 32px 16px;
}
.chat-messages {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.msg {
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--bg-card-2);
  max-width: 90%;
}
.msg-user {
  background: var(--bg-card);
  border-left: 3px solid var(--amber);
  align-self: flex-end;
}
.msg-assistant {
  border-left: 3px solid var(--rust);
  align-self: flex-start;
}
.msg-role {
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 4px;
  font-family: var(--font-sans);
}
.msg-body {
  white-space: pre-wrap;
  font-size: 16px;
  line-height: 1.55;
}

/* Loading indicator for HTMX */
.htmx-indicator { display: none; color: var(--muted); font-style: italic; padding: 12px; }
.htmx-request .htmx-indicator { display: block; }

/* ─────────────── Larger screens ─────────────── */
@media (min-width: 720px) {
  .page { padding-top: 28px; }
  .cassette-shelf {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
  .tape-detail h1 { font-size: 28px; }
}

@media (min-width: 1024px) {
  .cassette-shelf { grid-template-columns: repeat(3, 1fr); }
  .page { max-width: 920px; }
  /* On desktop, the chat sheet is overkill — could become a sidebar; keep
     the sheet for parity for now (Sprint 2 may split-pane). */
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
  .kar-sentence.active { transition: none; }
}
