/* Chat widget (public/js/components/chat). Hosts restyle via the --chat-* tokens. */
:root {
  --chat-text: hsl(var(--foreground));
  --chat-muted: hsl(var(--muted-foreground));
  --chat-border: hsl(var(--border));
  --chat-elev: hsl(var(--secondary));
  --chat-hover: hsl(var(--foreground) / 0.06);
  --chat-card: hsl(var(--card));
  --chat-input-text: hsl(var(--foreground));
  --chat-accent: var(--primary);
  --chat-user-bubble: hsl(var(--primary) / 0.12);
  --chat-code-bg: hsl(var(--secondary));
  --chat-code-border: hsl(var(--border));
  --chat-sidebar-bg: hsl(var(--card));
  --chat-sidebar-surface: hsl(0 0% 100% / 0.72);
  --chat-sidebar-fill: hsl(var(--foreground) / 0.07);
  --chat-sidebar-hover: hsl(var(--foreground) / 0.045);
  --chat-danger: #ef4444;
  --chat-danger-bg: rgb(239 68 68 / 0.14);
  --chat-log-padding: 1rem max(1rem, calc((100% - 760px) / 2)) 1rem;
  --chat-composer-gap: 0.3rem;
  --chat-composer-min-height: 0;
  --chat-composer-padding: 0.7rem 0.8rem 0.6rem;
  --chat-composer-radius: 24px;
  --chat-composer-border: hsl(var(--border));
  --chat-composer-bg: hsl(var(--card));
  --chat-composer-shadow: 0 1px 2px hsl(var(--foreground) / 0.04), 0 10px 30px -14px hsl(var(--foreground) / 0.18);
  --chat-composer-focus-border: hsl(var(--primary) / 0.5);
  --chat-composer-aura: radial-gradient(60% 120% at 50% 100%, hsl(var(--primary) / 0.14), transparent 70%);
}
.dark {
  /* Dark glass is solid, exactly like the navbar's .dark .glass (#0A0A0A). */
  --chat-sidebar-surface: hsl(0 0% 3.9%);
  --chat-sidebar-fill: #26232B;
  --chat-sidebar-hover: #1D1A21;
  --chat-composer-bg: hsl(var(--secondary) / 0.6);
  --chat-composer-border: hsl(var(--foreground) / 0.12);
  --chat-composer-shadow: inset 0 1px 0 rgb(255 255 255 / 0.06), 0 1px 2px rgb(0 0 0 / 0.3), 0 18px 48px -20px rgb(0 0 0 / 0.7);
  --chat-composer-aura: radial-gradient(65% 130% at 50% 100%, hsl(var(--primary) / 0.24), transparent 72%);
}

/* Log + messages */
.chat-log {
  flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 1.5rem;
  padding: var(--chat-log-padding);
}
.chat-log:empty { display: none; }
.chat-day { align-self: center; flex: 0 0 auto; margin: 0.3rem 0; font-size: 0.72rem; font-weight: 500; color: var(--chat-muted); }
.chat-msg { display: flex; flex-direction: column; gap: 0.25rem; max-width: 100%; }
.chat-msg.user { align-self: flex-end; align-items: flex-end; max-width: 75%; }
.chat-msg.assistant { align-self: flex-start; align-items: flex-start; }
.chat-msg-model { padding: 0 0.25rem; font-size: 0.72rem; font-weight: 500; line-height: 1.2; color: var(--chat-muted); }
.chat-msg-copy, .chat-msg-action {
  display: inline-flex; align-items: center; justify-content: center;
  width: 1.6rem; height: 1.6rem; border-radius: 8px;
  border: none; background: transparent; color: var(--chat-muted);
  cursor: pointer; margin-right: 0.25rem;
}
.chat-msg-copy:hover, .chat-msg-action:hover, .chat-msg-action[aria-expanded="true"] { color: var(--chat-text); background: var(--chat-hover); }
.chat-msg-action { width: 2rem; height: 2rem; margin-right: 0; }
.chat-msg-action[hidden] { display: none; }
.chat-msg-action:disabled { opacity: 0.4; cursor: default; color: var(--chat-muted); background: transparent; }
@media (pointer: coarse) {
  .chat-msg-copy, .chat-msg-action { width: 2.25rem; height: 2.25rem; }
}
/* Assistant reply action bar + its overflow menu */
.chat-msg-actions { position: relative; display: flex; align-items: center; gap: 0.125rem; margin-top: 0.1rem; }
.chat-msg-menu {
  position: absolute; top: calc(100% + 4px); z-index: 30; min-width: 12rem; padding: 0.3rem;
  border-radius: 12px; border: 1px solid hsl(var(--border)); background: hsl(var(--card)); color: var(--chat-text);
  box-shadow: 0 12px 32px rgb(0 0 0 / 0.25);
}
.chat-msg-menu.is-above { top: auto; bottom: calc(100% + 4px); }
.chat-msg-menu-item {
  display: flex; align-items: center; gap: 0.6rem; width: 100%; padding: 0.45rem 0.6rem;
  border: none; border-radius: 8px; cursor: pointer; background: transparent; color: var(--chat-text);
  font: inherit; font-size: 0.85rem; text-align: left; white-space: nowrap;
}
.chat-msg-menu-item:hover, .chat-msg-menu-item:focus-visible { background: var(--chat-hover); outline: none; }
.chat-msg-menu-item svg { flex: 0 0 auto; color: var(--chat-muted); }
.chat-msg-bubble {
  padding: 0.7rem 1rem; border-radius: 18px; font-size: 1rem; line-height: 1.65;
  border: none; white-space: pre-wrap; word-break: break-word;
  overflow: auto; max-width: 100%;
}
.chat-msg.user .chat-msg-bubble { background: var(--chat-user-bubble); padding: 0.6rem 0.95rem; border-radius: 20px; }
.chat-msg.assistant .chat-msg-bubble {
  width: 100%; min-width: 0; max-width: 100%;
  padding: 0; border-radius: 0; background: transparent; box-shadow: none;
}
.chat-msg-bubble pre {
  background: var(--chat-code-bg); border: 1px solid var(--chat-code-border);
  border-radius: 12px; padding: 0.6rem 0.8rem; overflow-x: auto; font-size: 0.8rem; margin: 0.4rem 0;
  white-space: break-spaces;
}
.chat-msg-bubble pre code { white-space: break-spaces; }
.chat-msg-bubble code { font-size: 0.85em; }
.chat-msg-img { max-width: 220px; max-height: 220px; border-radius: 10px; margin-top: 0.4rem; display: block; cursor: zoom-in; }

/* Rendered markdown */
.chat-md { white-space: normal; }
.chat-md.chat-md-plain { white-space: pre-wrap; }
.chat-md p, .chat-md ul, .chat-md ol, .chat-md blockquote, .chat-md table,
.chat-md h1, .chat-md h2, .chat-md h3, .chat-md h4, .chat-md h5, .chat-md h6 { margin: 0 0 0.85em; }
.chat-md > :last-child { margin-bottom: 0; }
.chat-md h1, .chat-md h2, .chat-md h3, .chat-md h4, .chat-md h5, .chat-md h6 { font-weight: 600; line-height: 1.35; }
.chat-md h1 { font-size: 1.2em; }
.chat-md h2 { font-size: 1.1em; }
.chat-md h3 { font-size: 1em; }
.chat-md ul { list-style: disc; padding-left: 1.4em; }
.chat-md ol { list-style: decimal; padding-left: 1.4em; }
.chat-md li + li { margin-top: 0.2em; }
.chat-md li > ul, .chat-md li > ol { margin: 0.2em 0 0; }
.chat-md a { color: hsl(var(--chat-accent)); text-decoration: underline; text-underline-offset: 2px; }
.chat-md strong { font-weight: 600; }
.chat-md blockquote { border-left: 3px solid var(--chat-border); padding-left: 0.75em; color: var(--chat-muted); }
.chat-md hr { border: 0; border-top: 1px solid var(--chat-border); margin: 0.85em 0; }
.chat-md table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; }
.chat-md th, .chat-md td { border: 1px solid var(--chat-border); padding: 0.3em 0.6em; text-align: left; }
.chat-md th { font-weight: 600; }

/* Status line while a reply is in flight */
.chat-thinking { display: flex; align-items: center; gap: 0.5rem; color: var(--chat-muted); }
/* While only "thinking" (no streamed content yet): drop the bubble chrome — just the logo + text (Scotty-style). */
.chat-msg.assistant .chat-msg-bubble:has(> .chat-thinking):has(> .chat-md:empty) {
  min-width: 0; width: auto; padding: 0;
  background: transparent; box-shadow: none; border: none;
}
.chat-thinking-logo { width: 2rem; height: 2rem; flex: 0 0 auto; display: inline-flex; color: hsl(var(--primary)); }
.chat-shimmer-text {
  font-size: 0.85rem; font-weight: 500;
  background: linear-gradient(90deg, var(--chat-muted) 40%, var(--chat-text) 50%, var(--chat-muted) 60%);
  background-size: 200% 100%;
  -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: chat-shimmer 1.583s linear infinite;
}
@keyframes chat-shimmer {
  from { background-position: 150% 0; }
  to { background-position: -50% 0; }
}
@media (prefers-reduced-motion: reduce) {
  .chat-shimmer-text { animation: none; background: none; color: var(--chat-muted); }
}

/* Empty conversation: greeting + composer centred */
.chat-empty {
  flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.5rem; padding: 0 1.5rem 1.75rem; text-align: center; font-size: 0.95rem; color: var(--chat-muted);
}
.chat-empty strong { font-size: clamp(1.9rem, 3vw, 2.75rem); font-weight: 600; letter-spacing: -0.025em; line-height: 1.1; color: var(--chat-text); }
.chat-empty > span { display: none; }

.chat-starters {
  flex: 0 0 auto; display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem;
  width: 100%; max-width: calc(760px + 2rem); margin: 0.9rem auto 0; padding: 0 1rem;
}
.chat-log:not(:empty) ~ .chat-starters { display: none; }
.chat-starter {
  display: inline-flex; align-items: center; gap: 0.45rem;
  padding: 0.5rem 0.85rem; border-radius: 9999px; cursor: pointer;
  border: 1px solid hsl(var(--border)); background: hsl(var(--card)); color: var(--chat-text);
  font: inherit; font-size: 0.85rem; font-weight: 500;
  transition: border-color 0.15s ease, background-color 0.15s ease, transform 0.1s ease;
}
.chat-starter:hover { border-color: hsl(var(--primary) / 0.45); background: hsl(var(--primary) / 0.08); }
.chat-starter:active { transform: scale(0.97); }
.chat-starter svg { flex: 0 0 auto; width: 15px; height: 15px; color: hsl(var(--primary)); }
@media (prefers-reduced-motion: reduce) { .chat-starter { transition: none; } }
@media (max-width: 640px) {
  .chat-starters { flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; scrollbar-width: none; }
  .chat-starters::-webkit-scrollbar { display: none; }
  .chat-starter { flex: 0 0 auto; font-size: 0.8rem; }
  .chat-msg-bubble { font-size: 0.9rem; line-height: 1.55; }
  .chat-empty strong { font-size: 1.6rem; }
  .chat-empty { font-size: 0.85rem; }
  .chat-md h1 { font-size: 1.15em; }
  .chat-md h2 { font-size: 1.05em; }
  /* Smaller placeholder without shrinking the input itself (keeps the 16px iOS no-zoom floor). */
  .chat-composer textarea::placeholder { font-size: 0.9rem; }
}
.chat-log:not(:empty) ~ .chat-empty { display: none; }

/* Composer (mobile block sits before the textarea base rule, as it always has) */
.chat-composer-zone { position: relative; flex: 0 0 auto; width: 100%; max-width: calc(760px + 2rem); margin: 0 auto; padding: 0 1rem; }
.chat-composer-zone::before {
  content: ''; position: absolute; inset: -14% -4% -40%; z-index: -1;
  background: var(--chat-composer-aura); pointer-events: none;
}
.chat-composer {
  position: relative;
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--chat-composer-gap); min-height: var(--chat-composer-min-height);
  padding: var(--chat-composer-padding); border-radius: var(--chat-composer-radius);
  border: 1px solid var(--chat-composer-border); background: var(--chat-composer-bg);
  box-shadow: var(--chat-composer-shadow);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.chat-composer:focus-within {
  border-color: var(--chat-composer-focus-border);
}
@media (prefers-reduced-motion: reduce) { .chat-composer { transition: none; } }
@media (max-width: 640px) {
  .chat-composer textarea { min-height: 40px; padding: 0.55rem 0.5rem; }
}
.chat-composer textarea {
  min-height: 44px; max-height: 200px; resize: none;
  line-height: 1.45; padding: 0.7rem 0.6rem; overflow-y: auto; scrollbar-width: thin;
  border-radius: 14px; background: transparent;
  border: none; color: var(--chat-input-text); outline: none;
  font-family: inherit; font-size: 0.9rem;
}
.chat-composer > textarea { flex: 1 1 100%; min-width: 0; padding: 0.35rem 0.45rem 0.5rem; min-height: 2.5rem; }
@media (max-width: 640px) {
  /* 16px floor stops iOS Safari zooming the page on focus. */
  .chat-composer textarea { font-size: 16px; }
}
.chat-composer > .chat-send {
  flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center;
  width: 2.25rem; height: 2.25rem; padding: 0; border-radius: 9999px; border: none; cursor: pointer;
  background: hsl(var(--chat-accent)); color: hsl(var(--primary-foreground)); font: inherit; font-size: 14px; font-weight: 500;
  box-shadow: 0 6px 16px -6px hsl(var(--primary) / 0.65);
  transition: filter 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}
.chat-composer > .chat-send svg { width: 18px; height: 18px; }
.chat-composer > .chat-send:hover { filter: brightness(1.06); }
.chat-composer > .chat-send:active { transform: scale(0.94); }
@media (prefers-reduced-motion: reduce) { .chat-composer > .chat-send { transition: none; } }
.chat-composer > .chat-send:disabled { opacity: 0.5; filter: none; cursor: default; }
.chat-composer > .chat-send.is-stop { background: var(--chat-text); color: hsl(var(--background)); }
.chat-composer > .chat-send.is-stop svg { width: 24px; height: 24px; }
.chat-composer:has(> .chat-attachments:not([hidden])) { flex-wrap: wrap; }
.chat-composer > .chat-attachments { flex: 1 1 100%; }
.chat-composer > .chat-attachments[hidden] { display: none; }
.chat-composer .chat-attach {
  flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center;
  width: 2.25rem; height: 2.25rem; padding: 0; border: none; border-radius: 9999px; cursor: pointer;
  background: transparent; color: var(--chat-muted); font-size: 1.4rem; line-height: 1; font-weight: 300;
}
.chat-composer .chat-attach:hover { background: var(--chat-hover); color: var(--chat-text); }
.chat-spacer { flex: 1 1 auto; }

/* Composer tools: icon buttons, cost badge, settings popover */
.chat-tool {
  flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center;
  width: 2.25rem; height: 2.25rem; border-radius: 9999px; border: none; cursor: pointer;
  background: transparent; color: var(--chat-muted); font-size: 1.25rem; line-height: 1;
}
.chat-tool:hover, .chat-tool[aria-expanded="true"] { color: var(--chat-text); background: var(--chat-hover); }
.chat-tool[hidden], .chat-cost[hidden], .chat-settings[hidden] { display: none; }
.chat-composer > .chat-tool {
  width: 2.25rem; height: 2.25rem; border-radius: 12px;
  border: 1px solid hsl(var(--border)); background: hsl(var(--secondary) / 0.5);
  transition: border-color 0.15s ease, background-color 0.15s ease;
}
.chat-composer > .chat-tool:hover, .chat-composer > .chat-tool[aria-expanded="true"] {
  color: var(--chat-text); border-color: hsl(var(--primary) / 0.45); background: hsl(var(--primary) / 0.08);
}
@media (prefers-reduced-motion: reduce) { .chat-composer > .chat-tool { transition: none; } }
.chat-cost {
  flex: 0 0 auto; display: inline-flex; align-items: center; gap: 0.15rem; height: 2.25rem; margin-right: 0.5rem; padding: 0 0.75rem 0 0.6rem;
  border-radius: 9999px; background: hsl(var(--primary) / 0.1); color: hsl(var(--primary));
  font-size: 0.875rem; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums;
}
.chat-cost svg { flex: 0 0 auto; width: 16px; height: 16px; }
.chat-settings {
  position: absolute; left: 1rem; bottom: calc(100% + 10px); z-index: 30;
  width: min(340px, calc(100% - 2rem)); max-height: min(65vh, 480px); overflow-y: auto;
  padding: 0.6rem 0.75rem 0.7rem; border-radius: 14px; border: 1px solid hsl(var(--border));
  background: hsl(var(--card));
  box-shadow: 0 1px 2px hsl(var(--foreground) / 0.05), 0 12px 32px -16px hsl(var(--foreground) / 0.28);
}
.chat-settings.is-below { bottom: auto; top: calc(100% + 10px); }
.chat-settings-head {
  display: flex; align-items: center; justify-content: space-between;
  margin: 0 0 0.6rem; padding: 0; border-bottom: none;
  font-size: 0.78rem; font-weight: 600; color: var(--chat-muted);
}
.chat-settings-head .chat-tool { width: 1.6rem; height: 1.6rem; font-size: 1rem; }
.chat-settings [data-help-trigger] { display: none; }
.chat-settings [data-form-body="basic"] > * + * { margin-top: 0.55rem !important; }
.chat-settings [data-form-body="basic"] label { font-size: 0.72rem !important; font-weight: 500 !important; color: var(--chat-muted) !important; }
.chat-settings [data-form-body="basic"] .mb-2, .chat-settings [data-form-body="basic"] .mb-2\.5 { margin-bottom: 0.3rem !important; }
.chat-settings [data-form-body="basic"] textarea { background: transparent !important; border: none !important; min-height: 2.5rem !important; height: 2.75rem; max-height: 4rem; }
.chat-settings [data-component="textarea"] div:has(> textarea) { background: transparent !important; border-color: hsl(var(--border)) !important; padding: 0.5rem 0.6rem !important; }
.chat-settings [data-component="textarea"] div:has(> textarea):focus-within { border-color: hsl(var(--primary) / 0.5) !important; }
/* Empty state centres the composer, so an open settings panel would collide with the greeting — hide it while open. */
.chat-main:has(.chat-settings:not([hidden])) > .chat-empty,
.chat-main:has(.chat-settings:not([hidden])) > .chat-starters { visibility: hidden; }

/* Model picker: composer trigger + searchable popover */
.chat-model-picker { position: relative; flex: 0 1 auto; min-width: 0; }
.chat-model-picker-trigger {
  display: inline-flex; align-items: center; gap: 0.45rem; max-width: 100%; height: 2.25rem; padding: 0 0.7rem 0 0.45rem;
  border: 1px solid hsl(var(--border)); border-radius: 12px; cursor: pointer;
  background: hsl(var(--secondary) / 0.5); color: var(--chat-text);
  font: inherit; font-size: 0.8rem; font-weight: 600;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}
.chat-model-picker-trigger:hover, .chat-model-picker-trigger[aria-expanded="true"] { border-color: hsl(var(--primary) / 0.45); background: hsl(var(--primary) / 0.08); }
@media (prefers-reduced-motion: reduce) { .chat-model-picker-trigger { transition: none; } }
.chat-model-picker-trigger svg { flex: 0 0 auto; }
.chat-model-picker-label { min-width: 0; max-width: 14rem; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-model-picker-icon {
  flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center; overflow: hidden;
  width: 1.4rem; height: 1.4rem; border-radius: 6px; background: var(--chat-elev);
  font-size: 0.7rem; font-weight: 600; color: var(--chat-text);
}
.chat-model-picker-icon img { width: 100%; height: 100%; object-fit: cover; display: block; }
.chat-model-picker-glyph {
  width: 68%; height: 68%; background-color: currentColor;
  -webkit-mask: var(--glyph) center / contain no-repeat; mask: var(--glyph) center / contain no-repeat;
}
.chat-model-picker-pop {
  position: absolute; left: 0; bottom: calc(100% + 8px); z-index: 30;
  display: flex; flex-direction: column; width: min(360px, calc(100vw - 2rem)); max-height: 440px;
  border-radius: 16px; border: 1px solid hsl(var(--border)); background: hsl(var(--card)); color: var(--chat-text);
  box-shadow: 0 12px 32px rgb(0 0 0 / 0.25); overflow: hidden;
}
.chat-model-picker-pop[hidden] { display: none; }
.chat-model-picker-pop.is-below { bottom: auto; top: calc(100% + 8px); }
.chat-model-picker-search {
  flex: 0 0 auto; display: flex; align-items: center; gap: 0.5rem; margin: 0.5rem; padding: 0 0.65rem;
  height: 2.25rem; border-radius: 10px; background: var(--chat-hover); color: var(--chat-muted);
}
.chat-model-picker-search input {
  flex: 1 1 auto; min-width: 0; height: 100%; border: none; outline: none; background: transparent;
  color: var(--chat-text); font: inherit; font-size: 0.85rem;
}
.chat-model-picker-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 0 0.35rem 0.4rem; }
.chat-model-picker-item {
  display: flex; align-items: center; gap: 0.6rem; width: 100%; padding: 0.4rem 0.5rem;
  border: none; border-radius: 10px; cursor: pointer; background: transparent; color: var(--chat-text);
  font: inherit; text-align: left;
}
.chat-model-picker-item:hover, .chat-model-picker-item.is-active { background: var(--chat-hover); }
.chat-model-picker-item.is-selected { background: var(--chat-sidebar-fill); }
.chat-model-picker-item .chat-model-picker-icon { width: 2rem; height: 2rem; border-radius: 8px; }
.chat-model-picker-text { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.chat-model-picker-name, .chat-model-picker-id { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-model-picker-name { font-size: 0.85rem; font-weight: 500; }
.chat-model-picker-id { font-size: 0.72rem; color: var(--chat-muted); }
.chat-model-picker-credits { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 0.2rem; font-size: 0.72rem; font-weight: 600; color: var(--chat-muted); }
.chat-model-picker-status { padding: 0.75rem 1rem; font-size: 0.8rem; color: var(--chat-muted); }
.chat-model-picker-status[hidden] { display: none; }
@media (max-width: 640px) {
  /* Zero basis keeps the tool row on one line so cost + send never wrap; the label ellipsizes instead. */
  .chat-model-picker { flex: 1 1 0; }
  .chat-composer:has(> .chat-model-picker) > .chat-spacer { display: none; }
  .chat-model-picker-label { max-width: none; }
  .chat-model-picker-trigger .chat-model-picker-icon { display: none; }
  /* Anchor the list to the composer zone so it spans the composer instead of overflowing from the trigger. */
  .chat-model-picker { position: static; }
  .chat-model-picker-pop { left: 1rem; right: 1rem; width: auto; }
  .chat-cost { padding: 0 0.55rem 0 0.4rem; margin-right: 0.25rem; }
}

/* Pending attachments */
.chat-attachments { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.chat-thumb { position: relative; width: 56px; height: 56px; border-radius: 10px; overflow: hidden; border: 1px solid hsl(var(--border)); background: hsl(var(--secondary) / 0.8); }
.chat-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.chat-thumb.loading { display: flex; align-items: center; justify-content: center; font-size: 0.65rem; color: hsl(var(--muted-foreground)); }
.chat-thumb-remove {
  position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; line-height: 16px; text-align: center;
  border-radius: 9999px; border: none; cursor: pointer; font-size: 12px; padding: 0;
  background: hsl(0 0% 0% / 0.6); color: #fff;
}

/* "Jump to latest" pill, anchored to the composer zone */
.chat-jump {
  position: absolute; bottom: calc(100% + 10px); left: 50%;
  z-index: 5; display: inline-flex; align-items: center; gap: 0.4rem;
  height: 2.25rem; padding: 0 0.85rem; border-radius: 9999px; cursor: pointer;
  border: 1px solid var(--chat-border); background: var(--chat-elev); color: var(--chat-text);
  font-family: 'Inter', sans-serif; font-size: 0.78rem; font-weight: 600;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.25);
  animation: chat-jump-in 0.2s ease;
  transform: translateX(-50%);
}
.chat-jump[hidden] { display: none; }
.chat-jump:hover { filter: brightness(1.15); }
.chat-jump .chat-jump-label { display: none; }
.chat-jump.fresh .chat-jump-label { display: inline; }
.chat-jump.fresh { border-color: hsl(var(--chat-accent) / 0.45); }
.chat-jump.fresh svg { color: hsl(var(--chat-accent)); }
@keyframes chat-jump-in {
  from { opacity: 0; transform: translate(-50%, 6px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
@media (prefers-reduced-motion: reduce) { .chat-jump { animation: none; } }

/* Shell: conversation list + chat. Desktop: 56px rail, hover overlays the full list, the toggle pins it. */
.chat-shell {
  position: relative; display: grid; grid-template-columns: minmax(0, 1fr); gap: 0;
  height: 100%; min-height: 0; overflow: hidden;
  container: chatshell / inline-size;
}
.chat-main {
  position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0;
  padding-bottom: 0.75rem;
  background: radial-gradient(60% 45% at 50% 40%, hsl(var(--primary) / 0.09), transparent 70%);
}
.chat-main:has(.chat-log:empty) { justify-content: center; }
/* Pin the vertical order with flex `order`: a model switch rebuilds log/empty/composer and
   re-appends them after the injected starters, which would otherwise jump to the top. */
.chat-main > .chat-head { order: 0; }
.chat-main > .chat-log { order: 1; }
.chat-main > .chat-empty { order: 2; }
.chat-main > .chat-composer-zone { order: 3; }
.chat-main > .chat-starters { order: 4; }
.chat-head { flex: 0 0 auto; display: flex; align-items: center; padding: 0.5rem 0.75rem 0.25rem; }
.chat-head .chat-sidebar-toggle { width: 2.25rem; height: 2.25rem; border-radius: 10px; background: var(--chat-hover); color: var(--chat-text); }
.chat-sidebar {
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
  border: none; border-right: 1px solid hsl(var(--border)); border-radius: 0;
  background: var(--chat-sidebar-surface);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
}
.chat-sidebar-head { flex: 0 0 auto; display: flex; flex-direction: column; align-items: flex-start; gap: 8px; padding: 10px 8px 2px; }
.chat-sidebar-head .chat-sidebar-toggle { width: 44px; height: 40px; border-radius: 10px; }
.chat-sidebar-toggle .chat-toggle-close, .chat-shell.is-sidebar-open .chat-sidebar-toggle .chat-toggle-open { display: none; }
.chat-shell.is-sidebar-open .chat-sidebar-toggle .chat-toggle-close { display: block; }
.chat-sidebar-new {
  display: flex; align-items: center; gap: 10px; width: 100%; height: 40px; padding: 0 12px;
  border: none; border-radius: 10px; cursor: pointer; overflow: hidden;
  background: hsl(var(--primary) / 0.12); color: hsl(var(--primary)); text-align: left;
  font: inherit; font-size: 14px; font-weight: 600;
  transition: background-color 0.15s ease;
}
.chat-sidebar-new svg { flex: 0 0 auto; }
.chat-sidebar-new-label { white-space: nowrap; }
.chat-sidebar-new:hover { background: hsl(var(--primary) / 0.18); }
@media (prefers-reduced-motion: reduce) { .chat-sidebar-new { transition: none; } }
.chat-sidebar-list { flex: 0 1 auto; min-height: 0; overflow-y: auto; padding: 0 6px 8px; display: flex; flex-direction: column; gap: 0; }
.chat-sidebar-list::before {
  content: 'Chats'; display: block; padding: 18px 11px 8px;
  font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--chat-muted);
}
.chat-sidebar-item {
  display: flex; align-items: center; width: 100%; height: 40px; flex: 0 0 40px;
  border-radius: 10px; background: transparent; color: var(--chat-text);
}
.chat-sidebar-item:hover { background: var(--chat-sidebar-hover); }
.chat-sidebar-item.is-active { background: var(--chat-sidebar-fill); }
.chat-sidebar-open {
  flex: 1 1 auto; align-self: stretch; min-width: 0;
  display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 0.15rem;
  padding: 0 11px; border: none; border-radius: inherit; cursor: pointer; text-align: left;
  background: transparent; color: inherit; font: inherit;
}
.chat-sidebar-del, .chat-sidebar-confirm button {
  flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; padding: 0; border: none; border-radius: 6px; cursor: pointer;
  background: transparent; color: var(--chat-muted);
}
.chat-sidebar-del { margin-right: 4px; }
.chat-sidebar-del:hover, .chat-sidebar-confirm-no:hover { color: var(--chat-text); background: var(--chat-hover); }
.chat-sidebar-confirm { flex: 1 1 auto; min-width: 0; display: flex; align-items: center; gap: 2px; padding: 0 4px 0 9px; }
.chat-sidebar-confirm-label { flex: 1 1 auto; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 14px; line-height: 20px; color: var(--chat-text); }
.chat-sidebar-confirm .chat-sidebar-confirm-yes { color: var(--chat-danger); }
.chat-sidebar-confirm .chat-sidebar-confirm-yes:hover { background: var(--chat-danger-bg); }
.chat-sidebar-confirm button:disabled { cursor: default; }
.chat-sidebar-item.is-confirming .chat-sidebar-open, .chat-sidebar-item.is-confirming .chat-sidebar-del { display: none; }
.chat-sidebar-item.is-pending { opacity: 0.55; pointer-events: none; }
@media (hover: hover) {
  .chat-sidebar-item:not(:hover):not(:focus-within) .chat-sidebar-del { display: none; }
}
@media (hover: none) {
  .chat-sidebar-del, .chat-sidebar-confirm button { width: 32px; height: 32px; }
}
.chat-sidebar-title { width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 14px; font-weight: 400; line-height: 20px; color: hsl(var(--foreground) / 0.92); }
.chat-sidebar-time { display: none; }
.chat-sidebar-item:has(.chat-sidebar-subtitle) { height: auto; flex-basis: auto; min-height: 40px; }
.chat-sidebar-item:has(.chat-sidebar-subtitle) .chat-sidebar-open { padding: 5px 9px; gap: 0; }
.chat-sidebar-subtitle { width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 12px; line-height: 16px; color: var(--chat-muted); }
.chat-sidebar-empty, .chat-sidebar-error { padding: 8px 15px; font-size: 13px; color: var(--chat-muted); }
.chat-sidebar-empty[hidden], .chat-sidebar-error[hidden], .chat-sidebar-more[hidden] { display: none; }
.chat-sidebar-more {
  flex: 0 0 auto; margin: 0 6px 8px; height: 32px; padding: 0 9px; border-radius: 10px; cursor: pointer; text-align: left;
  border: none; background: transparent; color: var(--chat-muted); font: inherit; font-size: 13px; font-weight: 500;
}
.chat-sidebar-more:hover:not(:disabled) { background: var(--chat-hover); color: var(--chat-text); }
.chat-sidebar-more:disabled { opacity: 0.5; cursor: default; }
.chat-sidebar-toggle {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2.25rem; height: 2.25rem; border-radius: 10px; cursor: pointer;
  border: none; background: transparent; color: var(--chat-muted);
}
.chat-sidebar-toggle:hover, .chat-shell.is-sidebar-open .chat-sidebar-toggle { color: var(--chat-text); background: var(--chat-hover); }
.chat-shell-backdrop { display: none; }
@media (min-width: 769px) {
  .chat-head { display: none; }
  .chat-shell { grid-template-columns: minmax(0, 1fr); }
  .chat-shell { --chat-log-padding: 1.25rem max(1rem, calc((100% - 760px) / 2)) 1rem; }
  /* Constant left inset = collapsed rail width, so the content stays put whether the panel is open or shut. */
  .chat-main { padding-left: 56px; }
  .chat-sidebar { position: absolute; top: 0; bottom: 0; left: 0; z-index: 20; width: 280px; }
  .chat-shell:not(.is-sidebar-open) .chat-sidebar { width: 56px; background: transparent; border-right-color: transparent; backdrop-filter: none; -webkit-backdrop-filter: none; }
  .chat-shell:not(.is-sidebar-open) .chat-sidebar-new { width: 44px; padding: 0; justify-content: center; }
  .chat-shell:not(.is-sidebar-open) .chat-sidebar-new-label,
  .chat-shell:not(.is-sidebar-open) .chat-sidebar-list,
  .chat-shell:not(.is-sidebar-open) .chat-sidebar-empty,
  .chat-shell:not(.is-sidebar-open) .chat-sidebar-error,
  .chat-shell:not(.is-sidebar-open) .chat-sidebar-more { display: none; }
}
@media (min-width: 769px) and (prefers-reduced-motion: no-preference) {
  .chat-sidebar { transition: width 0.2s ease, background-color 0.2s ease, border-color 0.2s ease; }
}
@media (max-width: 768px) {
  .chat-sidebar {
    position: absolute; top: 0; bottom: 0; left: 0; z-index: 41; width: min(85vw, 300px);
    background: var(--chat-sidebar-bg);
    transform: translateX(-100%); transition: transform 0.2s ease;
  }
  .chat-shell.is-sidebar-open .chat-sidebar { transform: none; }
  .chat-shell.is-sidebar-open .chat-shell-backdrop { display: block; position: absolute; inset: 0; z-index: 40; background: rgb(0 0 0 / 0.45); }
  .chat-composer-zone { padding: 0 0.6rem; }
  .chat-starters { padding: 0 0.6rem; }
  .chat-main:has(> .chat-empty):has(.chat-log:empty) { justify-content: flex-start; }
  .chat-main:has(.chat-log:empty) > .chat-empty { flex: 1 1 auto; margin: 0; justify-content: center; }
  .chat-main:has(.chat-log:empty) > .chat-starters { order: 3; margin: 0 auto 0.6rem; }
  .chat-main:has(.chat-log:empty) > .chat-composer-zone { order: 4; margin-bottom: 0; }
  /* Clear the iOS home indicator under the composer. */
  .chat-main { padding-bottom: max(0.75rem, env(safe-area-inset-bottom)); }
}
@media (prefers-reduced-motion: reduce) { .chat-sidebar { transition: none; } }

.chat-confirm-back {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center; padding: 1rem;
  background: rgb(0 0 0 / 0.5);
  animation: chat-confirm-in 0.12s ease;
}
.chat-confirm {
  width: 100%; max-width: 420px; padding: 1.25rem;
  border-radius: 16px; border: 1px solid hsl(var(--border));
  background: hsl(var(--card)); color: var(--chat-text);
  box-shadow: 0 24px 60px -12px rgb(0 0 0 / 0.5);
  animation: chat-confirm-pop 0.14s ease;
}
.chat-confirm-title { font-size: 1.05rem; font-weight: 700; line-height: 1.3; margin: 0 0 0.4rem; }
.chat-confirm-msg { font-size: 0.875rem; line-height: 1.5; color: var(--chat-muted); margin: 0 0 1.1rem; }
.chat-confirm-actions { display: flex; align-items: center; justify-content: flex-end; gap: 0.5rem; }
.chat-confirm-btn {
  border: none; border-radius: 10px; cursor: pointer; font: inherit; font-size: 0.875rem; font-weight: 600;
  padding: 0.55rem 1.1rem; transition: background-color 0.15s ease, filter 0.15s ease;
}
.chat-confirm-cancel { background: transparent; color: var(--chat-text); }
.chat-confirm-cancel:hover { background: var(--chat-hover); }
.chat-confirm-ok { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.chat-confirm-ok.is-danger { background: var(--chat-danger); color: hsl(0 0% 100%); }
.chat-confirm-ok:hover { filter: brightness(1.06); }
@keyframes chat-confirm-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes chat-confirm-pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .chat-confirm-back, .chat-confirm { animation: none; } }
