:root {
  --bg: #f5f5f1;
  --surface: #ffffff;
  --surface-2: #fafaf7;
  --ink: #17191d;
  --ink-2: #454b55;
  --muted: #6b717c;
  --line: #e3e3dc;
  --line-2: #d4d4cb;
  --accent: #1d5c57;
  --accent-ink: #ffffff;
  --accent-soft: #e3efed;
  --focus: #2f7f78;
  --hot: #c2410c;
  --hot-soft: #fdeee4;
  --warn: #a16207;
  --warn-soft: #fbf3dc;
  --info: #1d4f91;
  --info-soft: #e5edf8;
  --plan: #5b3fa3;
  --plan-soft: #eeeaf8;
  --ok: #2f6b3a;
  --ok-soft: #e5f1e7;
  --err: #b42318;
  --err-soft: #fbe9e7;
  --gray-soft: #eeeeea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 22, 26, .05), 0 1px 1px rgba(20, 22, 26, .03);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131517;
    --surface: #1b1e21;
    --surface-2: #202327;
    --ink: #eceeef;
    --ink-2: #c6cad0;
    --muted: #8e959f;
    --line: #2c3035;
    --line-2: #3a3f45;
    --accent: #5fb3a9;
    --accent-ink: #0d1413;
    --accent-soft: #1c2f2d;
    --focus: #7cc9bf;
    --hot: #f28a52;
    --hot-soft: #3a2418;
    --warn: #e0b24c;
    --warn-soft: #33290f;
    --info: #86b1ec;
    --info-soft: #1a2638;
    --plan: #b3a0ec;
    --plan-soft: #261f3a;
    --ok: #7fc48b;
    --ok-soft: #18291b;
    --err: #f08a80;
    --err-soft: #3a1c19;
    --gray-soft: #26292d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 2px; }
h1, h2, h3 { margin: 0; line-height: 1.25; text-wrap: balance; }
h1 { font-size: 22px; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; }
p { margin: 0; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

/* —— shell —— */
.shell { display: grid; grid-template-columns: 224px minmax(0, 1fr); min-height: 100vh; }
.side {
  position: sticky; top: 0; height: 100vh; padding: 18px 12px;
  border-right: 1px solid var(--line); background: var(--surface-2);
  display: flex; flex-direction: column; gap: 18px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px; font-weight: 650; }
.brand__mark {
  width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 14px;
}
.brand small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 7px 10px; border-radius: 8px; color: var(--ink-2);
}
.nav a:hover { background: var(--gray-soft); text-decoration: none; }
.nav a.is-active { background: var(--accent-soft); color: var(--ink); font-weight: 600; }
.nav .count { font-size: 12px; font-variant-numeric: tabular-nums; color: var(--muted); }
.nav .count--hot { color: var(--hot); font-weight: 650; }
.side__foot { margin-top: auto; padding: 0 8px; }
.linkbtn { background: none; border: 0; padding: 0; color: var(--muted); font: inherit; cursor: pointer; }
.linkbtn:hover { color: var(--ink); text-decoration: underline; }

.main { padding: 24px 28px 64px; min-width: 0; }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px 20px; margin-bottom: 18px; }
.page-head p { color: var(--muted); margin-top: 4px; }
.stack { display: grid; gap: 16px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }

/* —— cards —— */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.card__body { padding: 16px; }
.card__body > * + * { margin-top: 12px; }
.grid { display: grid; gap: 16px; }
.grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid--main { grid-template-columns: minmax(0, 1fr) 340px; align-items: start; }

.alert { display: flex; gap: 10px; align-items: flex-start; padding: 10px 14px; border-radius: 8px; border: 1px solid; }
.alert--warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 30%, transparent); }
.alert--err { background: var(--err-soft); border-color: color-mix(in srgb, var(--err) 30%, transparent); color: var(--ink); }
.alert--ok { background: var(--ok-soft); border-color: color-mix(in srgb, var(--ok) 30%, transparent); }
.flash { position: sticky; top: 12px; z-index: 5; margin-bottom: 16px; }

/* —— metrics —— */
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border-top: 1px solid var(--line); }
.metric { padding: 12px 16px; border-right: 1px solid var(--line); }
.metric:last-child { border-right: 0; }
.metric b { display: block; font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; }
.metric span { font-size: 12px; color: var(--muted); }
.metric--hot b { color: var(--hot); }
.metric--warn b { color: var(--warn); }

/* —— chips —— */
.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 550; line-height: 18px; white-space: nowrap;
  background: var(--gray-soft); color: var(--ink-2);
}
.chip--awaiting { background: var(--info-soft); color: var(--info); }
.chip--needs_human, .chip--ai_error { background: var(--warn-soft); color: var(--warn); }
.chip--scheduled { background: var(--plan-soft); color: var(--plan); }
.chip--replied { background: var(--ok-soft); color: var(--ok); }
.chip--hot { background: var(--hot-soft); color: var(--hot); }
.chip--err { background: var(--err-soft); color: var(--err); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); }
.dot--ok { background: var(--ok); } .dot--err { background: var(--err); } .dot--warn { background: var(--warn); }

/* —— tabs & filters —— */
.tabs { display: flex; gap: 2px; overflow-x: auto; border-bottom: 1px solid var(--line); margin-bottom: 14px; scrollbar-width: none; }
.tabs a { padding: 8px 12px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a:hover { color: var(--ink); text-decoration: none; }
.tabs a.is-active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.tabs .num { margin-left: 4px; font-size: 12px; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.filters input[type=search] { min-width: 220px; flex: 1 1 240px; max-width: 360px; }

/* —— thread list —— */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.item { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 16px; padding: 12px 16px; border-bottom: 1px solid var(--line); color: inherit; }
.item:last-child { border-bottom: 0; }
.item:hover { background: var(--surface-2); text-decoration: none; }
.item__who { display: flex; align-items: center; gap: 8px; min-width: 0; font-weight: 600; }
.item__who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item.is-unread .item__who::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.item__meta { display: flex; align-items: center; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.item__summary { grid-column: 1 / -1; color: var(--ink-2); }
.item__snippet { grid-column: 1 / -1; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

/* —— conversation —— */
.msg { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.msg--out { background: var(--surface-2); border-style: dashed; }
.msg__head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.msg__body { padding: 12px 14px; white-space: pre-wrap; overflow-wrap: anywhere; max-width: 72ch; }
.msg details { padding: 0 14px 12px; }
.msg details summary { cursor: pointer; color: var(--muted); font-size: 12.5px; }
.msg details pre { white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; color: var(--muted); margin: 8px 0 0; }

.draft textarea { min-height: 220px; font-size: 14.5px; line-height: 1.55; }
.sig { white-space: pre-wrap; color: var(--muted); font-size: 13px; border-top: 1px dashed var(--line-2); padding-top: 8px; }
.kv { margin: 0; display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 6px 12px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

/* —— forms —— */
label { display: block; font-weight: 550; margin-bottom: 5px; }
.hint { color: var(--muted); font-size: 12.5px; font-weight: 400; margin-top: 4px; }
.field + .field { margin-top: 14px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line-2); border-radius: 8px;
  background: var(--surface); color: var(--ink); font: inherit;
}
textarea { resize: vertical; min-height: 90px; }
textarea.kb { min-height: 420px; font-size: 13.5px; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--focus) 45%, transparent); border-color: var(--focus); }
.check { display: flex; align-items: flex-start; gap: 8px; font-weight: 400; }
.check input { margin-top: 3px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 16px; }
.form-grid .full { grid-column: 1 / -1; }
fieldset { border: 0; padding: 0; margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line-2);
  background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn--primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn--danger { color: var(--err); }
.btn--sm { padding: 5px 10px; font-size: 13px; }
.btn[disabled] { opacity: .55; cursor: progress; }

table.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th, .table td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { color: var(--muted); font-weight: 550; font-size: 12.5px; background: var(--surface-2); }
.table tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

pre.json { margin: 0; padding: 12px; background: var(--surface-2); border-radius: 8px; overflow-x: auto; font-family: var(--mono); font-size: 12.5px; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(380px, 100%); }

@media (max-width: 1100px) {
  .grid--main { grid-template-columns: minmax(0, 1fr); }
  .grid--3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 820px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .side { position: sticky; top: 0; z-index: 10; height: auto; flex-direction: row; align-items: center; padding: 8px 12px; gap: 10px; border-right: 0; border-bottom: 1px solid var(--line); }
  .brand small, .side__foot { display: none; }
  .nav { flex-direction: row; overflow-x: auto; scrollbar-width: none; }
  .nav a { white-space: nowrap; padding: 6px 9px; }
  .main { padding: 16px 16px 48px; }
  .grid--2, .grid--3, .form-grid { grid-template-columns: minmax(0, 1fr); }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric:nth-child(2) { border-right: 0; }
  .metric:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .kv { grid-template-columns: 96px minmax(0, 1fr); }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
