:root {
  /* Apple system palette */
  --bg: #000000;
  --bg-elev: #1c1c1e;
  --bg-elev-2: #2c2c2e;
  --bg-solid: #000000;
  --separator: rgba(120, 120, 128, 0.24);
  --text: #ffffff;
  --text-secondary: rgba(255, 255, 255, 0.55);
  --text-tertiary: rgba(255, 255, 255, 0.35);
  --accent: #0a84ff;
  --accent-hover: #4da3ff;
  --danger: #ff453a;
  --ok: #30d158;
  --warn: #ff9f0a;
  --user: #0a84ff;
  --user-text: #ffffff;
  --assistant: #2c2c2e;
  --assistant-text: #ffffff;
  --radius: 22px;
  --radius-sm: 12px;
  --radius-xs: 10px;
  --radius-pill: 999px;
  --sidebar-w: 300px;
  color-scheme: dark light;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f2f2f7;
    --bg-elev: #ffffff;
    --bg-elev-2: #ffffff;
    --bg-solid: #ffffff;
    --separator: rgba(60, 60, 67, 0.12);
    --text: #000000;
    --text-secondary: rgba(60, 60, 67, 0.60);
    --text-tertiary: rgba(60, 60, 67, 0.30);
    --accent: #007aff;
    --accent-hover: #0051d5;
    --danger: #ff3b30;
    --ok: #34c759;
    --warn: #ff9500;
    --user: #007aff;
    --user-text: #ffffff;
    --assistant: #e5e5ea;
    --assistant-text: #000000;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: -apple-system-body;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 17px;
  line-height: 1.47059;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
#app { display: flex; flex-direction: column; height: 100%; }

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 14px;
  padding-top: env(safe-area-inset-top);
  min-height: calc(44px + env(safe-area-inset-top));
  background: var(--bg-elev);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.33px solid var(--separator);
  flex-wrap: nowrap;
  position: relative;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand h1 { font-size: 17px; margin: 0; font-weight: 600; letter-spacing: -0.022em; }
.brand-logo { border-radius: 7px; width: 24px; height: 24px; }
.brand-actions { display: flex; align-items: center; gap: 2px; margin-left: 2px; }
.brand-actions .icon-btn { width: 32px; height: 32px; color: var(--text-secondary); }
.brand-actions .icon-btn:hover { color: var(--accent); }
.topbar-actions { display: flex; align-items: center; gap: 4px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; padding: 0;
  border: none; border-radius: var(--radius-xs);
  background: transparent; color: var(--accent); cursor: pointer;
  transition: background .15s ease, color .15s ease, transform .1s ease;
}
.icon-btn:hover { background: rgba(10, 132, 255, 0.12); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.icon-only { display: inline-flex; }
.status-online { color: var(--ok); }
.status-online:hover { background: rgba(48, 209, 88, 0.12); }
.status-offline { color: var(--warn); }
.status-offline:hover { background: rgba(255, 159, 10, 0.12); }
.icon-btn svg { display: block; pointer-events: none; }

.online-toggle {
  color: var(--ok);
  transition: color .2s ease, background .15s ease;
}
.online-toggle:hover { background: rgba(48, 209, 88, 0.12); }
.online-toggle.online-off {
  color: var(--text-secondary);
  opacity: .7;
}
.online-toggle.online-off:hover {
  color: var(--warn);
  background: rgba(255, 159, 10, 0.12);
  opacity: 1;
}

/* Layout */
.layout { flex: 1; display: flex; min-height: 0; position: relative; }
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--bg-elev);
  border-right: none;
  display: flex; flex-direction: column; min-height: 0;
  transition: width .25s cubic-bezier(0.32, 0.72, 0, 1);
  overflow: hidden;
  z-index: 20;
}
.sidebar[data-open="false"] { width: 0; }

.sidebar-handle {
  position: relative;
  flex-shrink: 0;
  width: 14px;
  align-self: stretch;
  display: flex; align-items: center; justify-content: center;
  background: transparent;
  border: none;
  border-right: 0.33px solid var(--separator);
  color: var(--text-tertiary);
  cursor: pointer;
  padding: 0;
  transition: transform .25s cubic-bezier(0.32, 0.72, 0, 1), border-color .2s ease, color .2s ease;
  z-index: 15;
}
.sidebar-handle:hover {
  color: var(--accent);
  border-right-color: var(--accent);
  background: rgba(10, 132, 255, 0.05);
}
.sidebar-handle svg {
  display: block;
  transition: transform .25s cubic-bezier(0.32, 0.72, 0, 1);
}
.sidebar[data-open="false"] ~ .sidebar-handle svg { transform: rotate(180deg); }

.sidebar-search {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  border-bottom: 0.33px solid var(--separator);
}
.sidebar-search #btn-new {
  flex-shrink: 0;
  color: var(--accent);
  background: transparent;
  border: 0.5px solid var(--separator);
}
.sidebar-search #btn-new:hover { background: rgba(10, 132, 255, 0.10); }
.sidebar-search input {
  flex: 1; padding: 7px 10px; border-radius: var(--radius-xs);
  background: var(--bg-elev-2); color: var(--text); border: none;
  font: inherit; font-size: 15px; outline: none;
}
.sidebar-search input::placeholder { color: var(--text-tertiary); }
.sidebar-search input:focus { box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.20); }
.chat-list { flex: 1; overflow-y: auto; padding: 8px 10px; display: flex; flex-direction: column; gap: 2px; }
.chat-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 11px; border-radius: var(--radius-xs); cursor: pointer;
  color: var(--text); font-size: 15px;
  transition: background .12s ease;
}
.chat-item:hover { background: rgba(120, 120, 128, 0.12); }
.chat-item.active { background: var(--accent); color: #fff; }
.chat-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .tokens {
  flex-shrink: 0;
  font-size: 11px;
  color: var(--text-tertiary);
  padding: 2px 5px;
  border-radius: 6px;
  background: rgba(120, 120, 128, 0.12);
  opacity: 0;
  transition: opacity .12s ease;
}
.chat-item:hover .tokens { opacity: 1; }
.chat-item.active .tokens { background: rgba(255,255,255,0.18); color: rgba(255,255,255,0.85); }
.chat-item .del {
  border: none; background: transparent; color: currentColor; cursor: pointer;
  font-size: 16px; padding: 0 2px; border-radius: 6px; opacity: 0;
  transition: opacity .12s ease;
}
.chat-item:hover .del,
.chat-item.active .del { opacity: 0.55; }
.chat-item .del:hover { opacity: 1; }
.chat-item.active .del { color: #fff; }
.chat-item.active .del:hover { opacity: 1; }

.chat-item .chat-rename {
  flex: 1;
  background: var(--bg-solid);
  color: var(--text);
  border: 0.5px solid var(--accent);
  border-radius: 6px;
  padding: 4px 8px;
  font: inherit;
  font-size: 15px;
  outline: none;
}
.chat-item .chat-rename:focus { box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.20); }

/* Chat */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-split {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
  overflow: hidden;
}
.messages {
  flex: 1; overflow-y: auto; padding: 16px 18px 10px;
  display: flex; flex-direction: column; gap: 10px;
  max-width: 880px; width: 100%; margin: 0 auto;
}
.messages.browser-active {
  flex: 1 1 60%;
  max-width: 60%;
  min-width: 280px;
  max-height: 100%;
  border-right: 0.33px solid var(--separator);
  border-bottom: none;
}
.msg {
  max-width: 76%;
  padding: 10px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap; word-wrap: break-word;
  font-size: 17px;
  line-height: 1.35;
}
.msg.user {
  align-self: flex-end;
  background: var(--user);
  color: var(--user-text);
  border-bottom-right-radius: 6px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--assistant);
  color: var(--assistant-text);
  border-bottom-left-radius: 6px;
}
.msg.system {
  align-self: center;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  border: 0.5px solid var(--separator);
  border-radius: var(--radius-sm);
  padding: 5px 12px;
}

/* Markdown rendering inside messages */
.md-list { margin: 8px 0; padding-left: 20px; }
.md-list li { margin: 4px 0; }
.md-table { border-collapse: collapse; margin: 8px 0; width: 100%; font-size: 15px; }
.md-table td, .md-table th { border: 0.5px solid var(--separator); padding: 6px 10px; text-align: left; }
.md-table th { background: var(--bg-elev-2); font-weight: 600; }
.msg h2, .msg h3, .msg h4 { margin: 10px 0 6px; font-weight: 700; }
.msg strong { font-weight: 700; }
.msg em { font-style: italic; }
.msg ul { list-style-type: disc; }
.msg ol { list-style-type: decimal; }
.msg.error {
  align-self: center;
  color: var(--danger);
  border: 0.5px solid var(--danger);
  background: rgba(255, 69, 58, 0.08);
  font-size: 13px;
  border-radius: var(--radius-sm);
  padding: 7px 12px;
}
.md-link {
  color: var(--accent);
  text-decoration: underline;
  cursor: default;
}
.msg .meta {
  display: block;
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 5px;
  font-weight: 400;
}

.msg-header {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  opacity: 0.8;
}

.msg-content {
  display: block;
}
.typing span { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: currentColor; margin: 0 2px; animation: blink 1.2s infinite both; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100% { opacity: .2; } 40% { opacity: 1; } }
.empty-hint { text-align: center; color: var(--text-secondary); font-size: 15px; margin-top: 28vh; line-height: 1.6; }

/* Composer */

.composer {
  width: 100%;
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg-elev);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.33px solid var(--separator);
}
.composer .connection-select,
.composer .agent-select {
  width: 160px; flex: 0 0 auto;
  padding: 9px 10px; border-radius: var(--radius-sm);
  background: var(--bg-elev-2); color: var(--text); border: 0.5px solid var(--separator);
  font: inherit; font-size: 13px; outline: none; cursor: pointer;
  min-height: 40px;
  -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%230a84ff' stroke-width='2.5' 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 8px center;
  padding-right: 24px;
}
.composer .connection-select option,
.composer .agent-select option { font-size: 14px; }
.composer .connection-select:focus,
.composer .agent-select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.15); }
.composer-input-wrap {
  flex: 1 1 auto;
  display: flex; flex-direction: column;
  min-width: 120px;
  gap: 6px;
}
.composer-input-row {
  display: flex; gap: 8px; align-items: flex-end;
}
.composer-input-row textarea {
  flex: 1 1 auto; resize: none; max-height: 160px; min-height: 40px;
  min-width: 80px;
  padding: 9px 14px; border-radius: var(--radius);
  background: var(--bg-elev-2); color: var(--text); border: 0.5px solid var(--separator);
  font: inherit; font-size: 17px; outline: none;
}
.composer-input-row textarea::placeholder { color: var(--text-tertiary); }
.composer-input-row textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.15); }
.attach-btn {
  width: 40px; height: 40px; flex-shrink: 0;
  border-radius: var(--radius);
  color: var(--text-secondary);
  transition: color .15s ease, background .15s ease, transform .1s ease;
}
.attach-btn:hover { color: #bf5af2; background: rgba(191, 90, 242, 0.12); }
.attach-btn:active { transform: scale(0.92); }
.browser-btn {
  width: 40px; height: 40px; flex-shrink: 0;
  border-radius: var(--radius);
  color: var(--text-secondary);
}
.browser-btn:hover { color: var(--accent); background: rgba(10, 132, 255, 0.12); }
.browser-btn.active { color: var(--accent); background: rgba(10, 132, 255, 0.18); }
.link-btn {
  width: 40px; height: 40px; flex-shrink: 0;
  border-radius: var(--radius);
  color: var(--text-secondary);
  position: relative;
}
.link-btn:hover { color: var(--ok); background: rgba(48, 209, 88, 0.12); }
.link-btn.linked { color: var(--ok); }
.link-btn.linked::after {
  content: "";
  position: absolute;
  inset: auto 5px 5px auto;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--ok);
  border: 1.5px solid var(--bg-elev);
}

.image-mode-btn {
  width: 40px; height: 40px; flex-shrink: 0;
  border-radius: var(--radius);
  color: var(--text-secondary);
}
.image-mode-btn:hover { color: var(--accent); background: rgba(10, 132, 255, 0.12); }
.image-mode-btn.active { color: var(--accent); background: rgba(10, 132, 255, 0.18); }
.image-mode-btn.active:hover { background: rgba(10, 132, 255, 0.26); }

.voice-recording-bar {
  flex: 1 1 auto;
  display: flex; align-items: center; gap: 10px;
  min-height: 40px; max-height: 160px;
  padding: 8px 14px;
  border-radius: var(--radius);
  background: var(--bg-elev-2);
  border: 0.5px solid var(--danger);
  color: var(--danger);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  animation: pulse-border 1.2s infinite ease-in-out;
}
.voice-recording-bar[hidden] { display: none; }
.voice-recording-bar .voice-wave {
  display: flex; align-items: center; gap: 3px;
  height: 18px; flex-shrink: 0;
}
.voice-recording-bar .voice-wave span {
  display: block; width: 4px; height: 100%;
  background: currentColor;
  border-radius: 2px;
  animation: wave 0.9s ease-in-out infinite;
}
.voice-recording-bar .voice-wave span:nth-child(1) { animation-delay: -0.4s; }
.voice-recording-bar .voice-wave span:nth-child(2) { animation-delay: -0.3s; }
.voice-recording-bar .voice-wave span:nth-child(3) { animation-delay: -0.2s; }
.voice-recording-bar .voice-wave span:nth-child(4) { animation-delay: -0.1s; }
.voice-recording-bar .voice-wave span:nth-child(5) { animation-delay: 0s; }
@keyframes wave {
  0%, 100% { transform: scaleY(0.3); }
  50% { transform: scaleY(1); }
}
@keyframes pulse-border {
  0%, 100% { border-color: var(--danger); }
  50% { border-color: rgba(255, 69, 58, 0.4); }
}
.voice-recording-text {
  flex: 1 1 auto;
  font-size: 17px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.voice-recording-bar.has-text .voice-recording-text {
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-y: auto;
  max-height: 140px;
}
.stop-voice-btn {
  width: 32px; height: 32px; flex-shrink: 0;
  border-radius: var(--radius-xs);
  color: currentColor;
  background: transparent;
}
.stop-voice-btn:hover { background: rgba(255, 69, 58, 0.12); }

.attachment-preview {
  display: flex; flex-wrap: wrap; gap: 6px;
  max-height: 86px; overflow-y: auto;
}
.attachment-preview[hidden] { display: none; }
.attachment-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 8px 4px 4px;
  background: rgba(10, 132, 255, 0.12);
  border: 0.5px solid rgba(10, 132, 255, 0.25);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text);
  max-width: 100%;
}
.attachment-chip img {
  width: 28px; height: 28px; object-fit: cover;
  border-radius: 5px;
  flex-shrink: 0;
}
.attachment-chip .file-icon {
  width: 28px; height: 28px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 5px;
  background: var(--bg-elev-2);
  color: var(--accent);
  font-size: 10px; font-weight: 600;
  text-transform: uppercase;
}
.attachment-chip .file-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 160px;
}
.attachment-chip .file-size {
  color: var(--text-secondary); font-size: 11px; flex-shrink: 0;
}
.attachment-chip .remove {
  border: none; background: transparent; color: var(--text-secondary);
  cursor: pointer; padding: 0; width: 18px; height: 18px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  flex-shrink: 0;
}
.attachment-chip .remove:hover { color: var(--danger); background: rgba(255, 69, 58, 0.12); }
.linked-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: rgba(48, 209, 88, 0.12);
  border: 0.5px solid rgba(48, 209, 88, 0.25);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--ok);
  max-width: 100%;
  cursor: default;
}
.linked-chip svg {
  display: block;
  flex-shrink: 0;
}
.linked-chip .link-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}
.linked-chip .remove {
  border: none; background: transparent; color: var(--text-secondary);
  cursor: pointer; padding: 0; width: 18px; height: 18px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  flex-shrink: 0;
}
.linked-chip .remove:hover { color: var(--danger); background: rgba(255, 69, 58, 0.12); }
.extracted-badge {
  font-size: 10px;
  color: var(--ok);
  border: 0.5px solid var(--ok);
  border-radius: var(--radius-pill);
  padding: 1px 6px;
  margin-left: 4px;
}
.stop {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; flex-shrink: 0;
  padding: 0; border-radius: var(--radius-sm);
  border: none; background: var(--danger); color: #fff;
  cursor: pointer;
  transition: transform .1s ease;
}
.stop:hover { filter: brightness(1.1); }
.stop:active { transform: scale(0.92); }
.stop[hidden] { display: none; }
.stop svg { display: block; pointer-events: none; }

.send {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; flex-shrink: 0;
  padding: 0; border-radius: var(--radius-sm);
  border: none; background: transparent; color: var(--accent);
  cursor: pointer;
  transition: transform .1s ease, opacity .15s ease, color .15s ease;
}
.send:hover { color: var(--accent-hover); }
.send:active { transform: scale(0.92); }
.send:disabled { opacity: 0.4; cursor: not-allowed; }
.send[hidden] { display: none; }
.send svg { display: block; pointer-events: none; }

/* Browser view */
.browser-view {
  flex: 1 1 50%;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 280px;
  overflow: hidden;
}
.browser-view[hidden] { display: none !important; }

.browser-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 0.33px solid var(--separator);
  flex-shrink: 0;
}
.browser-toolbar .icon-btn {
  width: 32px;
  height: 32px;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.browser-toolbar .icon-btn:hover {
  color: var(--accent);
  background: rgba(10, 132, 255, 0.12);
}
.browser-toolbar .icon-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}
.browser-toolbar .icon-btn:disabled:hover {
  color: var(--text-secondary);
  background: transparent;
}

.browser-url-wrap {
  flex: 1;
  min-width: 0;
}
.browser-url-input {
  width: 100%;
  padding: 7px 12px;
  border-radius: var(--radius-xs);
  background: var(--bg-elev-2);
  color: var(--text);
  border: 0.5px solid var(--separator);
  font: inherit;
  font-size: 14px;
  outline: none;
}
.browser-url-input::placeholder { color: var(--text-tertiary); }
.browser-url-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.15);
}

.browser-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
  background: #fff;
}
.browser-iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}
.browser-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-tertiary);
  background: var(--bg);
}
.browser-placeholder svg {
  opacity: 0.3;
}
.browser-placeholder p {
  margin: 0;
  font-size: 15px;
}
.browser-start-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px;
}
.browser-start-page p {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-secondary);
  margin: 0;
}
.browser-start-search {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 400px;
}
.browser-start-search input {
  flex: 1;
  padding: 10px 14px;
  border-radius: var(--radius-xs);
  background: var(--bg-elev-2);
  color: var(--text);
  border: 0.5px solid var(--separator);
  font: inherit;
  font-size: 15px;
  outline: none;
}
.browser-start-search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.15);
}
.browser-start-search input::placeholder {
  color: var(--text-tertiary);
}

/* Split handle for browser mode */
.split-handle {
  flex: 0 0 6px;
  cursor: col-resize;
  background: transparent;
  position: relative;
  z-index: 5;
  transition: background 0.15s ease;
}
.split-handle:hover,
.split-handle.active {
  background: var(--accent);
  opacity: 0.3;
}
.split-handle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 2px;
  height: 24px;
  border-radius: 1px;
  background: var(--separator);
}
.split-handle:hover::after {
  background: var(--accent);
}


.generated-image {
  display: block;
  max-width: 100%;
  max-height: 520px;
  border-radius: var(--radius-sm);
  margin: 10px 0;
  box-shadow: 0 4px 16px rgba(0,0,0,.15);
}

.search-citations {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.search-chip {
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg-elev-2);
  border: 0.5px solid var(--separator);
  border-radius: var(--radius-pill);
  padding: 3px 9px;
  text-decoration: none;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kb-progress {
  font-size: 13px;
  color: var(--text-secondary);
  padding: 8px 0;
}
.kb-progress.kb-error { color: var(--danger); }

/* Dialogs */
.dialog {
  border: none; border-radius: var(--radius);
  background: var(--bg-elev); color: var(--text); padding: 0;
  width: calc(100% - 40px);
  max-width: 540px;
  margin: auto;
  max-height: 90vh; overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
.dialog[open] { display: block; }
.dialog::backdrop { background: rgba(0,0,0,.4); backdrop-filter: blur(2px); }
.dialog h2 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.021em; }
.muted { color: var(--text-secondary); font-size: 13px; margin: 6px 0 0; line-height: 1.4; }

/* Settings content */
#settings-form { padding: 20px; }
.settings-section { margin: 18px 0 0; }
.section-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 8px;
}
.section-header h3 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.021em; }
.section-actions { display: flex; align-items: center; gap: 8px; }
.section-actions .add-btn { font-size: 15px; }
.add-btn {
  border: none; background: transparent; color: var(--accent);
  font: inherit; font-size: 17px; font-weight: 400; cursor: pointer;
  padding: 4px 0;
}
.add-btn:hover { color: var(--accent-hover); }
.add-btn:active { opacity: 0.6; }

/* Settings rows */
.rows { display: flex; flex-direction: column; }
.row-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 0;
  border-bottom: 0.33px solid var(--separator);
}
.row-item:last-child { border-bottom: none; }
.row-info { flex: 1; min-width: 0; }
.row-title { font-size: 16px; font-weight: 400; color: var(--text); }
.row-sub { font-size: 13px; color: var(--text-secondary); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.row-actions .icon-btn {
  width: 32px; height: 32px; color: var(--accent);
  background: transparent; border: none;
}
.row-actions .icon-btn:hover { background: rgba(120, 120, 128, 0.12); }
.row-actions .icon-btn.danger { color: var(--danger); }
.row-actions .icon-btn.danger:hover { background: rgba(255, 69, 58, 0.12); }
.row-actions .lock-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; flex-shrink: 0;
  color: var(--text-tertiary);
}
.default-badge {
  display: inline-block;
  font-size: 10px; font-weight: 600;
  color: var(--accent);
  border: 0.5px solid var(--accent);
  border-radius: var(--radius-pill);
  padding: 1px 6px;
  margin-left: 6px;
  vertical-align: middle;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

/* Toggle */
.toggle { position: relative; display: inline-block; width: 36px; height: 22px; flex-shrink: 0; }
.toggle input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.toggle-slider {
  position: absolute; inset: 0; border-radius: var(--radius-pill);
  background: rgba(120, 120, 128, 0.32); transition: background .2s ease;
}
.toggle-slider::before {
  content: ""; position: absolute; left: 2px; top: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.15);
  transition: transform .2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.toggle input:checked + .toggle-slider { background: var(--ok); }
.toggle input:checked + .toggle-slider::before { transform: translateX(14px); }
.toggle input:focus-visible + .toggle-slider { box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.25); }

/* Form rows inside dialogs */
.row { display: flex; flex-direction: column; gap: 6px; margin: 14px 0; font-size: 14px; color: var(--text-secondary); }
.row.row-inline {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.row.row-inline > span { flex: 1; }
.row textarea, .row input, .row select {
  padding: 10px 12px; border-radius: var(--radius-xs); border: 0.5px solid var(--separator);
  background: var(--bg-elev-2); color: var(--text); font: inherit; font-size: 17px; outline: none;
}
.row textarea::placeholder, .row input::placeholder, .row select::placeholder { color: var(--text-tertiary); }
.row textarea:focus, .row input:focus, .row select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.15); }
.row textarea { resize: vertical; }
.row select { cursor: pointer; -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%230a84ff' stroke-width='2.5' 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; padding-right: 28px;
}

/* Danger zone */
.danger-zone {
  margin-top: 24px; padding: 14px 0;
  border-top: 0.33px solid var(--separator);
}
.danger-zone h3 { margin: 0 0 4px; font-size: 15px; color: var(--danger); font-weight: 600; }
.danger-btn {
  margin-top: 10px; padding: 8px 14px; border-radius: var(--radius-xs);
  border: none; background: var(--danger); color: #fff;
  cursor: pointer; font: inherit; font-size: 15px; font-weight: 500;
  transition: transform .1s ease, filter .1s ease;
}
.danger-btn:hover { filter: brightness(1.1); }
.danger-btn:active { transform: scale(0.96); }

/* Dialog actions */
.dialog-actions { display: flex; justify-content: flex-end; gap: 12px; margin-top: 18px; flex-wrap: wrap; }
.chip {
  border: none; background: var(--bg-elev-2); color: var(--accent);
  padding: 8px 14px; border-radius: var(--radius-xs); cursor: pointer;
  font: inherit; font-size: 15px; font-weight: 500; line-height: 1.2;
  transition: transform .1s ease, background .1s ease;
}
.chip:hover { background: rgba(10, 132, 255, 0.14); }
.chip:active { transform: scale(0.96); }
.chip.primary { background: var(--accent); color: #fff; }
.chip.primary:hover { background: var(--accent-hover); }

/* Add/edit connection/agent dialog */
#add-form, #agent-form { padding: 20px; }
#add-title, #agent-title { margin-bottom: 0; }
#add-prompt, .muted { margin-top: 4px; }
#add-key-wrap { display: flex; flex-direction: column; gap: 6px; }
#add-key-wrap[hidden] { display: none; }

/* Responsive */
@media (max-width: 720px) {
  .icon-btn.icon-only { display: inline-flex; }
  .sidebar { position: absolute; inset: 0 auto 0 0; height: 100%; width: var(--sidebar-w); transform: translateX(0); transition: transform .25s cubic-bezier(0.32, 0.72, 0, 1); border-right: 0.33px solid var(--separator); }
  .sidebar[data-open="false"] { width: var(--sidebar-w); transform: translateX(-100%); }
  .sidebar-handle {
    position: absolute; inset: 0 auto 0 0; width: 14px;
    border-right: none; border-left: 0.33px solid var(--separator);
    background: var(--bg-elev);
    transition: left .25s cubic-bezier(0.32, 0.72, 0, 1), border-color .2s ease, color .2s ease;
  }
  .sidebar[data-open="true"] ~ .sidebar-handle { left: var(--sidebar-w); }
  .sidebar[data-open="false"] ~ .sidebar-handle { left: 0; }
  .msg { max-width: 86%; font-size: 16px; }
  .composer .connection-select,
  .composer .agent-select { width: 120px; flex: 0 0 auto; font-size: 12px; padding: 8px 22px 8px 8px; }
  .composer-input-wrap { min-width: 80px; }
  .composer-input-row textarea { min-width: 60px; }
  .attachment-chip .file-name { max-width: 100px; }

  .browser-toolbar {
    padding: 6px 8px;
    gap: 4px;
  }
  .browser-toolbar .icon-btn {
    width: 28px;
    height: 28px;
  }
  .browser-url-input {
    font-size: 13px;
    padding: 6px 10px;
  }

  /* On mobile, stack browser panels vertically */
  .messages.browser-active {
    max-width: 100%;
    max-height: 40%;
    border-right: none;
    border-bottom: 0.33px solid var(--separator);
  }
  .browser-view {
    min-width: 0;
  }
  .split-handle {
    cursor: row-resize;
    height: 6px;
    width: auto;
  }

  .dialog,
  .dialog[open] {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    top: auto;
    margin: 0;
    max-width: none;
    width: 100%;
    max-height: 88vh;
    border-radius: var(--radius) var(--radius) 0 0;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
    animation: sheetUp .28s cubic-bezier(0.32, 0.72, 0, 1);
  }
  .dialog::backdrop { background: rgba(0,0,0,.45); }
  @keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
}

/* Dialog fallback when showModal is unsupported */
.dialog-open {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
  display: block;
}
@media (max-width: 720px) {
  .dialog-open { top: auto; left: 0; right: 0; bottom: 0; transform: none; }
}

/* Unsupported File System Access API banner */
.fsa-banner {
  background: var(--warn);
  color: #000;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  text-align: center;
}
.fsa-banner[hidden] { display: none !important; }

/* Project panel in sidebar */
.project-panel {
  border-bottom: 0.33px solid var(--separator);
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: 40%;
}
.project-panel[hidden] { display: none !important; }
.project-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-elev-2);
}
.project-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  overflow: hidden;
}
.project-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-actions { display: flex; align-items: center; gap: 2px; }
.project-actions .icon-btn { width: 28px; height: 28px; color: var(--text-secondary); }
.project-actions .icon-btn:hover { color: var(--accent); }

/* File tree */
.file-tree {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px;
  font-size: 13px;
  user-select: none;
}
.file-tree ul {
  list-style: none;
  margin: 0;
  padding: 0 0 0 14px;
}
.file-tree > ul { padding-left: 0; }
.file-tree li {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 6px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-tree li:hover { background: rgba(120, 120, 128, 0.12); }
.file-tree li.active { background: var(--accent); color: #fff; }
.file-tree .folder-toggle {
  width: 14px; height: 14px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--text-tertiary);
  flex-shrink: 0;
}
.file-tree li.collapsed > ul { display: none; }
.file-tree li.collapsed > .folder-toggle svg { transform: rotate(-90deg); }
.file-tree .file-icon {
  width: 14px; height: 14px;
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  color: var(--text-tertiary);
}
.file-tree .file-name { overflow: hidden; text-overflow: ellipsis; }

/* Tool / thinking blocks in chat */
.tool-block {
  margin: 8px 0;
  border: 0.5px solid var(--separator);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  overflow: hidden;
}
.tool-block summary {
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  list-style: none;
}
.tool-block summary::-webkit-details-marker { display: none; }
.tool-block .tool-body {
  padding: 8px 10px;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  max-height: 300px;
  overflow-y: auto;
  border-top: 0.5px solid var(--separator);
}
.tool-block.tool-read summary { color: var(--accent); }
.tool-block.tool-write summary { color: var(--ok); }
.tool-block.tool-patch summary { color: var(--warn); }
.tool-block.tool-search summary { color: var(--text-secondary); }
.tool-block.tool-navigate summary { color: var(--accent); }
.tool-block.tool-click_link summary { color: var(--accent); }
.tool-block.tool-fill_form summary { color: var(--warn); }
.tool-block.tool-submit_form summary { color: var(--ok); }
.tool-block.tool-extract_links summary { color: var(--text-secondary); }
.tool-block.tool-extract_forms summary { color: var(--text-secondary); }
.tool-block.tool-get_page_structure summary { color: var(--accent); }
.tool-block.tool-thinking summary { color: var(--text-secondary); font-style: italic; }

.link-dialog { max-width: 360px; }
.link-dialog .dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 0.33px solid var(--separator);
  margin-bottom: 0;
}
.link-dialog .dialog-header h3 { margin: 0; font-size: 17px; }
.link-dialog-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px 16px;
}
.link-choice {
  display: grid;
  grid-template-columns: 32px 1fr;
  grid-template-rows: auto auto;
  gap: 2px 12px;
  align-items: center;
  text-align: left;
  padding: 12px;
  border-radius: var(--radius-sm);
  border: 0.5px solid var(--separator);
  background: var(--bg-elev-2);
  color: var(--text);
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}
.link-choice:hover {
  background: rgba(10, 132, 255, 0.10);
  border-color: var(--accent);
}
.link-choice svg {
  grid-row: 1 / 3;
  color: var(--accent);
}
.link-choice span:first-of-type {
  font-weight: 600;
  font-size: 15px;
}
.link-choice .link-desc {
  font-size: 13px;
  color: var(--text-secondary);
  grid-column: 2;
}

/* File preview dialog */
.preview-dialog { max-width: 900px; width: min(900px, 94vw); max-height: 84vh; }
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.dialog-header h3 { margin: 0; font-size: 16px; }
.preview-content {
  margin: 0;
  padding: 12px;
  background: var(--bg-elev-2);
  border-radius: var(--radius-sm);
  overflow: auto;
  max-height: calc(84vh - 80px);
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.45;
  white-space: pre;
}

/* Diff styling inside preview / tool blocks */
.diff-line {
  display: block;
  padding: 0 6px;
  margin: 0 -6px;
}
.diff-add { background: rgba(48, 209, 88, 0.15); color: var(--ok); }
.diff-remove { background: rgba(255, 69, 58, 0.15); color: var(--danger); }
.diff-hunk { color: var(--text-tertiary); }

/* Agent loop progress strip */
.agent-progress {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg-elev-2);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
}
.agent-progress .spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--separator);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Streaming cursor */
.stream-cursor {
  color: var(--accent);
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.msg.streaming {
  min-height: 1.5em;
}

/* Voice UI */
.voice-btn {
  width: 40px; height: 40px; flex-shrink: 0;
  border-radius: var(--radius);
  color: var(--text-secondary);
  transition: color .15s ease, background .15s ease, transform .1s ease;
}
.voice-btn:hover { color: var(--accent); background: rgba(10, 132, 255, 0.12); }
.voice-btn:active { transform: scale(0.92); }
.voice-btn.recording {
  color: var(--danger);
  background: rgba(255, 69, 58, 0.12);
  animation: pulse 1.2s infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }

.speak-btn {
  margin-top: 6px;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg-elev-1);
  border: 1px solid var(--separator);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.speak-btn:hover, .speak-btn.speaking {
  color: var(--accent);
  border-color: var(--accent);
}

/* Mobile tweaks for project panel */
@media (max-width: 720px) {
  .project-panel { max-height: 35%; }
  .preview-dialog { max-height: 92vh; width: 100%; border-radius: var(--radius) var(--radius) 0 0; }
}
