*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #F3F0E8;
  --bg2:        #FFFFFF;
  --bg3:        #EAE7DF;
  --bg4:        #DDD9D0;
  --border:     rgba(0,0,0,0.08);
  --border-s:   rgba(0,0,0,0.14);
  --text:       #1A1915;
  --text2:      #6B6560;
  --text3:      #9C958C;
  --accent:     #B0584A;
  --accent-dim: rgba(176,88,74,0.10);
  --accent-h:   #9C4D40;
  --accent-bd:  rgba(176,88,74,0.30);
  --danger:     #C53030;
  --danger-dim: rgba(197,48,48,0.10);
  --r-sm:  5px;
  --r:     10px;
  --r-lg:  14px;
  --shadow:    0 1px 2px rgba(0,0,0,0.05);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.12);
  --mono: 'SF Mono','JetBrains Mono','Cascadia Code',Menlo,monospace;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --kb-h: 0px;
  --h: 100vh;
  --h: 100dvh;
  --col-pad: 14px;
  --col-max: 760px;
  --head-h: 52px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg:       #2A2520;
    --bg2:      #352F29;
    --bg3:      #3D362F;
    --bg4:      #4A433B;
    --border:   rgba(255,255,255,0.06);
    --border-s: rgba(255,255,255,0.12);
    --text:     #ECE8E1;
    --text2:    #A39E96;
    --text3:    #706B63;
    --accent:   #C46B5B;
    --accent-dim: rgba(196,107,91,0.12);
    --accent-h: #D87C6D;
    --accent-bd: rgba(196,107,91,0.30);
    --danger:   #F87171;
    --danger-dim: rgba(248,113,113,0.12);
    --shadow:    0 1px 2px rgba(0,0,0,0.2);
    --shadow-lg: 0 4px 16px rgba(0,0,0,0.4);
  }
}

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px; line-height: 1.55;
  width: 100%;
  height: 100%; overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

/* ── Login ──────────────────────────────────────────────── */
#login {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.login-card {
  display: flex; flex-direction: column; gap: 12px;
  width: 100%; max-width: 340px;
  align-items: center; text-align: center;
}
.login-logo { font-size: 56px; color: var(--accent); line-height: 0.9; font-family: var(--mono); }
.login-title { font-size: 22px; font-weight: 600; letter-spacing: 0.5px; margin-bottom: 4px; }
#token-input,
#server-input {
  width: 100%; padding: 11px 14px;
  background: var(--bg2); border: 1px solid var(--border-s);
  border-radius: var(--r); color: var(--text); font-size: 14px;
  outline: none; font-family: var(--mono);
}
#token-input:focus,
#server-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim); }
.login-hint {
  font-size: 11px; color: var(--text3); line-height: 1.5;
  font-family: var(--mono); padding: 4px 4px 0; text-align: left;
}
.login-err { color: var(--danger); font-size: 12px; min-height: 16px; }
.btn-primary {
  width: 100%; padding: 10px;
  background: var(--accent); border: none; border-radius: var(--r);
  color: white; font-size: 14px; font-weight: 500;
  cursor: pointer; font-family: inherit;
}
.btn-primary:hover { background: var(--accent-h); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── App ────────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-columns: 240px 1fr;
  height: var(--h);
  position: fixed; inset: 0;
  overflow: hidden;
  transition: grid-template-columns 0.18s ease;
}
#app.sidebar-collapsed { grid-template-columns: 56px 1fr; }

aside {
  background: var(--bg3);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.side-head {
  height: calc(var(--head-h) + var(--safe-t));
  padding: var(--safe-t) 12px 0;
  display: flex; align-items: center; gap: 8px;
}
.side-brand {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 8px;
  overflow: hidden;
}
.side-collapse svg { transition: transform 0.18s ease; }
#app.sidebar-collapsed .side-collapse svg { transform: rotate(180deg); }
.side-search {
  display: none;
  padding: 6px 12px 10px;
}
.side-search.open { display: block; }
.side-search input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text);
  border-radius: var(--r);
  padding: 8px 10px;
  outline: none;
}
.side-search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim); }
.side-logo {
  font-size: 25px; color: var(--accent); line-height: 0.9;
  font-family: var(--mono); width: 30px; text-align: center;
  flex-shrink: 0;
}
.side-title {
  font-size: 14px; font-weight: 600; letter-spacing: 0.3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  width: 26px; height: 26px;
  background: transparent; border: 1px solid var(--border-s);
  border-radius: var(--r-sm); cursor: pointer; color: var(--text2);
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg4); color: var(--text); }
.icon-btn svg { width: 12px; height: 12px; }

.side-actions {
  padding: 4px 8px 0;
  display: flex; flex-direction: column; gap: 2px;
}
.side-action {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 8px;
  background: transparent; border: 0;
  border-radius: var(--r-sm);
  color: var(--text2); cursor: pointer;
  font: inherit; font-size: 13px;
  text-align: left;
  width: 100%;
}
.side-action:hover { background: var(--bg4); color: var(--text); }
.side-action-ico {
  width: 18px; height: 18px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text2);
  flex-shrink: 0;
}
.side-action-ico svg { width: 14px; height: 14px; }
.side-action:hover .side-action-ico { color: var(--text); }
.side-action-lbl {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.side-recents {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  padding-top: 10px;
}
.side-recents-title {
  padding: 4px 16px 6px;
  font-size: 10.5px; color: var(--text3);
  font-family: var(--mono); letter-spacing: 0.5px;
  text-transform: uppercase;
}
.conv-list { flex: 1; overflow-y: auto; padding: 2px 6px 6px; }
.conv-empty {
  color: var(--text3); font-size: 12px; text-align: center;
  padding: 30px 16px; font-family: var(--mono);
}
.conv-item {
  position: relative;
  padding: 7px 10px; padding-right: 52px;
  border-radius: var(--r-sm);
  cursor: pointer; color: var(--text2); font-size: 13px;
  margin-bottom: 1px; line-height: 1.35;
}
.conv-item .conv-title {
  display: block;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-item .conv-meta {
  display: block; font-size: 10.5px; color: var(--text3);
  margin-top: 1px; font-family: var(--mono);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-item:hover { background: var(--bg4); color: var(--text); }
.conv-item.active { background: var(--bg2); color: var(--text); box-shadow: var(--shadow); }
.conv-del, .conv-rename {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 20px; height: 20px;
  border: none; background: transparent; cursor: pointer;
  color: var(--text3); border-radius: var(--r-sm);
  display: none; align-items: center; justify-content: center;
}
.conv-rename { right: 28px; }
.conv-item:hover .conv-del, .conv-item:hover .conv-rename,
.conv-item.active .conv-del, .conv-item.active .conv-rename { display: flex; }
.conv-del:hover { background: var(--danger-dim); color: var(--danger); }
.conv-rename:hover { background: var(--bg3); color: var(--accent); }
.conv-del svg, .conv-rename svg { width: 11px; height: 11px; }

.side-foot {
  padding: 8px 10px;
  padding-bottom: calc(8px + var(--safe-b));
}
.side-user {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 6px;
  border-radius: var(--r-sm);
}
.side-user-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600;
  flex-shrink: 0;
}
.side-user-meta {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
}
.h-user-name {
  font-size: 12.5px; font-weight: 600; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.h-user-sub {
  font-size: 11px; color: var(--text3);
  font-family: var(--mono);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin: 0;
}
.side-user-action {
  border-color: transparent;
}

/* Collapsed state — hide labels, center icons */
#app.sidebar-collapsed .side-title,
#app.sidebar-collapsed .side-action-lbl,
#app.sidebar-collapsed .side-recents-title,
#app.sidebar-collapsed .conv-list,
#app.sidebar-collapsed .side-search,
#app.sidebar-collapsed .side-user-meta {
  display: none;
}
#app.sidebar-collapsed .side-head { padding: var(--safe-t) 8px 0; justify-content: center; }
#app.sidebar-collapsed .side-brand { flex: 0 0 auto; justify-content: center; }
#app.sidebar-collapsed .side-collapse { display: none; }
#app.sidebar-collapsed .side-actions { padding: 4px 6px 0; }
#app.sidebar-collapsed .side-action { justify-content: center; padding: 8px 0; }
#app.sidebar-collapsed .side-recents { padding-top: 0; }
#app.sidebar-collapsed .side-foot { padding: 8px 6px calc(8px + var(--safe-b)); }
#app.sidebar-collapsed .side-user { justify-content: center; padding: 6px 0; }
#app.sidebar-collapsed .side-user-action { display: none; }
/* Click anywhere on collapsed sidebar to expand */
#app.sidebar-collapsed aside { cursor: e-resize; }
.link-btn {
  background: transparent; border: none; cursor: pointer;
  color: var(--text3); font-family: var(--mono); font-size: 10.5px;
  padding: 2px 0;
}
.link-btn:hover { color: var(--accent); }
.link-btn.danger { color: var(--danger); }
.link-btn.danger:hover { color: var(--danger); text-decoration: underline; }

/* ── Chat ───────────────────────────────────────────────── */
main {
  display: flex; flex-direction: column;
  overflow: hidden;
  min-width: 0;
  height: 100%;
}

header.chat-head {
  display: flex; align-items: center; gap: 8px;
  height: calc(var(--head-h) + var(--safe-t));
  padding: var(--safe-t) var(--col-pad) 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
  position: sticky; top: 0; z-index: 4;
}
.hamburger { display: none; }
.head-settings { margin-left: auto; }
.chat-title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(58vw, 520px);
  min-width: 0;
  font-size: 13px; color: var(--text2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  cursor: text;
  text-align: center;
  pointer-events: auto;
}
.model-pill {
  padding: 4px 10px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  font-family: var(--mono); font-size: 11.5px; color: var(--text);
  cursor: pointer; display: inline-flex; align-items: center; gap: 5px;
  flex-shrink: 0;
}
.model-pill:hover { background: var(--bg4); }
.model-pill svg { color: var(--text3); width: 9px; height: 9px; }

#messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 0;
  -webkit-overflow-scrolling: touch;
  overflow-x: hidden;
  scrollbar-gutter: stable both-edges;
}
.msg-empty {
  color: var(--text3); text-align: center; padding: 60px 24px;
  font-size: 13px; font-family: var(--mono);
}
.welcome {
  display: flex; align-items: center; justify-content: center;
  min-height: 100%;
  padding: 24px 24px 80px;
}
.welcome-inner {
  display: flex; flex-direction: column; align-items: center; gap: 18px;
  margin-top: -60px;
}
.welcome-glyph {
  font-family: var(--mono);
  color: var(--accent);
  font-size: 84px; line-height: 1;
}
.welcome-greet {
  font-size: 26px; color: var(--text);
  font-weight: 500; letter-spacing: 0.2px;
}
@media (max-width: 720px) {
  .welcome-glyph { font-size: 64px; }
  .welcome-greet { font-size: 22px; }
  .welcome-inner { margin-top: -40px; }
}
.msg-wrap {
  max-width: var(--col-max);
  margin: 0 auto 12px;
  padding: 0 var(--col-pad);
  min-width: 0;
}
.msg { display: flex; flex-direction: column; gap: 3px; align-items: stretch; }
.msg-role {
  font-size: 10px; color: var(--text3);
  font-family: var(--mono); letter-spacing: 0.5px;
  margin-bottom: 1px;
}
.bubble {
  background: var(--bg2);
  border: 1px solid var(--border-s);
  border-radius: var(--r-lg);
  padding: 10px 12px;
  box-shadow: var(--shadow);
  font-size: 14px; line-height: 1.55;
  min-width: 0;
  max-width: 100%;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--accent-dim);
  border-color: var(--accent-bd);
  white-space: pre-wrap;
}
.msg.error .bubble {
  background: var(--danger-dim);
  border-color: var(--danger);
  color: var(--danger);
  font-family: var(--mono); font-size: 12.5px;
}
.bubble p { margin: 0 0 6px; }
.bubble p:last-child { margin: 0; }
.bubble code {
  background: var(--bg3); padding: 1px 4px; border-radius: 3px;
  font-family: var(--mono); font-size: 12.5px;
}
.bubble pre {
  background: var(--bg3); padding: 9px 12px; border-radius: var(--r-sm);
  overflow-x: auto; font-family: var(--mono); font-size: 12px; line-height: 1.5;
  margin: 6px 0;
}
.bubble pre code { background: transparent; padding: 0; font-size: inherit; }
.bubble ul, .bubble ol { padding-left: 20px; margin: 6px 0; }
.bubble li { margin: 2px 0; }
.bubble strong { font-weight: 600; }
.bubble a { color: var(--accent); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble blockquote {
  border-left: 3px solid var(--border-s);
  padding-left: 10px; color: var(--text2);
  margin: 6px 0;
}
.bubble table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  margin: 6px 0;
}
.bubble th, .bubble td { border: 1px solid var(--border); padding: 4px 8px; }
.bubble th { background: var(--bg3); }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 {
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.citations {
  margin-top: 9px; padding-top: 8px;
  border-top: 1px dashed var(--border-s);
  font-size: 11.5px; color: var(--text2); font-family: var(--mono);
}
.citations a {
  color: var(--accent); text-decoration: none;
  display: block; padding: 2px 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.citations a:hover { text-decoration: underline; }

.msg-meta {
  font-size: 10px; color: var(--text3); font-family: var(--mono);
  margin-top: 4px; opacity: 0.7;
}

.img-bubble img, .img-bubble svg {
  max-width: 100%; max-height: 420px;
  border-radius: var(--r-sm); display: block;
  cursor: zoom-in;
}

.tool-call {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px; background: var(--bg3);
  border-radius: var(--r-sm); font-size: 11.5px; color: var(--text2);
  font-family: var(--mono); margin-bottom: 6px;
}
.tool-call svg { color: var(--accent); width: 11px; height: 11px; }

.typing { display: inline-flex; gap: 4px; padding: 2px 0; }
.typing span {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--text3); animation: bounce 1.4s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.16s; }
.typing span:nth-child(3) { animation-delay: 0.32s; }
@keyframes bounce {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* ── Input ──────────────────────────────────────────────────────── */
.input-bar {
  flex-shrink: 0;
  padding: 8px 0 calc(8px + var(--safe-b));
  background: var(--bg);
  border-top: 1px solid var(--border);
  z-index: 3;
}
@media (prefers-color-scheme: light) {
  .input-bar { background: var(--bg); }
}
.input-wrap {
  max-width: var(--col-max);
  margin: 0 auto;
  padding: 0 var(--col-pad);
  min-width: 0;
}
.input-card {
  background: var(--bg2);
  border: 1px solid var(--border-s);
  border-radius: var(--r-lg);
  padding: 10px 12px 8px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, box-shadow 0.15s;
}
@media (min-width: 788px) {
  /* no-op; .input-wrap and .msg-wrap use identical max-width + padding */
}
.input-card:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
textarea {
  width: 100%; border: none; outline: none; background: transparent;
  color: var(--text); resize: none; font-family: inherit; font-size: 14px;
  line-height: 1.5; min-height: 22px; max-height: 180px;
}
textarea::placeholder { color: var(--text3); }

.input-actions { display: flex; align-items: center; gap: 5px; margin-top: 6px; }
.tool-btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 9px;
  background: transparent; border: 1px solid var(--border-s);
  border-radius: var(--r-sm); cursor: pointer; color: var(--text2);
  font-size: 11.5px; font-family: inherit; line-height: 1.2;
  transition: all 0.15s;
}
.tool-btn:hover { background: var(--bg3); color: var(--text); }
.tool-btn.active {
  background: var(--accent-dim); color: var(--accent);
  border-color: var(--accent-bd);
}
.tool-btn svg { width: 12px; height: 12px; }
#image-size {
  display: none;
  height: 24px;
  padding: 2px 6px;
  border: 1px solid var(--border-s);
  border-radius: var(--r-sm);
  background: var(--bg2);
  color: var(--text2);
  font-size: 11px;
  font-family: var(--mono);
}
.spacer { flex: 1; }
.send-btn {
  width: 28px; height: 28px;
  background: var(--accent); border: none; border-radius: var(--r-sm);
  color: white; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.send-btn:hover { background: var(--accent-h); }
.send-btn:disabled { opacity: 0.3; cursor: not-allowed; background: var(--text3); }
.send-btn svg { width: 12px; height: 12px; }

.input-hint {
  text-align: center; font-size: 10px; color: var(--text3);
  font-family: var(--mono); margin-top: 6px;
}

/* ── Dropdown ───────────────────────────────────────────── */
.dropdown {
  position: fixed; display: none;
  background: var(--bg2); border: 1px solid var(--border-s);
  border-radius: var(--r-sm); box-shadow: var(--shadow-lg);
  min-width: 140px; padding: 4px; z-index: 50;
}
.dropdown.show { display: block; }
.dd-item {
  padding: 6px 10px; cursor: pointer; border-radius: 3px;
  font-family: var(--mono); font-size: 12px; color: var(--text);
  display: flex; align-items: center; justify-content: space-between;
}
.dd-item:hover { background: var(--bg3); }
.dd-item.active { background: var(--accent-dim); color: var(--accent); }
.dd-item .check { color: var(--accent); }

/* ── Message tools / rich content ───────────────────────── */
.msg { position: relative; }
.msg-tools {
  position: absolute;
  right: 0;
  top: -22px;
  display: flex;
  gap: 4px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s;
}
.msg:hover .msg-tools, .msg:focus-within .msg-tools {
  opacity: 1;
  pointer-events: auto;
}
.msg-tools button, .code-bar button {
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text2);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 11px;
  cursor: pointer;
  font-family: inherit;
}
.msg-tools button:hover, .code-bar button:hover {
  color: var(--text);
  border-color: var(--border-s);
}
.code-wrap {
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
  margin: 10px 0;
  background: var(--bg);
}
.code-bar {
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 0 10px;
  background: var(--bg3);
  border-bottom: 1px solid var(--border);
  color: var(--text3);
  font: 11px/1 var(--mono);
}
.code-wrap pre {
  margin: 0 !important;
  border-radius: 0 !important;
}
.citations {
  display: grid;
  gap: 6px;
  margin-top: 12px;
}
.cite-card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
}
.cite-card:hover { border-color: var(--accent); }
.cite-num { color: var(--accent); font-family: var(--mono); }
.cite-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cite-host {
  grid-column: 2;
  color: var(--text3);
  font-size: 11px;
}
.img-bubble img {
  cursor: zoom-in;
  max-height: min(70vh, 720px);
  object-fit: contain;
}
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%) translateY(8px);
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  opacity: 0;
  pointer-events: none;
  transition: all 0.16s;
  z-index: 90;
  box-shadow: var(--shadow-lg);
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 48px 18px 18px;
  background: rgba(0,0,0,0.78);
}
.lightbox.show { display: flex; }
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--r);
  box-shadow: var(--shadow-lg);
}
.lightbox-close {
  position: absolute;
  right: 16px;
  top: 14px;
  border: 1px solid rgba(255,255,255,0.2);
  background: rgba(0,0,0,0.35);
  color: white;
  border-radius: var(--r);
  padding: 6px 10px;
  cursor: pointer;
}

/* ── Backdrop / mobile ──────────────────────────────────── */
.backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 20;
}
.backdrop.show { display: block; }

@media (max-width: 720px) {
  :root { --col-pad: 10px; --head-h: 48px; }
  #app { display: block; }
  main {
    position: absolute;
    inset: 0;
    width: auto;
    max-width: none;
    height: auto;
    padding-bottom: var(--kb-h);
  }
  header.chat-head,
  #messages,
  .input-bar {
    width: 100%;
    max-width: none;
  }
  #messages { scrollbar-gutter: auto; }
  aside {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: 280px; max-width: 85vw;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.22s ease-out;
    box-shadow: var(--shadow-lg);
  }
  aside.open { transform: translateX(0); }
  .hamburger { display: flex; }
  .input-hint { display: none; }
  .input-actions { flex-wrap: wrap; }
  textarea,
  .side-search input,
  #image-size {
    font-size: 16px;
  }
  .input-wrap {
    width: 100%;
    max-width: 100%;
  }
  .bubble { padding: 9px 12px; }
  .chat-title { max-width: calc(100% - 104px); }
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-s); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text3); }

/* ── Model dropdown grouping ────────────────────────────── */
.dd-group {
  padding: 6px 10px 4px;
  font-family: var(--mono); font-size: 10.5px;
  color: var(--text3); letter-spacing: 0.5px;
  text-transform: uppercase;
}
.dd-group + .dd-item { margin-top: 0; }
.dd-kind {
  font-size: 10px; color: var(--text3);
  margin-left: 6px;
}
.dd-empty {
  padding: 12px; color: var(--text3); font-size: 12px;
  font-family: var(--mono);
}

/* ── Settings modal ─────────────────────────────────────── */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: none;
  align-items: center; justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal.show { display: flex; }
.modal-card {
  background: var(--bg);
  border: 1px solid var(--border-s);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  width: 100%; max-width: 720px;
  max-height: calc(100vh - 40px);
  max-height: calc(100dvh - 40px);
  display: flex; flex-direction: column;
  overflow: hidden;
  min-height: 0;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
}
.modal-title { font-size: 14px; font-weight: 600; letter-spacing: 0.3px; }
.modal-body {
  padding: 14px 16px 18px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 18px;
  flex: 1 1 auto;
  min-height: 0;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.settings-section { display: flex; flex-direction: column; gap: 10px; }
.settings-h {
  font-size: 11px; color: var(--text3);
  font-family: var(--mono); letter-spacing: 0.6px;
  text-transform: uppercase;
}
.settings-row {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
}
.settings-row.presets { gap: 6px; }
.muted { color: var(--text3); font-size: 12px; font-family: var(--mono); }
.muted code { background: var(--bg3); padding: 2px 6px; border-radius: var(--r-sm); }
.btn-secondary {
  background: var(--bg2);
  border: 1px solid var(--border-s);
  border-radius: var(--r-sm);
  color: var(--text); font-size: 12px;
  padding: 5px 10px; cursor: pointer;
  font-family: inherit; line-height: 1.3;
}
.btn-secondary:hover { background: var(--bg3); border-color: var(--text3); }
.btn-secondary.danger { color: var(--danger); border-color: var(--danger); }
.btn-secondary.danger:hover { background: var(--danger-dim); }

.provider-list { display: flex; flex-direction: column; gap: 10px; }

/* Collapsible provider card */
.pcard {
  border: 1px solid var(--border-s);
  border-radius: var(--r);
  background: var(--bg2);
  overflow: hidden;
}
.pcard-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
  user-select: none;
}
.pcard-head:hover { background: var(--bg3); }
.pcard-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--accent);
  flex-shrink: 0;
}
.pcard-dot.off { background: var(--text3); }
.pcard-name {
  font-weight: 600; font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 50%;
}
.pcard-meta {
  flex: 1; min-width: 0;
  color: var(--text3); font-size: 11.5px; font-family: var(--mono);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.pc-key-ok { color: #4d8c5e; }
.pc-key-miss { color: var(--danger); }
.pcard-caret {
  color: var(--text3); font-size: 10px;
  transition: transform 0.15s ease;
  flex-shrink: 0;
}
.pcard.open .pcard-caret { transform: rotate(180deg); }
.pcard-body {
  display: none;
  padding: 6px 12px 14px;
  border-top: 1px solid var(--border);
  flex-direction: column; gap: 14px;
}
.pcard.open .pcard-body { display: flex; }
.pcard-foot {
  display: flex; gap: 10px; align-items: center;
  padding-top: 8px;
  flex-wrap: wrap;
}
.pcard-foot .btn-primary {
  width: auto;
  font-size: 12.5px; padding: 6px 16px;
  border-radius: var(--r-sm);
}
.pcard-foot .h-field-toggle { margin-left: auto; }
.pcard-saved {
  font-size: 11.5px; color: var(--accent);
  opacity: 0; transition: opacity 0.2s ease;
  font-family: var(--mono);
}
.pcard-saved.show { opacity: 1; }

/* Field (label-on-top, single column) */
.h-field { display: flex; flex-direction: column; gap: 5px; }
.h-field > label {
  font-size: 10.5px; color: var(--text3);
  font-family: var(--mono); letter-spacing: 0.4px;
  text-transform: uppercase;
}
.h-field > input[type=text],
.h-field > input[type=password],
.h-field > select,
.h-field-2col > input,
.h-field-with-btn > input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: 13px; padding: 8px 10px;
  outline: none; font-family: var(--mono);
  width: 100%;
}
.h-field > input:focus,
.h-field > select:focus,
.h-field-2col > input:focus,
.h-field-with-btn > input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim);
}
.h-field-2col { display: flex; gap: 6px; }
.h-field-2col > input { flex: 1; min-width: 0; }
.h-field-with-btn { display: flex; gap: 6px; }
.h-field-with-btn > input { flex: 1; min-width: 0; }
.h-field-btn {
  background: var(--bg2);
  border: 1px solid var(--border-s);
  border-radius: var(--r-sm);
  color: var(--text2); font-size: 11.5px;
  padding: 0 12px; cursor: pointer;
  font-family: inherit;
}
.h-field-btn:hover { background: var(--bg3); color: var(--text); }
.h-field-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; color: var(--text);
  cursor: pointer;
}
.h-field-toggle input[type=checkbox] { cursor: pointer; }

/* Model row card */
.pc-models {
  border-top: 1px dashed var(--border);
  padding-top: 12px;
}
.pc-models-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 8px;
  font-family: var(--mono); font-size: 10.5px; color: var(--text3);
  letter-spacing: 0.4px; text-transform: uppercase;
}
.model-rows { display: flex; flex-direction: column; gap: 6px; }
.mrow {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 8px 10px;
  display: flex; flex-direction: column; gap: 6px;
}
.mrow-main {
  display: flex; gap: 6px; align-items: center;
}
.mrow-main .m-id {
  flex: 1; min-width: 0;
  background: transparent;
  border: 0;
  color: var(--text);
  font-size: 13px; padding: 2px 4px;
  outline: none; font-family: var(--mono);
}
.mrow-main .m-id:focus {
  background: var(--bg2);
  border-radius: 4px;
}
.mrow-main .m-kind {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text2);
  font-size: 11.5px; padding: 3px 6px;
  outline: none; font-family: var(--mono);
  cursor: pointer;
}
.mrow-del {
  background: transparent; border: 0;
  color: var(--text3); cursor: pointer;
  width: 24px; height: 24px;
  border-radius: 4px;
  font-size: 16px; line-height: 1;
  font-family: inherit;
}
.mrow-del:hover { background: var(--danger-dim); color: var(--danger); }
.mrow-sub {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  font-size: 11.5px; color: var(--text2);
}
.mrow-link {
  background: transparent; border: 0;
  color: var(--accent); cursor: pointer;
  font-size: 11.5px; padding: 0;
  font-family: inherit;
}
.mrow-link:hover { text-decoration: underline; }
.mrow-flag {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11.5px; color: var(--text2);
  font-family: var(--mono);
  cursor: pointer;
}
.mrow-rename {
  display: none;
  margin-top: 4px;
}
.mrow.expanded .mrow-rename { display: flex; }
.mrow-rename .m-label {
  width: 100%;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: 12px; padding: 5px 8px;
  outline: none; font-family: var(--mono);
}
.mrow-sizes {
  display: flex;
  margin-top: 4px;
}
.mrow-sizes .m-sizes {
  width: 100%;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: 11.5px; padding: 5px 8px;
  outline: none; font-family: var(--mono);
}

@media (max-width: 600px) {
  .modal { padding: 0; align-items: stretch; justify-content: stretch; }
  .modal-card {
    max-height: 100vh;
    max-height: 100dvh;
    height: 100vh;
    height: 100dvh;
    width: 100%; max-width: none;
    border-radius: 0;
    border: none;
  }
  .modal-head {
    padding-top: calc(12px + var(--safe-t));
  }
  .modal-body {
    padding-bottom: calc(18px + var(--safe-b) + var(--kb-h));
  }
}
