/* Chill Semitone AI — music destination instrument (thread + start) */

/* Footer / EQ / FAB: keep equalizer alive, give chrome its own readable band */
body.cs-ai-mode {
  --layout-eq-ratio: 0.22;
  --site-footer-zone: 3rem;
  --layout-footer-zone: 3rem;
  --cs-ai-chrome-bottom: calc(
    var(--viewport-h, 100dvh) * var(--layout-eq-ratio) + var(--layout-footer-zone) + 0.85rem +
      env(safe-area-inset-bottom, 0px)
  );
  --start-bottom-reserve: var(--cs-ai-chrome-bottom);
}

/*
 * Chatting: fixed composer sits just above footer/EQ bars.
 * Smaller --cs-ai-chrome-bottom = lower on screen (toward the visualizer).
 */
body.cs-ai-chatting,
body.cs-ai-chatting.cs-ai-thread-long {
  --cs-ai-chrome-bottom: calc(
    var(--site-footer-zone, 3rem) + 1.15rem + env(safe-area-inset-bottom, 0px)
  );
}

html.is-proportional-desktop body.cs-ai-mode {
  --layout-eq-ratio: 0.13;
}

html.is-proportional-desktop body.cs-ai-chatting,
html.is-proportional-desktop body.cs-ai-chatting.cs-ai-thread-long {
  --cs-ai-chrome-bottom: calc(
    var(--site-footer-zone, 3rem) + 0.85rem + env(safe-area-inset-bottom, 0px)
  );
}

/* Beat styles.css start-footer lift/z-index — veil must sit on the bottom edge */
html.is-proportional-desktop body.cs-ai-mode.is-start-screen .site-footer,
html.is-proportional-desktop body.cs-ai-mode .site-footer,
body.cs-ai-mode .site-footer {
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  mix-blend-mode: normal;
  color: rgba(235, 228, 216, 0.94);
  /* Slightly lighter veil; solid at the bottom edge so EQ/chrome stay readable */
  background: linear-gradient(
    to top,
    rgba(8, 14, 28, 0.82) 0%,
    rgba(8, 14, 28, 0.68) 28%,
    rgba(8, 14, 28, 0.32) 62%,
    rgba(8, 14, 28, 0.1) 82%,
    transparent 100%
  );
  padding: 2.75rem 1rem calc(0.75rem + env(safe-area-inset-bottom, 0px));
  min-height: calc(5rem + env(safe-area-inset-bottom, 0px));
  box-sizing: border-box;
  pointer-events: none;
  filter: none;
}

body.cs-ai-mode .site-footer a {
  pointer-events: auto;
  text-decoration: underline;
  text-underline-offset: 2px;
}

body.cs-ai-mode .site-fab-cluster {
  bottom: calc(var(--site-footer-zone) + 0.85rem + env(safe-area-inset-bottom, 0px));
  z-index: var(--z-chrome, 420);
}

/* Lang switcher: same foreground band as FABs — above footer gradient / ambient blur */
body.cs-ai-mode .app-toolbar {
  bottom: calc(0.55rem + env(safe-area-inset-bottom, 0px));
  z-index: var(--z-chrome, 420);
  isolation: isolate;
  opacity: 1;
  filter: none;
  mix-blend-mode: normal;
  pointer-events: auto;
}

body.cs-ai-mode .app-toolbar .lang-switch {
  opacity: 1;
  filter: none;
  mix-blend-mode: normal;
  background: rgba(10, 22, 40, 0.92);
  border-color: rgba(235, 228, 216, 0.22);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.45);
}

body.cs-ai-mode .app-toolbar .lang-btn {
  opacity: 1;
  filter: none;
  mix-blend-mode: normal;
  color: rgba(235, 228, 216, 0.88);
  background: transparent;
}

body.cs-ai-mode .app-toolbar .lang-btn.is-active {
  background: var(--accent);
  color: #0a1628;
}

/* Covers stay vivid and above ambient blur / note-rain */
body.cs-ai-mode #song-wall-root {
  --song-wall-z: 300;
  z-index: 300;
  opacity: 1;
  filter: none;
  mix-blend-mode: normal;
}

body.cs-ai-mode #song-wall-root .song-wall__cover {
  opacity: 1;
  filter: none;
}

/* Leave a cover-sized gap so the corner logo does not hide the first tiles */
body.cs-ai-mode #song-wall-root .song-wall__track {
  padding-left: calc(
    var(--song-wall-pad-x, 1rem) + var(--song-wall-cover-size, 7.25rem) + var(--song-wall-gap, 0.8rem)
  );
}

/* Above cover wall so reset wins over Spotify links underneath.
   Same footprint as song-wall covers (see --song-wall-cover-size). */
body.cs-ai-mode .brand-logo.logo-corner {
  z-index: 430;
  width: var(--song-wall-cover-size, 7.25rem);
  height: var(--song-wall-cover-size, 7.25rem);
  top: var(--song-wall-pad-top, 0.75rem);
  left: var(--song-wall-pad-x, 1rem);
  pointer-events: auto;
  box-shadow: 0 0 18px var(--accent-glow), 0 2px 12px rgba(0, 0, 0, 0.45);
}

/* Center the textarea box only — send hangs outside to the right */
body.cs-ai-mode .cs-ai-shell .idea-input-group {
  --idea-send-btn-size: 2.5rem;
  --idea-send-btn-gap: 0.55rem;
  width: min(34rem, calc(100% - 2 * (var(--idea-send-btn-size) + var(--idea-send-btn-gap) + 0.35rem)));
  max-width: min(34rem, calc(100% - 2 * (var(--idea-send-btn-size) + var(--idea-send-btn-gap) + 0.35rem)));
  margin-left: auto;
  margin-right: auto;
}

@media (max-width: 640px) {
  body.cs-ai-mode {
    --layout-eq-ratio: 0.26;
    --site-footer-zone: 3.25rem;
    --layout-footer-zone: 3.25rem;
  }

  body.cs-ai-mode .site-fab-cluster {
    bottom: calc(var(--site-footer-zone) + 0.7rem + env(safe-area-inset-bottom, 0px));
    gap: 0.5rem;
  }
}

.cs-ai-shell {
  width: 100%;
}

.cs-ai-subtitle {
  margin: -0.35rem 0 1rem;
  opacity: 0.78;
  font-size: 1rem;
  line-height: 1.45;
  text-align: center;
  color: inherit;
  max-width: 28rem;
  margin-left: auto;
  margin-right: auto;
}

.cs-ai-thread {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0 0 0.85rem;
  padding: 0.35rem 0.2rem;
  scroll-behavior: smooth;
  min-height: 0;
  /* Scroll works; chrome stays invisible */
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.cs-ai-thread::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}

.cs-ai-thread[hidden] {
  display: none !important;
}

.cs-ai-msg {
  max-width: min(92%, 34rem);
  padding: 0.7rem 0.95rem;
  border-radius: 0.95rem;
  line-height: 1.5;
  font-size: 0.98rem;
  word-break: break-word;
  border: 1px solid transparent;
  text-align: left;
}

.cs-ai-msg--user {
  align-self: flex-end;
  background: rgba(255, 255, 255, 0.11);
  border-color: rgba(255, 255, 255, 0.08);
}

.cs-ai-msg--assistant {
  align-self: flex-start;
  background: rgba(8, 16, 32, 0.55);
  border-color: rgba(120, 160, 220, 0.14);
}

.cs-ai-msg__role {
  display: block;
  font-size: 0.68rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.62; /* ≥ 4.5:1 on the chat bubbles (WCAG AA) */
  margin-bottom: 0.28rem;
}

.cs-ai-msg__body {
  display: block;
}

.cs-ai-msg__body--plain {
  white-space: pre-wrap;
}

/* Markdown / KaTeX / ABC prose inside chat bubbles */
.cs-ai-prose {
  white-space: normal;
}

.cs-ai-prose > :first-child {
  margin-top: 0;
}

.cs-ai-prose > :last-child {
  margin-bottom: 0;
}

.cs-ai-prose p,
.cs-ai-prose ul,
.cs-ai-prose ol,
.cs-ai-prose pre,
.cs-ai-prose blockquote,
.cs-ai-prose .katex-display,
.cs-ai-prose .cs-ai-abc {
  margin: 0.55em 0;
}

.cs-ai-prose p {
  line-height: 1.55;
}

.cs-ai-prose a {
  color: var(--accent, #9ec5ff);
  text-decoration: underline;
  text-underline-offset: 0.12em;
}

.cs-ai-prose code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  padding: 0.12em 0.35em;
  border-radius: 0.35em;
  background: rgba(0, 0, 0, 0.28);
}

.cs-ai-prose pre {
  overflow-x: auto;
  padding: 0.7rem 0.85rem;
  border-radius: 0.65rem;
  background: rgba(0, 0, 0, 0.32);
  border: 1px solid rgba(255, 255, 255, 0.08);
  scrollbar-width: thin;
}

.cs-ai-prose pre code {
  padding: 0;
  background: none;
  font-size: 0.84em;
}

.cs-ai-prose blockquote {
  margin-left: 0;
  padding: 0.15rem 0 0.15rem 0.75rem;
  border-left: 3px solid rgba(212, 132, 90, 0.55);
  opacity: 0.92;
}

.cs-ai-prose ul,
.cs-ai-prose ol {
  padding-left: 1.25rem;
}

.cs-ai-prose li + li {
  margin-top: 0.2rem;
}

.cs-ai-prose h1,
.cs-ai-prose h2,
.cs-ai-prose h3,
.cs-ai-prose h4 {
  margin: 0.75em 0 0.35em;
  line-height: 1.25;
  font-weight: 600;
}

.cs-ai-prose h1 { font-size: 1.2em; }
.cs-ai-prose h2 { font-size: 1.1em; }
.cs-ai-prose h3 { font-size: 1.05em; }

.cs-ai-prose .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.15rem 0;
  scrollbar-width: thin;
}

.cs-ai-prose .katex {
  font-size: 1.05em;
}

.cs-ai-abc {
  overflow-x: auto;
  color: inherit;
  scrollbar-width: thin;
}

.cs-ai-abc svg {
  max-width: 100%;
  height: auto;
}

.cs-ai-abc-error {
  margin: 0.35rem 0 0;
  font-size: 0.82rem;
  opacity: 0.8;
  color: #e8a090;
}

.cs-ai-abc-source {
  opacity: 0.92;
}

/* Composer: same field geometry on start + chat; grows with content */
body.cs-ai-mode .cs-ai-shell .idea-textarea {
  min-height: var(--idea-input-min-height);
  max-height: none;
  overflow-y: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(154, 144, 128, 0.35) transparent;
}

body.cs-ai-mode .cs-ai-shell .idea-textarea:focus {
  outline: none;
  border-color: rgba(212, 132, 90, 0.55);
  box-shadow: 0 0 0 1px rgba(212, 132, 90, 0.22), 0 0 18px rgba(212, 132, 90, 0.12);
}

body.cs-ai-mode .cs-ai-shell .idea-textarea::-webkit-scrollbar {
  width: 5px;
}

body.cs-ai-mode .cs-ai-shell .idea-textarea::-webkit-scrollbar-thumb {
  background: rgba(154, 144, 128, 0.35);
  border-radius: 999px;
}

body.cs-ai-mode .cs-ai-shell .idea-textarea::-webkit-scrollbar-track {
  background: transparent;
}

.cs-ai-artifact {
  margin-top: 0.65rem;
  padding: 0.65rem 0.75rem;
  border-radius: 0.75rem;
  border: 1px solid rgba(120, 160, 220, 0.22);
  background: rgba(0, 0, 0, 0.22);
}

.cs-ai-artifact__label {
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  opacity: 0.6;
  margin-bottom: 0.35rem;
}

.cs-ai-artifact__title {
  font-weight: 600;
  margin: 0 0 0.25rem;
  font-size: 0.95rem;
}

.cs-ai-artifact__meta {
  font-size: 0.82rem;
  opacity: 0.75;
  margin: 0;
}

.cs-ai-artifact__link {
  display: inline-block;
  margin-top: 0.45rem;
  font-size: 0.85rem;
  color: var(--accent, #9ec5ff);
  text-decoration: none;
}

.cs-ai-artifact__link:hover {
  text-decoration: underline;
}

.cs-ai-artifact--song {
  border-color: rgba(200, 120, 180, 0.28);
  display: flex;
  gap: 0.75rem;
  align-items: center;
  text-decoration: none;
  color: inherit;
  padding: 0.55rem 0.7rem;
  transition: border-color 0.15s ease, background 0.15s ease;
}

a.cs-ai-artifact--song:hover {
  border-color: rgba(220, 150, 200, 0.45);
  background: rgba(0, 0, 0, 0.32);
  text-decoration: none;
}

.cs-ai-artifact__cover {
  flex: 0 0 auto;
  width: 3.35rem;
  height: 3.35rem;
  border-radius: 0.45rem;
  object-fit: cover;
  background: rgba(255, 255, 255, 0.06);
}

.cs-ai-artifact__body {
  min-width: 0;
  flex: 1 1 auto;
}

.cs-ai-artifact--song .cs-ai-artifact__link {
  margin-top: 0.2rem;
}

.cs-ai-artifact--image {
  display: block;
  max-width: min(100%, 18rem);
}

.cs-ai-artifact__image {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 0.55rem;
  margin-top: 0.4rem;
  background: rgba(0, 0, 0, 0.25);
}

.cs-ai-artifact--dna,
.cs-ai-artifact--lyrics,
.cs-ai-artifact--analysis {
  border-color: rgba(100, 180, 220, 0.26);
}

.cs-ai-status {
  text-align: center;
  font-size: 0.92rem;
  /* Slightly lighter warm silver so the dimmed/pulsing status stays ≥ 4.5:1 */
  color: #b8ae9e;
  opacity: 0.82;
  min-height: 1.35rem;
  transition: opacity 0.25s ease;
}

.cs-ai-status.is-waiting {
  opacity: 0.9;
  animation: cs-ai-wait-pulse 1.6s ease-in-out infinite;
}

@keyframes cs-ai-wait-pulse {
  /* Keep the dimmest frame readable (≥ 4.5:1); the pulse stays visible. */
  0%,
  100% {
    opacity: 0.72;
  }
  50% {
    opacity: 0.95;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cs-ai-status.is-waiting {
    animation: none;
  }
}

.cs-ai-composer {
  flex-shrink: 0;
  width: 100%;
  padding-bottom: 0.15rem;
}

/* Start: host does not scroll — StartLayout owns geometry */
body.cs-ai-mode:not(.cs-ai-chatting) .screen-host.is-start-screen,
body.cs-ai-mode:not(.cs-ai-chatting) .screen-host.cs-ai-host {
  overflow: hidden !important;
  overflow-y: hidden !important;
}

/* Music stays discoverable — covers stay crisp and clickable (no wash-out under blur) */
body.cs-ai-chatting #song-wall-root {
  opacity: 1;
  pointer-events: auto;
}

/* Chat layout: single column — thread scrolls, composer pinned */
body.cs-ai-chatting .app-root {
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

body.cs-ai-chatting .screen-host.cs-ai-host,
body.cs-ai-chatting .screen-host[data-layout='chat'] {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden !important;
  overflow-y: hidden !important;
  justify-content: flex-start;
  /* Thread fades under the wall — no hard top cut */
  padding: 0 1.25rem 0;
  padding-bottom: 0;
}

body.cs-ai-chatting .cs-ai-shell--chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  height: 100%;
  max-width: min(42rem, 100%);
  margin: 0 auto;
  /* Room for fixed composer sitting on the chrome line */
  padding-bottom: calc(var(--cs-ai-chrome-bottom) + 5.25rem);
  box-sizing: border-box;
  transition: padding-bottom 0.28s ease;
}

body.cs-ai-chatting .cs-ai-shell--chat .cs-ai-thread:not([hidden]) {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  margin: 0 0 0.55rem;
  /* Scroll content up under covers; fade instead of a hard edge */
  padding-top: calc(var(--song-wall-height, 6.5rem) + 1.85rem);
  -webkit-mask-image: linear-gradient(
    to bottom,
    transparent 0,
    transparent calc(var(--song-wall-height, 6.5rem) * 0.28),
    rgba(0, 0, 0, 0.45) calc(var(--song-wall-height, 6.5rem) * 0.72),
    #000 calc(var(--song-wall-height, 6.5rem) + 0.35rem),
    #000 100%
  );
  mask-image: linear-gradient(
    to bottom,
    transparent 0,
    transparent calc(var(--song-wall-height, 6.5rem) * 0.28),
    rgba(0, 0, 0, 0.45) calc(var(--song-wall-height, 6.5rem) * 0.72),
    #000 calc(var(--song-wall-height, 6.5rem) + 0.35rem),
    #000 100%
  );
}

body.cs-ai-chatting .cs-ai-shell--chat .cs-ai-composer {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  width: min(42rem, calc(100% - 2.5rem));
  bottom: var(--cs-ai-chrome-bottom);
  z-index: 360;
  margin: 0;
  padding: 0.35rem 0 0.15rem;
  box-sizing: border-box;
  transition: bottom 0.28s ease;
}

/*
 * Mobile chrome band (≤767px; desktop proportional layout untouched).
 * Bottom-up: footer line (© · Songs) → FAB row + DA/EN switch → composer.
 * Space is reserved explicitly so nothing overlaps; z-index is unchanged.
 */
@media (max-width: 767px) {
  body.cs-ai-mode {
    --cs-fab-size: 3.25rem;
    --cs-fab-gap: 0.5rem;
    --cs-fab-inset: 0.75rem;
    /* Footer text line incl. its bottom padding sits below this offset */
    --cs-chrome-row-bottom: calc(1.85rem + env(safe-area-inset-bottom, 0px));
    --cs-chrome-row-top: calc(var(--cs-chrome-row-bottom) + var(--cs-fab-size));
    /* Start screen: StartLayout keeps the composer above the FAB row (read by UiScale) */
    --start-bottom-reserve-mobile: calc(var(--cs-chrome-row-top) + 0.75rem);
  }

  body.cs-ai-mode .contact-fab,
  body.cs-ai-mode .team-chat-fab,
  body.cs-ai-mode .donate-fab,
  body.cs-ai-mode .links-panel-fab {
    bottom: var(--cs-chrome-row-bottom);
    width: var(--cs-fab-size);
    height: var(--cs-fab-size);
  }

  body.cs-ai-mode .contact-fab {
    left: var(--cs-fab-inset);
  }

  body.cs-ai-mode:has(.contact-fab) .team-chat-fab {
    left: calc(var(--cs-fab-inset) + (var(--cs-fab-size) + var(--cs-fab-gap)) * 1);
  }

  body.cs-ai-mode:has(.team-chat-fab) .donate-fab {
    left: calc(var(--cs-fab-inset) + (var(--cs-fab-size) + var(--cs-fab-gap)) * 2);
  }

  body.cs-ai-mode:has(.donate-fab) .links-panel-fab {
    left: calc(var(--cs-fab-inset) + (var(--cs-fab-size) + var(--cs-fab-gap)) * 3);
  }

  /* DA/EN shares the FAB row (vertically centred) instead of the footer text line */
  body.cs-ai-mode .app-toolbar {
    bottom: calc(var(--cs-chrome-row-bottom) + (var(--cs-fab-size) - var(--toolbar-h, 2rem)) / 2);
  }

  /* Chat: composer rests just above the FAB row */
  body.cs-ai-mode.cs-ai-chatting,
  body.cs-ai-mode.cs-ai-chatting.cs-ai-thread-long {
    --cs-ai-chrome-bottom: calc(var(--cs-chrome-row-top) + 0.6rem);
  }

  /* Short phones: the start logo yields height so the composer stays in view */
  body.cs-ai-mode .brand-logo.logo-start {
    width: clamp(4.5rem, calc(100svh - 31rem), 12.5rem);
    height: clamp(4.5rem, calc(100svh - 31rem), 12.5rem);
  }
}

/* Narrow phones (320–380px): four FABs + DA/EN fit on one row with ≥44px targets */
@media (max-width: 380px) {
  body.cs-ai-mode {
    --cs-fab-size: 2.85rem;
    --cs-fab-gap: 0.45rem;
  }

  /* Reserve room for the send button on its own side only, so the field is wide
     enough that the placeholder does not wrap to four lines. */
  body.cs-ai-mode .cs-ai-shell .idea-input-group {
    width: calc(100% - (var(--idea-send-btn-size) + var(--idea-send-btn-gap) + 0.35rem));
    max-width: calc(100% - (var(--idea-send-btn-size) + var(--idea-send-btn-gap) + 0.35rem));
    margin-left: 0;
    margin-right: auto;
  }
}

/* No-JS fallback (only rendered inside <noscript>) */
.noscript-intro {
  position: relative;
  z-index: 2;
  max-width: 34rem;
  margin: 0 auto;
  padding: 18vh 1.5rem 6rem;
  text-align: center;
  color: var(--text, #ebe4d8);
  line-height: 1.55;
}

.noscript-intro h1 {
  margin: 0 0 0.75rem;
  font-size: clamp(1.75rem, 6vw, 2.5rem);
  font-weight: 600;
}

.noscript-intro p {
  margin: 0 0 1rem;
}

.noscript-intro a {
  color: var(--accent, #d4845a);
}
