:root {
  --bg: #f6f5f3; --surface: #ffffff; --surface-2: #faf8f6; --border: #e7e2dd;
  --text: #1c1917; --text-2: #57534e; --muted: #8a837c;
  --accent: #d9583a; --accent-soft: #fbe9e3; --accent-ink: #9a3412;
  --good: #15803d; --good-soft: #dcfce7; --warn: #b45309; --warn-soft: #fef3c7; --bad: #b91c1c; --bad-soft: #fee2e2;
  --chart: #d9583a; --grid: #ece7e2;
  --wa-bg: #efeae2; --wa-in: #ffffff; --wa-out: #d9fdd3;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141210; --surface: #1d1a17; --surface-2: #24201c; --border: #342f2a;
    --text: #f5f2ee; --text-2: #c8c1b9; --muted: #948b82;
    --accent: #f07a5c; --accent-soft: #3a221b; --accent-ink: #fdba9f;
    --good: #4ade80; --good-soft: #14321f; --warn: #fbbf24; --warn-soft: #3a2c0c; --bad: #f87171; --bad-soft: #3b1515;
    --chart: #f07a5c; --grid: #2c2824;
    --wa-bg: #0b141a; --wa-in: #202c33; --wa-out: #005c4b;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.5 Inter, system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
.muted { color: var(--muted); } .small { font-size: 12px; }
.error { color: var(--bad); min-height: 1em; }
a { color: var(--accent-ink); }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; display: grid; gap: 12px; padding: 32px; }
.brandmark { width: 48px; height: 48px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 800; display: grid; place-items: center; margin: 0 auto; }
.brandmark.sm { width: 34px; height: 34px; font-size: 13px; border-radius: 9px; margin: 0; }

#app { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px; display: flex; flex-direction: column; gap: 16px; position: sticky; top: 0; height: 100vh; }
.side-head { display: flex; gap: 10px; align-items: center; padding: 0 6px; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow: auto; }
nav a { padding: 8px 10px; border-radius: 8px; color: var(--text-2); text-decoration: none; cursor: pointer; display: flex; justify-content: space-between; align-items: center; }
nav a:hover { background: var(--surface-2); }
nav a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.side-foot { display: flex; justify-content: space-between; align-items: center; padding: 0 6px; }
main { padding: 28px 32px 48px; min-width: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); margin-bottom: 16px; }
.kpi .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 28px; font-weight: 700; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--text-2); }
.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); margin-bottom: 16px; }
.three { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin-bottom: 16px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.clickable { cursor: pointer; } tr.clickable:hover td { background: var(--surface-2); }
tr.inactive td { opacity: .5; }

.btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px; padding: 8px 14px; font: inherit; font-weight: 500; cursor: pointer; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(.95); }
.btn.good { background: var(--good); border-color: var(--good); color: #fff; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.link { border: 0; background: none; color: var(--accent-ink); padding: 4px; }
.btn:disabled { opacity: .5; cursor: default; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.pill { background: var(--accent); color: #fff; border-radius: 99px; font-size: 11px; padding: 0 7px; font-weight: 700; }

form.stack, .stack { display: grid; gap: 12px; }
label { display: grid; gap: 4px; font-size: 13px; font-weight: 500; color: var(--text-2); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; }
textarea { resize: vertical; min-height: 80px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.search { max-width: 280px; }

.callout { background: var(--warn-soft); color: var(--text); border-radius: 10px; padding: 12px 14px; margin-bottom: 16px; font-size: 13px; }

/* charts */
.spark { width: 100%; height: 110px; display: block; }
.spark rect.bar { fill: var(--chart); }
.spark rect.hit { fill: transparent; }
.spark rect.hit:hover + rect.bar, .spark g:hover rect.bar { fill: var(--accent-ink); }
.spark line { stroke: var(--grid); }
.spark text { fill: var(--muted); font-size: 10px; }
.hbars { display: grid; gap: 8px; }
.hbar { display: grid; grid-template-columns: 110px 1fr 36px; gap: 8px; align-items: center; font-size: 13px; }
.hbar .track { height: 10px; background: var(--grid); border-radius: 4px; overflow: hidden; }
.hbar .fill { height: 100%; background: var(--chart); border-radius: 0 4px 4px 0; }
.hbar .v { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-2); }
.tip { position: fixed; pointer-events: none; background: var(--text); color: var(--bg); font-size: 12px; padding: 4px 8px; border-radius: 6px; z-index: 50; }

/* templates */
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.tpl { padding: 0; overflow: hidden; display: flex; flex-direction: column; }
.tpl img { width: 100%; aspect-ratio: 4/5; object-fit: cover; background: var(--surface-2); display: block; border-bottom: 1px solid var(--border); }
.tpl .body { padding: 12px 14px; display: grid; gap: 6px; flex: 1; }
.tpl .title { font-weight: 600; }
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; }
.editor .preview img { width: 100%; border-radius: 10px; border: 1px solid var(--border); }

/* modal & toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 16px; z-index: 40; }
.modal-card { background: var(--surface); border-radius: 14px; padding: 22px; width: 100%; max-width: 920px; max-height: calc(100vh - 32px); overflow: auto; }
.modal-card.narrow { max-width: 520px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; max-width: calc(100vw - 32px); }

/* simulator */
.sim { display: grid; grid-template-columns: 260px minmax(0, 420px); gap: 20px; align-items: start; }
.phone { background: var(--wa-bg); border-radius: 18px; border: 1px solid var(--border); height: 640px; display: flex; flex-direction: column; overflow: hidden; }
.phone-head { background: #075e54; color: #fff; padding: 12px 14px; font-weight: 600; }
.chat { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 85%; padding: 7px 10px; border-radius: 10px; white-space: pre-wrap; word-wrap: break-word; font-size: 13.5px; box-shadow: 0 1px 1px rgba(0,0,0,.08); }
.msg.in { background: var(--wa-in); align-self: flex-start; }
.msg.out { background: var(--wa-out); align-self: flex-end; }
.msg img, .msg video { max-width: 100%; border-radius: 6px; display: block; margin-bottom: 4px; }
.msg .footer { font-size: 11px; color: var(--muted); margin-top: 4px; }
.msg .wa-btns { display: grid; gap: 4px; margin-top: 8px; }
.msg .wa-btn { background: var(--surface); border: 1px solid var(--border); color: #027eb5; border-radius: 8px; padding: 6px; text-align: center; cursor: pointer; font-weight: 500; }
.msg .tmpl { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 4px; }
.composer { display: flex; gap: 6px; padding: 8px; background: var(--surface); border-top: 1px solid var(--border); }
.composer input[type=text] { flex: 1; }
.composer label.attach { cursor: pointer; display: grid; place-items: center; padding: 0 10px; font-size: 18px; }
.who { display: grid; gap: 6px; }
.who button { text-align: left; }
.who button.active { border-color: var(--accent); background: var(--accent-soft); }
.list-rows { display: grid; gap: 4px; }
.list-rows button { text-align: left; }

@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  nav { flex-direction: row; flex-wrap: wrap; }
  main { padding: 20px 16px 40px; }
  .editor, .sim { grid-template-columns: 1fr; }
}
a.btn { text-decoration: none; }
