/* Nomatica chat — Apple/SF-styled, dark, fluid, no framework.
 *
 * Design system tokens are aligned with the existing colorfulmoves brand
 * (purple accent #a855f7, deep background #030308). On top of that we use
 * the Apple "system" font stack so the page feels native on iOS/macOS.
 *
 * Animation philosophy: 200ms cubic-bezier ease for everything stateful
 * (hover, focus, panel transitions). No bounce, no spring — Apple uses
 * "smooth" not "playful."
 */

:root {
  --bg: #030308;
  --bg-elev-1: #0c0c14;
  --bg-elev-2: #14141d;
  --bg-card: #1a1a26;
  --line: #26263a;
  --line-soft: #1e1e2e;
  --text: #f3f3f7;
  --text-muted: #9a9aae;
  --text-faint: #6a6a7a;
  --accent: #a855f7;
  --accent-2: #6f3cff;
  --accent-cyan: #38bdf8;
  --success: #22c55e;
  --warn: #f59e0b;
  --error: #ef4444;

  --font-system: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
                 "Helvetica Neue", "Segoe UI", Roboto, system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Menlo", "Cascadia Code", monospace;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-2: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-3: 0 16px 40px rgba(0, 0, 0, 0.5);

  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --duration: 200ms;
}

/* ── Reset + base ─────────────────────────────────────────────────────── */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-system);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

a {
  color: var(--accent);
  text-decoration: none;
  transition: color var(--duration) var(--ease);
}
a:hover { color: var(--accent-2); }

button {
  font-family: inherit;
  cursor: pointer;
  background: none;
  border: none;
  color: inherit;
  font-size: inherit;
  padding: 0;
}

input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  outline: none;
}

::selection {
  background: rgba(168, 85, 247, 0.35);
  color: var(--text);
}

/* ── Skip link (a11y) ─────────────────────────────────────────────────── */

.skip {
  position: absolute;
  top: -100px;
  left: 8px;
  background: var(--accent);
  color: #fff;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  z-index: 1000;
  transition: top var(--duration) var(--ease);
}
.skip:focus { top: 8px; }

/* ── Layout ───────────────────────────────────────────────────────────── */

.chat-body {
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: auto 1fr;
  /* On mobile the topbar is shown above the chat; on desktop it's
   * display: none so this row collapses to 0px. */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
.chat-topbar { grid-column: 1 / -1; }

/* ── Sidebar ──────────────────────────────────────────────────────────── */

.chat-sidebar {
  background: var(--bg-elev-1);
  border-right: 1px solid var(--line-soft);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform var(--duration) var(--ease);
  z-index: 30;
}

.sidebar-header {
  padding: 14px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  border-bottom: 1px solid var(--line-soft);
}

.sidebar-header .brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.new-chat-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: #fff;
  font-weight: 500;
  font-size: 13px;
  transition: background var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.new-chat-btn:hover { background: var(--accent-2); }
.new-chat-btn:active { transform: scale(0.98); }

.sidebar-section {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px 8px;
}
.sidebar-section::-webkit-scrollbar { width: 6px; }
.sidebar-section::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }

.sidebar-label {
  margin: 8px 8px 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.conversation-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  line-height: 1.3;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
  position: relative;
}
.conversation-item:hover { background: var(--bg-elev-2); color: var(--text); }
.conversation-item.is-active {
  background: var(--bg-card);
  color: var(--text);
}
.conversation-item .conv-title {
  flex: 1 1 auto;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conversation-item .conv-delete {
  opacity: 0;
  width: 22px; height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--text-faint);
  transition: opacity var(--duration) var(--ease), background var(--duration) var(--ease);
}
.conversation-item:hover .conv-delete { opacity: 1; }
.conversation-item .conv-delete:hover { background: var(--bg-elev-1); color: var(--error); }

.sidebar-footer {
  border-top: 1px solid var(--line-soft);
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 12px;
}

/* Tier pill in the sidebar footer. Shows the user's current plan and
 * links to /pricing for upgrades. Color-coded by tier. */
.footer-tier {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--text-muted);
  text-decoration: none;
  transition: border-color var(--duration) var(--ease), color var(--duration) var(--ease);
}
.footer-tier:hover { color: var(--text); border-color: var(--accent); }
.tier-pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: var(--bg-elev-2);
  color: var(--text-muted);
  border: 1px solid var(--line);
}
.tier-pill[data-tier="free"]    { color: var(--text-faint); }
.tier-pill[data-tier="basic"]   { color: var(--accent-cyan); border-color: var(--accent-cyan); }
.tier-pill[data-tier="premium"] { color: var(--accent);      border-color: var(--accent); }
.tier-pill[data-tier="pro"]     { color: var(--warn);         border-color: var(--warn); }

.api-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-faint);
  transition: background var(--duration) var(--ease);
}
.status-dot--checking { background: var(--warn); animation: pulse 1.4s ease-in-out infinite; }
.status-dot--ok { background: var(--success); }
.status-dot--err { background: var(--error); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

.footer-link {
  color: var(--text-faint);
  font-size: 12px;
}
.footer-link:hover { color: var(--text); }

/* ── Backdrop (mobile sidebar) ────────────────────────────────────────── */

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration) var(--ease);
  z-index: 25;
}
.sidebar-backdrop.is-open { opacity: 1; pointer-events: auto; }

/* ── Mobile top bar (hidden on desktop) ───────────────────────────────── */

.chat-topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--bg-elev-1);
  border-bottom: 1px solid var(--line-soft);
  /* Height includes the safe-area inset on notched devices so the
   * topbar's content sits below the status bar instead of under it. */
  height: calc(56px + env(safe-area-inset-top, 0px));
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  position: sticky;
  top: 0;
  z-index: 20;
}
.chat-topbar .brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 600;
}

/* ── Icon button (SF Symbol-style) ────────────────────────────────────── */

.icon-btn {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--text-muted);
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.icon-btn:hover { background: var(--bg-elev-2); color: var(--text); }
.icon-btn:active { background: var(--line); }

/* ── Main thread ──────────────────────────────────────────────────────── */

.chat-main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* ── Empty state ──────────────────────────────────────────────────────── */

.thread-empty {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  overflow-y: auto;
}

.empty-card {
  max-width: 720px;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  animation: fadeUp 0.6s var(--ease) both;
}
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.empty-logo {
  width: 56px;
  height: 56px;
  filter: drop-shadow(0 4px 16px rgba(168, 85, 247, 0.4));
}

.empty-card h1 {
  margin: 0;
  font-size: clamp(24px, 4vw, 32px);
  font-weight: 600;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #fff 0%, #c4b5fd 50%, #a855f7 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.empty-sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 15px;
  max-width: 540px;
}

.suggestion-grid {
  margin-top: 24px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  width: 100%;
}
.suggestion-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  text-align: left;
  gap: 6px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--bg-elev-1);
  color: var(--text);
  transition: border-color var(--duration) var(--ease), background var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.suggestion-card:hover { border-color: var(--accent); background: var(--bg-elev-2); }
.suggestion-card:active { transform: scale(0.99); }
.sugg-icon { font-size: 20px; line-height: 1; }
.sugg-title { font-weight: 600; font-size: 14px; }
.sugg-desc { font-size: 12.5px; color: var(--text-muted); line-height: 1.45; }

.empty-fineprint {
  margin: 18px 0 0;
  font-size: 12px;
  color: var(--text-faint);
}

/* ── Active thread ────────────────────────────────────────────────────── */

.thread {
  flex: 1 1 auto;
  overflow-y: auto;
  scroll-behavior: smooth;
}
.thread::-webkit-scrollbar { width: 8px; }
.thread::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }

.thread-inner {
  max-width: 760px;
  margin: 0 auto;
  /* Reduced from 32px → 12px top so the chat window uses more of the
   * screen, especially when the topbar already occupies the very top. */
  padding: 12px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.msg {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  animation: msgIn 0.3s var(--ease) both;
}
@keyframes msgIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.msg .avatar {
  flex: 0 0 32px;
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 13px;
  color: #fff;
  background: var(--bg-card);
  border: 1px solid var(--line);
  flex-shrink: 0;
  overflow: hidden;
}
.msg.user .avatar {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
}
.msg.assistant .avatar {
  background: var(--bg-card);
}
.msg.assistant .avatar img {
  width: 24px; height: 24px;
}

.msg .bubble {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg .role {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}
.msg .role .role-meta {
  font-weight: 400;
  font-size: 11.5px;
  color: var(--text-faint);
}

.msg .content { white-space: pre-wrap; }
.msg .content p { margin: 0 0 12px; }
.msg .content p:last-child { margin-bottom: 0; }
.msg .content strong { color: var(--text); font-weight: 600; }
.msg .content em { color: var(--text); font-style: italic; }
.msg .content code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  padding: 1px 6px;
  border-radius: 6px;
}
.msg .content pre {
  background: var(--bg-elev-1);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg .content pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: 13px;
  color: var(--text);
}

/* Code-block copy button — positioned in the top-right of the <pre>,
 * shown on hover/focus so the chat scroll doesn't show button chrome. */
.msg .content pre {
  position: relative;
}
.msg .content pre .code-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 3px 8px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: var(--bg-elev-2);
  color: var(--text-muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  opacity: 0;
  transition: opacity var(--duration) var(--ease), background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.msg .content pre:hover .code-copy,
.msg .content pre .code-copy:focus-visible {
  opacity: 1;
}
.msg .content pre .code-copy:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.msg .content pre .code-copy.is-copied {
  opacity: 1;
  background: var(--success);
  color: #fff;
  border-color: var(--success);
}
.msg .content ul, .msg .content ol {
  margin: 4px 0 12px;
  padding-left: 22px;
}
.msg .content li { margin: 2px 0; }
.msg .content a { color: var(--accent-cyan); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; }
.msg .content blockquote {
  margin: 8px 0;
  padding: 8px 14px;
  border-left: 3px solid var(--accent);
  background: var(--bg-elev-1);
  color: var(--text-muted);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.msg .content table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
}
.msg .content th, .msg .content td {
  border: 1px solid var(--line);
  padding: 6px 10px;
  text-align: left;
}
.msg .content th { background: var(--bg-elev-1); font-weight: 600; }

.msg.streaming .bubble::after {
  content: '▍';
  display: inline-block;
  color: var(--accent);
  animation: caret 1s steps(2) infinite;
  margin-left: 2px;
  font-weight: 100;
}
@keyframes caret { 0%, 50% { opacity: 1; } 51%, 100% { opacity: 0; } }

.msg.error .bubble {
  color: var(--error);
  font-size: 14px;
}

/* ── Composer ─────────────────────────────────────────────────────────── */

.composer {
  border-top: 1px solid var(--line-soft);
  background: linear-gradient(to top, var(--bg) 0%, var(--bg-elev-1) 100%);
  padding: 12px 16px 16px;
  flex-shrink: 0;
  transition: background var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.composer.is-dragover {
  background: linear-gradient(to top, var(--bg-elev-2) 0%, var(--bg-elev-1) 100%);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.composer-inner {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Drawer-style select (used inside the settings drawer). */
.drawer-select {
  width: 100%;
  max-width: none;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 13.5px;
  color: var(--text);
  cursor: pointer;
  transition: border-color var(--duration) var(--ease);
}
.drawer-select:hover { border-color: var(--accent); }
.drawer-select:focus { border-color: var(--accent); }

/* Range input inside the drawer — wider than the toolbar version. */
.field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

#tempRange {
  width: 100%;
  accent-color: var(--accent);
}

.settings-btn { margin-left: auto; }

.attach-btn { color: var(--text-muted); }
.attach-btn:hover { color: var(--accent); }
.attach-btn[aria-busy="true"] { opacity: 0.5; pointer-events: none; }

/* Attachment strip — small row of file previews that appear above the
 * textarea once files are selected. Each chip is a rounded pill with a
 * thumbnail (for images) or a generic file icon, the filename, and an
 * X button to remove. */
.attachment-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 4px;
}
.attachment-strip[hidden] { display: none; }
.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px 4px;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--text-muted);
  max-width: 200px;
}
.attachment-chip-thumb {
  flex: 0 0 28px;
  width: 28px; height: 28px;
  border-radius: 4px;
  background: var(--bg-elev-1);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.attachment-chip-thumb img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.attachment-chip-thumb svg {
  width: 16px; height: 16px; color: var(--text-faint);
}
.attachment-chip-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1 1 auto;
}
.attachment-chip-remove {
  flex: 0 0 auto;
  width: 18px; height: 18px;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  transition: color var(--duration) var(--ease), background var(--duration) var(--ease);
}
.attachment-chip-remove:hover { color: var(--error); background: rgba(239,68,68,0.1); }
.attachment-chip-remove svg { width: 12px; height: 12px; }

/* Attachment chips inside message bubbles (after send). */
.msg .attachment-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 8px;
  padding: 0;
  list-style: none;
}
.msg .attachment-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 4px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  color: var(--text-muted);
  max-width: 240px;
}
.msg.user .attachment-item {
  background: rgba(255,255,255,0.08);
  border-color: rgba(255,255,255,0.12);
  color: rgba(255,255,255,0.85);
}
.msg .attachment-thumb {
  flex: 0 0 28px;
  width: 28px; height: 28px;
  border-radius: 4px;
  background: var(--bg-elev-2);
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.msg.user .attachment-thumb { background: rgba(255,255,255,0.12); }
.msg .attachment-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.msg .attachment-thumb svg { width: 14px; height: 14px; }
.msg .attachment-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1 1 auto;
}

.composer-input {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  background: var(--bg-elev-1);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 8px 8px 8px 6px;
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
  box-shadow: var(--shadow-2);
}
.composer-input:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(168, 85, 247, 0.12), var(--shadow-2);
}

/* Left-aligned icon buttons (attach, emoji) and right-aligned buttons
 * (settings, send, stop) inside the input row. */
.composer-input .icon-btn {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.composer-input .icon-btn:hover { color: var(--text); background: var(--bg-elev-2); }
.composer-input .icon-btn:active { transform: scale(0.95); }
.composer-input .emoji-btn[aria-expanded="true"] { color: var(--accent); background: var(--bg-elev-2); }

/* Inline settings gear inside the input row — smaller, ghosted. */
.settings-inline {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  margin-left: 2px;
  transition: color var(--duration) var(--ease), background var(--duration) var(--ease);
}
.settings-inline:hover { color: var(--text); background: var(--bg-elev-2); }

/* Compact one-line "current selection" hint below the input. */
.composer-current {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px 2px;
  font-size: 11.5px;
  color: var(--text-faint);
}
.composer-current[hidden] { display: none; }
.composer-current-agent { font-weight: 600; color: var(--text-muted); }
.composer-current-sep { opacity: 0.5; }

/* Emoji popover — a small panel that appears when the user clicks the
 * emoji button. Positioned above the input row, just like iMessage. */
.emoji-popover {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  z-index: 30;
  padding: 8px;
  background: var(--bg-elev-1);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-3);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  max-width: 320px;
}
.emoji-popover[hidden] { display: none; }
.emoji-popover button {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  font-size: 20px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--duration) var(--ease);
}
.emoji-popover button:hover { background: var(--bg-elev-2); }

.composer-input textarea {
  flex: 1 1 auto;
  resize: none;
  min-height: 24px;
  max-height: 200px;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  padding: 4px 0;
  font-family: inherit;
}

.send-btn {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--duration) var(--ease), transform var(--duration) var(--ease), opacity var(--duration) var(--ease);
  margin-left: 8px;
}
.send-btn:hover:not(:disabled) { background: var(--accent-2); }
.send-btn:active:not(:disabled) { transform: scale(0.95); }
.send-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* Stop button — shown only during streaming. Sized the same as the
 * send button so the input bar doesn't shift height. */
.stop-btn {
  flex: 0 0 auto;
  height: 32px;
  padding: 0 12px 0 10px;
  border-radius: 16px;
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--line);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  transition: background var(--duration) var(--ease), border-color var(--duration) var(--ease), transform var(--duration) var(--ease);
  margin-left: 8px;
}
.stop-btn:hover {
  background: var(--error);
  border-color: var(--error);
  color: #fff;
}
.stop-btn:active { transform: scale(0.96); }
.stop-btn svg { width: 10px; height: 10px; }
.stop-btn[hidden] { display: none; }

.composer-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11.5px;
  color: var(--text-faint);
  padding: 0 4px;
}
.composer-meta kbd {
  font-family: var(--font-mono);
  font-size: 10.5px;
  padding: 1px 5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--bg-elev-2);
  color: var(--text-muted);
}
.meta-warn {
  color: var(--warn);
}

/* ── Settings drawer ──────────────────────────────────────────────────── */

.settings-drawer {
  position: fixed;
  inset: 0;
  z-index: 100;
}
.settings-drawer[hidden] { display: none; }

.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  animation: fadeIn var(--duration) var(--ease) both;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.drawer-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 420px;
  max-width: 100vw;
  height: 100%;
  background: var(--bg-elev-1);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  animation: drawerIn var(--duration) var(--ease) both;
  box-shadow: var(--shadow-3);
}
@keyframes drawerIn { from { transform: translateX(100%); } to { transform: translateX(0); } }

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--line-soft);
}
.drawer-header h2 { margin: 0; font-size: 16px; font-weight: 600; }

.drawer-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}
.field input, .field textarea {
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  resize: vertical;
  transition: border-color var(--duration) var(--ease);
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field-hint { margin: 4px 0 0; font-size: 12px; color: var(--text-faint); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  transition: background var(--duration) var(--ease), transform var(--duration) var(--ease);
  cursor: pointer;
  border: 1px solid transparent;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-2); }
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--line);
}
.btn-ghost:hover { background: var(--bg-elev-2); color: var(--text); border-color: var(--accent); }

/* ── Responsive (mobile) ──────────────────────────────────────────────── */

@media (max-width: 820px) {
  .chat-body { grid-template-columns: 1fr; }
  .chat-topbar { display: flex; }
  .chat-sidebar {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    width: min(300px, 85vw);
    transform: translateX(-100%);
    z-index: 30;
    box-shadow: var(--shadow-3);
  }
  .chat-sidebar.is-open { transform: translateX(0); }
  .composer { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
}

@media (max-width: 480px) {
  .empty-card h1 { font-size: 22px; }
  /* Further tightened on phones so messages start higher. */
  .thread-inner { padding: 8px 10px 12px; max-width: 100%; }
  .composer { padding: 8px 10px 12px; }
  .composer-inner { gap: 4px; }
  .composer-input { padding: 8px 10px; gap: 6px; }
  .composer-input .icon-btn { width: 32px; height: 32px; }
  .composer-input .icon-btn svg { width: 18px; height: 18px; }
  .settings-inline { width: 28px; height: 28px; }
  .drawer-panel { width: 100vw; }
  /* Prevent iOS Safari auto-zoom on focus by forcing 16px+ text in form controls. */
  .composer-input textarea,
  .field input,
  .field textarea { font-size: 16px; }
  /* Compact messages on phones. */
  .msg { gap: 8px; }
  .msg .avatar { flex-basis: 28px; width: 28px; height: 28px; font-size: 11px; }
  .msg .bubble { padding: 10px 12px; font-size: 14.5px; }
  .msg .content pre { font-size: 12.5px; padding: 12px; }
  /* Topbar brand span hidden on very narrow screens — icon only. */
  .chat-topbar .brand span { display: none; }
  /* Composer send button more compact on phones. */
  .send-btn { padding: 6px 12px; }
  .composer-input textarea { min-height: 40px; max-height: 140px; }
  /* Drawer select is full-width and easier to tap. */
  .picker-select { font-size: 15px; padding: 10px 12px; }
  /* The chat must use the FULL visible viewport — not be cropped by
   * the address bar. We use 100dvh (dynamic viewport height) which
   * shrinks as the URL bar collapses on mobile Safari. */
  .chat-body { height: 100dvh; min-height: 100dvh; }
}

/* Honor iOS safe-area inset for the chat-topbar (notch). */
@supports (padding: max(0px)) {
  .chat-topbar { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
}

/* When the keyboard is open on iOS/Android, the visualViewport shrinks.
   We translate the composer up by the difference between window.innerHeight
   and visualViewport.height so it sits just above the keyboard.
   Set --keyboard-inset from JS. */
:root {
  --keyboard-inset: 0px;
}
.composer {
  transform: translateY(var(--keyboard-inset));
  transition: transform 80ms linear;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}