:root {
  color-scheme: light dark;

  --bg-light: #f8fafc;
  --bg-dark: #0b0f17;
  --surface-light: #ffffff;
  --surface-dark: #111827;
  --surface-alt-light: #f1f5f9;
  --surface-alt-dark: #1b2436;
  --border-light: #e2e8f0;
  --border-dark: #253047;
  --text-light: #0f172a;
  --text-dark: #f1f5f9;
  --muted-light: #64748b;
  --muted-dark: #94a3b8;
  --accent-light: #2563eb;
  --accent-dark: #60a5fa;
  --danger-light: #dc2626;
  --danger-dark: #f87171;

  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --surface-alt: var(--surface-alt-light);
  --border: var(--border-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --accent: var(--accent-light);
  --danger: var(--danger-light);

  @media (prefers-color-scheme: dark) {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --surface-alt: var(--surface-alt-dark);
    --border: var(--border-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --accent: var(--accent-dark);
    --danger: var(--danger-dark);
  }

  @supports (color: light-dark(white, black)) {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --surface-alt: light-dark(var(--surface-alt-light), var(--surface-alt-dark));
    --border: light-dark(var(--border-light), var(--border-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --accent: light-dark(var(--accent-light), var(--accent-dark));
    --danger: light-dark(var(--danger-light), var(--danger-dark));
  }

  accent-color: var(--accent);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error-text { color: var(--danger); font-size: 0.9rem; margin: 0; }

/* Buttons & inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 0.45rem 0.85rem;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--surface-alt); }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-alt); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 0.3rem 0.6rem; font-size: 0.8rem; }

input, select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
}

/* Login */
.login-shell {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
}
.login-card h1 { margin: 0.5rem 0 0.25rem; font-size: 1.4rem; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, #2563eb, #06b6d4);
}
.stack { display: flex; flex-direction: column; gap: 0.75rem; margin-top: 1.25rem; }

/* App shell */
.app-shell {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 0.6rem; }
.topbar-left h1 { margin: 0; font-size: 1.05rem; font-weight: 650; }
.brand-dot {
  width: 10px; height: 10px; border-radius: 999px;
  background: var(--accent);
}
.topbar-center { flex: 1; max-width: 360px; }
.topbar-center input { width: 100%; }

.pill {
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
}

.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 360px 1fr;
}

.list-pane {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--border);
}
.segmented {
  display: inline-flex;
  background: var(--surface-alt);
  padding: 2px;
  border-radius: 8px;
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
}
.seg-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.chip-row {
  display: flex;
  gap: 0.4rem;
  padding: 0.5rem 0.85rem;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
}
.chip-row:empty { display: none; }
.chip {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 0.78rem;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.chip.active {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

.message-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}
.msg-item {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.msg-item:hover { background: var(--surface-alt); }
.msg-item.selected { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.msg-item-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.8rem;
}
.msg-item-title {
  font-weight: 500;
  font-size: 0.94rem;
  display: flex;
  align-items: center;
  gap: 0.45rem;
}
.msg-item.unread .msg-item-title { font-weight: 700; }
.unread-dot {
  width: 8px; height: 8px; border-radius: 999px;
  background: var(--accent);
  flex-shrink: 0;
}
.msg-item-preview {
  font-size: 0.82rem;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Badges */
.agent-badge {
  display: inline-flex;
  align-items: center;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.12rem 0.5rem;
  border-radius: 999px;
}
.color-slate { background: #64748b22; color: #64748b; }
.color-sky { background: #0ea5e922; color: #0284c7; }
.color-green { background: #22c55e22; color: #16a34a; }
.color-amber { background: #f59e0b22; color: #d97706; }
.color-rose { background: #f43f5e22; color: #e11d48; }
.color-teal { background: #14b8a622; color: #0d9488; }
.color-indigo { background: #6366f122; color: #4f46e5; }

.priority-badge {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.1rem 0.45rem;
  border-radius: 6px;
  text-transform: uppercase;
}
.priority-high { background: #ef444422; color: var(--danger); }
.priority-low { background: var(--surface-alt); color: var(--muted); }

/* Detail pane */
.detail-pane {
  overflow-y: auto;
  padding: 1.75rem 2.25rem;
}
.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
}
.detail-article { max-width: 760px; margin: 0 auto; }
.detail-meta-row { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.detail-header h2 { margin: 0.6rem 0 0.5rem; font-size: 1.5rem; line-height: 1.25; }
.tag-row { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-bottom: 0.85rem; }
.tag {
  font-size: 0.75rem;
  padding: 0.12rem 0.5rem;
  border-radius: 6px;
  background: var(--surface-alt);
  color: var(--muted);
}
.detail-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.25rem;
}

.markdown-body h1, .markdown-body h2, .markdown-body h3 { margin-top: 1.25rem; margin-bottom: 0.5rem; }
.markdown-body p { margin: 0.6rem 0; }
.markdown-body ul { padding-left: 1.3rem; }
.markdown-body pre {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  padding: 0.85rem 1rem;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 0.86rem;
}
.markdown-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
  background: var(--surface-alt);
  padding: 0.12rem 0.35rem;
  border-radius: 4px;
}
.markdown-body pre code { background: none; padding: 0; }

.attachment-grid {
  margin-top: 1.5rem;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
}
.attachment-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface);
}
.attachment-card img { width: 100%; height: auto; display: block; }
.attachment-card figcaption { padding: 0.45rem 0.65rem; font-size: 0.78rem; color: var(--muted); }

/* Dialogs */
dialog {
  width: min(560px, calc(100vw - 2rem));
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  padding: 1.25rem 1.4rem;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}
.dialog-header h2 { margin: 0; font-size: 1.15rem; }
.agent-form {
  display: grid;
  grid-template-columns: 1fr 1fr auto auto;
  gap: 0.5rem;
  margin: 0.85rem 0;
}
.token-banner {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.8rem;
  margin: 0.75rem 0;
}
.token-banner code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  word-break: break-all;
  flex: 1;
}
.modal-list { display: flex; flex-direction: column; gap: 0.55rem; margin-top: 0.75rem; }
.modal-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.qr-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  margin: 1rem 0;
}
.qr-svg-wrap {
  width: 220px;
  height: 220px;
  padding: 12px;
  background: #ffffff;
  border-radius: 12px;
  border: 1px solid var(--border);
  display: grid;
  place-items: center;
}
.qr-svg-wrap svg { width: 100%; height: 100%; display: block; }
.qr-meta { display: flex; align-items: center; gap: 0.75rem; }
.manual-fallback { width: 100%; }
.section-label { font-size: 0.9rem; margin: 1.1rem 0 0.4rem; }

@media (max-width: 820px) {
  .workspace { grid-template-columns: 1fr; }
  .list-pane { max-height: 45vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .agent-form { grid-template-columns: 1fr; }
  .topbar-center { display: none; }
}
