:root {
  --bg: #ffffff;
  --bg-side: #f9f9f9;
  --bg-elevated: #ffffff;
  --bg-inset: #f4f4f4;
  --border: #e5e5e5;
  --border-strong: #d4d4d4;
  --text: #0d0d0d;
  --text-muted: #6b6b6b;
  --text-faint: #9a9a9a;
  --accent: #10a37f;
  --accent-hover: #0e8f70;
  --danger: #d93b3b;
  --user-bubble: #f4f4f4;
  --code-bg: #f6f6f6;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.05);
  --radius: 12px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
          "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #212121;
    --bg-side: #171717;
    --bg-elevated: #2a2a2a;
    --bg-inset: #2f2f2f;
    --border: #383838;
    --border-strong: #4a4a4a;
    --text: #ececec;
    --text-muted: #a8a8a8;
    --text-faint: #7a7a7a;
    --user-bubble: #2f2f2f;
    --code-bg: #1b1b1b;
    --shadow: 0 1px 3px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
#app { display: flex; height: 100dvh; overflow: hidden; }

#sidebar {
  position: relative;
  width: 268px; flex: 0 0 268px;
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transition: margin-left .18s ease;
}
#sidebar.collapsed { margin-left: -268px; }

.sidebar-top { padding: 12px; }
.new-chat {
  width: 100%; padding: 10px 12px;
  background: transparent; color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 10px;
  font-size: 14px; text-align: left;
}
.new-chat:hover { background: var(--bg-inset); }

.conversation-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.conv {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px; margin-bottom: 2px;
  border-radius: 9px; cursor: pointer;
  font-size: 14px; color: var(--text);
}
.conv:hover { background: var(--bg-inset); }
.conv.active { background: var(--bg-inset); font-weight: 500; }
.conv-title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); flex: 0 0 7px;
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .3 } }
.conv .del {
  opacity: 0; border: 0; background: none; color: var(--text-faint);
  padding: 2px 4px; border-radius: 5px; line-height: 1;
}
.conv:hover .del { opacity: 1; }
.conv .del:hover { color: var(--danger); background: var(--bg); }

.sidebar-bottom {
  border-top: 1px solid var(--border);
  padding: 10px 12px; display: flex; align-items: center; gap: 8px;
}
.who { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.avatar {
  width: 26px; height: 26px; border-radius: 50%; flex: 0 0 26px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 13px; font-weight: 600;
}
.who-name {
  font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.admin-card { max-height: min(85dvh, 700px); overflow: auto; }
.admin-create { display: flex; align-items: end; gap: 10px; }
.admin-create .stack { flex: 1; }
.admin-user { display: flex; justify-content: space-between; align-items: center;
  gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
#activation-link { width: 100%; padding: 8px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg-inset); }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  min-height: 56px;
}
.title-wrap { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px; }
#chat-title {
  font-size: 15px; font-weight: 600; margin: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-status { font-size: 12px; color: var(--text-faint); white-space: nowrap; }

/* model / effort pickers */
.model-controls { display: flex; align-items: center; gap: 8px; flex: none; margin-left: auto; }
.field { display: flex; align-items: center; gap: 4px; min-width: 0; }
.field span { display: none; }
.field select {
  background: var(--bg-inset); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px 6px; font-size: 12px;
  color: var(--text-muted); max-width: 130px;
}
.field select:hover { color: var(--text); }
.reasoning-switch {
  position: relative; display: inline-flex; flex: none;
  width: 50px; height: 28px; cursor: pointer; touch-action: manipulation;
}
.reasoning-switch input {
  position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%;
  margin: 0; opacity: 0; cursor: inherit;
}
.reasoning-track {
  width: 100%; height: 100%; border: 1px solid var(--border-strong);
  border-radius: 999px; background: var(--bg-inset); transition: background .18s ease;
}
.reasoning-thumb {
  position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
  display: grid; place-items: center; border-radius: 50%;
  background: var(--bg-elevated); color: var(--text-muted);
  box-shadow: 0 1px 3px rgba(0,0,0,.18); transition: transform .18s ease;
}
.reasoning-thumb svg { width: 16px; height: 16px; }
.reasoning-switch input:checked + .reasoning-track { background: var(--accent); border-color: var(--accent); }
.reasoning-switch input:checked + .reasoning-track .reasoning-thumb {
  transform: translateX(22px); color: var(--accent);
}
.reasoning-switch input:focus-visible + .reasoning-track {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.reasoning-switch input:disabled + .reasoning-track { opacity: .45; }
.reasoning-switch input:disabled { cursor: not-allowed; }

.ghost {
  background: none; border: 1px solid transparent; border-radius: 8px;
  color: var(--text-muted); padding: 5px 9px; font-size: 13px;
}
.ghost:hover { background: var(--bg-inset); color: var(--text); }
.ghost.icon { font-size: 16px; padding: 4px 8px; }

/* ---------- banner ---------- */
.banner {
  margin: 10px 16px 0; padding: 10px 14px;
  border: 1px solid var(--border-strong); border-left: 3px solid var(--danger);
  border-radius: 8px; background: var(--bg-inset);
  font-size: 13px; color: var(--text-muted);
}
.banner code { font-family: var(--mono); font-size: 12px; }

/* ---------- transcript ---------- */
.transcript {
  flex: 1; overflow-y: auto;
  padding: 24px 0 8px;
}
.inner {
  max-width: 760px; min-height: 100%; margin: 0 auto; padding: 0 20px;
  display: flex; flex-direction: column;
}
.inner > :first-child { margin-top: auto; }
.inner > :last-child { margin-bottom: 8px; }

.empty-state { max-width: 620px; margin: 12vh auto 0; padding: 0 20px; text-align: center; }
.empty-state h2 { font-size: 24px; font-weight: 600; margin: 0 0 8px; }
.empty-state p { color: var(--text-muted); font-size: 14px; margin: 0 0 24px; }
.examples { display: flex; flex-direction: column; gap: 8px; }
.example {
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 14px; text-align: left;
  font-size: 14px; color: var(--text-muted);
}
.example:hover { border-color: var(--border-strong); color: var(--text); }

.msg { margin-bottom: 26px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .body {
  background: var(--user-bubble); border-radius: 18px;
  padding: 10px 16px; max-width: 80%; white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.assistant .body { max-width: 100%; }

/* reasoning */
.reasoning {
  border: 1px solid var(--border); border-radius: 10px;
  margin-bottom: 12px; background: var(--bg-side); overflow: hidden;
}
.reasoning > summary {
  cursor: pointer; padding: 8px 12px; font-size: 13px;
  color: var(--text-muted); list-style: none; display: flex; align-items: center; gap: 7px;
}
.reasoning > summary::-webkit-details-marker { display: none; }
.reasoning > summary::before {
  content: "▸"; font-size: 10px; color: var(--text-faint); transition: transform .15s;
}
.reasoning[open] > summary::before { transform: rotate(90deg); }
.reasoning .thinking { font-size: 12px; color: var(--text-faint); font-style: italic; }
.reasoning .reasoning-text {
  padding: 8px 14px 12px; font-family: var(--serif);
  font-size: 14px; line-height: 1.75; color: var(--text-muted);
  overflow-wrap: anywhere; max-height: 340px; overflow-y: auto;
}
/* Streamed text keeps its newlines; rendered markdown does its own layout. */
.reasoning .reasoning-text.thinking { white-space: pre-wrap; }
/* Markdown inside the panel is secondary content, so it runs a step smaller
   and never wider than the panel. */
.reasoning-text.md { font-size: 13.5px; }
.reasoning-text.md h1,
.reasoning-text.md h2,
.reasoning-text.md h3 { font-size: 15px; margin: 14px 0 8px; }
.reasoning-text.md p { margin-bottom: 10px; }
.reasoning-text.md pre { font-size: 12px; }
.reasoning-text.md table { font-size: 13px; }
.reasoning-text.md ul,
.reasoning-text.md ol { margin-bottom: 10px; padding-left: 22px; }

/* markdown */
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 0 0 14px; overflow-wrap: anywhere; }
.md h1, .md h2, .md h3 { margin: 22px 0 10px; line-height: 1.35; }
.md h1 { font-size: 21px; } .md h2 { font-size: 18px; } .md h3 { font-size: 16px; }
.md ul, .md ol { margin: 0 0 14px; padding-left: 24px; }
.md li { margin-bottom: 5px; }
.md a { color: var(--accent); }
.md blockquote {
  margin: 0 0 14px; padding: 2px 0 2px 14px;
  border-left: 3px solid var(--border-strong); color: var(--text-muted);
}
.md code {
  font-family: var(--mono); font-size: .875em;
  background: var(--bg-inset); border-radius: 5px; padding: 2px 5px;
}
.md pre {
  background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; overflow-x: auto; margin: 0 0 14px;
}
.md pre code { background: none; padding: 0; font-size: 13px; line-height: 1.6; }
.md table { border-collapse: collapse; margin: 0 0 14px; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.md th { background: var(--bg-inset); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }

.cursor {
  display: inline-block; width: 7px; height: 15px; margin-left: 2px;
  background: var(--text); vertical-align: text-bottom;
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0 } }

.error-line {
  color: var(--danger); font-size: 13px;
  border-left: 3px solid var(--danger); padding-left: 10px; margin: 12px 0;
}
.send-error {
  max-width: 760px; margin: 0 auto 10px;
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--bg-inset); border-radius: 8px; padding: 9px 12px;
}
.send-error span { flex: 1; }
.error-close {
  background: none; border: 0; color: var(--text-faint);
  font-size: 12px; padding: 0 2px; line-height: 1.4;
}
.error-close:hover { color: var(--danger); }
.usage {
  margin-top: 10px; font-size: 11px; color: var(--text-faint);
  font-family: var(--mono);
}

/* ---------- composer ---------- */
#composer-wrap {
  border-top: 1px solid var(--border);
  padding: 14px 20px calc(14px + env(safe-area-inset-bottom));
  background: var(--bg);
}
.composer {
  max-width: 760px; margin: 0 auto;
  border: 1px solid var(--border-strong); border-radius: 22px;
  background: var(--bg-elevated); box-shadow: var(--shadow);
  padding: 10px 12px 8px; display: flex; flex-direction: column; gap: 6px;
}
.composer:focus-within { border-color: var(--accent); }
#input {
  width: 100%; border: 0; outline: 0; resize: none; background: none;
  max-height: 220px; line-height: 1.6; font-size: 15px;
  padding: 2px 4px;
}
.composer-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.hint { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap;
  text-overflow: ellipsis; font-size: 11px; color: var(--text-faint); }
.send {
  background: var(--accent); color: #fff; border: 0; border-radius: 9px;
  padding: 7px 16px; font-size: 14px; font-weight: 500;
}
.send:hover:not(:disabled) { background: var(--accent-hover); }
.send:disabled { background: var(--border-strong); cursor: not-allowed; opacity: .7; }
.stop {
  background: none; border: 1px solid var(--border-strong); border-radius: 9px;
  padding: 6px 14px; font-size: 14px; color: var(--text);
}
.stop:hover { background: var(--bg-inset); }

/* ---------- attachments in the composer ---------- */
.pending {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 0 2px 6px;
}
.chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 220px;
  background: var(--bg-inset); border: 1px solid var(--border);
  border-radius: 9px; padding: 3px 4px 3px 3px; font-size: 12px;
}
.chip img { width: 26px; height: 26px; object-fit: cover; border-radius: 6px; }
.chip-tag {
  display: inline-grid; place-items: center; min-width: 26px; height: 26px;
  padding: 0 4px; border-radius: 6px; background: var(--bg);
  font-family: var(--mono); font-size: 9px; color: var(--text-muted);
}
.chip-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 130px; color: var(--text-muted);
}
.chip-x {
  background: none; border: 0; color: var(--text-faint);
  font-size: 11px; padding: 0 3px; line-height: 1.2;
}
.chip-x:hover { color: var(--danger); }
.composer.dropping { border-color: var(--accent); border-style: dashed; }
.ghost.busy { opacity: .6; }

/* ---------- attachments shown in a message ---------- */
.msg-attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.msg-image img {
  display: block; max-width: 200px; max-height: 200px; border-radius: 10px;
  border: 1px solid var(--border);
}
.msg-file {
  display: inline-block; background: var(--bg-inset); border: 1px solid var(--border);
  border-radius: 9px; padding: 6px 10px; font-size: 13px; color: var(--text);
  text-decoration: none; max-width: 240px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.msg-file:hover { border-color: var(--accent); }
.user-text { white-space: pre-wrap; }

/* ---------- generated images ---------- */
.image-strip { display: flex; flex-wrap: wrap; gap: 12px; margin: 4px 0 10px; }
.image-card {
  margin: 0; border: 1px solid var(--border); border-radius: 12px;
  overflow: hidden; background: var(--bg-inset); max-width: 340px;
}
.image-card img { display: block; width: 100%; height: auto; }
.image-card .image-frame a { display: block; width: 100%; }
.image-card figcaption {
  padding: 7px 10px 9px; font-size: 11px; color: var(--text-faint);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden;
}
.image-actions { display: flex; gap: 6px; padding: 8px 9px 9px; }
.image-action {
  flex: 1; background: var(--bg-elevated); border: 1px solid var(--border-strong);
  border-radius: 8px; padding: 5px 8px; font-size: 12px; color: var(--text);
}
.image-action:hover { border-color: var(--accent); color: var(--accent); }
/* A card that is not finished yet: same footprint as a picture so the layout
   does not jump when the image arrives. */
.image-frame {
  display: flex; align-items: center; justify-content: center;
  min-height: 132px; background: var(--bg-inset);
}
.image-frame.working {
  flex-direction: column; gap: 10px;
  border-bottom: 1px dashed var(--border);
}
.image-frame.failed { flex-direction: column; gap: 6px; background: transparent; }
.image-icon {
  width: 22px; height: 22px; border-radius: 50%;
  display: grid; place-items: center; font-size: 13px; font-weight: 600;
  background: var(--danger); color: #fff;
}
.image-note { font-size: 12px; color: var(--text-faint); }
.image-card.state-queued { border-style: dashed; }
.image-card.state-running { border-color: var(--accent); }
.image-why { color: var(--danger) !important; }
.spinner {
  width: 13px; height: 13px; border-radius: 50%;
  border: 2px solid var(--border-strong); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- resource library gallery ---------- */
/* Dim the transcript while the sidebar overlay is open on a phone. */
body.sidebar-open::after {
  content: ''; position: fixed; inset: 0; z-index: 19;
  background: rgba(0, 0, 0, .28);
}

.library-backdrop {
  position: fixed; inset: 0; z-index: 60; padding: 0; border: 0;
  background: rgba(0, 0, 0, .42);
}
.library {
  position: fixed; top: 0; right: 0; z-index: 61;
  width: min(740px, calc(100vw - 48px)); height: 100dvh;
  display: flex; flex-direction: column;
  background: var(--bg-elevated); box-shadow: -18px 0 48px rgba(0, 0, 0, .18);
}
.library-head {
  display: flex; align-items: center; gap: 12px;
  padding: 18px 24px; border-bottom: 1px solid var(--border);
}
.library-heading { flex: 1; min-width: 0; }
.library-heading h2 { margin: 0; font-size: 20px; line-height: 1.35; }
.library-heading p { margin: 3px 0 0; color: var(--text-muted); font-size: 12px; }
.lib-upload {
  flex: none; min-height: 36px; padding: 5px 14px;
  border: 1px solid var(--border-strong); border-radius: 9px;
  background: var(--bg-elevated); font-size: 13px;
}
.lib-upload:hover { background: var(--bg-inset); }
.library-storage {
  --storage-fill: var(--accent);
  padding: 13px 24px 15px; border-bottom: 1px solid var(--border);
}
.library-storage.near-limit { --storage-fill: var(--danger); }
.library-storage-labels {
  display: flex; justify-content: space-between; gap: 12px;
  margin-bottom: 7px; color: var(--text-muted); font-size: 12px;
}
.library-storage.near-limit #library-storage-percent { color: var(--danger); font-weight: 600; }
#library-storage-progress {
  display: block; width: 100%; height: 8px; overflow: hidden;
  appearance: none; border: 0; border-radius: 999px; background: var(--bg-inset);
}
#library-storage-progress::-webkit-progress-bar { background: var(--bg-inset); border-radius: 999px; }
#library-storage-progress::-webkit-progress-value { background: var(--storage-fill); border-radius: 999px; }
#library-storage-progress::-moz-progress-bar { background: var(--storage-fill); border-radius: 999px; }
.library-usage {
  flex: 1; font-size: 11px; color: var(--text-faint);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.library-list { flex: 1; overflow-y: auto; padding: 22px 24px 32px; }
.library-empty {
  padding: 56px 20px; border: 1px dashed var(--border-strong);
  border-radius: 14px; color: var(--text-muted); text-align: center; font-size: 13px;
}
.lib-section + .lib-section { margin-top: 30px; }
.lib-section-title { margin: 0 0 12px; font-size: 13px; font-weight: 600; color: var(--text-muted); }
.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(166px, 1fr)); gap: 14px; }
.lib-item {
  position: relative; min-width: 0; border: 1px solid var(--border);
  border-radius: 14px; background: var(--bg-elevated);
}
.lib-item:hover, .lib-item:focus-within { border-color: var(--border-strong); box-shadow: var(--shadow); }
.lib-item:focus-within { z-index: 2; }
.lib-preview {
  width: 100%; aspect-ratio: 4 / 3; border: 0; padding: 0;
  border-radius: 13px 13px 0 0; overflow: hidden;
  background: var(--bg-inset); display: grid; place-items: center;
  color: var(--text-muted); font: 600 17px/1 var(--mono);
}
.lib-preview img { width: 100%; height: 100%; object-fit: contain; }
.lib-preview.image-preview:hover img { transform: scale(1.03); }
.lib-preview img { transition: transform .15s ease; }
.lib-content { padding: 10px; }
.lib-info { min-width: 0; }
.lib-name {
  font-size: 13px; font-weight: 500; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.lib-meta { font-size: 11px; color: var(--text-faint); font-family: var(--mono); }
.lib-actions { display: flex; gap: 6px; margin-top: 9px; }
.lib-btn {
  flex: 1; min-height: 34px; border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
  border-radius: 8px; background: color-mix(in srgb, var(--accent) 10%, var(--bg-elevated));
  color: var(--accent); font-size: 12px; font-weight: 600;
}
.lib-btn:hover { background: color-mix(in srgb, var(--accent) 18%, var(--bg-elevated)); }
.lib-more { position: relative; flex: none; }
.lib-more > summary {
  width: 36px; height: 34px; display: grid; place-items: center;
  border: 1px solid var(--border); border-radius: 8px;
  cursor: pointer; list-style: none; line-height: 1; font-size: 20px;
  color: var(--text-muted);
}
.lib-more > summary::-webkit-details-marker { display: none; }
.lib-more > summary:hover, .lib-more[open] > summary { background: var(--bg-inset); }
.lib-menu {
  position: absolute; bottom: calc(100% + 4px); right: 0; z-index: 3;
  min-width: 128px; padding: 4px; border: 1px solid var(--border);
  border-radius: 9px; background: var(--bg-elevated); box-shadow: var(--shadow);
}
.lib-menu button {
  display: block; width: 100%; min-height: 34px; padding: 5px 10px;
  border: 0; border-radius: 6px; background: none;
  text-align: left; font-size: 12px;
}
.lib-menu button:hover { background: var(--bg-inset); }
.lib-menu button.delete { color: var(--danger); }
.library :is(button, summary):focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

/* ---------- login ---------- */
.login-screen {
  min-height: 100dvh; display: grid; place-items: center;
  background: var(--bg-side); padding: 20px;
}
.login-card {
  width: 100%; max-width: 360px; background: var(--bg-elevated);
  border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); padding: 28px;
}
.login-card h1 { margin: 0; font-size: 22px; text-align: center; }
.login-sub { text-align: center; color: var(--text-muted); font-size: 13px; margin: 4px 0 22px; }
.stack { display: block; margin-bottom: 14px; }
.stack span { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 5px; }
.stack input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border-strong);
  border-radius: 9px; background: var(--bg);
}
.stack input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.login-actions { display: flex; gap: 8px; margin-top: 20px; }
.login-actions .send { flex: 1; }
.login-error { color: var(--danger); font-size: 13px; margin: 12px 0 0; }
.login-note { text-align: center; color: var(--text-muted); font-size: 12px; margin: 12px 0 0; }
.code-sent { font-size: 13px; color: var(--text-muted); margin: 0 0 14px; overflow-wrap: anywhere; }
.code-sent strong { color: var(--text); font-weight: 600; }
.stack input.code-input { font-size: 22px; letter-spacing: .4em; text-align: center; font-variant-numeric: tabular-nums; }
.captcha { margin: 4px 0 6px; min-height: 65px; }
.login-links { display: flex; justify-content: space-between; margin-top: 12px; }
.login-links.login-mode { justify-content: center; margin-top: 16px; }
.link-button {
  border: 0; background: none; padding: 2px 0;
  color: var(--text-muted); font-size: 13px;
}
.link-button:hover:not(:disabled) { color: var(--accent); text-decoration: underline; }
.link-button:disabled { cursor: default; opacity: .6; }

/* ---------- responsive ---------- */
@media (max-width: 760px) {
  #composer-wrap { padding: 6px 12px calc(6px + env(safe-area-inset-bottom)); }
  #composer { padding: 5px 9px; gap: 2px; border-radius: 16px; }
  #input { padding: 0 3px; line-height: 1.35; }
  #app.keyboard-visible {
    position: fixed;
    top: var(--visual-viewport-top);
    left: 0;
    width: 100%;
    height: var(--visual-viewport-height);
  }
  #app.keyboard-visible #composer-wrap { padding-bottom: 6px; }
  #sidebar {
    height: 100dvh;
    box-shadow: var(--shadow);
  }
  .msg.user .body { max-width: 92%; }
  #topbar { padding: 6px 10px; gap: 6px; }
  #topbar .title-wrap { min-width: 0; }
  #topbar .chat-status { max-width: 55px; overflow: hidden; text-overflow: ellipsis; }
  #topbar .model-controls { gap: 4px; }
  #topbar .field select { max-width: none; padding: 3px 2px; font-size: 16px; }
  #topbar #model { width: 120px; }
  .transcript { padding-top: 16px; }

  /* Keep the whole action row on one line: the hint is the only part that can
     be dropped without losing a control, so it goes first. */
  .composer-actions { flex-wrap: nowrap; gap: 6px; }
  .composer-actions #attach { margin-right: auto; }
  .hint { display: none; }
  #attach { padding: 6px 8px; font-size: 15px; }
  /* Buttons must keep their label on one line; white-space alone is not
     enough without nowrap plus a no-shrink floor. */
  .send, .stop { padding: 7px 12px; white-space: nowrap; flex: none; }
  .field { flex: none; }

  /* The gallery uses the whole phone screen, with touch-sized controls. */
  .library { width: 100%; }
  .library-head { padding: max(14px, env(safe-area-inset-top)) 16px 14px; }
  .library-storage { padding: 12px 16px 14px; }
  .library-heading h2 { font-size: 18px; }
  .library-heading p { display: none; }
  .library-list { padding: 16px 14px max(28px, env(safe-area-inset-bottom)); }
  .lib-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .lib-content { padding: 8px; }
  .lib-btn, .lib-more > summary { min-height: 40px; }
  .lib-more > summary { width: 40px; }
  .lib-menu button { min-height: 40px; }
  /* Chips and cards get finger-sized hit areas. */
  .chip { max-width: 46vw; }
  .chip-x { padding: 4px 6px; font-size: 13px; }
  .image-card { max-width: 100%; }
  .image-action { padding: 9px 8px; font-size: 13px; }
  .lib-btn { font-size: 12px; }
  .ghost.icon { padding: 8px 10px; }
  .msg-image img { max-width: 62vw; max-height: 62vw; }
}

/* Stop iOS from zooming when a control smaller than 16px takes focus. */
@media (hover: none) {
  .chip-x, .image-action, .lib-btn, .ghost, .stop { touch-action: manipulation; }
  #composer-wrap { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
}
@media (max-width: 760px) and (hover: none) {
  #composer-wrap { padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
  #input { font-size: 16px; }
}

/* ---------- personal library dock + upward drawer ---------- */
.library-dock {
  width: 100%; min-height: 44px; border: 0; border-top: 1px solid var(--border);
  background: transparent; padding: 9px 12px; display: flex; align-items: center;
  gap: 8px; color: var(--text); text-align: left; font-size: 13px;
}
.library-dock:hover, .library-dock[aria-expanded="true"] { background: var(--bg-inset); }
.library-dock-icon { color: var(--text-muted); font-size: 15px; }
.library-dock .library-usage { margin-left: auto; flex: none; font-family: var(--mono); }
.library-dock-chevron { color: var(--text-faint); transition: transform .15s ease; }
.library-dock[aria-expanded="true"] .library-dock-chevron { transform: rotate(180deg); }
.library-head .ghost { flex: none; }

/* The attachment control always keeps the paperclip footprint while its
   spinner temporarily replaces it, so upload completion cannot leave stale UI. */
.attach-button { width: 34px; height: 34px; display: grid; place-items: center; }
.paperclip { width: 19px; height: 19px; fill: none; stroke: currentColor;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.attach-spinner { display: none; width: 15px; height: 15px; border-radius: 50%;
  border: 2px solid var(--border-strong); border-top-color: var(--accent);
  animation: spin .8s linear infinite; }
.attach-button.busy .paperclip { display: none; }
.attach-button.busy .attach-spinner { display: block; }

/* ---------- persisted turn actions ---------- */
.turn-actions {
  display: flex; align-items: center; gap: 4px; margin-top: 9px;
  color: var(--text-faint); min-height: 32px;
}
.turn-action {
  width: 32px; height: 32px; flex: none; display: grid; place-items: center;
  border: 0; background: transparent; border-radius: 7px; padding: 0;
  color: inherit;
}
.turn-action svg { width: 17px; height: 17px; fill: none; stroke: currentColor;
  stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.turn-action:hover:not(:disabled) { color: var(--text); background: var(--bg-inset); }
.turn-action:disabled { cursor: not-allowed; opacity: .4; }
.turn-details-toggle {
  cursor: pointer; width: 32px; height: 32px; flex: none; padding: 0; border: 0;
  background: transparent;
  display: grid; place-items: center; border-radius: 7px;
  color: var(--text-muted);
}
.turn-details-toggle::before { content: "›"; display: block;
  font: 24px/1 var(--sans); transition: transform .15s; }
.turn-details-toggle[aria-expanded="true"]::before { transform: rotate(90deg); }
.turn-details-toggle:hover { color: var(--text); background: var(--bg-inset); }
.turn-metrics {
  display: flex; flex: 1; align-items: center; gap: 10px; min-width: 0;
  overflow-x: auto; color: var(--text-muted);
  font: 11px/1.7 var(--mono); white-space: nowrap;
}
.turn-metrics > span { display: inline-flex; align-items: center; gap: 3px; flex: none; }
.turn-metrics b { font-weight: 500; }

/* ---------- prompt details + image lightbox ---------- */
.image-prompt {
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 25%, var(--border));
  background: color-mix(in srgb, var(--accent) 8%, var(--bg-elevated));
  color: var(--accent); font-size: 12px;
}
.image-prompt > summary { cursor: pointer; list-style: none; padding: 8px 10px; font-weight: 600; }
.image-prompt > summary::-webkit-details-marker { display: none; }
.image-prompt > summary::before { content: "›"; display: inline-block; margin-right: 6px; transition: transform .15s; }
.image-prompt[open] > summary::before { transform: rotate(90deg); }
.image-prompt p { margin: 0; padding: 0 10px 9px; color: var(--text-muted); line-height: 1.55; overflow-wrap: anywhere; }
.image-frame button, .msg-image, .lib-preview.image-preview { cursor: zoom-in; }
.image-frame .image-open { width: 100%; border: 0; padding: 0; background: transparent; }
.lightbox {
  position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.86);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 52px 22px 24px;
}
.lightbox img { max-width: min(94vw, 1400px); max-height: calc(100vh - 110px);
  object-fit: contain; border-radius: 8px; box-shadow: 0 12px 48px rgba(0,0,0,.45); }
.lightbox-close { position: absolute; top: 14px; right: 16px; width: 38px; height: 38px;
  border: 0; border-radius: 50%; background: rgba(255,255,255,.14); color: #fff; font-size: 18px; }
.lightbox-close:hover { background: rgba(255,255,255,.25); }
.lightbox-caption { max-width: 760px; margin: 10px 0 0; color: #ddd; font-size: 13px;
  text-align: center; overflow-wrap: anywhere; }
body.lightbox-open { overflow: hidden; }

@media (max-width: 760px) {
  #attach { padding: 0; font-size: inherit; }
  #sidebar {
    position: fixed; top: 0; left: 0; z-index: 20;
    margin-left: 0; transform: translateX(0);
    transition: transform .18s ease;
  }
  #sidebar.collapsed { margin-left: 0; transform: translateX(-100%); }
}
.msg-image.image-open { border: 0; padding: 0; background: transparent; }
