/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ee;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --border: rgba(11, 11, 11, 0.10);
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #2a78d6;
  --accent-hover: #256abf;
  --accent-text: #ffffff;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --danger: #d03b3b;
  --danger-wash: rgba(208, 59, 59, 0.10);
  --good: #006300;
  --good-wash: rgba(12, 163, 12, 0.12);
  --warn: #8a5a00;
  --warn-wash: rgba(250, 178, 25, 0.18);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 8px 24px rgba(0, 0, 0, .06);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --border: rgba(255, 255, 255, 0.10);
    --grid: #2c2c2a;
    --axis: #383835;
    --accent: #3987e5;
    --accent-hover: #5598e7;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --danger: #e66767;
    --danger-wash: rgba(230, 103, 103, 0.14);
    --good: #0ca30c;
    --good-wash: rgba(12, 163, 12, 0.16);
    --warn: #fab219;
    --warn-wash: rgba(250, 178, 25, 0.14);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --text: #ffffff; --text-2: #c3c2b7;
  --muted: #898781; --border: rgba(255, 255, 255, 0.10); --grid: #2c2c2a; --axis: #383835;
  --accent: #3987e5; --accent-hover: #5598e7; --accent-wash: rgba(57, 135, 229, 0.16);
  --danger: #e66767; --danger-wash: rgba(230, 103, 103, 0.14); --good: #0ca30c;
  --good-wash: rgba(12, 163, 12, 0.16); --warn: #fab219; --warn-wash: rgba(250, 178, 25, 0.14);
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page); color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
p { margin: 0 0 8px; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.secondary { color: var(--text-2); }
.small { font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }
.boot { padding: 48px; color: var(--muted); text-align: center; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.shell {
  display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh;
  /* paint the sidebar column full height even when the page scrolls past it */
  background: linear-gradient(to right, var(--surface) 219px, var(--border) 219px, var(--border) 220px, transparent 220px);
}
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 16px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; font-weight: 700; font-size: 16px; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; }
.brand-mark svg { width: 16px; height: 16px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text-2); font-weight: 500;
}
.nav a svg { width: 18px; height: 18px; flex: none; }
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-wash); color: var(--accent); }
.sidebar-foot { margin-top: auto; padding: 12px 8px 0; border-top: 1px solid var(--border); display: grid; gap: 8px; }
.sidebar-foot .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.main { padding: 24px 32px 48px; max-width: 1280px; width: 100%; min-width: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .sub { color: var(--text-2); margin-top: 2px; }
.crumbs { font-size: 13px; color: var(--muted); margin-bottom: 4px; }

@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; background: none; }
  .sidebar { min-width: 0; border-right: 0; border-bottom: 1px solid var(--border); position: static; height: auto; flex-direction: row; align-items: center; overflow-x: auto; padding: 8px 12px; }
  .brand { padding: 0 8px 0 0; }
  .brand span { display: none; }
  .nav { display: flex; gap: 2px; }
  .nav a { padding: 6px 8px; white-space: nowrap; }
  .nav a span { display: none; }
  .sidebar-foot { margin: 0 0 0 auto; border: 0; padding: 0; display: flex; }
  .sidebar-foot .who, .sidebar-foot button span { display: none; }
  .main { padding: 16px; }
}

/* ---------- cards & grids ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head .sub { color: var(--muted); font-size: 12.5px; }
.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)); }
.stack { display: grid; gap: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
@media (max-width: 900px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile .label { color: var(--text-2); font-size: 12.5px; }
.tile .value { font-size: 24px; font-weight: 600; margin-top: 2px; }
.tile .hint { color: var(--muted); font-size: 12px; }
.meter { height: 6px; border-radius: 3px; background: var(--accent-wash); margin-top: 8px; overflow: hidden; }
.meter > div { height: 100%; background: var(--accent); border-radius: 3px; }

/* ---------- forms ---------- */
label.field { display: grid; gap: 4px; font-weight: 500; font-size: 13px; color: var(--text-2); }
label.field .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.form { display: grid; gap: 14px; }
.form-row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--axis);
  border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0;
}
textarea { resize: vertical; min-height: 80px; }
textarea.code { font-family: var(--mono); font-size: 12.5px; line-height: 1.5; min-height: 320px; tab-size: 2; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
input[type="checkbox"] { width: auto; accent-color: var(--accent); }
label.check { display: inline-flex; align-items: center; gap: 8px; font-weight: 400; color: var(--text); cursor: pointer; }
select { appearance: auto; }
.input-sm { padding: 6px 8px; font-size: 13px; }

/* ---------- buttons ---------- */
button, .btn {
  font: inherit; font-weight: 500; border-radius: 8px; padding: 8px 14px; cursor: pointer;
  border: 1px solid var(--axis); background: var(--surface); color: var(--text);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; line-height: 1.3;
}
button:hover, .btn:hover { background: var(--surface-2); text-decoration: none; }
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { background: var(--accent-hover); }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
button.danger:hover { background: var(--danger-wash); }
button.ghost { border-color: transparent; background: transparent; color: var(--text-2); }
button.ghost:hover { background: var(--surface-2); color: var(--text); }
button.sm { padding: 5px 10px; font-size: 13px; }
button.icon { padding: 6px; }
button svg { width: 16px; height: 16px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.empty .btn, .empty button { margin-top: 12px; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 14px; color: var(--muted); font-size: 13px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input[type="search"] { max-width: 280px; }
.toolbar select { width: auto; max-width: 100%; }

/* ---------- badges & chips ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.good { background: var(--good-wash); color: var(--good); }
.badge.warn { background: var(--warn-wash); color: var(--warn); }
.badge.bad { background: var(--danger-wash); color: var(--danger); }
.badge.info { background: var(--accent-wash); color: var(--accent); }
.chip { display: inline-block; padding: 1px 8px; border-radius: 6px; background: var(--surface-2); font-size: 12px; margin: 1px 4px 1px 0; color: var(--text-2); }
.chip.button { cursor: pointer; border: 1px solid var(--border); }
.chip.button:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center;
  padding: 16px; z-index: 50; animation: fade .12s ease-out;
}
.modal {
  background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); width: 100%; max-width: 560px;
  max-height: calc(100vh - 32px); display: flex; flex-direction: column; border: 1px solid var(--border);
}
.modal.wide { max-width: 860px; }
.modal-head { padding: 18px 20px 0; display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.modal-body { padding: 16px 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--grid); display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
@keyframes fade { from { opacity: 0; } }

/* ---------- toasts ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 60; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 10px;
  box-shadow: var(--shadow); font-size: 13.5px; animation: fade .15s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth .card { width: 100%; max-width: 440px; padding: 28px; }
.auth .brand { justify-content: center; padding-bottom: 8px; font-size: 20px; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; margin: 16px 0 20px; overflow-x: auto; }
.tabs button { flex: 1; justify-content: center; border: 0; background: transparent; color: var(--text-2); }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
details.more summary { cursor: pointer; color: var(--accent); font-weight: 500; margin: 4px 0; }
details.more[open] summary { margin-bottom: 12px; }
.error-text { color: var(--danger); font-size: 13px; }

/* ---------- charts ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .bar:hover, .chart .bar.hover { opacity: .8; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; color: var(--text-2); margin-bottom: 8px; }
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.legend .swatch { width: 12px; height: 3px; border-radius: 2px; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  box-shadow: var(--shadow); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; min-width: 130px; z-index: 5;
}
.tooltip .t-title { font-weight: 600; margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tooltip .t-row span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); }
.tooltip .swatch { width: 8px; height: 8px; border-radius: 50%; }
.chart-table { margin-top: 8px; max-height: 260px; overflow: auto; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent); font-size: 12.5px; font-weight: 500; }
.link-btn:hover { background: none; text-decoration: underline; }

/* ---------- editors ---------- */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 1100px) { .editor { grid-template-columns: 1fr; } }
.preview-frame { width: 100%; height: 640px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.preview-subject { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; background: var(--surface-2); }
.rule { display: grid; grid-template-columns: minmax(140px, 1.2fr) minmax(130px, 1fr) minmax(120px, 1.2fr) auto; gap: 8px; align-items: center; }
@media (max-width: 700px) { .rule { grid-template-columns: 1fr 1fr; } }
.step {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; background: var(--surface);
  border-left: 3px solid var(--accent);
}
.step.t-wait { border-left-color: var(--muted); }
.step.t-condition { border-left-color: var(--series-2); }
.step.t-unsubscribe { border-left-color: var(--danger); }
.step-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.step-num { font-weight: 600; color: var(--muted); font-size: 12px; min-width: 48px; }
.steps { display: grid; gap: 10px; }
.connector { width: 2px; height: 10px; background: var(--axis); margin: -10px 0 0 24px; }
.kv { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr) auto; gap: 8px; margin-bottom: 8px; }
.progress { height: 8px; border-radius: 4px; background: var(--accent-wash); overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); }
.dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; }
.copy-field { display: flex; gap: 8px; }
.copy-field input { font-family: var(--mono); font-size: 12.5px; }
