:root {
  --bg: #f5f5f2;
  --surface: #ffffff;
  --sunken: #efeeea;
  --border: #e2e0da;
  --border-strong: #cfccc4;
  --text: #1d1c1a;
  --muted: #6d6b65;
  --accent: #2d55c9;
  --accent-text: #ffffff;
  --blue-bg: #e5ecfb;   --blue-fg: #2347a8;
  --green-bg: #dff3e6;  --green-fg: #1a6b42;
  --red-bg: #fbe6e3;    --red-fg: #a8302a;
  --amber-bg: #faefd4;  --amber-fg: #7d5200;
  --orange-bg: #fde6d3; --orange-fg: #a44300;
  --gray-bg: #eceae5;   --gray-fg: #5c5a55;
  --warm: #2d55c9;
  --cold: #8fa6e6;
  --queued: #e2c27a;
  --shadow: 0 12px 40px rgba(20, 20, 15, 0.16);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161614;
    --surface: #1f1f1c;
    --sunken: #282825;
    --border: #33322e;
    --border-strong: #48463f;
    --text: #ecebe6;
    --muted: #a09d95;
    --accent: #8aa5f5;
    --accent-text: #10172e;
    --blue-bg: #1d2a4d;   --blue-fg: #a9bef8;
    --green-bg: #173524;  --green-fg: #8fd6ab;
    --red-bg: #45201d;    --red-fg: #f2a59f;
    --amber-bg: #3d2e0c;  --amber-fg: #ecc56e;
    --orange-bg: #47260f; --orange-fg: #f5b182;
    --gray-bg: #2e2d2a;   --gray-fg: #b3b0a8;
    --warm: #8aa5f5;
    --cold: #4a5c94;
    --queued: #7d6a3a;
    --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", "Microsoft YaHei", sans-serif;
  font-variant-numeric: tabular-nums;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h2 { font-size: 14px; font-weight: 600; margin: 0 0 12px; }
code { font: 12.5px/1.5 ui-monospace, "SF Mono", Menlo, monospace; background: var(--sunken); padding: 2px 6px; border-radius: 4px; }

/* --- top bar */
.top { position: sticky; top: 0; z-index: 5; background: var(--surface); border-bottom: 1px solid var(--border); }
.top-inner { max-width: 1180px; margin: 0 auto; padding: 0 24px; height: 52px; display: flex; align-items: center; gap: 28px; }
.brand { font-weight: 700; letter-spacing: 0.04em; }
#nav { display: flex; gap: 4px; flex: 1; }
#nav a { padding: 6px 12px; border-radius: 6px; color: var(--muted); }
#nav a:hover { text-decoration: none; color: var(--text); background: var(--sunken); }
#nav a.active { color: var(--text); background: var(--sunken); font-weight: 600; }
.health { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green-fg); }
.dot.paused { background: var(--red-fg); }
.dot.idle { background: var(--border-strong); }

main { max-width: 1180px; margin: 0 auto; padding: 24px 24px 64px; }
.stack > * + * { margin-top: 16px; }
.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 20px 0; text-align: center; }

/* --- building blocks */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px 18px; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 16px; align-items: start; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

button, .button {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 6px; padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
button:hover { background: var(--sunken); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--red-fg); border-color: var(--red-fg); }
button.quiet { border-color: transparent; background: none; color: var(--muted); padding: 4px 8px; }
button.quiet:hover { color: var(--text); background: var(--sunken); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input[type="search"], input[type="text"], select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 6px; padding: 6px 10px; min-width: 0;
}
textarea { width: 100%; resize: vertical; min-height: 64px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--gray-bg); color: var(--gray-fg); }
.badge.sent { background: var(--blue-bg); color: var(--blue-fg); }
.badge.replied { background: var(--green-bg); color: var(--green-fg); }
.badge.opted_out { background: var(--red-bg); color: var(--red-fg); }
.badge.pending { background: var(--amber-bg); color: var(--amber-fg); }
.badge.sending, .badge.flag { background: var(--orange-bg); color: var(--orange-fg); }
.tier { font-size: 12px; color: var(--muted); border: 1px solid var(--border); border-radius: 4px; padding: 0 6px; }

.banner { display: flex; align-items: center; gap: 16px; padding: 12px 16px; border-radius: 10px; background: var(--red-bg); color: var(--red-fg); }
.banner strong { font-weight: 600; }
.banner .grow { flex: 1; }
.banner.soft { background: var(--amber-bg); color: var(--amber-fg); }

/* --- overview */
.tiles { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 860px) { .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.tile .label { color: var(--muted); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 650; line-height: 1.25; margin-top: 2px; }
.tile .sub { color: var(--muted); font-size: 12.5px; }

.dist { display: flex; height: 10px; border-radius: 999px; overflow: hidden; background: var(--sunken); gap: 2px; }
.dist span { min-width: 3px; }
.seg-new { background: var(--border-strong); }
.seg-pending { background: var(--queued); }
.seg-sending { background: var(--orange-fg); }
.seg-sent { background: var(--blue-fg); }
.seg-replied { background: var(--green-fg); }
.seg-opted_out { background: var(--red-fg); }
.seg-skipped, .seg-failed { background: var(--gray-fg); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 12px; font-size: 13px; color: var(--muted); }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 6px; }
.legend b { color: var(--text); font-weight: 600; margin-left: 4px; }

.reply { display: block; width: 100%; text-align: left; border: 0; border-top: 1px solid var(--border); border-radius: 0; padding: 11px 2px; background: none; white-space: normal; }
.reply:first-of-type { border-top: 0; }
.reply:hover { background: var(--sunken); }
.reply .who { display: flex; align-items: baseline; gap: 8px; }
.reply .who .name { font-weight: 600; }
.reply .who .time { margin-left: auto; color: var(--muted); font-size: 12.5px; }
.reply .text { color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }

.meter + .meter { margin-top: 14px; }
.meter .row { display: flex; justify-content: space-between; font-size: 13px; }
.meter .track { height: 8px; border-radius: 999px; background: var(--sunken); margin-top: 6px; overflow: hidden; }
.meter .fill { height: 100%; border-radius: 999px; background: var(--accent); }
.facts { margin: 14px 0 0; padding-top: 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); }
.stuck { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--border); }
.stuck .grow { flex: 1; min-width: 0; }

/* --- tables */
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-weight: 500; font-size: 12.5px; color: var(--muted); padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; }
.table-card { padding: 0; overflow-x: auto; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--sunken); }
.cell-main { font-weight: 600; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.toolbar .grow { flex: 1; }
.toolbar input[type="search"] { width: 260px; }
.pager { display: flex; align-items: center; justify-content: flex-end; gap: 10px; color: var(--muted); font-size: 13px; }

/* --- drawer */
.overlay { position: fixed; inset: 0; background: rgba(15, 15, 10, 0.35); z-index: 10; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(480px, 100vw); z-index: 11; background: var(--surface); border-left: 1px solid var(--border); box-shadow: var(--shadow); overflow-y: auto; padding: 20px 22px 40px; }
.drawer-head { display: flex; align-items: flex-start; gap: 12px; }
.drawer-head h1 { font-size: 20px; margin: 0; flex: 1; line-height: 1.3; overflow-wrap: anywhere; }
.drawer section { margin-top: 22px; }
.kv { display: grid; grid-template-columns: 84px minmax(0, 1fr); gap: 6px 12px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.message { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.message + .message { margin-top: 8px; }
.message .meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.message pre, .preview pre { font: inherit; white-space: pre-wrap; overflow-wrap: anywhere; margin: 8px 0 0; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: grid; grid-template-columns: 118px minmax(0, 1fr); gap: 10px; padding: 7px 0; border-top: 1px solid var(--border); }
.timeline li:first-child { border-top: 0; }
.timeline time { color: var(--muted); font-size: 12.5px; padding-top: 1px; }
.timeline .what { font-weight: 600; }
.timeline .detail { color: var(--muted); overflow-wrap: anywhere; white-space: pre-wrap; }

/* --- campaigns */
.summary { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; }
.summary .big { font-size: 22px; font-weight: 650; }
.summary .grow { flex: 1; }
.day { display: grid; grid-template-columns: 96px minmax(0, 1fr) 96px; gap: 12px; align-items: center; padding: 3px 0; font-size: 13px; }
.day .bar { display: flex; height: 12px; gap: 2px; }
.day .bar span { border-radius: 3px; }
.day .bar .warm { background: var(--warm); }
.day .bar .cold { background: var(--cold); }
.day .count { color: var(--muted); text-align: right; }
.preview { border-top: 1px solid var(--border); padding: 12px 0; }
.preview:first-of-type { border-top: 0; }
.preview .meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.preview .meta .name { font-weight: 600; }

/* --- login, templates, sending */
.locked #nav, .locked #health, .locked #logout { display: none; }
.login { max-width: 360px; margin: 12vh auto 0; display: grid; gap: 14px; }
.login h1 { font-size: 18px; margin: 0; }
input[type="password"], input[type="tel"] { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: 6px; padding: 6px 10px; min-width: 0; }
.template textarea { margin-top: 12px; line-height: 1.6; }
.form-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.form-row + .form-row, .form-row + textarea, .card > p + .form-row { margin-top: 10px; }
.card > p { margin: 0 0 10px; }
.log { font: 12.5px/1.7 ui-monospace, "SF Mono", Menlo, monospace; white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; max-height: 420px; overflow-y: auto; color: var(--muted); }

.account-budget .row { display: flex; justify-content: space-between; margin-bottom: 8px; }
.account-budget + .account-budget { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.form-row h2 { margin: 0; }
details summary { cursor: pointer; margin-top: 12px; font-size: 13px; }
details .log { margin-top: 8px; }
.card > .form-row + .banner, .card > .form-row + .form-row, .card > .form-row + p { margin-top: 12px; }

.steps { margin: 0 0 14px; padding-left: 22px; }
.steps li + li { margin-top: 6px; }

a.button { display: inline-block; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: 6px; padding: 6px 12px; white-space: nowrap; }
a.button:hover { text-decoration: none; background: var(--sunken); }

/* --- toasts */
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 20; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 8px; box-shadow: var(--shadow); max-width: min(560px, 90vw); white-space: pre-line; }
.toast.error { background: var(--red-fg); color: #fff; }
