:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #faf8f3;
  --ink: #1f1a12;
  --muted: #72685a;
  --border: #e7e1d5;
  --gold: #a9842f;
  --gold-ink: #7d5f1c;
  --gold-soft: #f4ecd9;
  --side: #1c1810;
  --side-ink: #e9e1cf;
  --side-muted: #a39881;
  --bubble-in: #eceae4;
  --bubble-out: #a9842f;
  --bubble-out-ink: #ffffff;
  --c-delivered: #2f8f4e;
  --c-sent: #3b74c4;
  --c-pending: #a7a196;
  --c-unconfirmed: #d79b26;
  --c-failed: #c8432f;
  --c-skipped: #8b6bb5;
  --danger: #b73a28;
  --shadow: 0 1px 2px rgba(31, 26, 18, 0.06), 0 6px 20px rgba(31, 26, 18, 0.06);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12100b;
    --surface: #1b1812;
    --surface-2: #211d16;
    --ink: #f0e9db;
    --muted: #a99f8c;
    --border: #322c21;
    --gold: #d2ad59;
    --gold-ink: #e3c47f;
    --gold-soft: #2c2516;
    --side: #0d0b07;
    --bubble-in: #2a261e;
    --bubble-out: #8c6b22;
    --c-pending: #6f6a60;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14.5px/1.5 "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--gold-ink); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
h1, h2, h3 { margin: 0; line-height: 1.25; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--gold); }
.btn.primary { background: var(--gold); border-color: var(--gold); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; filter: none; }
.linkbtn { background: none; border: 0; color: inherit; font: inherit; cursor: pointer; padding: 0; text-decoration: underline; }
.iconbtn { background: none; border: 0; cursor: pointer; font-size: 16px; padding: 4px 6px; border-radius: 8px; color: var(--muted); }
.iconbtn:hover { background: var(--surface-2); color: var(--ink); }

input, textarea, select {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px; padding: 9px 11px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--gold); padding: 0; }
label { display: block; font-weight: 600; font-size: 13px; }
label > input, label > textarea, label > select { margin-top: 5px; font-weight: 400; }
.form-err { color: var(--danger); min-height: 1em; margin: 8px 0; font-size: 13px; }
.field { margin-bottom: 14px; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); padding: 32px 28px; border-radius: 18px; box-shadow: var(--shadow); text-align: center; }
.login-card h1 { font-size: 21px; margin-top: 8px; }
.login-card label { text-align: left; margin-top: 14px; }
.login-card .btn { margin-top: 6px; }
.login-logo { width: 72px; }
.login-card .sub { margin: 4px 0 6px; }
.login-card .alt { margin-top: 16px; font-size: 13px; }
.login-card .ok-box { background: rgba(47, 143, 78, 0.1); color: var(--c-delivered); border-radius: 10px; padding: 12px; font-size: 14px; text-align: left; margin-top: 14px; }
.ts { margin-top: 14px; min-height: 65px; display: flex; justify-content: center; }
.pwhint { font-size: 12px; color: var(--muted); text-align: left; margin-top: 4px; }
.linkbox { display: flex; gap: 6px; margin-top: 8px; }
.linkbox input { font-size: 12.5px; }

/* ---------- Shell ---------- */
.shell { display: flex; height: 100vh; height: 100dvh; }
.side {
  width: 232px; flex: 0 0 232px; background: var(--side); color: var(--side-ink);
  display: flex; flex-direction: column; padding: 18px 12px;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 0 8px 18px; }
.brand img { width: 40px; height: 40px; border-radius: 10px; object-fit: cover; background: #fff; }
.brand strong { display: block; font-size: 15px; }
.brand span { font-size: 12px; color: var(--side-muted); }
#nav { display: flex; flex-direction: column; gap: 2px; }
#nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 9px;
  color: var(--side-ink); text-decoration: none; font-weight: 500;
}
#nav a i { font-style: normal; width: 20px; text-align: center; }
#nav a:hover { background: rgba(255, 255, 255, 0.06); }
#nav a.active { background: rgba(210, 173, 89, 0.18); color: #f3d892; }
.badge { margin-left: auto; background: var(--gold); color: #fff; font-size: 11.5px; border-radius: 999px; padding: 1px 7px; font-style: normal; }
.side-foot { margin-top: auto; padding: 8px; display: flex; flex-direction: column; gap: 10px; font-size: 13px; }
.balance { background: rgba(255, 255, 255, 0.06); border-radius: 10px; padding: 10px 12px; cursor: pointer; }
.balance .label { display: block; color: var(--side-muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em; }
.balance .value { font-size: 18px; font-weight: 700; }
.mode-pill { background: #6b4e12; color: #ffe4a3; border-radius: 8px; padding: 4px 8px; font-size: 12px; text-align: center; }
.side-foot .linkbtn { color: var(--side-muted); text-align: left; }

.view { flex: 1; min-width: 0; overflow: auto; }
.page { max-width: 1100px; margin: 0 auto; padding: 26px 28px 60px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { font-size: 22px; }
.page-head .grow { flex: 1; }
.crumb { font-size: 13px; color: var(--muted); text-decoration: none; display: inline-block; margin-bottom: 6px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card.pad { padding: 20px; }
.card h2 { font-size: 15.5px; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; align-items: start; }
.stack > * + * { margin-top: 18px; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .big { font-size: 34px; display: block; margin-bottom: 6px; }

/* Tables */
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.tbl td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.click { cursor: pointer; }
.tbl tr.click:hover td { background: var(--surface-2); }
.tbl .r { text-align: right; }
.tblwrap { overflow-x: auto; }

.pill { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.pill.gold { background: var(--gold-soft); color: var(--gold-ink); border-color: transparent; }
.pill.red { background: rgba(200, 67, 47, 0.12); color: var(--c-failed); border-color: transparent; }
.pill.green { background: rgba(47, 143, 78, 0.12); color: var(--c-delivered); border-color: transparent; }
.pill.blue { background: rgba(59, 116, 196, 0.12); color: var(--c-sent); border-color: transparent; }
.st { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; }
.st::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c, var(--c-pending)); }

/* ---------- Inbox ---------- */
.inbox { display: flex; height: 100%; }
.threads { width: 340px; flex: 0 0 340px; border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; }
.threads-head { padding: 16px 14px 10px; display: flex; flex-direction: column; gap: 10px; }
.threads-head .row { display: flex; align-items: center; gap: 8px; }
.threads-head h1 { font-size: 20px; flex: 1; }
.threads-head .filters { display: flex; gap: 6px; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 11px; font: inherit; font-size: 12.5px; cursor: pointer; color: var(--muted); }
.chip.on { background: var(--gold-soft); color: var(--gold-ink); border-color: transparent; font-weight: 600; }
.thread-list { overflow-y: auto; flex: 1; }
.thread { display: flex; gap: 12px; padding: 11px 14px; cursor: pointer; align-items: center; text-decoration: none; color: inherit; border-left: 3px solid transparent; }
.thread:hover { background: var(--surface-2); }
.thread.active { background: var(--gold-soft); border-left-color: var(--gold); }
.avatar { width: 42px; height: 42px; border-radius: 50%; flex: 0 0 42px; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 15px; }
.thread .meta { flex: 1; min-width: 0; }
.thread .top { display: flex; gap: 8px; align-items: baseline; }
.thread .name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread .time { font-size: 12px; color: var(--muted); }
.thread .snippet { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread.unread .name, .thread.unread .snippet { font-weight: 700; color: var(--ink); }
.thread.unread .time { color: var(--gold-ink); font-weight: 700; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--gold); flex: 0 0 9px; }

.convo { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.convo-head { display: flex; align-items: center; gap: 12px; padding: 12px 18px; background: var(--surface); border-bottom: 1px solid var(--border); }
.convo-head .who { flex: 1; min-width: 0; }
.convo-head .who strong { display: block; font-size: 15.5px; }
.convo-head .who span { font-size: 12.5px; color: var(--muted); }
.convo-body { flex: 1; overflow-y: auto; padding: 18px 22px; display: flex; flex-direction: column; gap: 3px; }
.daysep { text-align: center; font-size: 12px; color: var(--muted); margin: 14px 0 8px; }
.msg { max-width: min(560px, 78%); display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 9px 13px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in .bubble { background: var(--bubble-in); border-bottom-left-radius: 6px; }
.msg.out .bubble { background: var(--bubble-out); color: var(--bubble-out-ink); border-bottom-right-radius: 6px; }
.msg.blast .bubble { background: var(--surface); color: var(--ink); border: 1px dashed var(--gold); }
.msg.failed .bubble { background: rgba(200, 67, 47, 0.14); color: var(--ink); }
.msg .info { font-size: 11.5px; color: var(--muted); margin: 3px 6px 8px; }
.msg .info .err { color: var(--c-failed); }
.msg .tag { font-size: 11px; font-weight: 600; color: var(--gold-ink); margin: 0 6px 3px; }
.msg img { max-width: 260px; border-radius: 12px; display: block; margin-top: 4px; }
.sysnote { align-self: center; font-size: 12px; color: var(--muted); background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; margin: 4px 0 8px; }
.composer { display: flex; gap: 10px; align-items: flex-end; padding: 12px 16px; background: var(--surface); border-top: 1px solid var(--border); }
.composer textarea { resize: none; max-height: 160px; border-radius: 20px; padding: 10px 16px; }
.composer .send { border-radius: 50%; width: 42px; height: 42px; padding: 0; font-size: 17px; flex: 0 0 42px; }
.counter { font-size: 11.5px; color: var(--muted); padding: 0 18px 8px; background: var(--surface); text-align: right; }
.convo-empty { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; }
.optout-bar { background: rgba(200, 67, 47, 0.1); color: var(--c-failed); padding: 8px 18px; font-size: 13px; }

/* ---------- Compose ---------- */
.group-pick { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; }
.group-opt { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; font-weight: 500; }
.group-opt.on { border-color: var(--gold); background: var(--gold-soft); }
.group-opt .n { margin-left: auto; font-size: 12px; color: var(--muted); }
.compose-text { min-height: 170px; resize: vertical; }
.textmeta { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); margin-top: 6px; }
.textmeta .warn { color: var(--c-unconfirmed); }
.seg-toggle { display: flex; gap: 0; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; width: fit-content; }
.seg-toggle button { border: 0; background: var(--surface); padding: 8px 16px; font: inherit; font-weight: 600; cursor: pointer; color: var(--muted); }
.seg-toggle button.on { background: var(--gold); color: #fff; }
.est { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.est .k { font-size: 12px; color: var(--muted); }
.est .v { font-size: 20px; font-weight: 700; }
.est .v.small { font-size: 15px; }
.phone-prev { background: var(--surface-2); border-radius: 18px; padding: 16px; min-height: 120px; }
.phone-prev .bubble { background: var(--bubble-in); display: inline-block; max-width: 100%; border-radius: 18px; border-bottom-left-radius: 6px; }

/* ---------- Dashboard ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .k { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.kpi .k::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c, transparent); }
.kpi .v { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .s { font-size: 12px; color: var(--muted); }
.sbar { display: flex; height: 10px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.sbar span { display: block; height: 100%; }
.sbar.thin { height: 6px; min-width: 90px; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12.5px; color: var(--muted); margin-top: 10px; }
.legend span::before { content: ""; display: inline-block; width: 9px; height: 9px; border-radius: 3px; background: var(--c); margin-right: 6px; }
.blast-body { white-space: pre-wrap; background: var(--surface-2); border-radius: 12px; padding: 14px 16px; }
.dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 13.5px; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; }
.donut-wrap { display: flex; gap: 22px; align-items: center; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs a { padding: 8px 14px; text-decoration: none; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.on { color: var(--ink); border-bottom-color: var(--gold); }
.trunc { max-width: 380px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Group cards */
.gcards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.gcard { padding: 18px; text-decoration: none; color: inherit; display: block; transition: border-color 0.15s, transform 0.15s; }
.gcard:hover { border-color: var(--gold); transform: translateY(-1px); }
.gcard h3 { font-size: 16px; }
.gcard .n { font-size: 28px; font-weight: 700; margin-top: 8px; }

/* Modal */
.modal-bg { position: fixed; inset: 0; background: rgba(15, 12, 7, 0.45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: var(--surface); border-radius: 16px; width: 100%; max-width: 480px; max-height: 90vh; overflow: auto; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); }
.modal.wide { max-width: 640px; }
.modal-head { padding: 18px 20px 6px; display: flex; align-items: center; }
.modal-head h2 { flex: 1; font-size: 17px; }
.modal-body { padding: 10px 20px; }
.modal-foot { padding: 12px 20px 18px; display: flex; gap: 8px; justify-content: flex-end; }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 10px; opacity: 0; pointer-events: none; transition: all 0.2s; z-index: 60; font-weight: 500; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); color: #fff; }

.notice { background: var(--gold-soft); color: var(--gold-ink); border-radius: 10px; padding: 12px 14px; font-size: 13.5px; }
.notice.red { background: rgba(200, 67, 47, 0.1); color: var(--c-failed); }
.howto { font-size: 13.5px; }
.howto li { margin: 4px 0; }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 5px; padding: 0 5px; font-size: 12.5px; }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  .shell { flex-direction: column; }
  .side { width: 100%; flex: 0 0 auto; flex-direction: row; align-items: center; padding: 6px 8px; gap: 6px; overflow-x: auto; }
  .brand { padding: 0 6px 0 2px; }
  .brand div, .side-foot .linkbtn, .side-foot .mode-pill { display: none; }
  #nav { flex-direction: row; }
  #nav a span { display: none; }
  #nav a { padding: 8px 10px; position: relative; }
  #nav a .badge { position: absolute; top: 0; right: -2px; font-size: 10px; padding: 0 5px; }
  .side-foot { margin: 0 0 0 auto; padding: 0; flex-direction: row; }
  .balance { padding: 4px 10px; }
  .balance .label { display: none; }
  .balance .value { font-size: 14px; }
  .view { flex: 1; min-height: 0; }
  .page { padding: 18px 16px 50px; }
  .grid2 { grid-template-columns: 1fr; }
  .threads { width: 100%; flex: 1 1 auto; border-right: 0; }
  .inbox.has-convo .threads { display: none; }
  .inbox:not(.has-convo) .convo { display: none; }
  .convo-body { padding: 14px 12px; }
  .msg { max-width: 88%; }
  .hide-sm { display: none; }
}
@media (min-width: 861px) { .show-sm { display: none !important; } }
