.page-chat {
  --bg-elev: rgba(16, 18, 34, 0.88);
  --bg-elev-2: rgba(28, 30, 54, 0.9);
  --border: rgba(var(--ink-rgb), 0.1);
  --text-dim: #a3a9c7;
  --text-faint: #6c7293;
  --accent: #39e0ff;
  --accent-2: #ff4fd8;
  --user-bubble: linear-gradient(135deg, #3b5bff, #8b5cff);
  --bot-bubble: rgba(14, 16, 30, 0.86);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  --maxw: min(1100px, 94vw);
}

.page-chat main { padding-top: var(--header-h); min-height: 0; }

.app {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - var(--header-h));
  max-width: 100%;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px clamp(14px, 4vw, 28px);
  border-bottom: 1px solid var(--border);
  background: rgba(var(--bg-rgb), 0.7);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo {
  font-size: 26px;
  filter: drop-shadow(0 2px 6px rgba(139, 92, 255, 0.4));
}
.brand-text { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.brand-text strong {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: 0.2px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand-sub { font-size: 12px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.topbar-actions { display: flex; align-items: center; gap: 10px; }
.model-badge {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  padding: 5px 10px;
  border-radius: 8px;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-ghost {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 13px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.btn-ghost:hover { background: var(--bg-elev-2); border-color: rgba(var(--ink-rgb), 0.22); }
.btn-ghost:active { transform: scale(0.97); }

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px clamp(12px, 4vw, 28px) 12px;
  scroll-behavior: smooth;
}

.msg-row { width: 100%; max-width: var(--maxw); margin: 0 auto 20px; display: flex; gap: 12px; align-items: flex-start; }
.msg-row.user { flex-direction: row-reverse; }

.avatar {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 17px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
}
.avatar.bot { background: linear-gradient(135deg, rgba(57, 224, 255, 0.16), rgba(139, 92, 255, 0.22)); }

.bubble {
  position: relative;
  padding: 16px 20px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.65;
  box-shadow: var(--shadow);
  word-wrap: break-word;
  overflow-wrap: break-word;
  min-width: 0;
}
.msg-row.user .bubble {
  max-width: min(72%, 520px);
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 6px;
}
.msg-row.bot .bubble {
  flex: 1;
  max-width: calc(100% - 48px);
  background: var(--bot-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

/* Markdown content */
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 6px 0 8px; line-height: 1.3; }
.bubble h1 { font-size: 20px; }
.bubble h2 { font-size: 18px; }
.bubble h3 { font-size: 16px; }
.bubble ul, .bubble ol { margin: 8px 0 12px; padding-left: 22px; }
.bubble li { margin: 4px 0; }
.bubble a { color: var(--accent); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble code {
  background: rgba(var(--ink-rgb), 0.08);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.bubble pre { background: rgba(var(--ink-rgb), 0.05); border: 1px solid var(--border); padding: 12px; border-radius: 6px; overflow-x: auto; }
.bubble pre code { background: none; padding: 0; }
.bubble img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  border: 1px solid var(--border);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
  background: var(--bg-elev-2);
}

/* ---- Movie list cards (trending, top 10, etc.) ---- */
.bubble ol.movie-list {
  list-style: none;
  padding: 0;
  margin: 14px 0 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.bubble li.movie-card {
  display: grid;
  grid-template-columns: 36px 1fr 108px;
  grid-template-areas: "rank body poster";
  gap: 0 18px;
  align-items: start;
  margin: 0;
  padding: 16px 18px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.movie-rank {
  grid-area: rank;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  background: rgba(139, 92, 255, 0.12);
  border-radius: 8px;
  align-self: start;
}

.movie-body {
  grid-area: body;
  min-width: 0;
}

.movie-title {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.35;
  margin-bottom: 5px;
  color: var(--text);
}

.movie-rating {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 6px;
}

.movie-overview {
  font-size: 14px;
  line-height: 1.55;
  color: var(--text-dim);
}

.movie-poster {
  grid-area: poster;
  width: 108px;
  height: 162px;
  object-fit: cover;
  border-radius: 6px;
  justify-self: end;
  align-self: start;
}

/* ---- Cast / crew / logo grid ---- */
.bubble ul.cast-grid {
  list-style: none;
  padding: 0;
  margin: 12px 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.bubble li.cast-card {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 8px;
  padding: 12px 10px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.cast-photo {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 50%;
  border: 2px solid var(--border);
}

.cast-name {
  font-size: 12.5px;
  line-height: 1.35;
  color: var(--text-dim);
}

/* Single-movie hero poster */
.bubble .hero-poster {
  display: block;
  max-width: 200px;
  width: 100%;
  height: auto;
  margin: 10px 0 0;
}

/* Poster + trailer side-by-side for movie detail replies */
.bubble .movie-media {
  display: grid;
  grid-template-columns: minmax(130px, 200px) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
  width: 100%;
  margin: 16px 0;
}

.bubble .movie-media .hero-poster {
  margin: 0;
  max-width: 100%;
}

.bubble .movie-media .youtube-embed {
  margin: 0;
  width: 100%;
  max-width: none;
}

/* YouTube trailer embeds */
.bubble .youtube-embed {
  position: relative;
  display: block;
  clear: both;
  width: 100%;
  max-width: min(720px, 100%);
  margin: 16px 0;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #000;
  border: 1px solid var(--border);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

.bubble .youtube-embed--featured {
  max-width: min(800px, 100%);
}

.bubble.bubble--rich {
  max-width: min(calc(100% - 48px), 920px);
}

.bubble .youtube-embed iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

.bubble hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.bubble table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 13px; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 9px; text-align: left; }

/* Tool badges */
.tools {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}
.tool-badge {
  font-size: 11px;
  color: var(--accent);
  background: rgba(139, 92, 255, 0.1);
  border: 1px solid rgba(139, 92, 255, 0.3);
  padding: 2px 9px;
  border-radius: 8px;
  font-weight: 500;
}
.tools-label { font-size: 11px; color: var(--text-faint); align-self: center; margin-right: 2px; }

/* Typing indicator */
.typing { display: inline-flex; gap: 5px; padding: 4px 2px; }
.typing span {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.error-text { color: var(--accent-2); }

/* ---------- Welcome / empty state ---------- */
.welcome {
  max-width: var(--maxw);
  margin: 6vh auto 0;
  text-align: center;
  padding: 20px;
  animation: fadeUp 0.5s ease both;
}
.welcome-orb { display: grid; place-items: center; margin: 0 auto 14px; width: 96px; height: 96px; border-radius: 50%; background: radial-gradient(circle, rgba(139, 92, 255, 0.35), transparent 70%); }
.welcome-orb img { filter: drop-shadow(0 0 18px rgba(139, 92, 255, 0.7)); }
.welcome h1 { margin: 6px 0 10px; font-size: clamp(26px, 4vw, 38px); font-weight: 700; }
.welcome p { color: var(--text-dim); margin: 0 auto 22px; max-width: 460px; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.chip {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 15px;
  border-radius: 8px;
  font-size: 13.5px;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.15s, background 0.15s;
}
.chip:hover { border-color: var(--accent); background: var(--bg-elev-2); transform: translateY(-2px); }

/* ---------- Composer ---------- */
.composer {
  border-top: 1px solid var(--border);
  background: rgba(var(--bg-rgb), 0.85);
  backdrop-filter: blur(12px);
  padding: 14px clamp(12px, 4vw, 28px) calc(10px + env(safe-area-inset-bottom));
}
.composer-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 8px 8px 16px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer-inner:focus-within {
  border-color: rgba(139, 92, 255, 0.6);
  box-shadow: 0 0 0 3px rgba(139, 92, 255, 0.15);
}
#input {
  flex: 1;
  min-width: 0;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  line-height: 1.5;
  max-height: 180px;
  overflow-y: hidden;
  padding: 7px 0;
}
#input::placeholder { color: var(--text-faint); }

.send-btn {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  border: none;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: #0b0f1a;
  background: linear-gradient(135deg, var(--cyan), var(--violet) 55%, var(--pink));
  transition: transform 0.1s, opacity 0.15s, filter 0.15s;
}
.send-btn:hover:not(:disabled) { filter: brightness(1.08); }
.send-btn:active:not(:disabled) { transform: scale(0.92); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.composer-hint {
  max-width: var(--maxw);
  margin: 8px auto 0;
  text-align: center;
  font-size: 11.5px;
  color: var(--text-faint);
}

@keyframes fadeUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
.msg-row { animation: fadeUp 0.35s ease both; }

/* Scrollbar */
.messages::-webkit-scrollbar { width: 10px; }
.messages::-webkit-scrollbar-thumb { background: rgba(var(--ink-rgb), 0.14); border-radius: 6px; border: 2px solid var(--bg); }
.messages::-webkit-scrollbar-thumb:hover { background: rgba(var(--ink-rgb), 0.24); }

/* Wide content guards: never let media/tables push horizontal scroll */
.bubble pre,
.bubble .youtube-embed { max-width: 100%; }
.bubble table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ---------- Responsive ---------- */

/* Tablets */
@media (max-width: 820px) {
  .bubble ul.cast-grid { grid-template-columns: repeat(3, 1fr); }
  .msg-row { gap: 10px; }
}

/* Phones */
@media (max-width: 600px) {
  :root { --maxw: 100%; }
  .brand-sub { display: none; }
  .btn-ghost span { display: none; }
  .btn-ghost { padding: 8px; }
  .brand-logo { font-size: 22px; }
  .brand-text strong { font-size: 17px; }
  .topbar { padding: 12px 14px; gap: 8px; }
  .msg-row { gap: 8px; margin-bottom: 16px; }
  .msg-row.user .bubble { max-width: 86%; }
  .msg-row.bot .bubble { max-width: calc(100% - 40px); padding: 12px 14px; font-size: 14.5px; }
  .avatar { width: 30px; height: 30px; font-size: 15px; }
  .bubble h1 { font-size: 18px; }
  .bubble h2 { font-size: 16.5px; }
  .bubble h3 { font-size: 15px; }
  .bubble li.movie-card {
    grid-template-columns: 30px 1fr;
    grid-template-areas:
      "rank body"
      "poster poster";
    gap: 8px 10px;
    padding: 14px;
  }
  .movie-poster {
    width: 100%;
    max-width: 130px;
    height: auto;
    aspect-ratio: 2 / 3;
    justify-self: center;
    grid-column: 1 / -1;
  }
  .bubble .hero-poster { max-width: 160px; margin-inline: auto; }
  .bubble .movie-media {
    grid-template-columns: 1fr;
    gap: 14px;
  }
  .bubble .movie-media .hero-poster {
    max-width: 180px;
    margin-inline: auto;
  }
  .bubble .youtube-embed,
  .bubble .youtube-embed--featured {
    max-width: 100%;
  }
  .bubble ul.cast-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .cast-photo { width: 64px; height: 64px; }
  .model-badge { max-width: 30vw; }
  .messages { padding: 16px 12px 10px; }
  .send-btn { width: 40px; height: 40px; }
}

/* Small phones (e.g. iPhone SE, 375px) */
@media (max-width: 380px) {
  .msg-row.bot .bubble { padding: 11px 12px; }
  .bubble ul.cast-grid { gap: 8px; }
  .cast-photo { width: 56px; height: 56px; }
  .composer-inner { padding: 6px 6px 6px 12px; }
}

@media (min-width: 900px) {
  .bubble ul.cast-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (min-width: 1400px) {
  :root { --maxw: min(1200px, 92vw); }
}

[data-theme="light"] .page-chat {
  --bg-elev: #ffffff;
  --bg-elev-2: #eef0f8;
  --text-dim: var(--muted);
  --text-faint: var(--faint);
  --bot-bubble: #ffffff;
  --shadow: 0 1px 2px rgba(18, 20, 45, 0.06), 0 10px 28px -14px rgba(18, 20, 45, 0.22);
  --accent: var(--cyan);
  --accent-2: var(--pink);
}

/* ---------- animated chat background ---------- */
.page-chat .app { position: relative; z-index: 1; }
.chat-bg { position: fixed; inset: var(--header-h) 0 0; z-index: 0; overflow: hidden; pointer-events: none; }
.aurora { position: absolute; border-radius: 50%; filter: blur(90px); opacity: 0.38; will-change: transform; }
.aurora-a { width: 46vmax; height: 46vmax; left: -14vmax; top: -16vmax; background: radial-gradient(circle, var(--cyan), transparent 65%); animation: drift-a 26s ease-in-out infinite alternate; }
.aurora-b { width: 50vmax; height: 50vmax; right: -18vmax; bottom: -22vmax; background: radial-gradient(circle, var(--violet), transparent 65%); animation: drift-b 32s ease-in-out infinite alternate; }
.aurora-c { width: 30vmax; height: 30vmax; left: 38%; top: 30%; background: radial-gradient(circle, var(--pink), transparent 65%); opacity: 0.22; animation: drift-c 22s ease-in-out infinite alternate; }
@keyframes drift-a { to { transform: translate(18vw, 14vh) scale(1.15); } }
@keyframes drift-b { to { transform: translate(-16vw, -12vh) scale(1.1); } }
@keyframes drift-c { to { transform: translate(-20vw, 16vh) scale(0.85); } }
.chat-neural { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0.5; }
.chat-scan {
  position: absolute; left: 0; right: 0; top: 0; height: 140px;
  background: linear-gradient(180deg, transparent, rgba(57, 224, 255, 0.05) 45%, rgba(57, 224, 255, 0.16) 50%, rgba(139, 92, 255, 0.05) 55%, transparent);
  animation: chat-scan 10s cubic-bezier(.55, 0, .45, 1) infinite;
}
@keyframes chat-scan { 0% { transform: translateY(-160px); opacity: 0; } 15%, 85% { opacity: 1; } 100% { transform: translateY(100vh); opacity: 0; } }
[data-theme="light"] .aurora { opacity: 0.2; }
[data-theme="light"] .chat-neural { opacity: 0.35; }

.welcome-orb { position: relative; animation: orb-float 6s ease-in-out infinite; }
.welcome-orb::before, .welcome-orb::after {
  content: ""; position: absolute; inset: 8px; border-radius: 50%; border: 2px solid rgba(139, 92, 255, 0.55);
  animation: orb-ripple 3.2s ease-out infinite;
}
.welcome-orb::after { animation-delay: 1.6s; border-color: rgba(57, 224, 255, 0.5); }
@keyframes orb-float { 50% { transform: translateY(-8px); } }
@keyframes orb-ripple { 0% { transform: scale(0.75); opacity: 0.9; } 100% { transform: scale(1.6); opacity: 0; } }

@media (max-width: 760px), (hover: none) {
  .chat-neural, .chat-scan { display: none; }
  .aurora { filter: blur(60px); }
}
