/* TuneLens Companion — standalone chat surface.
   Scoped under .tl-companion so nothing leaks into other pages.
   Colors derive from the main-app theme tokens defined in main.css
   (:root) so the companion stays visually in sync — including light mode. */

.tl-companion {
  --c-bg: var(--bg);
  --c-panel: var(--surface);
  --c-panel-2: color-mix(in srgb, var(--surface) 88%, var(--text));
  --c-border: var(--border);
  --c-border-2: color-mix(in srgb, var(--border) 70%, var(--text-muted));
  --c-text: var(--text);
  --c-text-dim: var(--text-muted);
  --c-text-faint: color-mix(in srgb, var(--text-muted) 70%, var(--bg));
  --c-accent: var(--accent);
  --c-accent-soft: color-mix(in srgb, var(--accent) 14%, transparent);
  --c-accent-border: color-mix(in srgb, var(--accent) 38%, transparent);
  --c-ink: #fff;

  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(120% 80% at 50% 0%, var(--surface) 0%, var(--bg) 60%);
  padding: 24px 12px;
  font-family: 'Inter', system-ui, sans-serif;
  color: var(--c-text);
  -webkit-font-smoothing: antialiased;
}

.tl-companion * { box-sizing: border-box; }

/* Phone frame on desktop; full-bleed on small screens. */
.c-frame {
  width: 420px;
  max-width: 100%;
  height: min(860px, 100vh - 48px);
  background: var(--c-bg);
  border-radius: 28px;
  border: 1px solid var(--c-border);
  box-shadow: 0 40px 90px rgba(0, 0, 0, 0.55);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}
@media (max-width: 480px) {
  /* Safe-area insets keep content clear of notch + home indicator.
     Frame fills the remaining content box so we avoid the iOS 100vh bug
     (100vh includes hidden browser chrome, causing the bottom to be cut off). */
  .tl-companion { padding: env(safe-area-inset-top, 0) 0 env(safe-area-inset-bottom, 0); }
  .c-frame { height: 100%; border-radius: 0; border: none; }
  .c-composer-bar textarea { max-height: 20vh; }
}

/* Views fill the frame; only one is shown at a time (mobile). */
.c-view { position: absolute; inset: 0; display: none; flex-direction: column; background: var(--c-bg); }
.c-view.is-active { display: flex; }

/* ── Desktop: two-pane app shell ─────────────────────────────────────────────
   The recent list (home) becomes a persistent left rail beside the chat pane;
   auth / onboarding / profile stay as full-frame overlays. */
@media (min-width: 900px) {
  .tl-companion { padding: 28px; }
  .c-frame {
    width: min(1100px, 100vw - 56px);
    height: min(860px, 100vh - 56px);
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    grid-template-rows: 100%;
  }
  /* Home rail + chat pane are always laid out in the grid. */
  #view-home, #view-chat { position: static; inset: auto; display: flex; min-height: 0; min-width: 0; }
  #view-home { border-right: 1px solid var(--c-border); }
  /* Full-frame overlays sit above the two panes when active. */
  #view-auth, #view-onboarding, #view-profile { position: absolute; inset: 0; z-index: 30; }
  /* The back button is redundant when the rail is always visible. */
  #chat-back { display: none; }
  /* The out-of-credits sheet centers in the chat pane rather than full-width. */
  .c-modal { align-items: center; }
  .c-modal-card { max-width: 380px; }
}

.c-header {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px 12px;
}
.c-header .c-logo { display: flex; align-items: center; gap: 9px; font-size: 18px; font-weight: 700; letter-spacing: -0.2px; }
.c-logo svg { color: var(--c-accent); }
.c-header .c-spacer { flex: 1; }
.c-iconbtn {
  width: 36px; height: 36px; border-radius: 10px;
  border: 1px solid var(--c-border-2); background: var(--c-panel-2);
  color: var(--c-text-dim); display: flex; align-items: center; justify-content: center;
  cursor: pointer; padding: 0; flex: none;
}
.c-iconbtn:hover { border-color: var(--c-accent); color: var(--c-accent); }

.c-scroll { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 8px 16px 18px; }

/* Buttons */
.c-btn {
  height: 52px; border: none; border-radius: 15px;
  background: var(--c-accent); color: var(--c-ink); text-decoration: none;
  font-family: inherit; font-size: 15px; font-weight: 700; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.c-btn:disabled { opacity: 0.5; cursor: default; }
.c-btn--ghost { background: transparent; border: 1px solid var(--c-border-2); color: var(--c-text); }
.c-btn--full { width: 100%; }

.c-input, .c-textarea {
  width: 100%; border-radius: 12px; background: var(--c-panel);
  border: 1px solid var(--c-border-2); color: var(--c-text);
  font-family: inherit; font-size: 15px; outline: none; padding: 13px 15px;
}
.c-input { height: 50px; }
.c-input:focus, .c-textarea:focus { border-color: var(--c-accent); }
.c-textarea { resize: none; line-height: 1.5; }

.c-label {
  display: block; font-size: 11px; font-weight: 600; letter-spacing: 1px;
  color: var(--c-text-faint); text-transform: uppercase; margin: 0 0 8px;
}
.c-h1 { margin: 0 0 8px; font-size: 25px; font-weight: 700; letter-spacing: -0.5px; line-height: 1.18; }
.c-sub { margin: 0; font-size: 14px; color: var(--c-text-dim); line-height: 1.5; }

/* Chip toggles */
.c-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.c-chip {
  padding: 9px 14px; border-radius: 999px; background: var(--c-panel);
  border: 1px solid var(--c-border-2); color: var(--c-text-dim);
  font-family: inherit; font-size: 13.5px; font-weight: 500; cursor: pointer;
}
.c-chip.is-on { background: var(--c-accent-soft); border-color: var(--c-accent-border); color: var(--c-accent); }

/* Profile / onboarding form */
.c-form { display: flex; flex-direction: column; gap: 20px; margin-top: 22px; }
.c-field { display: flex; flex-direction: column; }

/* Tag input (go-to plugins) */
.c-taginput {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  width: 100%; min-height: 50px; border-radius: 12px; background: var(--c-panel);
  border: 1px solid var(--c-border-2); padding: 8px 10px;
}
.c-taginput:focus-within { border-color: var(--c-accent); }
.c-taginput-field {
  flex: 1; min-width: 90px; border: none; outline: none; background: transparent;
  color: var(--c-text); font-family: inherit; font-size: 14px; padding: 4px 2px;
}
.c-tag {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 6px 5px 11px;
  border-radius: 999px; background: var(--c-accent-soft); border: 1px solid var(--c-accent-border);
  color: var(--c-accent); font-size: 13px;
}
.c-tag button { border: none; background: none; color: inherit; cursor: pointer; font-size: 12px; line-height: 1; padding: 2px; opacity: 0.8; }
.c-tag button:hover { opacity: 1; }

/* Search */
.c-search { height: 44px; font-size: 14px; }

/* Conversation list */
.c-conv {
  position: relative;
  background: var(--c-panel);
  border: 1px solid var(--c-border); border-radius: 16px;
  display: flex; align-items: stretch; margin-bottom: 10px;
}
.c-conv:hover { border-color: var(--c-border-2); background: var(--c-panel-2); }
.c-conv.is-selected { border-color: var(--c-accent-border); background: var(--c-accent-soft); }
.c-conv-main {
  flex: 1; min-width: 0; text-align: left; background: none; border: none; cursor: pointer;
  display: flex; flex-direction: column; gap: 8px; padding: 14px 4px 14px 15px; font-family: inherit;
}
.c-conv-menu {
  flex: none; width: 40px; border: none; background: none; cursor: pointer;
  color: var(--c-text-faint); font-size: 18px; line-height: 1; border-radius: 0 16px 16px 0;
}
.c-conv-menu:hover { color: var(--c-text); }
.c-conv-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.c-conv-title { font-size: 15px; font-weight: 600; color: var(--c-text); line-height: 1.25;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-conv-time { font-size: 11px; color: var(--c-text-faint); white-space: nowrap; }
.c-conv-snippet { font-size: 13px; color: var(--c-text-dim); line-height: 1.45;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.c-empty { text-align: center; color: var(--c-text-faint); font-size: 14px; padding: 40px 20px; }

/* Row overflow menu */
.c-menu {
  position: absolute; top: 44px; right: 10px; z-index: 20;
  background: var(--c-panel-2); border: 1px solid var(--c-border-2);
  border-radius: 12px; padding: 5px; min-width: 130px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.5);
}
.c-menu button {
  display: block; width: 100%; text-align: left; border: none; background: none;
  color: var(--c-text); font-family: inherit; font-size: 14px; padding: 9px 12px;
  border-radius: 8px; cursor: pointer;
}
.c-menu button:hover { background: var(--c-panel); }
.c-menu button.c-menu-danger { color: var(--danger); }

/* Chat */
.c-msgs { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 16px; }
.c-msg-user {
  margin-left: auto; max-width: 88%; background: var(--c-accent-soft);
  border: 1px solid var(--c-accent-border); border-radius: 16px 16px 6px 16px;
  padding: 11px 14px; font-size: 14.5px; line-height: 1.45; color: var(--c-text);
}
.c-msg-ai { display: flex; gap: 10px; max-width: 96%; }
.c-msg-ai .c-avatar {
  width: 30px; height: 30px; border-radius: 9px; background: var(--c-accent-soft);
  border: 1px solid var(--c-accent-border); color: var(--c-accent); flex: none;
  display: flex; align-items: center; justify-content: center;
}
.c-msg-ai .c-bubble { flex: 1; min-width: 0; font-size: 14.5px; line-height: 1.55; color: var(--c-text); }
.c-status { font-size: 12px; color: var(--c-text-faint); font-style: italic; display: inline-flex; align-items: center; gap: 8px; }
.c-typing { display: inline-flex; gap: 4px; }
.c-typing i { width: 5px; height: 5px; border-radius: 50%; background: var(--c-text-dim); animation: c-bounce 1.2s infinite ease-in-out; }
.c-typing i:nth-child(2) { animation-delay: 0.15s; }
.c-typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes c-bounce {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30%           { opacity: 1;    transform: translateY(-3px); }
}

/* Markdown bits inside AI bubbles */
.c-bubble p { margin: 0 0 10px; }
.c-bubble p:last-child { margin-bottom: 0; }
.c-bubble ul, .c-bubble ol { margin: 8px 0; padding-left: 20px; }
.c-bubble li { margin: 3px 0; }
.c-bubble strong { color: var(--c-text); }
.c-bubble code { background: var(--c-panel-2); padding: 1px 5px; border-radius: 5px; font-size: 0.9em; }

/* Waveform player (current track, above composer) */
.c-player {
  display: flex; flex-direction: column; gap: 6px; margin: 0 16px 8px;
  background: var(--c-panel); border: 1px solid var(--c-border-2);
  border-radius: 14px; padding: 9px 11px;
}
.c-player-head { display: flex; align-items: center; gap: 8px; }
.c-player-row { display: flex; align-items: center; gap: 10px; }
.c-play {
  width: 38px; height: 38px; border-radius: 50%; border: none; flex: none;
  background: var(--c-accent); color: var(--c-ink); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.c-track-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.c-wave { width: 100%; min-width: 0; height: 40px; }
.c-track-time {
  display: flex; justify-content: space-between;
  font-size: 10px; color: var(--c-text-faint); font-family: 'JetBrains Mono', monospace;
}
.c-track-name {
  flex: 1; min-width: 0; font-size: 11px; color: var(--c-text-dim);
  font-family: 'JetBrains Mono', monospace;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.c-track-x { cursor: pointer; color: var(--c-text-faint); background: none; border: none; flex: none; font-size: 13px; }
.c-track-x:hover { color: var(--danger); }

/* Measured-facts strip — deterministic DSP results, tap a pill to ask about it */
.c-metrics {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-top: 2px; padding-top: 8px; border-top: 1px solid var(--c-border);
}
.c-metrics-loading {
  font-size: 11px; color: var(--c-text-faint);
  font-family: 'JetBrains Mono', monospace;
}
.c-metric {
  display: inline-flex; align-items: baseline; gap: 5px;
  background: var(--c-bg); border: 1px solid var(--c-border-2); border-radius: 999px;
  padding: 4px 10px; cursor: pointer; font-family: inherit;
}
.c-metric:hover:not(:disabled) { border-color: var(--c-accent); }
.c-metric:disabled { cursor: default; }
.c-metric-k {
  font-size: 9.5px; font-weight: 700; letter-spacing: 0.8px; text-transform: uppercase;
  color: var(--c-text-faint); font-family: 'JetBrains Mono', monospace;
}
.c-metric-v {
  font-size: 12px; font-weight: 600; color: var(--c-text);
  font-family: 'JetBrains Mono', monospace; white-space: nowrap;
}
.c-metric--warn { border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.c-metric--warn .c-metric-v { color: var(--danger); }
.c-metric--warn:hover:not(:disabled) { border-color: var(--danger); }

/* Structured feedback cards (inside AI bubble) */
.c-fb-summary { margin: 0 0 11px; font-size: 14.5px; line-height: 1.5; color: var(--c-text); }
.c-fb-card {
  background: var(--c-bg); border: 1px solid var(--c-border); border-radius: 14px;
  padding: 12px 14px; margin-bottom: 9px; display: flex; flex-direction: column; gap: 8px;
}
.c-fb-label {
  font-size: 10.5px; font-weight: 700; letter-spacing: 1.2px; color: var(--c-accent);
  font-family: 'JetBrains Mono', monospace;
}
.c-fb-note { display: flex; gap: 8px; align-items: flex-start; font-size: 13.5px; line-height: 1.5; color: var(--c-text); }
.c-fb-arrow { color: var(--c-accent); flex: none; }
.c-fb-tslabel {
  font-size: 10.5px; font-weight: 700; letter-spacing: 1.2px; color: var(--c-text-faint);
  font-family: 'JetBrains Mono', monospace; margin: 6px 0 7px;
}
.c-fb-ts {
  width: 100%; text-align: left; background: var(--c-bg); border: 1px solid var(--c-border);
  border-radius: 12px; padding: 9px 11px; margin-bottom: 6px; cursor: pointer;
  display: flex; gap: 10px; align-items: center;
}
.c-fb-ts:hover { border-color: var(--c-accent); background: var(--c-panel-2); }
.c-fb-time {
  font-size: 11px; font-weight: 700; color: var(--c-accent); background: var(--c-accent-soft);
  font-family: 'JetBrains Mono', monospace; padding: 3px 7px; border-radius: 6px; flex: none;
}
.c-fb-tstext { font-size: 12.5px; color: var(--c-text-dim); line-height: 1.4; }

/* Inline tap-to-seek timestamp inside a conversational reply */
.c-ts-link {
  font: inherit; font-size: 0.92em; font-weight: 600;
  font-family: 'JetBrains Mono', monospace;
  color: var(--c-accent); background: var(--c-accent-soft);
  border: 1px solid var(--c-accent-border); border-radius: 6px;
  padding: 0 5px; margin: 0 1px; cursor: pointer; line-height: 1.5;
  vertical-align: baseline;
}
.c-ts-link:hover { background: var(--c-accent-border); }
.c-fb-chips { display: flex; flex-wrap: wrap; gap: 8px; padding-top: 4px; }
.c-fb-chip {
  border: 1px solid var(--c-border-2); background: transparent; border-radius: 999px;
  padding: 7px 13px; font-family: inherit; font-size: 12.5px; font-weight: 500;
  color: var(--c-text); cursor: pointer;
}
.c-fb-chip:hover { border-color: var(--c-accent); color: var(--c-accent); }

/* Composer */
.c-composer { flex: none; padding: 10px 14px 22px; border-top: 1px solid var(--c-border); background: var(--c-bg); }
.c-composer-bar {
  display: flex; align-items: flex-end; gap: 6px; background: var(--c-panel-2);
  border: 1px solid var(--c-border-2); border-radius: 22px; padding: 6px 6px 6px 12px;
}
.c-composer-bar textarea {
  flex: 1; min-width: 0; border: none; background: transparent; outline: none; color: var(--c-text);
  font-family: inherit; font-size: 14.5px; line-height: 1.45; padding: 8px 0;
  resize: none; overflow-y: auto; max-height: 35vh;
}
.c-circle-btn {
  width: 38px; height: 38px; border-radius: 50%; border: none; background: var(--c-accent);
  color: var(--c-ink);
  display: flex; align-items: center; justify-content: center; cursor: pointer; flex: none;
}
.c-circle-btn:disabled { background: var(--c-border-2); cursor: default; }
.c-plain-btn { border: none; background: transparent; cursor: pointer; display: flex; padding: 4px; color: var(--c-text-dim); }

/* Empty-chat starter */
.c-starter { padding: 24px 18px; }
.c-starter-actions { display: flex; gap: 11px; margin: 20px 0; }
.c-starter-actions .c-card {
  flex: 1; height: 92px; border-radius: 15px; border: 1px dashed var(--c-border-2); background: var(--c-bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  cursor: pointer; color: var(--c-text); font-size: 13.5px; font-weight: 600;
}
.c-starter-actions .c-card:hover { border-color: var(--c-accent); background: var(--c-panel); }

.c-toast-spacer { height: 0; }
.c-error { color: var(--danger); font-size: 13px; margin: 8px 0 0; }
/* Soft inline note when a reply streamed but the stream ended on an error. */
.c-bubble-note {
  margin: 12px 0 0; padding-left: 10px; font-size: 12.5px; line-height: 1.45;
  color: var(--c-text-dim);
  border-left: 2px solid color-mix(in srgb, var(--danger) 55%, transparent);
}
.c-hidden { display: none !important; }

/* Drag-and-drop overlay (desktop) */
.c-dropzone {
  position: absolute; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--c-bg) 82%, transparent); backdrop-filter: blur(2px);
  padding: 18px;
}
.c-dropzone-inner {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  width: 100%; max-width: 420px; padding: 40px 24px; border-radius: 18px;
  border: 2px dashed var(--c-accent-border); background: var(--c-accent-soft);
  color: var(--c-accent); pointer-events: none;
}
.c-dropzone-text { margin: 0; font-size: 15px; font-weight: 600; color: var(--c-text); }

/* Out-of-credits modal (bottom sheet within the phone frame) */
.c-modal {
  position: absolute; inset: 0; z-index: 50;
  display: flex; align-items: flex-end; justify-content: center; padding: 14px;
}
.c-modal-backdrop { position: absolute; inset: 0; background: rgba(4, 5, 7, 0.62); }
.c-modal-card {
  position: relative; width: 100%; background: var(--c-panel);
  border: 1px solid var(--c-border-2); border-radius: 20px; padding: 22px 20px 16px;
  display: flex; flex-direction: column; align-items: center; gap: 9px; text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
  animation: c-sheet-up 0.22s ease;
}
@keyframes c-sheet-up { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.c-modal-icon {
  width: 46px; height: 46px; border-radius: 50%; flex: none;
  background: var(--c-accent-soft); color: var(--c-accent);
  display: flex; align-items: center; justify-content: center;
}
.c-modal-title { margin: 2px 0 0; font-size: 18px; font-weight: 700; }
.c-modal-text { margin: 0 0 6px; font-size: 13.5px; color: var(--c-text-dim); line-height: 1.5; }
.c-modal-dismiss { color: var(--c-text-faint); font-size: 13px; font-weight: 600; justify-content: center; padding: 8px; }
