:root {
  --bg: #f4f6fb; --panel: #ffffff; --panel-2: #f8fafc; --line: #e3e8f0;
  --text: #0f172a; --muted: #64748b; --accent: #2563eb; --accent-soft: #e0eaff;
  --good: #059669; --warn: #d97706; --bad: #dc2626; --bad-soft: #fee2e2; --warn-soft: #fef3c7; --good-soft: #d1fae5;
  --p5: #dc2626; --p4: #ea580c; --p3: #64748b; --p2: #94a3b8; --p1: #cbd5e1;
  --radius: 14px; --shadow: 0 1px 2px rgba(15,23,42,.05), 0 4px 16px rgba(15,23,42,.05);
  --sidebar: #0f172a; --sidebar-text: #cbd5e1;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120; --panel: #111a2e; --panel-2: #0f172a; --line: #1e293b;
    --text: #e2e8f0; --muted: #8b9ab3; --accent: #60a5fa; --accent-soft: #1e2a4a;
    --bad-soft: #3b1219; --warn-soft: #3a2a0c; --good-soft: #0b3328;
    --p3: #94a3b8; --p2: #64748b; --p1: #475569;
    --shadow: none; --sidebar: #070c18;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--bad); min-height: 1.2em; }
a { color: var(--accent); cursor: pointer; }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2);
}
textarea { min-height: 180px; resize: vertical; line-height: 1.5; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
label.inline { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
label.inline input { width: auto; }

.btn { border: 1px solid var(--line); background: var(--panel); padding: 8px 14px; border-radius: 10px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.good { background: var(--good); border-color: var(--good); color: #fff; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: progress; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: grid; gap: 12px; text-align: center; }

/* shell */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100%; }
.sidebar { background: var(--sidebar); color: var(--sidebar-text); padding: 18px 12px; display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh; }
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; color: #fff; padding: 0 8px; }
.brand.big { font-size: 26px; justify-content: center; color: var(--text); }
.logo { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 9px; background: #2563eb; color: #fff; font-size: 16px; }
nav { display: flex; flex-direction: column; gap: 2px; }
nav a { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; color: var(--sidebar-text); text-decoration: none; }
nav a span { width: 18px; text-align: center; opacity: .8; }
nav a b { font-weight: 500; flex: 1; }
nav a i { font-style: normal; font-size: 12px; background: #1e293b; color: #e2e8f0; border-radius: 99px; padding: 1px 8px; }
nav a i:empty { display: none; }
nav a:hover { background: #1e293b; }
nav a.active { background: #1d4ed8; color: #fff; }
nav a.active i { background: rgba(255,255,255,.2); }
.agent-pill { margin-top: auto; display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 10px 12px; border-radius: 10px; background: #111c33; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #64748b; flex: none; }
.dot.on { background: #34d399; box-shadow: 0 0 0 4px rgba(52,211,153,.18); }
.dot.busy { background: #fbbf24; animation: pulse 1s infinite; }
.dot.err { background: #f87171; }
@keyframes pulse { 50% { opacity: .35; } }

main { min-width: 0; padding: 0 28px 40px; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 20px 0 16px; position: sticky; top: 0; background: var(--bg); z-index: 5; }
.topbar h1 { margin: 0; font-size: 22px; flex: 1; }
.top-actions { display: flex; gap: 8px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 12px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.card h2 .spacer { flex: 1; }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: 1.4fr 1fr; }
.stats { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; cursor: pointer; box-shadow: var(--shadow); }
.stat:hover { border-color: var(--accent); }
.stat .n { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 12.5px; color: var(--muted); }
.stat.hot .n { color: var(--bad); }
.stat.warm .n { color: var(--warn); }
.stat.cool .n { color: var(--accent); }

.briefing { background: linear-gradient(135deg, var(--accent-soft), var(--panel)); }
.briefing pre { white-space: pre-wrap; font: inherit; margin: 0; }
.briefing .meta { font-size: 12px; color: var(--muted); margin-top: 10px; }

/* message list */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type=search] { flex: 1 1 200px; width: auto; }
.toolbar select { width: auto; flex: 0 1 180px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; margin-bottom: 12px; scrollbar-width: none; }
.tabs button { border: 0; background: transparent; padding: 7px 12px; border-radius: 99px; cursor: pointer; color: var(--muted); white-space: nowrap; }
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.list { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.msg { display: grid; grid-template-columns: 6px 1fr auto; gap: 12px; padding: 12px 14px 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.msg:last-child { border-bottom: 0; }
.msg:hover { background: var(--panel-2); }
.msg .bar { border-radius: 0 4px 4px 0; }
.msg .top { display: flex; gap: 8px; align-items: baseline; min-width: 0; }
.msg .from { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg.unread .from::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); margin-right: 6px; vertical-align: middle; }
.msg .subj { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg.read .subj, .msg.read .from { font-weight: 400; }
.msg .sum { color: var(--muted); font-size: 13.5px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.msg .side { text-align: right; font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; align-items: flex-end; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.chip { font-size: 11.5px; padding: 1px 8px; border-radius: 99px; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.chip.acct { color: #fff; border: 0; }
.chip.p5 { background: var(--bad-soft); color: var(--p5); border-color: transparent; font-weight: 600; }
.chip.p4 { background: var(--warn-soft); color: var(--p4); border-color: transparent; font-weight: 600; }
.chip.reply { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.chip.spam { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.chip.suspect { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.chip.good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

/* accounts strip */
.acct-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.acct-row:last-child { border-bottom: 0; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.acct-row .email { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kv { font-size: 12.5px; color: var(--muted); }

/* bars */
.bars { display: grid; gap: 8px; }
.barrow { display: grid; grid-template-columns: 110px 1fr 36px; gap: 8px; align-items: center; font-size: 13px; }
.barrow .track { height: 8px; border-radius: 99px; background: var(--panel-2); overflow: hidden; }
.barrow .fill { height: 100%; border-radius: 99px; background: var(--accent); }
.barrow .num { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.spark { display: flex; align-items: flex-end; gap: 6px; height: 90px; }
.spark .col { flex: 1; display: flex; flex-direction: column-reverse; gap: 2px; align-items: stretch; }
.spark .seg { border-radius: 3px; min-height: 2px; }
.spark-labels { display: flex; gap: 6px; font-size: 11px; color: var(--muted); }
.spark-labels span { flex: 1; text-align: center; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 5px; }

/* drawer */
.drawer { position: fixed; inset: 0; z-index: 40; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(2,6,23,.45); }
.drawer-panel { position: absolute; top: 0; right: 0; bottom: 0; width: min(760px, 100%); background: var(--bg); overflow-y: auto; padding: 20px 24px 60px; box-shadow: -10px 0 40px rgba(0,0,0,.2); }
.drawer-panel h2 { margin: 6px 0 4px; font-size: 20px; }
.ai-box { background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; margin: 12px 0; }
.ai-box ul { margin: 6px 0 0; padding-left: 18px; }
.body { white-space: pre-wrap; word-wrap: break-word; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; font-size: 14.5px; max-height: 55vh; overflow: auto; }
details { margin: 10px 0; }
summary { cursor: pointer; color: var(--muted); font-size: 13.5px; }
.reasons { font-size: 13px; color: var(--muted); margin: 6px 0 0; padding-left: 18px; }

/* drafts */
.draft { margin-bottom: 16px; }
.draft .orig { font-size: 13.5px; color: var(--muted); margin-bottom: 10px; }
.draft textarea { min-height: 160px; }

/* rules */
.rule { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--line); }
.rule:last-child { border-bottom: 0; }
.rule .desc { flex: 1; font-size: 13.5px; }
.rule .desc b { font-size: 14.5px; }
.builder-line { display: grid; grid-template-columns: 1.2fr 1fr 1.5fr auto; gap: 6px; margin-bottom: 6px; }
.builder-line.action { grid-template-columns: 1.2fr 1.5fr auto; }
.switch { position: relative; width: 38px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 99px; cursor: pointer; transition: .15s; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + span { background: var(--good); }
.switch input:checked + span::after { transform: translateX(16px); }
.range-row { display: flex; align-items: center; gap: 10px; }
.range-row input[type=range] { flex: 1; padding: 0; }
.range-row output { width: 36px; text-align: right; font-weight: 600; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.note { background: var(--warn-soft); border-radius: 10px; padding: 10px 12px; font-size: 13.5px; margin: 10px 0; }
.note.info { background: var(--accent-soft); }

.activity { font-size: 13px; }
.activity div { padding: 6px 0; border-bottom: 1px solid var(--line); display: flex; gap: 10px; }
.activity time { color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 60; box-shadow: 0 8px 30px rgba(0,0,0,.3); max-width: calc(100% - 32px); }
.toast.bad { background: var(--bad); }
.toast { display: flex; align-items: center; gap: 14px; }
.toast-action { background: none; border: 0; color: #93c5fd; font-weight: 700; cursor: pointer; padding: 4px 2px; text-transform: uppercase; letter-spacing: .03em; }

/* swipe left to delete */
.swipe { position: relative; overflow: hidden; border-bottom: 1px solid var(--line); transition: height .22s ease, opacity .22s ease; }
.swipe:last-child { border-bottom: 0; }
.swipe .msg { position: relative; z-index: 1; background: var(--panel); border-bottom: 0; transition: transform .22s ease; touch-action: pan-y; }
.swipe .msg.swiping { transition: none; }
.swipe .msg:hover { background: var(--panel-2); }
.swipe-bg { position: absolute; inset: 0; background: #94a3b8; color: #fff; display: flex; align-items: center; justify-content: flex-end; padding-right: 22px; font-weight: 600; transition: background .15s; }
.swipe.armed .swipe-bg { background: var(--bad); }
.swipe.gone { height: 0 !important; opacity: 0; border-bottom-width: 0; }

/* ---------- tablet & phone ---------- */
@media (max-width: 1100px) {
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid.cols-2 { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: auto; z-index: 30;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); flex-direction: row; gap: 0;
  }
  .sidebar .brand, .agent-pill { display: none; }
  nav { flex-direction: row; width: 100%; justify-content: space-around; }
  nav a { flex-direction: column; gap: 2px; padding: 6px 4px; font-size: 10.5px; position: relative; flex: 1; text-align: center; }
  nav a span { font-size: 18px; width: auto; }
  nav a b { font-weight: 500; }
  nav a i { position: absolute; top: 0; right: 10%; font-size: 10px; padding: 0 5px; background: var(--bad); }
  nav a[data-view=accounts], nav a[data-view=rules] { display: none; }
  main { padding: 0 16px calc(84px + env(safe-area-inset-bottom)); }
  .topbar { padding: calc(12px + env(safe-area-inset-top)) 0 10px; }
  .topbar h1 { font-size: 20px; }
  .hide-sm { display: none; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat .n { font-size: 22px; }
  .card { padding: 14px; }
  .drawer-panel { padding: calc(14px + env(safe-area-inset-top)) 16px 90px; }
  .builder-line, .builder-line.action { grid-template-columns: 1fr; padding-bottom: 8px; border-bottom: 1px dashed var(--line); }
  .msg { grid-template-columns: 5px 1fr; }
  .msg .side { grid-column: 2; flex-direction: row; justify-content: flex-start; }
  .barrow { grid-template-columns: 90px 1fr 30px; }
}
