/* ---------- Tokens ---------- */
:root {
  --bg: #FAF9F5;
  --bg-side: #F4F2EC;
  --surface: #FFFFFF;
  --bubble: #F0EEE6;
  --hover: rgba(31, 30, 29, 0.055);
  --active: rgba(31, 30, 29, 0.085);
  --border: rgba(31, 30, 29, 0.11);
  --border-strong: rgba(31, 30, 29, 0.2);
  --text: #1F1E1D;
  --text-2: #3D3D3A;
  --muted: #73726C;
  --accent: #C96442;
  --accent-hover: #B4573A;
  --accent-soft: rgba(201, 100, 66, 0.12);
  --on-accent: #FFFFFF;
  --code-block: #F4F2EC;
  --code-inline: rgba(31, 30, 29, 0.06);
  --danger: #B3402F;
  --danger-soft: rgba(179, 64, 47, 0.08);
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.035), 0 0 0 0.5px rgba(31, 30, 29, 0.16);
  --shadow-pop: 0 8px 28px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(31, 30, 29, 0.14);
  --backdrop: rgba(20, 20, 19, 0.32);

  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-serif: ui-serif, "New York", "Iowan Old Style", Georgia, Cambria, "Times New Roman", serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --font-resp: var(--font-serif);

  --side-w: 280px;
  --col: 768px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #262624;
    --bg-side: #1F1E1D;
    --surface: #30302E;
    --bubble: #141413;
    --hover: rgba(250, 249, 245, 0.06);
    --active: rgba(250, 249, 245, 0.1);
    --border: rgba(222, 220, 209, 0.12);
    --border-strong: rgba(222, 220, 209, 0.22);
    --text: #ECEAE2;
    --text-2: #CFCCC3;
    --muted: #9C9A92;
    --accent: #D97757;
    --accent-hover: #C96A4C;
    --accent-soft: rgba(217, 119, 87, 0.16);
    --code-block: #1F1E1D;
    --code-inline: rgba(250, 249, 245, 0.08);
    --danger: #EC8572;
    --danger-soft: rgba(236, 133, 114, 0.1);
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.18), 0 0 0 0.5px rgba(222, 220, 209, 0.2);
    --shadow-pop: 0 8px 28px rgba(0, 0, 0, 0.4), 0 0 0 0.5px rgba(222, 220, 209, 0.2);
    --backdrop: rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #262624;
  --bg-side: #1F1E1D;
  --surface: #30302E;
  --bubble: #141413;
  --hover: rgba(250, 249, 245, 0.06);
  --active: rgba(250, 249, 245, 0.1);
  --border: rgba(222, 220, 209, 0.12);
  --border-strong: rgba(222, 220, 209, 0.22);
  --text: #ECEAE2;
  --text-2: #CFCCC3;
  --muted: #9C9A92;
  --accent: #D97757;
  --accent-hover: #C96A4C;
  --accent-soft: rgba(217, 119, 87, 0.16);
  --code-block: #1F1E1D;
  --code-inline: rgba(250, 249, 245, 0.08);
  --danger: #EC8572;
  --danger-soft: rgba(236, 133, 114, 0.1);
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.18), 0 0 0 0.5px rgba(222, 220, 209, 0.2);
  --shadow-pop: 0 8px 28px rgba(0, 0, 0, 0.4), 0 0 0 0.5px rgba(222, 220, 209, 0.2);
  --backdrop: rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}
:root[data-font="sans"] { --font-resp: var(--font-ui); }

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-user-select: none; user-select: none; }
::selection { background: var(--accent-soft); }

[data-icon] { display: inline-flex; align-items: center; justify-content: center; flex: none; }
[data-icon] > svg { width: 20px; height: 20px; display: block; }

.icon-btn {
  width: 36px; height: 36px;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--text-2);
  display: inline-flex; align-items: center; justify-content: center;
  flex: none;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn:active { background: var(--active); }

.mark { color: var(--accent); }

/* ---------- Layout ---------- */
.app {
  position: fixed;
  left: 0; right: 0; top: 0;
  height: var(--app-h, 100dvh);
  display: flex;
  overflow: hidden;
}

.sidebar {
  width: var(--side-w);
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--bg-side);
  border-right: 0.5px solid var(--border);
  padding: env(safe-area-inset-top) 8px 0 max(8px, env(safe-area-inset-left));
  transition: margin-left 0.22s ease, transform 0.22s ease;
  z-index: 30;
}
.scrim { display: none; }

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

@media (min-width: 900px) {
  .app.side-collapsed .sidebar { margin-left: calc(-1 * var(--side-w)); }
  .app:not(.side-collapsed) #open-side { visibility: hidden; }
}
@media (max-width: 899px) {
  .sidebar {
    position: absolute;
    top: 0; bottom: 0; left: 0;
    width: min(86vw, 320px);
    transform: translateX(-102%);
    box-shadow: none;
  }
  .app.side-open .sidebar { transform: none; box-shadow: var(--shadow-pop); }
  .scrim {
    display: block;
    position: absolute; inset: 0;
    background: var(--backdrop);
    opacity: 0; pointer-events: none;
    transition: opacity 0.22s ease;
    z-index: 25;
  }
  .app.side-open .scrim { opacity: 1; pointer-events: auto; }
}

/* ---------- Sidebar ---------- */
.side-head {
  height: 52px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 2px 0 8px;
}
.brand {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--font-serif);
  font-size: 20px;
  letter-spacing: -0.01em;
}
.brand .mark > svg { width: 22px; height: 22px; }

.new-chat {
  display: flex; align-items: center; gap: 10px;
  margin: 4px 0 6px;
  padding: 6px 8px;
  border: 0; border-radius: 10px;
  background: transparent;
  font-size: 15px; font-weight: 500;
  color: var(--accent);
  text-align: left;
}
.new-chat:hover { background: var(--hover); }
.plus-dot {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
}
.plus-dot > svg { width: 16px; height: 16px; }

.side-search {
  display: flex; align-items: center; gap: 8px;
  margin: 0 0 6px;
  padding: 0 10px;
  height: 38px;
  border-radius: 10px;
  background: var(--hover);
  color: var(--muted);
}
.side-search > [data-icon] > svg { width: 16px; height: 16px; }
.side-search input {
  flex: 1; min-width: 0;
  border: 0; outline: 0; background: transparent;
  font-size: 16px;
  color: var(--text);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0 -4px;
  padding: 0 4px 12px;
}
.chat-group {
  font-size: 12px; font-weight: 500;
  color: var(--muted);
  padding: 14px 10px 4px;
}
.chat-item {
  display: flex; align-items: center;
  height: 36px;
  border-radius: 8px;
  color: var(--text-2);
  font-size: 14px;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); color: var(--text); }
.chat-item .chat-open {
  flex: 1; min-width: 0; height: 100%;
  border: 0; background: transparent;
  text-align: left;
  padding: 0 4px 0 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item .more { width: 30px; height: 30px; opacity: 0; color: var(--muted); }
.chat-item:hover .more, .chat-item.active .more { opacity: 1; }
@media (hover: none) { .chat-item .more { opacity: 1; } }
.chat-empty { color: var(--muted); font-size: 14px; padding: 16px 10px; }

.side-foot {
  border-top: 0.5px solid var(--border);
  padding: 6px 0 max(8px, env(safe-area-inset-bottom));
}
.side-btn {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0; border-radius: 8px;
  background: transparent;
  font-size: 14px;
  color: var(--text-2);
}
.side-btn:hover { background: var(--hover); }
.side-btn > [data-icon] > svg { width: 18px; height: 18px; }

/* ---------- Top bar ---------- */
.topbar {
  flex: none;
  display: flex; align-items: center; gap: 2px;
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  min-height: calc(52px + env(safe-area-inset-top));
  z-index: 5;
}
.chat-title {
  flex: 1; min-width: 0;
  border: 0; background: transparent;
  height: 36px;
  border-radius: 8px;
  padding: 0 8px;
  font-size: 15px;
  color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  text-align: center;
}
.chat-title:not(:empty):hover { background: var(--hover); }
#incognito-btn[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); }

/* ---------- Messages ---------- */
.scroller {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.messages {
  max-width: var(--col);
  margin: 0 auto;
  padding: 8px 16px 24px;
}
.app.is-empty .messages { display: none; }
.app:not(.is-empty) .welcome { display: none; }

.welcome {
  min-height: 100%;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 24px 20px 48px;
  text-align: center;
}
.greeting {
  margin: 0;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: clamp(28px, 7vw, 40px);
  letter-spacing: -0.015em;
  line-height: 1.2;
  color: var(--text-2);
}
.greeting .mark > svg { width: 1.05em; height: 1.05em; }
.welcome-note { margin: 14px 0 0; color: var(--muted); font-size: 14px; max-width: 360px; }

.setup-card {
  margin-top: 24px;
  width: 100%; max-width: 440px;
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 16px;
  font-size: 14.5px; line-height: 1.5;
  color: var(--text-2);
  text-align: left;
}
.setup-card p { margin: 0 0 12px; }
.setup-card a { color: var(--accent); font-size: 14px; }
.setup-row { display: flex; gap: 8px; margin-bottom: 10px; }
.setup-row input { flex: 1; min-width: 0; }

/* Centered composer on wide screens for a fresh chat, like the original */
@media (min-width: 900px) and (min-height: 560px) {
  .app.is-empty .main { justify-content: center; }
  .app.is-empty .topbar { position: absolute; top: 0; left: 0; right: 0; }
  .app.is-empty .scroller { flex: none; overflow: visible; }
  .app.is-empty .welcome { min-height: 0; padding-bottom: 28px; }
  .app.is-empty .composer-wrap { padding-bottom: 12vh; }
}

.msg { margin: 0 0 22px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.bubble {
  max-width: min(85%, 640px);
  background: var(--bubble);
  border-radius: 18px;
  padding: 10px 16px;
  font-size: 16px; line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.bubble .imgs { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0 8px; justify-content: flex-end; }
.bubble .imgs:last-child { margin-bottom: 2px; }
.bubble .imgs img { max-height: 180px; max-width: 100%; border-radius: 10px; display: block; }

.edit-box { width: 100%; max-width: min(100%, 640px); }
.edit-box textarea {
  width: 100%;
  min-height: 90px;
  resize: vertical;
  font-size: 16px; line-height: 1.5;
  padding: 10px 14px;
  border-radius: 14px;
  border: 0.5px solid var(--border-strong);
  background: var(--surface);
  outline: 0;
}
.edit-box .btn-row { justify-content: flex-end; }

.msg-actions {
  display: flex; align-items: center; gap: 2px;
  margin-top: 4px;
  min-height: 30px;
  color: var(--muted);
}
.msg-actions .icon-btn { width: 30px; height: 30px; color: var(--muted); }
.msg-actions .icon-btn > [data-icon] > svg, .msg-actions .icon-btn > svg { width: 16px; height: 16px; }
.msg.user .msg-actions { opacity: 0; transition: opacity 0.15s; }
.msg.user:hover .msg-actions, .msg.user:focus-within .msg-actions { opacity: 1; }
@media (hover: none) { .msg.user .msg-actions { opacity: 1; } }
.meta {
  min-width: 0;
  border: 0; background: transparent;
  color: inherit;
  font-size: 12px;
  margin-left: 2px;
  padding: 6px;
  border-radius: 6px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.meta:hover { background: var(--hover); }
.continue-btn {
  flex: none;
  height: 28px;
  padding: 0 12px;
  margin: 0 4px;
  border-radius: 8px;
  border: 0.5px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}
.continue-btn:hover { background: var(--hover); }
.notice { font-style: normal; line-height: 1.45; }

.typing { padding: 6px 0 2px; color: var(--accent); display: flex; align-items: center; gap: 10px; min-height: 36px; }
.typing > svg { width: 24px; height: 24px; flex: none; animation: ember 1.6s ease-in-out infinite; }
.gen-text { color: var(--muted); font-size: 13px; }
.gen-stop {
  height: 28px;
  padding: 0 12px;
  border-radius: 8px;
  border: 0.5px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}
.versions { display: inline-flex; align-items: center; font-size: 12px; color: var(--muted); }
.versions .icon-btn { width: 26px; height: 28px; font-size: 18px; line-height: 1; }
.versions .icon-btn:disabled { opacity: 0.35; cursor: default; background: transparent; }
@keyframes ember {
  0% { transform: rotate(0deg) scale(0.9); opacity: 0.75; }
  50% { transform: rotate(90deg) scale(1.08); opacity: 1; }
  100% { transform: rotate(180deg) scale(0.9); opacity: 0.75; }
}
@media (prefers-reduced-motion: reduce) { .typing > svg { animation: none; } }

.notice { font-size: 13px; color: var(--muted); font-style: italic; margin-top: 6px; }
.error-box {
  margin-top: 6px;
  border: 0.5px solid var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px; line-height: 1.45;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

/* Thinking */
.thinking { margin: 0 0 10px; }
.thinking > summary {
  list-style: none;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px 4px 6px;
  margin-left: -6px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  -webkit-user-select: none; user-select: none;
}
.thinking > summary::-webkit-details-marker { display: none; }
.thinking > summary:hover { background: var(--hover); color: var(--text-2); }
.thinking > summary > [data-icon] > svg { width: 16px; height: 16px; transition: transform 0.15s; }
.thinking[open] > summary .chev > svg { transform: rotate(180deg); }
.thinking-body {
  margin: 6px 0 4px;
  padding: 2px 0 2px 14px;
  border-left: 2px solid var(--border-strong);
  color: var(--muted);
  font-size: 14px; line-height: 1.55;
  max-height: 340px;
  overflow-y: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ---------- Markdown ---------- */
.md {
  font-family: var(--font-resp);
  font-size: 16.5px;
  line-height: 1.65;
  color: var(--text);
  overflow-wrap: break-word;
}
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 0.9em; }
.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 { font-weight: 600; line-height: 1.3; margin: 1.3em 0 0.5em; }
.md h1 { font-size: 1.45em; }
.md h2 { font-size: 1.25em; }
.md h3 { font-size: 1.1em; }
.md h4, .md h5, .md h6 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 0.9em; padding-left: 1.6em; }
.md li { margin: 0.3em 0; }
.md li > ul, .md li > ol { margin: 0.3em 0 0; }
.md li.task { list-style: none; margin-left: -1.3em; }
.md li.task input { accent-color: var(--accent); margin: 0 0.35em 0 0; vertical-align: -1px; }
.md a { color: inherit; text-decoration: underline; text-decoration-color: var(--muted); text-underline-offset: 2px; }
.md a:hover { color: var(--accent); text-decoration-color: currentColor; }
.md blockquote { margin: 0 0 0.9em; padding: 0 0 0 1em; border-left: 3px solid var(--border-strong); color: var(--text-2); }
.md hr { border: 0; border-top: 0.5px solid var(--border-strong); margin: 1.5em 0; }
.md code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: var(--code-inline);
  border: 0.5px solid var(--border);
  border-radius: 5px;
  padding: 0.1em 0.35em;
}
.code-block {
  margin: 1em 0;
  border: 0.5px solid var(--border);
  border-radius: 10px;
  background: var(--code-block);
  overflow: hidden;
  font-family: var(--font-ui);
}
.code-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 6px 4px 14px;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 0.5px solid var(--border);
}
.copy-code {
  border: 0; background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 6px;
}
.copy-code:hover { background: var(--hover); color: var(--text); }
.code-block pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code-block code {
  background: none; border: 0; padding: 0;
  font-size: 13.5px; line-height: 1.55;
  white-space: pre;
}
.table-wrap { overflow-x: auto; margin: 0 0 1em; }
.md table { border-collapse: collapse; font-family: var(--font-ui); font-size: 14.5px; line-height: 1.45; }
.md th, .md td { border-bottom: 0.5px solid var(--border-strong); padding: 7px 12px 7px 0; text-align: left; vertical-align: top; }
.md th { font-weight: 600; }
.md .al-center { text-align: center; }
.md .al-right { text-align: right; }

/* ---------- Composer ---------- */
.composer-wrap {
  flex: none;
  position: relative;
  width: 100%;
  max-width: calc(var(--col) + 24px);
  margin: 0 auto;
  padding: 0 max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
html.kb-open .composer-wrap { padding-bottom: 8px; }
.composer {
  background: var(--surface);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 10px 10px 8px 12px;
  transition: box-shadow 0.15s;
}
.composer:focus-within { box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05), 0 0 0 0.5px var(--border-strong); }
.composer textarea {
  display: block;
  width: 100%;
  border: 0; outline: 0;
  background: transparent;
  resize: none;
  font-size: 16px; line-height: 1.5;
  padding: 4px 4px 2px;
  max-height: 40vh;
  min-height: 28px;
  overflow-y: auto;
}
.composer textarea::placeholder { color: var(--muted); }
.composer-row { display: flex; align-items: center; gap: 6px; margin-top: 8px; }
.spacer { flex: 1; }
.tool-btn {
  height: 32px; min-width: 32px;
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  padding: 0 7px;
  border: 0.5px solid var(--border-strong);
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  font-size: 13.5px;
}
.tool-btn:hover { background: var(--hover); }
.tool-btn > svg, .tool-btn > [data-icon] > svg { width: 18px; height: 18px; }
.tool-btn[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.tool-btn:disabled { opacity: 0.4; cursor: default; }
.think-label { padding-right: 2px; }
@media (max-width: 380px) { .think-label { display: none; } }

.model-btn {
  display: inline-flex; align-items: center; gap: 2px;
  min-width: 0;
  height: 32px;
  padding: 0 4px 0 8px;
  border: 0; border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  font-size: 14px;
}
.model-btn:hover { background: var(--hover); }
#model-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 44vw; }
.model-btn > [data-icon] > svg { width: 16px; height: 16px; color: var(--muted); }

.send-btn {
  width: 32px; height: 32px;
  flex: none;
  border: 0; border-radius: 9px;
  background: var(--accent);
  color: var(--on-accent);
  display: inline-flex; align-items: center; justify-content: center;
}
.send-btn > svg { width: 18px; height: 18px; }
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.45; cursor: default; background: var(--accent); }

.attachments { display: flex; gap: 8px; flex-wrap: wrap; padding: 2px 2px 8px; }
.thumb { position: relative; width: 64px; height: 64px; }
.thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; border: 0.5px solid var(--border); display: block; }
.thumb button {
  position: absolute; top: -6px; right: -6px;
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 0;
  background: var(--text); color: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 0;
}
.thumb button > svg { width: 12px; height: 12px; }

.footnote { margin: 6px 0 0; text-align: center; font-size: 12px; color: var(--muted); }
@media (max-width: 899px) { .app:not(.is-empty) .footnote { display: none; } }

.jump {
  position: absolute;
  top: -46px; left: 50%;
  transform: translateX(-50%);
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 0;
  background: var(--surface);
  color: var(--text-2);
  box-shadow: var(--shadow);
}
.jump > svg { width: 18px; height: 18px; }

/* ---------- Buttons & fields ---------- */
.btn {
  border: 0.5px solid var(--border-strong);
  background: var(--surface);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 14px;
  color: var(--text);
  white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--danger); }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.btn-row.end { justify-content: flex-end; margin-top: 12px; }
.msg-imgs { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.msg-imgs img { max-height: 220px; max-width: 100%; border-radius: 10px; display: block; }

input[type="text"], input[type="password"], input[type="search"], textarea, select {
  font-size: 16px;
}
.sheet input[type="text"], .sheet input[type="password"], .sheet textarea, .sheet select, .setup-row input {
  width: 100%;
  background: var(--surface);
  border: 0.5px solid var(--border-strong);
  border-radius: 10px;
  padding: 9px 12px;
  outline: 0;
  color: var(--text);
}
.sheet input:focus, .sheet textarea:focus, .sheet select:focus, .setup-row input:focus { border-color: var(--accent); }
.sheet textarea { resize: vertical; line-height: 1.45; }
.sheet select {
  -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a887f' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 18px;
  padding-right: 36px;
}
input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: none; }

/* ---------- Sheets (dialogs) ---------- */
dialog.sheet {
  border: 0;
  padding: 0;
  margin: auto;
  width: min(560px, calc(100vw - 32px));
  max-width: none;
  max-height: min(86dvh, 760px);
  border-radius: 16px;
  background: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow-pop);
  overflow: hidden;
}
dialog.sheet[open] { display: flex; flex-direction: column; }
dialog.sheet::backdrop { background: var(--backdrop); }
#model-dlg { height: min(86dvh, 720px); }
@media (max-width: 640px) {
  dialog.sheet {
    width: 100%;
    margin: auto 0 0;
    border-radius: 16px 16px 0 0;
    max-height: calc(var(--app-h, 100dvh) - env(safe-area-inset-top) - 24px);
    padding-bottom: env(safe-area-inset-bottom);
  }
  #model-dlg { height: calc(var(--app-h, 100dvh) - env(safe-area-inset-top) - 24px); }
}
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 10px 8px 18px;
  flex: none;
}
.sheet-head h2 { margin: 0; font-family: var(--font-serif); font-weight: 500; font-size: 21px; }
.sheet-body { overflow-y: auto; padding: 0 18px 20px; overscroll-behavior: contain; }
.sheet-search {
  display: flex; align-items: center; gap: 8px;
  margin: 0 14px 8px;
  padding: 0 12px;
  height: 42px;
  border-radius: 10px;
  background: var(--surface);
  border: 0.5px solid var(--border-strong);
  color: var(--muted);
  flex: none;
}
.sheet-search > [data-icon] > svg { width: 17px; height: 17px; }
.sheet-search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text); }

.model-list { flex: 1; overflow-y: auto; padding: 0 8px 16px; overscroll-behavior: contain; }
.model-section { font-size: 12px; font-weight: 600; color: var(--muted); padding: 12px 10px 4px; }
.model-row {
  display: flex; align-items: center; gap: 6px;
  border-radius: 10px;
}
.model-row:hover { background: var(--hover); }
.model-row.sel { background: var(--active); }
.model-pick {
  flex: 1; min-width: 0;
  border: 0; background: transparent;
  text-align: left;
  padding: 9px 4px 9px 10px;
  display: flex; flex-direction: column; gap: 2px;
}
.model-pick b { font-weight: 500; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-pick small { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-row .star { color: var(--muted); }
.model-row .star[aria-pressed="true"] { color: var(--accent); }
.model-row .star > [data-icon] > svg, .model-row .star > svg { width: 18px; height: 18px; }
.model-row .star[aria-pressed="true"] svg { fill: currentColor; }
.model-empty { padding: 24px 12px; color: var(--muted); font-size: 14px; text-align: center; }

.set-group { padding: 4px 0 14px; border-bottom: 0.5px solid var(--border); margin-bottom: 10px; }
.set-group:last-child { border-bottom: 0; }
.set-group h3 {
  margin: 10px 0 6px;
  font-size: 12px; font-weight: 600;
  letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--muted);
}
.field { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; }
.field > span { font-size: 14px; color: var(--text-2); }
.key-row { display: flex; gap: 8px; }
.key-row input { flex: 1; min-width: 0; }
.field-row { display: flex; align-items: center; gap: 12px; margin: 10px 0; flex-wrap: wrap; }
.check { display: flex; gap: 12px; align-items: flex-start; margin: 14px 0; font-size: 15px; cursor: pointer; }
.check input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
.check small { display: block; font-size: 13px; color: var(--muted); margin-top: 2px; line-height: 1.4; }
.check code, .muted code { font-family: var(--font-mono); font-size: 12px; }
.muted { color: var(--muted); font-size: 14px; }
.small { font-size: 13px; line-height: 1.45; }
.small-link { color: var(--accent); font-size: 14px; }

/* ---------- Popover & toast ---------- */
.popover {
  position: fixed;
  z-index: 60;
  min-width: 168px;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow-pop);
  padding: 4px;
}
.popover button {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  border: 0; background: transparent;
  text-align: left;
  font-size: 14px;
  padding: 9px 10px;
  border-radius: 8px;
}
.popover button:hover { background: var(--hover); }
.popover button.danger { color: var(--danger); }
.popover button > [data-icon] > svg { width: 16px; height: 16px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(130px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  z-index: 100;
  background: var(--text);
  color: var(--bg);
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-line;
  box-shadow: var(--shadow-pop);
}

/* Scrollbars (desktop) */
@media (hover: hover) {
  .scroller, .chat-list, .model-list, .sheet-body, .thinking-body { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
}
