:root {
  --bg:         #1C1714;
  --bg2:        #2A2420;
  --bg3:        #332c28;
  --border:     #3d3430;
  --text:       #E8E3DC;
  --text-dim:   #a09890;
  --accent:     #A98B65;
  --accent2:    #A6876B;
  --accent-dim: rgba(169,139,101,0.15);
  --danger:     #c0705c;
  --radius:     10px;
  --radius-sm:  6px;
  --font:       -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono:  'Fira Code', 'Cascadia Code', 'Courier New', monospace;
  --sidebar-w:  280px;
  --topbar-h:   52px;
  --input-h:    auto;
  --trans:      0.18s ease;
}

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

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

/* ── Scrollbars ────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-dim); }

/* ── Layout ────────────────────────────────── */
body {
  display: flex;
  position: relative;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 100;
  backdrop-filter: blur(2px);
}
.sidebar-overlay.active { display: block; }

.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 110;
  transform: translateX(-100%);
  transition: transform var(--trans);
}
.sidebar.open { transform: translateX(0); }

/* ── Main shifts right when sidebar is open on desktop ── */
.main {
  transition: margin-left var(--trans);
}

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

.new-chat-row {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
}
.btn-new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--trans), color var(--trans), border-color var(--trans);
}
.btn-new-chat:hover { background: var(--border); border-color: var(--text-dim); }
.btn-new-chat svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn-temp-chat {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg3);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--trans), color var(--trans);
}
.btn-temp-chat:hover { background: var(--border); color: var(--text); }
.btn-temp-chat svg { width: 18px; height: 18px; }

.sidebar-close {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--trans), background var(--trans);
  flex-shrink: 0;
}
.sidebar-close svg { width: 16px; height: 16px; }
.sidebar-close:hover { color: var(--text); background: var(--bg3); }

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

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  cursor: pointer;
  border-radius: 0;
  transition: background var(--trans);
  border-left: 2px solid transparent;
  position: relative;
}
.conv-item:hover { background: var(--bg3); }
.conv-item.active {
  background: var(--accent-dim);
  border-left-color: var(--accent);
}

.conv-item-text {
  flex: 1;
  font-size: 14px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.conv-item-date {
  font-size: 11px;
  color: var(--text-dim);
  flex-shrink: 0;
}
.conv-item-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--trans);
  flex-shrink: 0;
}
.conv-item:hover .conv-item-actions { opacity: 1; }
.conv-item-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 3px 4px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--trans), background var(--trans);
  line-height: 1;
}
.conv-item-btn svg { width: 13px; height: 13px; }
.conv-item-btn:hover { color: var(--text); background: var(--bg3); }
.conv-item-btn.danger { color: var(--text-dim); }
.conv-item-btn.danger:hover { color: #c0504a; background: rgba(192,80,74,0.12); }
.conv-item-btn.pinned { color: var(--accent); }

.conv-item-rename-input {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--accent);
  color: var(--text);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 13px;
  font-family: var(--font);
  outline: none;
  min-width: 0;
}

.sidebar-footer {
  padding: 10px 10px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 4px;
}

.sidebar-footer-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--trans), color var(--trans), border-color var(--trans);
}
.sidebar-footer-btn svg { width: 15px; height: 15px; flex-shrink: 0; }
.sidebar-footer-btn:hover {
  background: var(--bg3);
  color: var(--text);
  border-color: var(--accent);
}

.usage-btn-wrap {
  position: relative;
}
.usage-btn-wrap .icon-btn {
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.usage-btn-wrap .icon-btn:hover {
  border-color: var(--accent);
  background: var(--bg3);
}
.usage-popup {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  min-width: 220px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  z-index: 200;
  box-shadow: 0 4px 24px rgba(0,0,0,0.4);
}
.usage-popup-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 10px;
}
.usage-popup-body { display: flex; flex-direction: column; gap: 6px; }
.usage-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text);
}
.usage-row span:first-child { color: var(--text-dim); }
.usage-model {
  font-family: monospace;
  font-size: 12px;
}
.usage-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* ── Main ──────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100vh;
  overflow: hidden;
  touch-action: pan-y;
}

/* ── Topbar ─────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  height: var(--topbar-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.menu-btn {
  flex-shrink: 0;
}
.menu-btn svg { width: 20px; height: 20px; }

.conv-title-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.conv-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1;
}

.rename-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity var(--trans), color var(--trans), background var(--trans);
  flex-shrink: 0;
}
.rename-btn svg { width: 14px; height: 14px; }
.topbar:hover .rename-btn { opacity: 1; }
.rename-btn:hover { color: var(--accent); background: var(--bg3); }

.conv-title-input {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--accent);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 3px 8px;
  font-size: 15px;
  font-weight: 600;
  font-family: var(--font);
  outline: none;
  min-width: 0;
  width: 100%;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.icon-btn svg { width: 20px; height: 20px; }
#topbar-settings-btn { background: var(--bg3); color: var(--text); }
#topbar-settings-btn:hover { background: var(--border); }

.cost-display {
  font-size: 11px;
  color: var(--text-dim);
  font-family: var(--font-mono);
  padding: 3px 7px;
  background: var(--bg3);
  border-radius: 99px;
  border: 1px solid var(--border);
}
.cost-display:empty { display: none; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--trans), color var(--trans);
}
.icon-btn:hover { background: var(--bg3); color: var(--text); }

/* ── Chat area ──────────────────────────────── */
.chat-area {
  flex: 1 1 0;
  min-height: 0;
  height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  -webkit-transform: translateZ(0);
}

.messages {
  max-width: 860px;
  margin: 0 auto;
  padding: 24px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  touch-action: pan-y;
}

/* ── Message bubbles ────────────────────────── */
.msg-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 16px;
}

.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}
.msg-row.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  margin-top: 2px;
}
.msg-avatar.user { background: var(--accent); color: var(--bg); }
.msg-avatar.assistant { background: var(--bg3); color: var(--accent); border: 1px solid var(--border); }

.msg-bubble {
  max-width: min(76%, 620px);
  padding: 11px 15px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.7;
  word-break: break-word;
}
.msg-bubble.user {
  background: var(--accent);
  color: var(--bg);
  border-radius: var(--radius) var(--radius) 2px var(--radius);
}
.msg-bubble.assistant {
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius) var(--radius) var(--radius) 2px;
}

.msg-meta {
  font-size: 10px;
  color: var(--text-dim);
  padding: 2px 4px;
  display: flex;
  gap: 8px;
  align-items: center;
}
.msg-meta.user { justify-content: flex-end; }

/* ── Markdown content ───────────────────────── */
.msg-content {
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-content p, .msg-content ul, .msg-content ol, .msg-content pre, .msg-content blockquote, .msg-content h1, .msg-content h2, .msg-content h3, .msg-content table {
  white-space: normal;
}
.msg-content pre, .msg-content pre code { white-space: pre; }
.msg-content p { margin-bottom: 8px; }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content ul, .msg-content ol { padding-left: 20px; margin-bottom: 8px; }
.msg-content li { margin-bottom: 3px; }
.msg-content h1, .msg-content h2, .msg-content h3 { 
  color: var(--accent);
  margin: 12px 0 6px;
  font-weight: 600;
}
.msg-content h1 { font-size: 1.2em; }
.msg-content h2 { font-size: 1.1em; }
.msg-content h3 { font-size: 1em; }
.msg-content code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: var(--bg3);
  padding: 1px 5px;
  border-radius: 4px;
  color: var(--accent2);
}
.msg-content pre {
  background: #111 !important;
  border-radius: var(--radius-sm);
  margin: 8px 0;
  overflow-x: auto;
}
.msg-content pre code {
  background: none;
  padding: 12px 14px;
  display: block;
  font-size: 13px;
  color: var(--text);
}
.msg-content blockquote {
  border-left: 3px solid var(--accent);
  padding: 4px 12px;
  color: var(--text-dim);
  margin: 8px 0;
  background: var(--accent-dim);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.msg-content a { color: var(--accent); text-decoration: none; }
.msg-content a:hover { text-decoration: underline; }
.msg-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 13px;
}
.msg-content th, .msg-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.msg-content th { background: var(--bg3); color: var(--accent); }
.msg-content hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }

/* User bubble plain text */
.msg-bubble.user .msg-content { color: var(--bg); }

/* ── Activity log ───────────────────────────── */
.activity-log {
  max-width: min(76%, 600px);
  margin-left: 38px;
  margin-bottom: 4px;
}

.activity-toggle {
  display: flex;
  align-items: center;
  width: 100%;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  cursor: pointer;
  gap: 8px;
  text-align: left;
  transition: background var(--trans), border-color var(--trans);
  color: var(--text-dim);
}
.activity-log[data-open] .activity-toggle {
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}
.activity-toggle:hover { background: var(--bg3); border-color: var(--accent2); }

.activity-toggle-label {
  flex: 1;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--text-dim);
}

/* Spinner shown while active */
.activity-spinner {
  width: 11px; height: 11px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
/* Checkmark shown when done */
.activity-check {
  width: 11px; height: 11px;
  color: #7ec88a;
  flex-shrink: 0;
  display: none;
}
.activity-log.done .activity-spinner { display: none; }
.activity-log.done .activity-check { display: block; }

.activity-chevron {
  width: 12px; height: 12px;
  flex-shrink: 0;
  transition: transform var(--trans);
}
.activity-log:not([data-open]) .activity-chevron { transform: rotate(180deg); }

.activity-items {
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  background: var(--bg);
  max-height: 50vh;
  overflow-y: auto;
}
.activity-log:not([data-open]) .activity-items { display: none; }

.activity-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 10px;
  font-size: 11px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
}
.activity-item:last-child { border-bottom: none; }

.activity-item svg { width: 11px; height: 11px; flex-shrink: 0; color: var(--accent2); }

.activity-item-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Per-item spinner while pending */
.activity-item-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
.activity-item.done .activity-item-dot { display: none; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Typing indicator ───────────────────────── */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 10px 14px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: fit-content;
}
.typing-dot {
  width: 7px; height: 7px;
  background: var(--accent);
  border-radius: 50%;
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}
.thinking-timer {
  font-size: 11px;
  color: var(--text-dim);
  letter-spacing: 0.02em;
  margin-left: 4px;
  font-variant-numeric: tabular-nums;
}

/* ── Thinking block ─────────────────────────── */
.thinking-block {
  max-width: min(76%, 600px);
  margin-left: 38px;
  margin-bottom: 4px;
}
.thinking-toggle {
  display: flex;
  align-items: center;
  width: 100%;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  cursor: pointer;
  gap: 8px;
  text-align: left;
  color: var(--text-dim);
  font-size: 12px;
  transition: background var(--trans), border-color var(--trans);
}
.thinking-block[data-open] .thinking-toggle {
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
}
.thinking-toggle:hover { background: var(--bg3); border-color: var(--accent2); }
.thinking-spinner {
  width: 12px; height: 12px;
  border: 2px solid var(--accent2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.thinking-block.done .thinking-spinner { display: none; }
.thinking-chevron {
  width: 13px; height: 13px;
  margin-left: auto;
  flex-shrink: 0;
  transition: transform var(--trans);
  color: var(--text-dim);
}
.thinking-block:not([data-open]) .thinking-chevron { transform: rotate(-90deg); }
.thinking-body {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
}
.thinking-block:not([data-open]) .thinking-body { display: none; }
.thinking-text {
  margin: 0;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-dim);
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
}
.thinking-block[data-open] .thinking-text {
  max-height: none;
}
.thinking-content {
  margin: 0;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-dim);
}

/* ── Message action buttons ──────────────────── */
.msg-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 3px;
  /* assistant: indent to match bubble (avatar 28px + gap 10px) */
  padding-left: 38px;
  opacity: 0;
  transition: opacity var(--trans);
  min-height: 26px;
}
/* show on hover (pointer devices) */
.msg-group:hover .msg-actions { opacity: 1; }
/* always show on touch devices */
@media (hover: none) {
  .msg-actions { opacity: 1; }
}

/* assistant row: copy btn then meta text, both left-aligned */
.msg-actions.assistant {
  justify-content: flex-start;
}
.msg-actions-meta {
  font-size: 11px;
  color: var(--text-dim);
  letter-spacing: 0.01em;
  padding-left: 8px;
  white-space: nowrap;
}

/* user row: align right, indent from right edge to match bubble */
.msg-actions.user {
  justify-content: flex-end;
  padding-left: 0;
  padding-right: 38px; /* avatar 28px + gap 10px */
}

.msg-action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  /* min touch target 44px tall via padding */
  padding: 5px 9px;
  font-size: 12px;
  cursor: pointer;
  transition: color var(--trans), background var(--trans), border-color var(--trans);
  /* mobile: larger tap area */
  min-height: 32px;
}
.msg-action-btn svg { width: 13px; height: 13px; flex-shrink: 0; }
.msg-action-btn:hover { color: var(--text); background: var(--bg3); border-color: var(--accent2); }
.msg-action-btn:active { transform: scale(0.95); }
.retry-btn { color: var(--accent); border-color: var(--accent2); font-size: 12px; gap: 5px; }
.retry-btn:hover { background: rgba(169,139,101,0.1); }
.error-bubble .error-text { color: #e07070; font-size: 13px; }

/* ── Scroll to bottom btn ───────────────────── */
.scroll-bottom-btn {
  position: fixed;
  bottom: 140px;
  right: 18px;
  width: 36px; height: 36px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-dim);
  transition: background var(--trans), color var(--trans), opacity var(--trans);
  z-index: 20;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.scroll-bottom-btn:hover { background: var(--bg3); color: var(--text); }
.scroll-bottom-btn svg { width: 16px; height: 16px; }
.scroll-bottom-btn.hidden { opacity: 0; pointer-events: none; }

/* ── Input area ─────────────────────────────── */
.input-area {
  background: var(--bg2);
  border-top: 1px solid var(--border);
  padding: 10px 14px 12px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  flex-shrink: 0;
}

.input-pills {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.pill {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 99px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: all var(--trans);
  white-space: nowrap;
}
.pill svg { width: 12px; height: 12px; flex-shrink: 0; }
.pill:hover { border-color: var(--accent); color: var(--text); }
.pill.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}
.pill.model-pill { margin-left: auto; }
/* Right-side pill group */
.pills-right {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}
.pill.model-pill { margin-left: 0; }

/* Thinking pill wrapper (for dropup positioning) */
.thinking-pill-wrap {
  position: relative;
}
.pill.thinking-pill {
  border-color: var(--accent2);
  color: var(--accent);
  gap: 4px;
}
.pill.thinking-pill:hover,
.pill.thinking-pill.open {
  background: var(--accent-dim);
  border-color: var(--accent);
}
.pill-chevron {
  width: 11px !important;
  height: 11px !important;
  transition: transform var(--trans);
  color: var(--accent2);
}
.pill.thinking-pill.open .pill-chevron { transform: rotate(180deg); }

/* Thinking dropup */
.thinking-dropup {
  position: absolute;
  bottom: calc(100% + 7px);
  right: 0;
  min-width: 200px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  z-index: 200;
}
.thinking-dropup-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 14px;
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  transition: background var(--trans);
}
.thinking-dropup-item:hover { background: var(--bg3); }
.thinking-dropup-item.selected { background: var(--accent-dim); }
.dropup-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
  transition: border-color var(--trans), background var(--trans);
}
.thinking-dropup-item.selected .dropup-dot {
  background: var(--accent);
  border-color: var(--accent);
}
.dropup-name {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
  min-width: 52px;
}
.thinking-dropup-item.selected .dropup-name { color: var(--accent); }
.dropup-desc {
  font-size: 11px;
  color: var(--text-dim);
}

.input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.msg-input {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 14px;
  font-family: var(--font);
  resize: none;
  outline: none;
  line-height: 1.5;
  max-height: 180px;
  overflow-y: auto;
  transition: border-color var(--trans);
}
.msg-input::placeholder { color: var(--text-dim); }
.msg-input:focus { border-color: var(--accent); }

.send-btn {
  width: 40px; height: 40px;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--trans), transform var(--trans);
}
.send-btn svg { width: 18px; height: 18px; }
.send-btn:hover { background: var(--accent2); }
.send-btn:active { transform: scale(0.95); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.send-btn .stop-icon { display: none; }
.send-btn.stop-mode .send-icon { display: none; }
.send-btn.stop-mode .stop-icon { display: block; }
.send-btn.stop-mode { background: #7a3a3a; }
.send-btn.stop-mode:hover { background: #9b4a4a; }

/* ── Panels ─────────────────────────────────── */
.panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 200;
  backdrop-filter: blur(2px);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.panel-backdrop.hidden { display: none; }

.panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(400px, 96vw);
  background: var(--bg2);
  border-left: 1px solid var(--border);
  z-index: 210;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--trans);
}
.panel:not(.hidden) { transform: translateX(0); }
.panel.hidden { transform: translateX(100%); pointer-events: none; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.panel-header h2 { font-size: 15px; font-weight: 600; color: var(--text); }
.panel-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: color var(--trans), background var(--trans);
}
.panel-close:hover { color: var(--text); background: var(--bg3); }
.panel-close svg { width: 20px; height: 20px; display: block; }

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 600;
  margin-bottom: -6px;
}

/* ── Model list ─────────────────────────────── */
.model-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.model-card {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color var(--trans), background var(--trans);
  position: relative;
}
.model-card:hover { border-color: var(--accent2); background: var(--bg); }
.model-card.selected {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.model-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.model-card-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.model-card-check {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity var(--trans);
}
.model-card.selected .model-card-check { opacity: 1; }
.model-card-check::after {
  content: '';
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--bg);
}

.model-card-desc {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.model-card-pricing {
  display: flex;
  gap: 12px;
  font-size: 11px;
  font-family: var(--font-mono);
}
.model-price-in { color: var(--accent); }
.model-price-out { color: var(--accent2); }
.model-price-label { color: var(--text-dim); }

.model-card-badges {
  display: flex;
  gap: 5px;
  margin-top: 6px;
  flex-wrap: wrap;
}
.badge {
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 99px;
  border: 1px solid var(--border);
  color: var(--text-dim);
  background: var(--bg);
}
.badge.green  { color: #7ec88a; border-color: #7ec88a30; background: #7ec88a12; }
.badge.blue   { color: #7ab3d6; border-color: #7ab3d630; background: #7ab3d612; }
.badge.purple { color: #b89fd8; border-color: #b89fd830; background: #b89fd812; }

/* ── Thinking levels (panel) ────────────────── */
.thinking-levels {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.thinking-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 14px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color var(--trans), background var(--trans);
}
.thinking-option input[type=radio] { display: none; }
.thinking-option:hover { border-color: var(--accent2); }
.thinking-option:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-dim);
}
.thinking-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  border: 2px solid var(--text-dim);
  flex-shrink: 0;
  transition: border-color var(--trans), background var(--trans);
}
.thinking-option:has(input:checked) .thinking-dot {
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.thinking-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  min-width: 56px;
}
.thinking-option:has(input:checked) .thinking-name { color: var(--accent); }
.thinking-desc {
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.4;
}

/* ── Memory panel ───────────────────────────── */
.memory-add {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.memory-search-row { display: flex; gap: 6px; }

.input-field {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13px;
  font-family: var(--font);
  outline: none;
  min-width: 0;
  transition: border-color var(--trans);
}
.input-field::placeholder { color: var(--text-dim); }
.input-field:focus { border-color: var(--accent); }

.select-field {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 12px;
  font-family: var(--font);
  outline: none;
  cursor: pointer;
  transition: border-color var(--trans);
}
.select-field:focus { border-color: var(--accent); }

.btn-primary {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--trans);
}
.btn-primary:hover { background: var(--accent2); }

.memory-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.memory-item {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  transition: border-color var(--trans);
}
.memory-item:hover { border-color: var(--accent2); }
.memory-item-body { flex: 1; min-width: 0; }
.memory-item-content {
  font-size: 13px;
  color: var(--text);
  word-break: break-word;
  margin-bottom: 3px;
}
.memory-item-cat {
  font-size: 10px;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.memory-item-delete {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  transition: color var(--trans);
  font-size: 13px;
  line-height: 1;
  flex-shrink: 0;
}
.memory-item-delete:hover { color: var(--danger); }

/* ── Settings panel ─────────────────────────── */
.settings-row {
  display: flex;
  gap: 8px;
}
.settings-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: -8px;
}

/* ── Empty state ────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 60px 20px;
  color: var(--text-dim);
  text-align: center;
}
.empty-state-icon {
  width: 48px; height: 48px;
  opacity: 0.3;
}
.empty-state h3 { font-size: 16px; color: var(--text); font-weight: 500; opacity: 0.7; }
.empty-state p { font-size: 13px; max-width: 280px; }

.empty-state-temp {
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  margin: 20px;
  background: var(--bg3);
}
.empty-state-temp .empty-state-icon { opacity: 0.2; }
.empty-state-temp h3 { font-size: 14px; color: var(--text-dim); font-weight: 500; }
.empty-state-temp p { font-size: 12px; color: var(--text-dim); opacity: 0.8; }

/* ── Copy button on code blocks ─────────────── */
pre { position: relative; }
.copy-btn {
  position: absolute;
  top: 7px; right: 7px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  padding: 3px 8px;
  font-size: 10px;
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--trans), color var(--trans);
}
pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text); }

/* ── Auth overlay ────────────────────────────── */
.auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}
.auth-overlay.hidden { display: none !important; }
body.auth-visible .main,
body.auth-visible .sidebar,
body.auth-visible .sidebar-overlay,
body.auth-visible .panel,
body.auth-visible .panel-backdrop,
body.auth-visible .confirm-modal { display: none !important; }
.auth-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 32px;
  width: 100%;
  max-width: 320px;
}
.auth-box h2 {
  margin: 0 0 4px 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}
.auth-hint {
  margin: 0 0 20px 0;
  font-size: 12px;
  color: var(--text-dim);
}
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-input {
  padding: 10px 12px;
  font-size: 15px;
  font-family: var(--font);
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
}
.auth-input:focus { border-color: var(--accent); }
.auth-submit {
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--trans);
}
.auth-submit:hover { background: var(--accent2); }
.auth-error {
  margin: 12px 0 0 0;
  font-size: 13px;
  color: var(--danger);
}

/* ── PWA: safe areas (notch, home indicator) ──── */
html.pwa .topbar {
  padding-top: env(safe-area-inset-top);
  min-height: calc(var(--topbar-h) + env(safe-area-inset-top));
  height: auto;
}
html.pwa .sidebar {
  padding-top: env(safe-area-inset-top);
}
html.pwa .panel {
  padding-top: env(safe-area-inset-top);
}
html.pwa .input-area {
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}
html.pwa .input-area::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: env(safe-area-inset-bottom);
  transform: translateY(100%);
  background: var(--bg2);
}
html.pwa .scroll-bottom-btn {
  bottom: calc(140px + env(safe-area-inset-bottom));
}
@media (max-width: 767px) {
  html.pwa .chat-area {
    padding-bottom: calc(280px + env(safe-area-inset-bottom));
  }
  html.pwa .messages {
    padding-bottom: calc(80px + env(safe-area-inset-bottom));
  }
  html.pwa .scroll-bottom-btn {
    bottom: calc(290px + env(safe-area-inset-bottom));
  }
}

/* ── Utility ────────────────────────────────── */
.hidden { display: none !important; }

/* ── Mobile: fix input bar (iOS 100vh bug) ──── */
@media (max-width: 767px) {
  .main {
    height: 100%;
    min-height: 100vh;
    min-height: -webkit-fill-available;
  }
  .input-area {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 50;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.2);
  }
  .chat-area {
    padding-bottom: 240px;
    scroll-behavior: auto;
  }
  .messages {
    padding-bottom: 60px;
  }
  .scroll-bottom-btn { bottom: 250px; }
  /* 16px prevents iOS zoom on input focus */
  .msg-input, .auth-input, .input-field, .select-field { font-size: 16px; }
  /* Panel: narrower so backdrop is tappable to close */
  .panel { width: 85vw; }
  .panel-close {
    min-width: 44px;
    min-height: 44px;
    padding: 10px;
    margin: -6px -8px -6px 0;
  }
  .panel-header {
    padding: 12px 14px;
  }
  .panel-footer-mobile {
    display: block;
    padding: 12px 14px 24px;
    padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px));
    flex-shrink: 0;
    border-top: 1px solid var(--border);
  }
  .panel-close-btn-mobile {
    width: 100%;
    padding: 12px 16px;
    font-size: 15px;
    font-weight: 500;
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    cursor: pointer;
    transition: background var(--trans);
  }
  .panel-close-btn-mobile:hover,
  .panel-close-btn-mobile:active {
    background: var(--border);
  }
}
html { height: -webkit-fill-available; }
body { min-height: 100vh; min-height: -webkit-fill-available; }

/* ── Desktop layout ─────────────────────────── */
@media (min-width: 768px) {
  .chat-area { padding-bottom: 0; }
  /* Sidebar stays fixed on desktop too — toggle pushes content */
  .sidebar-overlay { display: none !important; }
  .sidebar-close { display: none; }
  body.sidebar-open .main { margin-left: var(--sidebar-w); }
  .scroll-bottom-btn { right: 24px; bottom: 120px; }
  .messages { max-width: 960px; }
}

/* ── Confirm modal ───────────────────────────── */
.confirm-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.confirm-modal-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 28px;
  min-width: 280px;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.confirm-modal-box p {
  margin: 0;
  font-size: 15px;
  color: var(--text);
  line-height: 1.5;
}
.confirm-modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}
.confirm-btn {
  padding: 8px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  transition: background var(--trans), border-color var(--trans);
}
.confirm-btn:hover { background: var(--bg4, #3a3330); }
.confirm-btn.danger {
  background: #7a3a3a;
  border-color: #9b4a4a;
  color: #f5c6c6;
}
.confirm-btn.danger:hover { background: #9b4a4a; }
