/* ───── Theme-independent tokens ───── */
:root {
  --sidebar-w: 280px;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  /* Full viewport height that respects mobile browser chrome (URL bar, etc.) */
  --app-height: 100vh;

  /* Code blocks stay dark in every theme (readable on any background, like
     VSCode's light mode keeps terminals dark). */
  --code-bg: #0b0e14;
  --code-border: #2a2f3a;
  --code-text: #e6e8ee;
}

@supports (height: 100dvh) {
  :root { --app-height: 100dvh; }
}

/* ───── Light theme (default) ───── */
:root,
[data-theme="light"] {
  --bg: #f4f6fb;
  --bg-elev: #ffffff;
  --bg-elev-2: #f1f3f9;
  --bg-elev-3: #e7eaf3;
  --border: #dfe3ec;
  --border-strong: #c5ccda;
  --text: #1a1d26;
  --text-muted: #5f6676;
  --text-dim: #8b93a7;
  --accent: #4c6bf5;
  --accent-hover: #3a5af0;
  --accent-contrast: #ffffff;
  --user-bubble: #dfe8ff;
  --assistant-bubble: #ffffff;
  --danger: #dc2626;
  --danger-bg: #fde8e8;
  color-scheme: light;
}

/* ───── Dark theme ───── */
[data-theme="dark"] {
  --bg: #0f1115;
  --bg-elev: #171a21;
  --bg-elev-2: #1f232c;
  --bg-elev-3: #262b36;
  --border: #2a2f3a;
  --border-strong: #3a4050;
  --text: #e6e8ee;
  --text-muted: #8b93a7;
  --text-dim: #5f6676;
  --accent: #7c9cff;
  --accent-hover: #93aeff;
  --accent-contrast: #0b0d12;
  --user-bubble: #263049;
  --assistant-bubble: #181c25;
  --danger: #ff6b6b;
  --danger-bg: #3a1d1f;
  color-scheme: dark;
}

/* ───── Solarized (warm light, coder classic) ───── */
[data-theme="solarized"] {
  --bg: #fdf6e3;
  --bg-elev: #fefbf0;
  --bg-elev-2: #eee8d5;
  --bg-elev-3: #e3dcc2;
  --border: #dcd5bb;
  --border-strong: #c8c29a;
  --text: #586e75;
  --text-muted: #839496;
  --text-dim: #93a1a1;
  --accent: #268bd2;
  --accent-hover: #1d6a9e;
  --accent-contrast: #fdf6e3;
  --user-bubble: #e3ebf5;
  --assistant-bubble: #fefbf0;
  --danger: #dc322f;
  --danger-bg: #fce1df;
  color-scheme: light;
}

/* ───── Midnight (very dark blue-black) ───── */
[data-theme="midnight"] {
  --bg: #05060a;
  --bg-elev: #0b0e18;
  --bg-elev-2: #121626;
  --bg-elev-3: #1a1f33;
  --border: #1f2540;
  --border-strong: #323a5c;
  --text: #e6eaff;
  --text-muted: #8a93b8;
  --text-dim: #5b6388;
  --accent: #6288ff;
  --accent-hover: #7a9eff;
  --accent-contrast: #05060a;
  --user-bubble: #1a2245;
  --assistant-bubble: #0f1326;
  --danger: #ff5c66;
  --danger-bg: #2e0f14;
  color-scheme: dark;
}

/* ───── Dracula (dark purple, classic dev theme) ───── */
[data-theme="dracula"] {
  --bg: #282a36;
  --bg-elev: #343746;
  --bg-elev-2: #3d4052;
  --bg-elev-3: #4a4d5e;
  --border: #44475a;
  --border-strong: #565a70;
  --text: #f8f8f2;
  --text-muted: #bfbfbf;
  --text-dim: #8889a0;
  --accent: #bd93f9;
  --accent-hover: #ceb0ff;
  --accent-contrast: #282a36;
  --user-bubble: #44475a;
  --assistant-bubble: #343746;
  --danger: #ff5555;
  --danger-bg: #3f1c20;
  color-scheme: dark;
}

/* ───── Nord (arctic blue-gray) ───── */
[data-theme="nord"] {
  --bg: #2e3440;
  --bg-elev: #3b4252;
  --bg-elev-2: #434c5e;
  --bg-elev-3: #4c566a;
  --border: #434c5e;
  --border-strong: #4c566a;
  --text: #eceff4;
  --text-muted: #d8dee9;
  --text-dim: #8d97a9;
  --accent: #88c0d0;
  --accent-hover: #a3d4e0;
  --accent-contrast: #2e3440;
  --user-bubble: #434c5e;
  --assistant-bubble: #3b4252;
  --danger: #bf616a;
  --danger-bg: #3b2225;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* The HTML `hidden` attribute must always win, even when a stylesheet sets
   display: flex / inline-flex / block on the same element. Without this rule
   modals and other [hidden] elements stay visible. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button {
  font: inherit;
  cursor: pointer;
}

button:disabled { opacity: 0.6; cursor: not-allowed; }

input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
  outline: none;
  width: 100%;
}

input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
}

/* ---------- Button variants ---------- */
.primary-btn {
  background: var(--accent);
  color: var(--accent-contrast);
  border: 0;
  border-radius: 8px;
  padding: 9px 14px;
  font-weight: 600;
}
.primary-btn:hover { background: var(--accent-hover); }

.ghost-btn {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
}
.ghost-btn:hover { background: var(--bg-elev-2); }

.danger-btn {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid #4a2528;
  border-radius: 8px;
  padding: 9px 12px;
  font-weight: 500;
}
.danger-btn:hover { background: #4a2528; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 18px;
}
.icon-btn:hover { background: var(--bg-elev-3); }

/* ---------- Login ---------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.login-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.login-card h1 { margin: 0 0 6px 0; font-size: 22px; }
.muted { color: var(--text-muted); margin: 0 0 20px 0; font-size: 14px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.login-card button {
  background: var(--accent);
  color: var(--accent-contrast);
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  font-weight: 600;
}
.login-card button:hover { background: var(--accent-hover); }
.error { color: var(--danger); font-size: 14px; margin: 0; }

/* ---------- App layout ---------- */
body.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: var(--app-height);
  overflow: hidden;
}

.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

/* The sidebar backdrop is only used on mobile (when the sidebar is a slide-in
   overlay). On desktop it must not take any layout space. The mobile media
   query below re-enables it when the sidebar is open. */
.sidebar-backdrop { display: none; }

.sidebar-top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.sidebar-top .primary-btn { flex: 1; }

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.chat-group-label {
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-dim);
  padding: 12px 10px 4px 10px;
  letter-spacing: 0.5px;
}

.chat-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 8px;
  color: var(--text);
  padding: 9px 10px;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.chat-item:hover { background: var(--bg-elev-2); }
.chat-item.active { background: var(--bg-elev-3); }

.chat-item-sub {
  display: block;
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 2px;
}

.sidebar-bottom {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sidebar-user {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 6px;
  color: var(--text-muted);
  font-size: 12px;
  min-width: 0;
}
.sidebar-user-name {
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar-user-role {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  flex-shrink: 0;
}
.sidebar-user-role.admin { color: var(--accent); }

.sidebar-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.sidebar-buttons .ghost-btn {
  padding: 8px 10px;
  font-size: 13px;
}

/* ---------- Main ---------- */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}

.chat-title {
  flex: 1;
  background: transparent;
  border: 1px solid transparent;
  font-size: 16px;
  font-weight: 600;
  padding: 8px 10px;
  min-width: 0;
}
.chat-title:hover { border-color: var(--border); }
.chat-title:focus { border-color: var(--accent); background: var(--bg-elev-2); }

.controls { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.picker {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
}

.picker select {
  min-width: 140px;
  padding: 7px 10px;
  font-size: 13px;
}

.picker-label { white-space: nowrap; }

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  scroll-behavior: smooth;
}

.messages-inner {
  max-width: 880px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 85%;
}

.message.user { align-self: flex-end; align-items: flex-end; }
.message.assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 12px 14px;
  border-radius: 14px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  line-height: 1.5;
  border: 1px solid var(--border);
}

/* Plain-text bubbles (user messages, streaming assistant) preserve newlines */
.bubble.plain { white-space: pre-wrap; }

.message.user .bubble { background: var(--user-bubble); border-color: var(--border-strong); }
.message.assistant .bubble { background: var(--assistant-bubble); }

.meta { font-size: 11px; color: var(--text-muted); }

.bubble img {
  max-width: 260px;
  max-height: 260px;
  border-radius: 8px;
  display: block;
  margin-top: 8px;
  cursor: zoom-in;
  transition: transform 0.1s ease;
}
.bubble img:hover { transform: scale(1.02); }

/* ---------- Image lightbox ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-top: max(20px, env(safe-area-inset-top));
  padding-bottom: max(20px, env(safe-area-inset-bottom));
  cursor: zoom-out;
  animation: lightbox-in 0.15s ease;
}
@keyframes lightbox-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 6px;
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.6);
}
.lightbox-close {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  right: 16px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
}
.lightbox-close:hover { background: rgba(255, 255, 255, 0.2); }

/* ---------- Install banner ---------- */
.install-banner {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: 90px; /* sit above the composer */
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--bg-elev-3);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: 12px 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  max-width: 560px;
  margin: 0 auto;
  animation: install-slide-up 0.25s ease;
}
@keyframes install-slide-up {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.install-banner-icon {
  font-size: 28px;
  flex-shrink: 0;
  line-height: 1;
}

.install-banner-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.install-banner-text strong {
  font-size: 14px;
  color: var(--text);
}
.install-banner-text span {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}

.install-banner-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.install-banner-actions button {
  font-size: 13px;
  padding: 8px 12px;
}

@media (max-width: 800px) {
  .install-banner {
    left: 10px;
    right: 10px;
    bottom: 84px;
    padding: 10px 12px;
    gap: 10px;
    flex-wrap: wrap;
  }
  .install-banner-icon { font-size: 24px; }
  .install-banner-text strong { font-size: 13px; }
  .install-banner-actions {
    width: 100%;
    justify-content: flex-end;
  }
}

/* ---------- Drag-and-drop indicator ---------- */
body.drag-over::before {
  content: 'Laat los om afbeelding toe te voegen';
  position: fixed;
  inset: 14px;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  font-weight: 600;
  font-size: 16px;
  background: rgba(124, 156, 255, 0.08);
  border: 2px dashed var(--accent);
  border-radius: 16px;
  pointer-events: none;
  backdrop-filter: blur(1px);
}

/* ---------- Markdown content in assistant bubbles ---------- */
.bubble.md > :first-child { margin-top: 0; }
.bubble.md > :last-child { margin-bottom: 0; }

.bubble.md p {
  margin: 0 0 8px 0;
}

.bubble.md h1,
.bubble.md h2,
.bubble.md h3,
.bubble.md h4 {
  margin: 16px 0 8px 0;
  line-height: 1.3;
}
.bubble.md h1 { font-size: 20px; }
.bubble.md h2 { font-size: 17px; }
.bubble.md h3 { font-size: 15px; }
.bubble.md h4 { font-size: 14px; color: var(--text-muted); }

.bubble.md ul,
.bubble.md ol {
  margin: 4px 0 8px 0;
  padding-left: 22px;
}
.bubble.md li { margin: 3px 0; }
.bubble.md li > p { margin: 0; }

.bubble.md blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-muted);
}

.bubble.md hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.bubble.md a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.bubble.md a:hover { color: var(--accent-hover); }

.bubble.md strong { font-weight: 700; color: var(--text); }
.bubble.md em { font-style: italic; }
.bubble.md del { text-decoration: line-through; opacity: 0.7; }

.bubble.md code {
  background: var(--code-bg);
  color: var(--code-text);
  border: 1px solid var(--code-border);
  padding: 1px 6px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 0.88em;
}

.bubble.md pre {
  position: relative;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  padding: 12px 14px;
  padding-top: 32px; /* make room for the copy button */
  border-radius: 8px;
  overflow-x: auto;
  margin: 10px 0;
  -webkit-overflow-scrolling: touch;
}
.bubble.md pre code {
  background: transparent;
  border: 0;
  padding: 0;
  font-family: var(--mono);
  font-size: 13px;
  color: var(--code-text);
  white-space: pre;
  display: block;
}

.bubble.md pre .code-copy {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 9px;
  font-size: 11px;
  font-family: var(--sans);
  cursor: pointer;
  transition: all 0.15s ease;
  opacity: 0;
}
.bubble.md pre:hover .code-copy,
.bubble.md pre .code-copy:focus-visible { opacity: 1; }
.bubble.md pre .code-copy:hover {
  background: rgba(124, 156, 255, 0.15);
  color: var(--accent);
  border-color: var(--accent);
}
.bubble.md pre .code-copy.copied {
  opacity: 1;
  color: #8be6a0;
  border-color: #2e5a3e;
  background: rgba(46, 90, 62, 0.2);
}

/* On mobile, always show the copy button since there's no hover */
@media (hover: none) {
  .bubble.md pre .code-copy { opacity: 1; }
}

.bubble pre {
  background: var(--code-bg);
  color: var(--code-text);
  border: 1px solid var(--code-border);
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 13px;
}

.bubble code { font-family: var(--mono); font-size: 13px; }

.empty-chat {
  margin: auto;
  color: var(--text-dim);
  text-align: center;
  max-width: 420px;
}
.empty-chat h2 { color: var(--text-muted); font-weight: 600; }

/* ---------- Composer ---------- */
.composer {
  padding: 14px 16px 18px 16px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
  border-top: 1px solid transparent;
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 880px;
  margin: 0 auto;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px;
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 220px;
  min-height: 38px;
  background: transparent;
  border: 0;
  padding: 8px 6px;
  width: auto;
}
.composer textarea:focus { border-color: transparent; }

#send-btn, #stop-btn { height: 38px; }

.attachments {
  max-width: 880px;
  margin: 0 auto 8px auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 8px;
}

.attachment {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.attachment img { width: 100%; height: 100%; object-fit: cover; display: block; }

.attachment button {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  padding: 0;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 12px;
  line-height: 1;
  border: 0;
}

.streaming::after {
  content: '\25AE';
  margin-left: 2px;
  animation: blink 1s steps(2, start) infinite;
  color: var(--accent);
}

@keyframes blink { to { visibility: hidden; } }

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(2px);
}

.modal-card {
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 900px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
}

.modal-card-small { max-width: 420px; }

.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h2 { margin: 0; font-size: 16px; }

.modal-body {
  padding: 18px;
  overflow-y: auto;
  min-height: 0;
}

.modal-footer {
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* ---------- Agents modal ---------- */
.agents-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 18px;
  padding: 0;
}

.agents-list-panel {
  border-right: 1px solid var(--border);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 400px;
  max-height: 70vh;
  overflow-y: auto;
}

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

.agent-list li button {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  color: var(--text);
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 14px;
}
.agent-list li button:hover { background: var(--bg-elev-2); }
.agent-list li.active button { background: var(--bg-elev-3); }

.agent-edit-panel {
  padding: 18px 18px 18px 0;
  min-height: 400px;
  max-height: 70vh;
  overflow-y: auto;
}

#agent-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#agent-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

#agent-form textarea {
  resize: vertical;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.5;
}

.form-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 4px;
}
.form-actions .spacer { flex: 1; }

.checkbox-label {
  display: flex !important;
  flex-direction: row !important;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  line-height: 1.4;
}
.checkbox-label input[type="checkbox"] {
  margin-top: 2px;
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}
.checkbox-label .hint { color: var(--text-muted); }

.empty-state {
  color: var(--text-dim);
  text-align: center;
  padding: 40px 20px;
}

/* ---------- Settings block inside Users modal ---------- */
.settings-block {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 20px;
}
.settings-title {
  margin: 0 0 6px 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.settings-help {
  margin: 0 0 12px 0;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ---------- Theme picker ---------- */
.theme-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
@media (max-width: 500px) {
  .theme-picker { grid-template-columns: repeat(2, 1fr); }
}
.theme-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px;
  background: var(--bg-elev-2);
  border: 2px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  min-width: 0;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.theme-option:hover { background: var(--bg-elev-3); }
.theme-option input[type="radio"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.theme-option:has(input:checked) {
  border-color: var(--accent);
  background: var(--bg-elev-3);
}
.theme-label {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}
.theme-swatch {
  width: 64px;
  height: 44px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  display: block;
  position: relative;
  overflow: hidden;
}
/* Fake UI preview inside the swatch: sidebar stripe + bubble */
.theme-swatch::before {
  content: '';
  position: absolute;
  top: 0; bottom: 0; left: 0;
  width: 18px;
  border-right: 1px solid currentColor;
  opacity: 0.25;
}
.theme-swatch::after {
  content: '';
  position: absolute;
  right: 6px;
  top: 8px;
  width: 24px;
  height: 10px;
  border-radius: 4px;
  background: #4c6bf5;
}
.theme-swatch-light    { background: #f4f6fb; color: #c5ccda; }
.theme-swatch-light::after    { background: #4c6bf5; }

.theme-swatch-dark     { background: #171a21; color: #3a4050; }
.theme-swatch-dark::after     { background: #7c9cff; }

.theme-swatch-solarized { background: #fdf6e3; color: #c8c29a; }
.theme-swatch-solarized::after { background: #268bd2; }

.theme-swatch-midnight { background: #0b0e18; color: #323a5c; }
.theme-swatch-midnight::after { background: #6288ff; }

.theme-swatch-dracula  { background: #282a36; color: #565a70; }
.theme-swatch-dracula::after  { background: #bd93f9; }

.theme-swatch-nord     { background: #3b4252; color: #4c566a; }
.theme-swatch-nord::after     { background: #88c0d0; }

/* ---------- Memories / Users modal body ---------- */
.modal-help {
  margin: 0 0 16px 0;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.5;
}
.modal-help code {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent);
}

.memory-form {
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.memory-form textarea {
  resize: vertical;
  min-height: 68px;
  font-family: var(--sans);
  font-size: 14px;
}

.memory-form-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.memory-form-row select,
.memory-form-row input {
  flex: 1;
  min-width: 140px;
}

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

.memory-item {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.memory-item-content {
  flex: 1;
  font-size: 14px;
  line-height: 1.5;
  min-width: 0;
  word-wrap: break-word;
}

.memory-item-meta {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
  display: flex;
  gap: 8px;
  align-items: center;
}

.memory-item-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.memory-item-actions button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
}
.memory-item-actions button:hover {
  background: var(--bg-elev-3);
  color: var(--text);
}
.memory-item-actions button.danger:hover {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: #4a2528;
}

.badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--bg-elev-3);
  color: var(--text-muted);
}
.badge.admin { background: rgba(124, 156, 255, 0.15); color: var(--accent); }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev-3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  z-index: 60;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  animation: toast-in 0.2s ease;
}
.toast.success { border-color: #2e5a3e; color: #a8e6b4; }
.toast.error { border-color: #4a2528; color: var(--danger); }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------- Mobile ---------- */
.mobile-only { display: none; }

@media (max-width: 800px) {
  body.app {
    grid-template-columns: 1fr;
  }

  /* Sidebar slides in from the left */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 86vw;
    max-width: 320px;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 20px 0 40px rgba(0, 0, 0, 0.5);
    /* Safe area insets so the sidebar doesn't hide under notches */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.app.sidebar-open .sidebar { transform: translateX(0); }

  /* Real backdrop element — pseudo-elements can't receive taps reliably */
  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 39;
  }
  body.app.sidebar-open .sidebar-backdrop { display: block; }

  .mobile-only { display: inline-flex; }

  /* Top bar: compact 2-row grid.
     Row 1: [hamburger] [chat title]
     Row 2: [model]     [agent]      [delete]
     Explicit grid so the title can never wrap to its own row. */
  .topbar {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    column-gap: 8px;
    row-gap: 6px;
    align-items: center;
    padding: 6px 10px;
    padding-top: max(6px, env(safe-area-inset-top));
  }
  #sidebar-toggle { grid-row: 1; grid-column: 1; }
  .chat-title {
    grid-row: 1;
    grid-column: 2;
    font-size: 15px;
    padding: 6px 8px;
    min-width: 0;
    width: 100%;
  }
  .controls {
    grid-row: 2;
    grid-column: 1 / -1;
    display: flex;
    gap: 6px;
    padding: 0;
    min-width: 0;
  }
  .topbar .picker-label { display: none; }
  .controls .picker {
    flex: 1 1 0;
    min-width: 0;
  }
  .controls .picker select {
    width: 100%;
    min-width: 0;
    padding: 6px 8px;
    font-size: 13px;
    height: 34px;
  }
  #delete-chat-btn {
    flex: 0 0 auto;
    padding: 6px 10px;
    font-size: 12px;
    height: 34px;
  }
  /* Hamburger button: tighter to match the compact header */
  #sidebar-toggle.icon-btn {
    width: 38px;
    min-width: 38px;
    height: 34px;
    font-size: 18px;
  }

  /* Messages: more width, less margin */
  .messages {
    padding: 14px 10px 14px 10px;
  }
  .message { max-width: 94%; }
  .bubble { padding: 10px 12px; }
  .bubble img { max-width: 100%; }
  .message.assistant { align-self: stretch; }
  .message.user { max-width: 90%; }

  /* Composer: safe area + bigger touch targets + 16px font to prevent iOS zoom */
  .composer {
    padding: 10px 10px;
    padding-bottom: max(12px, calc(env(safe-area-inset-bottom) + 10px));
  }
  .composer-row {
    padding: 6px;
    gap: 6px;
    border-radius: 12px;
  }
  .composer textarea {
    font-size: 16px; /* anything below 16px triggers iOS auto-zoom on focus */
    padding: 8px 8px;
    min-height: 44px;
    /* Align buttons with the first line of the textarea */
  }
  .icon-btn, #send-btn, #stop-btn {
    height: 44px;
    min-width: 44px;
    padding: 0 14px;
    flex-shrink: 0;
    align-self: flex-end;
  }

  /* Modals */
  .modal { padding: 12px; align-items: flex-end; }
  .modal-card {
    max-height: 88vh;
    max-height: 88dvh;
    border-radius: 14px 14px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal-card-small { border-radius: 14px; }
  .modal-body { padding: 14px; }
  .modal-help { font-size: 13px; }

  .agents-layout { grid-template-columns: 1fr; }
  .agents-list-panel {
    border-right: 0;
    border-bottom: 1px solid var(--border);
    max-height: 160px;
    min-height: auto;
  }
  .agent-edit-panel { padding: 14px; }

  /* Memories / users modal rows wrap on narrow screens */
  .memory-form-row { flex-wrap: wrap; }
  .memory-form-row select,
  .memory-form-row input {
    flex: 1 1 100%;
    min-width: 0;
  }
  .memory-item { flex-wrap: wrap; gap: 8px; }
  .memory-item-actions { width: 100%; justify-content: flex-end; }

  /* Sidebar buttons already fit in two columns; no change needed there */
  .chat-list { padding: 6px; }
  .chat-item { padding: 11px 10px; font-size: 14px; }
  .chat-item-sub { font-size: 12px; }

  /* Prevent iOS zoom on all form inputs inside modals */
  input[type="text"],
  input[type="password"],
  input[type="number"],
  textarea,
  select {
    font-size: 16px;
  }
}

/* Extra-small phones */
@media (max-width: 380px) {
  .controls .picker select { font-size: 13px; }
  #delete-chat-btn { padding: 7px 10px; }
  .chat-title { font-size: 14px; }
}
