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

:root {
  --bg: #ffffff;
  --bg-surface: #ffffff;
  --bg-msg-user: #8a4a20;
  --bg-msg-assistant: #ffffff;
  --text: #1a1a1a;
  --text-secondary: #555555;
  --text-tertiary: #999999;
  --text-on-accent: #ffffff;
  --accent: #9a5a2e;
  --accent-hover: #7d4820;
  --accent-light: rgba(154, 90, 46, 0.08);
  --border: #e5e7eb;
  --border-light: #e2e8f0;
  --surface: #ffffff;
  --input-bg: #ffffff;
  --code-bg: #f3f4f6;
  --radius: 12px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
  --transition: 150ms ease;

  /* Three-tier UI: User bubble */
  --user-bg: #f7f6f2;
  --user-text: #1a1a1a;
  --user-border: #e8e7e1;

  /* Three-tier UI: Assistant framing */
  --assistant-accent: #e5e5e5;

  /* Three-tier UI: Machinery cards */
  --machinery-bg: #ffffff;
  --machinery-header-bg: #f9f9f9;
  --machinery-border: #eeeeee;
  --machinery-text: #333333;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* Screen-reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

.screen { display: flex; flex-direction: column; height: 100dvh; }
.screen[hidden] { display: none; }

/* ═══ Login ═══ */
#login-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f7f7f8 0%, #ede8e3 100%);
}

.login-box {
  background: var(--bg-surface);
  padding: 2.5rem 2rem 2rem;
  border-radius: var(--radius-lg);
  width: min(400px, 90vw);
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.login-logo {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  background: var(--accent);
  color: white;
  font-size: 1.5rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
}

.login-box h1 {
  margin-bottom: 0.25rem;
  color: var(--text);
  font-size: 1.5rem;
  font-weight: 700;
}

.login-subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
}

.login-box form { text-align: left; }

.login-box input {
  width: 100%;
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--text);
  font-size: 1rem;
  transition: border-color var(--transition);
}

.login-box input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(154, 90, 46, 0.15);
}

.login-box button {
  width: 100%;
  padding: 0.8rem;
  margin-top: 0.25rem;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--text-on-accent);
  font-size: 1rem;
  cursor: pointer;
  font-weight: 600;
  transition: background var(--transition);
}

.login-box button:hover { background: var(--accent-hover); }
.login-box button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.error { color: #dc2626; margin-top: 0.75rem; font-size: 0.875rem; }

/* ═══ Chat Header ═══ */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.header-logo {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 0.9rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

header h1 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9ca3af;
  transition: background 300ms;
  flex-shrink: 0;
}

.status-dot.connected { background: #22c55e; }
.status-dot.disconnected { background: #ef4444; }

/* ═══ Context Strip (above composer) ═══ */
.context-strip {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  max-width: 780px;
  margin: 0 auto 0.5rem;
  padding: 0.25rem 0.5rem;
}
.context-strip[hidden] { display: none; }

.context-strip-track {
  flex: 1;
  height: 6px;
  background: #d1d5db;
  border-radius: 3px;
  overflow: hidden;
}

.context-strip-fill {
  height: 100%;
  border-radius: 3px;
  width: 0;
  background: #16a34a;
  transform-origin: left;
  transition: width 400ms ease, background 400ms ease;
}
.context-strip-fill.green { background: #16a34a; }
.context-strip-fill.warn { background: #ca8a04; }
.context-strip-fill.hot { background: #ea580c; }
.context-strip-fill.danger { background: #dc2626; }

.context-strip-fill.compacting {
  background: var(--accent) !important;
  animation: contextPulse 1.2s ease-in-out infinite;
}
@keyframes contextPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}
@media (prefers-reduced-motion: reduce) {
  .context-strip-fill.compacting { animation: none; }
  .context-strip-fill { transition: none; }
}

.context-strip-text {
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
  flex-shrink: 0;
  font-weight: 500;
}

@media (max-width: 600px) {
  .context-strip-text { font-size: 0.7rem; }
  .context-strip-text.verbose { display: none; }
}

.header-actions {
  display: flex;
  gap: 0.25rem;
}

.header-actions button {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
}

.header-actions button:hover {
  background: var(--accent-light);
  color: var(--text);
}

.header-actions button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ═══ Messages ═══ */
main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem;
  scroll-behavior: auto;
  min-width: 0;
}

.message {
  max-width: min(900px, 100%);
  margin: 0 auto 1.75rem;
  padding: 0.875rem 1.125rem;
  border-radius: var(--radius);
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: break-word;
  min-width: 0;
  animation: msgIn 200ms ease-out;
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user {
  background: var(--bg-msg-user);
  color: var(--text-on-accent);
  margin-left: auto;
  margin-right: 0;
  max-width: 75%;
  border-bottom-right-radius: 4px;
}

/* user links handled in bubble section below */

.message.assistant {
  background: transparent;
  border: none;
  margin-left: 0;
  margin-right: auto;
  max-width: 100%;
  box-shadow: none;
  border-radius: 0;
}

.message.system {
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.8rem;
  max-width: 100%;
  background: none;
  padding: 0.25rem;
  box-shadow: none;
  animation: none;
}

/* Token metadata - smaller, more subtle */
.message.system.tokens {
  font-size: 0.75rem;
  color: #9ca3af;
  padding: 0;
  margin-bottom: 0.5rem;
}

.thinking {
  color: var(--text-secondary);
  font-style: italic;
}

.thinking::after {
  content: '';
  animation: dots 1.5s steps(4) infinite;
}

.activity {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-secondary);
  font-style: normal;
  min-width: 0;
  max-width: 100%;
}

.activity::after {
  content: none;
}

.activity-badge {
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: rgba(139, 92, 246, 0.15);
  color: #a78bfa;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  white-space: nowrap;
  flex-shrink: 0;
}

.activity-label {
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-family: 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
  font-size: 0.82rem;
  color: var(--text-secondary);
}

@keyframes dots {
  0% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
}

/* ═══ Empty State ═══ */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 3rem 1.5rem;
  min-height: 60vh;
}

.empty-state[hidden] { display: none; }

.empty-logo {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  background: var(--accent);
  color: white;
  font-size: 1.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1.25rem;
  box-shadow: var(--shadow-md);
}

.empty-state h2 {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.5rem;
}

.empty-state p {
  color: var(--text-secondary);
  font-size: 0.95rem;
  max-width: 440px;
  line-height: 1.5;
  margin-bottom: 1.75rem;
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  max-width: 500px;
}

.suggestion-chip {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-surface);
  color: var(--text);
  font-size: 0.875rem;
  cursor: pointer;
  transition: all var(--transition);
  box-shadow: var(--shadow-sm);
}

.suggestion-chip:hover {
  border-color: var(--accent);
  background: var(--accent-light);
  box-shadow: var(--shadow-md);
}

.suggestion-chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ═══ Markdown content ═══ */
.message {
  overflow-x: hidden;
  min-width: 0;
}
.message * {
  min-width: 0;
  max-width: 100%;
}
.message h1, .message h2, .message h3 { margin: 0.5em 0 0.3em; }
.message p { margin: 0.4em 0; }
.message ul, .message ol { margin: 0.4em 0; padding-left: 1.5em; }
.message a { color: var(--accent); }
.message p,
.message li,
.message a,
.message span,
.message div {
  overflow-wrap: anywhere;
}

/* Tables */
.message table {
  border-collapse: collapse;
  width: 100%;
  max-width: 100%;
  table-layout: fixed;
  font-size: 0.875em;
  margin: 0.5em 0;
}
.message thead { background: var(--code-bg); }
.message th, .message td {
  padding: 0.4em 0.6em;
  border: 1px solid var(--border);
  text-align: left;
  white-space: normal;
  overflow-wrap: anywhere;
}
.message th { font-weight: 600; }
.message tbody tr:nth-child(even) { background: rgba(0,0,0,0.02); }

.message code {
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 6px;
  font-size: 0.875em;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

.message pre {
  background: #1e1e1e;
  color: #d4d4d4;
  border-radius: var(--radius);
  padding: 1rem;
  overflow-x: hidden;
  margin: 0.75em 0;
  position: relative;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.message pre code {
  background: none;
  padding: 0;
  font-size: 0.85em;
  color: inherit;
  white-space: inherit;
  overflow-wrap: inherit;
  word-break: inherit;
}

.copy-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  padding: 0.25rem 0.6rem;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 6px;
  background: rgba(255,255,255,0.08);
  color: #9ca3af;
  cursor: pointer;
  font-size: 0.75rem;
  transition: all var(--transition);
}

.copy-btn:hover {
  background: rgba(255,255,255,0.15);
  color: #d4d4d4;
}

/* ═══ Workspace download links ═══ */
a.workspace-download {
  display: inline-block;
  padding: 0.35rem 0.75rem;
  background: var(--bg-msg-user);
  color: var(--text-on-accent);
  border-radius: 6px;
  font-size: 0.85rem;
  text-decoration: none;
  transition: opacity var(--transition);
}

a.workspace-download:hover {
  opacity: 0.85;
}

/* ═══ Scroll to bottom ═══ */
.scroll-anchor {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
}

.scroll-anchor[hidden] { display: none; }

.scroll-anchor button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
  transition: all var(--transition);
}

.scroll-anchor button:hover {
  background: var(--accent-light);
  color: var(--text);
}

/* ═══ Offline Banner ═══ */
.offline-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.4rem;
  background: #fef2f2;
  color: #dc2626;
  font-size: 0.8rem;
  border-bottom: 1px solid #fecaca;
}

.offline-banner[hidden] { display: none; }

/* ═══ Footer / Composer ═══ */
footer {
  padding: 0.75rem 1rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  background: var(--bg);
  flex-shrink: 0;
  min-width: 0;
  overflow-x: hidden;
}

#chat-form {
  max-width: 780px;
  margin: 0 auto;
  position: relative;
  min-width: 0;
  width: 100%;
}

.file-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.file-chips[hidden] { display: none; }

/* Non-image file chip (pill style) */
.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  max-width: 100%;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-secondary);
  font-size: 0.8rem;
}

/* Image preview card (pre-send) */
.file-chip.has-thumb {
  position: relative;
  padding: 0;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  max-width: 180px;
}

.file-chip-thumb {
  display: block;
  width: 100%;
  max-height: 120px;
  object-fit: cover;
}

.file-chip.has-thumb .file-chip-name {
  display: block;
  padding: 0.25rem 0.5rem;
  font-size: 0.7rem;
  background: var(--bg-surface);
  border-top: 1px solid var(--border-light);
}

.file-chip.has-thumb .file-chip-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  color: white;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  backdrop-filter: blur(4px);
}

.file-chip.has-thumb .file-chip-remove:hover {
  background: rgba(220, 38, 38, 0.85);
  color: white;
}

.file-chip-name {
  max-width: min(260px, 60vw);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-chip-remove {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.1rem;
}

.file-chip-remove:hover { color: var(--text); }

/* Messenger-style composer bar */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 0;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 0.35rem 0.35rem 0.35rem 0.5rem;
  transition: border-color var(--transition), box-shadow var(--transition);
  box-shadow: var(--shadow-sm);
  min-width: 0;
  max-width: 100%;
}

.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(154, 90, 46, 0.1);
}

.composer-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  transition: all var(--transition);
}

.composer-icon:hover {
  color: var(--text);
  background: var(--accent-light);
}

.composer-icon:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.composer-icon:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Send button - filled circle when active */
.send-icon {
  background: var(--accent);
  color: white;
}

.send-icon:hover {
  background: var(--accent-hover);
  color: white;
}

.send-icon:disabled {
  background: var(--border);
  color: var(--text-secondary);
  opacity: 0.6;
}

/* Stop button - red square */
.stop-icon {
  background: #dc2626;
  color: white;
}

.stop-icon:hover {
  background: #b91c1c;
  color: white;
}

.stop-icon[hidden] { display: none; }

#chat-input {
  flex: 1;
  padding: 0.5rem 0.5rem;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  resize: none;
  max-height: 150px;
  line-height: 1.4;
  min-height: 24px;
}

#chat-input:focus { outline: none; }
#chat-input::placeholder { color: #9ca3af; }

/* File badges in user messages */
.file-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.file-badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.75rem;
  line-height: 1.2;
}

.file-badge-thumb {
  width: 100%;
  max-height: 300px;
  border-radius: 10px;
  object-fit: contain;
  cursor: pointer;
  transition: opacity var(--transition);
}

.file-badge-thumb:hover { opacity: 0.9; }

/* ═══ Sidebar ═══ */

#chat-screen { flex-direction: row; }

#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width var(--transition);
}

.sidebar.collapsed { width: 0; }

.sidebar-overlay { display: none; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.sidebar-title {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
}

.sidebar-icon-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
}

.sidebar-icon-btn:hover {
  background: var(--accent-light);
  color: var(--text);
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0;
}

.sidebar-footer {
  flex-shrink: 0;
  padding: 0.5rem;
  border-top: 1px solid var(--border-light);
}

.sidebar-footer-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all var(--transition);
}

.sidebar-footer-btn:hover {
  background: var(--accent-light);
  color: var(--text);
}

.sidebar-section { margin-bottom: 0.5rem; }

.sidebar-section-label {
  padding: 0.4rem 1rem 0.25rem;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.sidebar-list { display: flex; flex-direction: column; }

.sidebar-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.5rem 1rem;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.875rem;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background var(--transition);
  line-height: 1.4;
}

.sidebar-item { position: relative; padding-right: 2rem; }
.sidebar-item:hover { background: var(--accent-light); }

.sidebar-item-delete {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  display: grid;
  place-items: center;
  width: 1.4rem;
  height: 1.4rem;
  border-radius: 4px;
  font-size: 1rem;
  color: var(--text-secondary);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}
.sidebar-item:hover .sidebar-item-delete,
.sidebar-item:focus-within .sidebar-item-delete { opacity: 1; pointer-events: auto; }
.sidebar-item-delete:hover { background: rgba(220, 38, 38, 0.15); color: #dc2626; }
@media (hover: none) {
  .sidebar-item-delete { opacity: 0.7; pointer-events: auto; }
}

.sidebar-item.active {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 500;
}

.sidebar-item .sidebar-item-sub {
  display: block;
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 0.1rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-item.active .sidebar-item-sub { color: var(--accent); opacity: 0.7; }

.sidebar-empty {
  padding: 0.5rem 1rem;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

/* Header sidebar toggle button */
.header-sidebar-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.header-sidebar-btn:hover {
  background: var(--accent-light);
  color: var(--text);
}

/* ═══ Mobile ═══ */
@media (max-width: 600px) {
  .message.user { max-width: 95%; }
  .message.assistant { max-width: 100%; }
  header h1 { font-size: 0.95rem; }
  #chat-input { font-size: 16px; } /* prevents iOS zoom */
  .file-chip-name { max-width: 48vw; }

  .empty-state { padding: 2rem 1rem; min-height: 50vh; }
  .empty-state h2 { font-size: 1.25rem; }
  .empty-logo { width: 52px; height: 52px; font-size: 1.5rem; }
  .suggestions { gap: 0.4rem; }
  .suggestion-chip { font-size: 0.8rem; padding: 0.4rem 0.8rem; }

  footer { padding: 0.5rem; padding-bottom: max(0.5rem, env(safe-area-inset-bottom)); }

  .composer {
    border-radius: 20px;
    padding: 0.25rem 0.25rem 0.25rem 0.4rem;
  }

  .composer-icon { width: 34px; height: 34px; }

  /* Mobile sidebar: full-screen overlay */
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 280px;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 200ms ease;
    box-shadow: var(--shadow-lg);
  }

  .sidebar.open { transform: translateX(0); }
  .sidebar.collapsed { width: 280px; transform: translateX(-100%); }

  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 199;
    background: rgba(0,0,0,0.4);
  }

  .sidebar-overlay[hidden] { display: none !important; }
}

/* Question cards */
.question-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  margin: 0.5rem 0;
  max-width: 600px;
}
.question-card.answered { opacity: 0.6; }
.question-header {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
  margin-bottom: 0.25rem;
}
.question-text {
  font-size: 0.9rem;
  color: var(--text);
  margin-bottom: 0.75rem;
}
.question-options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.question-option {
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all var(--transition);
}
.question-option:hover {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}
.question-option:disabled { opacity: 0.5; cursor: default; }
.question-checkbox {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  cursor: pointer;
}
.question-submit {
  padding: 0.4rem 1rem;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 0.85rem;
  cursor: pointer;
}
.question-submit:disabled { opacity: 0.5; }
.question-selected {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  color: var(--accent);
  font-style: italic;
}

/* Command palette */
.command-palette {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  max-height: 280px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 0.5rem;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
  z-index: 100;
}
.command-palette[hidden] { display: none; }
.palette-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  transition: background var(--transition);
}
.palette-item:hover, .palette-item.active {
  background: var(--accent-light);
}
.palette-name {
  font-size: 0.85rem;
  color: var(--text);
  font-family: monospace;
}
.palette-badge {
  font-size: 0.65rem;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  background: var(--border);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* ═══ Reduced Motion ═══ */
@media (prefers-reduced-motion: reduce) {
  .message, .thinking::after, .context-strip-fill {
    animation: none !important;
    transition: none !important;
  }
}

/* ═══ Dark Mode ═══ */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f0f;
    --bg-surface: #1a1a1a;
    --bg-msg-user: #8a4a20;
    --bg-msg-assistant: #0f0f0f;
    --text: #e0e0e0;
    --text-secondary: #a0a0a0;
    --text-tertiary: #666666;
    --text-on-accent: #ffffff;
    --accent: #b5783c;
    --accent-hover: #c98a4a;
    --accent-light: rgba(181, 120, 60, 0.12);
    --border: #252525;
    --border-light: #222222;
    --input-bg: #1a1a1a;
    --code-bg: #1f2430;
    --surface: #1a1a1a;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.5);

    /* Three-tier dark mode */
    --user-bg: #1e1d1b;
    --user-text: #e0e0e0;
    --user-border: #2a2927;
    --assistant-accent: #222222;
    --machinery-bg: #151515;
    --machinery-header-bg: #1a1a1a;
    --machinery-border: #252525;
    --machinery-text: #cccccc;
  }

  #login-screen {
    background: linear-gradient(135deg, #121316 0%, #1a1d22 100%);
  }

  .message pre {
    background: #0d1117;
    color: #e6edf3;
  }

  .copy-btn {
    border-color: rgba(255,255,255,0.1);
    background: rgba(255,255,255,0.05);
  }

  .context-strip-track {
    background: #2a2f38;
  }

  .scroll-anchor button {
    background: var(--bg-surface);
    border-color: var(--border);
  }

  .offline-banner {
    background: #2d1515;
    color: #f87171;
    border-bottom-color: #4a1d1d;
  }
}

/* ═══ Composer Meta Row ═══ */
.composer-meta {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.5rem 0;
  max-width: 780px;
  margin: 0 auto;
}

.meta-chip {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-surface);
  color: var(--text-secondary);
  font-size: 0.7rem;
  font-weight: 500;
  cursor: default;
  white-space: nowrap;
}

button.meta-chip {
  cursor: pointer;
  transition: all var(--transition);
}

button.meta-chip:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-light);
}

.model-menu {
  position: fixed;
  min-width: 180px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.25rem;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
  z-index: 1000;
}
.model-menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background var(--transition);
}
.model-menu-item:hover, .model-menu-item.active {
  background: var(--accent-light);
}
.model-menu-name {
  font-size: 0.8rem;
  color: var(--text);
}
.model-menu-item.active .model-menu-name {
  color: var(--accent);
  font-weight: 600;
}
.model-menu-tick {
  font-size: 0.75rem;
  color: var(--accent);
}



/* ═══ Three-tier UI: User bubble (right-aligned, warm paper) ═══ */
.message.user {
  background: var(--user-bg);
  color: var(--user-text);
  border: 1px solid var(--user-border);
  border-radius: 1.25rem;
  max-width: 85%;
  margin-left: auto;
  margin-right: 0;
  position: relative;
  padding: 0.85rem 1.25rem 0.85rem 2rem;
  font-family: var(--font-prose, -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif);
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}
.message.user::before {
  content: "❯";
  position: absolute;
  left: 0.85rem;
  top: 0.85rem;
  opacity: 0.3;
  font-family: ui-monospace, Menlo, Consolas, "SF Mono", monospace;
  font-weight: bold;
  color: var(--text);
}
.message.user a { color: var(--accent); }

.cli-block {
  font-family: ui-monospace, Menlo, Consolas, "SF Mono", monospace;
  font-size: 13px;
  line-height: 1.55;
  margin: 6px 0;
  min-width: 0;
  max-width: 100%;
}
details.cli-block { display: block; }
details.cli-block > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 6px;
  padding: 2px 4px 2px 0;
  border-radius: 4px;
  transition: background 0.15s;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}
details.cli-block > summary::-webkit-details-marker { display: none; }
details.cli-block > summary:hover { background: rgba(0,0,0,0.04); }
@media (prefers-color-scheme: dark) {
  details.cli-block > summary:hover { background: rgba(255,255,255,0.05); }
}
.caret {
  flex: 0 0 auto;
  width: 10px;
  display: inline-block;
  transition: transform 0.2s ease;
  color: var(--muted, #8b949e);
}
.caret::before { content: "▸"; }
details[open] > summary > .caret { transform: rotate(90deg); }

.marker {
  flex: 0 0 auto;
  width: 14px;
  font-weight: 600;
  text-align: left;
}
.marker-assist { color: #1a7f37; }
.marker-tool   { color: #8250df; }
.marker-think  { color: var(--muted, #8b949e); font-style: italic; }
.marker-result { color: var(--muted, #8b949e); }
@media (prefers-color-scheme: dark) {
  .marker-assist { color: #7ee787; }
  .marker-tool   { color: #bc8cff; }
}

.cli-body {
  margin: 4px 0 4px 24px;
  padding: 8px 10px;
  background: rgba(0,0,0,0.04);
  border-left: 2px solid rgba(130,80,223,0.25);
  border-radius: 4px;
  font-size: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  overflow-x: hidden;
  max-height: 360px;
  overflow-y: auto;
  min-width: 0;
  max-width: calc(100% - 24px);
}
@media (prefers-color-scheme: dark) {
  .cli-body {
    background: rgba(255,255,255,0.04);
    border-left-color: rgba(188,140,255,0.35);
  }
}

.cli-thinking > summary { color: var(--muted, #8b949e); font-style: italic; }
.cli-thinking-body { color: var(--muted, #8b949e); font-style: italic; }

.tool-name,
.tool-parens,
.tool-args {
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: normal;
}
.tool-name   { font-weight: 600; color: #8250df; }
.tool-parens { color: var(--muted, #8b949e); }
.tool-args   { color: var(--text); }
@media (prefers-color-scheme: dark) { .tool-name { color: #bc8cff; } }

/* tool_result nested inside tool_use — indent + left stripe for hierarchy */
details.cli-tool_use > details.cli-tool_result,
details.cli-tool_use > .cli-tool_result {
  margin-left: 22px;
  border-left: 2px solid rgba(130,80,223,0.25);
  padding-left: 10px;
  margin-top: 2px;
}
@media (prefers-color-scheme: dark) {
  details.cli-tool_use > details.cli-tool_result,
  details.cli-tool_use > .cli-tool_result {
    border-left-color: rgba(188,140,255,0.35);
  }
}

.cli-result-label { color: var(--muted, #8b949e); font-size: 12px; }
.cli-result-hint  { color: var(--accent); font-size: 11px; margin-left: 4px; }
.cli-result-inline { color: var(--muted, #8b949e); white-space: pre-wrap; word-break: break-word; }

/* ═══ Three-tier UI: Assistant prose (left-border thread) ═══ */
.cli-assistant_text {
  display: flex;
  gap: 6px;
  align-items: flex-start;
  padding: 0.5rem 0 0.5rem 1.25rem;
  border-left: 2px solid var(--assistant-accent);
  margin: 0.25rem 0;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}
.cli-assistant-text {
  flex: 1 1 auto;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif;
  font-size: 1.05rem;
  line-height: 1.65;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}
.cli-assistant-text p:first-child { margin-top: 0; }
.cli-assistant-text p:last-child  { margin-bottom: 0; }

/* ═══ Three-tier UI: Machinery cards (tool calls, thinking, multi-line results) ═══ */
.cli-machinery {
  border: 1px solid var(--machinery-border);
  border-radius: 0.5rem;
  background: var(--machinery-bg);
  margin: 0.75rem 0;
  overflow: hidden;
  min-width: 0;
  max-width: 100%;
}
.cli-machinery-header {
  padding: 0.5rem 1rem !important;
  background: var(--machinery-header-bg) !important;
  font-size: 0.8rem !important;
  color: var(--text-secondary) !important;
}
.cli-machinery[open] .cli-machinery-header {
  border-bottom: 1px solid var(--machinery-border);
}
.cli-machinery .cli-body {
  border-left: none;
  background: var(--machinery-bg);
  margin: 0;
  padding: 1rem;
  max-height: 400px;
}

.cli-result {
  color: var(--text-tertiary, #999);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif;
  font-size: 0.75rem;
  padding: 0.5rem 0;
  margin-top: 0.25rem;
  border-top: none;
}
@media (prefers-color-scheme: dark) {
  .cli-result { border-top-color: rgba(255,255,255,0.08); }
}
