:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #ef4444;
  --success: #10b981;
  --warning: #f59e0b;
  --radius-sm: 6px;
  --radius: 10px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f172a;
    --surface: #1e293b;
    --border: #334155;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
  }
}

:root[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --border: #334155;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
}

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

html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  height: 100vh;
  overflow: hidden;
}

/* ─── App shell (email-style layout) ─────────────────────────────────────── */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.app-title { font-size: 16px; font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

/* Compact header inside the left pane (replaces the full-width top bar) */
.list-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.btn-icon { padding: 6px 9px; font-size: 15px; line-height: 1; }

.panes {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* Left pane */
.pane-list {
  width: 320px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.list-search { padding: 10px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.list-search input { padding: 8px 12px; }
.list-scroll { flex: 1; overflow-y: auto; min-height: 0; }

/* Center pane: conversation is the dominant surface, full height */
.pane-convo {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}

/* Right rail: controls on top, spacious reply below — narrow */
.pane-actions {
  width: 300px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.actions-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.actions-bar .btn { flex: 1; justify-content: center; min-width: 92px; }
.convo-empty { flex: 1; display: flex; align-items: center; justify-content: center; }
.convo { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.convo-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.convo-title { font-size: 16px; font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-back { color: var(--accent); cursor: pointer; text-decoration: none; font-size: 18px; white-space: nowrap; background: none; border: none; padding: 4px 6px; }
.nav-back:hover { text-decoration: none; opacity: 0.7; }
.mobile-only { display: none; }

/* Collapse toggle for old messages */
.collapse-toggle {
  align-self: center;
  margin: 4px 0 8px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 99px;
  padding: 5px 14px;
  font-size: 13px;
  cursor: pointer;
}
.collapse-toggle:hover { background: var(--surface); color: var(--text); }

@media (max-width: 860px) {
  .panes { flex-direction: column; }
  .pane-list { width: 100%; }
  .pane-convo, .pane-actions { display: none; }
  .pane-actions { width: 100%; border-left: none; border-top: 1px solid var(--border); flex: none; max-height: 45vh; }
  .app-shell.convo-open .pane-list { display: none; }
  .app-shell.convo-open .pane-convo { display: flex; }
  .app-shell.convo-open .pane-actions { display: flex; }
  .reply-form { flex: none; }
  .reply-form textarea { min-height: 80px; }
  .mobile-only { display: inline-block; }
}

/* Card */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
  transition: opacity 0.15s, background 0.15s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { opacity: 0.85; }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--border); color: var(--text); }

/* Form */
input, textarea, select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  transition: border-color 0.15s;
  outline: none;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

/* Status badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}
.badge-running { background: #fef3c7; color: #92400e; }
.badge-completed { background: #d1fae5; color: #065f46; }
.badge-failed { background: #fee2e2; color: #991b1b; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .badge-running { background: #451a03; color: #fcd34d; }
  :root:not([data-theme="light"]) .badge-completed { background: #022c22; color: #6ee7b7; }
  :root:not([data-theme="light"]) .badge-failed { background: #450a0a; color: #fca5a5; }
}

:root[data-theme="dark"] .badge-running { background: #451a03; color: #fcd34d; }
:root[data-theme="dark"] .badge-completed { background: #022c22; color: #6ee7b7; }
:root[data-theme="dark"] .badge-failed { background: #450a0a; color: #fca5a5; }

/* Session list */
.session-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
  gap: 10px;
  border-left: 3px solid transparent;
}
.session-item:hover { background: var(--bg); }
.session-item.active { background: var(--bg); border-left-color: var(--accent); }
.session-info { flex: 1; min-width: 0; }
.session-path { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-meta { font-size: 13px; color: var(--text-muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Messages */
.messages {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}
.message { display: flex; flex-direction: column; gap: 4px; }
.message-role { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); }
.message-user { align-items: flex-end; }
.message-user .message-content {
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  padding: 10px 14px;
  max-width: 85%;
}
.message-assistant .message-content {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
}

/* Markdown */
.md-content h1 { font-size: 1.35em; margin-block: 0.6em 0.3em; }
.md-content h2 { font-size: 1.15em; margin-block: 0.5em 0.25em; }
.md-content h3 { font-size: 1.05em; margin-block: 0.5em 0.25em; }
.md-content p { margin-block: 0.4em; }
.md-content p:first-child { margin-top: 0; }
.md-content p:last-child { margin-bottom: 0; }
.md-content ul, .md-content ol { padding-inline-start: 1.5em; margin-block: 0.4em; }
.md-content li + li { margin-top: 0.2em; }
.md-content code {
  font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
  font-size: 0.875em;
  background: var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.md-content pre {
  margin-block: 0.6em;
  border-radius: var(--radius-sm);
  overflow-x: auto;
}
.md-content pre code {
  display: block;
  padding: 12px 16px;
  border: none;
  border-radius: var(--radius-sm);
  background: #0f172a;
  color: #e2e8f0;
  font-size: 13px;
  line-height: 1.6;
}
.md-content blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  color: var(--text-muted);
  margin-block: 0.4em;
}
.md-content table { border-collapse: collapse; width: 100%; margin-block: 0.5em; font-size: 14px; overflow-x: auto; display: block; }
.md-content th, .md-content td { padding: 6px 12px; border: 1px solid var(--border); text-align: left; }
.md-content th { background: var(--bg); font-weight: 600; }
.md-content a { color: var(--accent); }
.md-content hr { border: none; border-top: 1px solid var(--border); margin-block: 1em; }

/* Stream */
.stream-area {
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  background: var(--bg);
  min-height: 48px;
  font-size: 14px;
}
.stream-area.active { border-top-color: var(--accent); }

.reconnect-notice {
  padding: 7px 16px;
  background: #fef3c7;
  color: #92400e;
  font-size: 13px;
  text-align: center;
  border-top: 1px solid #fde68a;
}

/* Reply form — vertical, fills the right rail */
.reply-form {
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--surface);
  min-height: 0;
}
.reply-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); }
.reply-form textarea {
  flex: 1;
  min-height: 120px;
  resize: none;
}
.reply-buttons { display: flex; gap: 8px; flex-shrink: 0; }
.btn-mic { flex: 0 0 auto; justify-content: center; padding: 12px 14px; font-size: 14px; }
.btn-mic.recording { background: var(--danger, #e5484d); color: #fff; animation: mic-pulse 1.2s ease-in-out infinite; }
@keyframes mic-pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.6; } }
.btn-send { flex: 1; justify-content: center; padding: 12px; font-size: 15px; }
.voice-status { font-size: 12px; padding: 6px 8px; border-radius: 6px; background: var(--surface-2, #f1f1f1); color: var(--text-muted); flex-shrink: 0; }
.voice-status.voice-recording { background: #fde8e8; color: #c0392b; }
.voice-status.voice-busy { background: #e8f0fe; color: #1a56db; }
.voice-status.voice-ok { background: #e6f4ea; color: #1e7e34; }
.voice-status.voice-error { background: #fde8e8; color: #c0392b; }

/* Attachment chips (staged, not yet uploaded) */
.attachments { display: flex; flex-wrap: wrap; gap: 6px; flex-shrink: 0; }
.attach-chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 4px 6px 4px 4px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg); font-size: 12px;
}
.attach-thumb { width: 28px; height: 28px; object-fit: cover; border-radius: 4px; flex-shrink: 0; }
.attach-icon { font-size: 16px; line-height: 1; }
.attach-chip .attach-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 140px; }
.attach-chip .attach-size { color: var(--text-muted); flex-shrink: 0; }
.attach-remove {
  border: none; background: transparent; color: var(--text-muted); cursor: pointer;
  font-size: 13px; padding: 0 2px; line-height: 1; flex-shrink: 0;
}
.attach-remove:hover { color: var(--danger); }

/* Attachments rendered inside a message bubble */
.msg-attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.msg-attach { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: inherit; }
.msg-attach-img img {
  max-width: 220px; max-height: 220px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); display: block;
}
.msg-attach-file {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); font-size: 13px;
}
.msg-attach-file:hover { background: var(--bg); }
.msg-attach-file .attach-name { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-attach-file .attach-size { color: var(--text-muted); }

/* Full-window drag-and-drop overlay */
.drop-overlay {
  position: fixed; inset: 0; z-index: 1000; display: flex; align-items: center; justify-content: center;
  background: rgba(37, 99, 235, 0.12); backdrop-filter: blur(1px); pointer-events: none;
}
.drop-overlay-inner {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 28px 40px; border: 2px dashed var(--accent); border-radius: var(--radius);
  background: var(--surface); color: var(--accent); font-size: 20px; font-weight: 600;
  box-shadow: var(--shadow);
}
.drop-overlay-inner small { font-size: 12px; font-weight: 400; color: var(--text-muted); }

/* Auth page */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow);
}
.auth-card h1 { font-size: 20px; margin-bottom: 24px; text-align: center; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 5px; color: var(--text-muted); }
.err { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* Loading */
.loading { display: flex; align-items: center; gap: 8px; color: var(--text-muted); font-size: 14px; }
.spinner {
  width: 16px; height: 16px; flex-shrink: 0;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  width: 100%; max-width: 480px;
  padding: 24px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}
.modal h2 { font-size: 17px; margin-bottom: 16px; }
.modal-footer { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

/* Empty */
.empty {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-muted);
}
.empty h3 { font-size: 16px; margin-bottom: 6px; color: var(--text); }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.section-header h2 { font-size: 15px; font-weight: 600; }

.hidden { display: none !important; }
