:root {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #f1f3f6; --border: #e3e6eb;
  --text: #14181f; --muted: #667085; --accent: #4f46e5; --accent-ink: #ffffff;
  --good: #16a34a; --bad: #dc2626; --warn: #d97706; --info: #0891b2; --idle: #94a3b8;
  --c-meta: #4f46e5; --c-google: #0d9488; --c-tiktok: #db2777;
  --radius: 12px; --shadow: 0 1px 2px rgba(16,24,40,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116; --surface: #161a21; --surface-2: #1d222b; --border: #2a303b;
    --text: #e7eaf0; --muted: #98a2b3; --accent: #818cf8; --accent-ink: #0e1116;
    --good: #4ade80; --bad: #f87171; --warn: #fbbf24; --info: #22d3ee; --idle: #64748b;
    --c-meta: #818cf8; --c-google: #2dd4bf; --c-tiktok: #f472b6;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); display: flex; min-height: 100vh; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 12px; }

.sidebar { width: 230px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border); padding: 20px 14px; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.logo { display: flex; align-items: center; gap: 9px; margin: 0 8px 24px; }
.logo-mark { width: 28px; height: 28px; color: var(--accent); flex-shrink: 0; }
.wordmark { font-size: 21px; font-weight: 600; letter-spacing: -.6px; }
.wordmark b { color: var(--accent); font-weight: 800; }
nav a { display: block; padding: 9px 10px; border-radius: 8px; cursor: pointer; color: var(--text); margin-bottom: 2px; user-select: none; }
nav a:hover { background: var(--surface-2); }
nav a.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.sidebar-foot { margin-top: auto; }
.sidebar-foot .btn { width: 100%; text-align: center; }

main { flex: 1; padding: 28px 32px; min-width: 0; }
.view { display: none; }
.view.active { display: block; }
.view-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
h1 { margin: 0; font-size: 24px; letter-spacing: -.4px; }
h3 { margin: 0 0 10px; font-size: 15px; }
.muted { color: var(--muted); margin: 4px 0 0; }
.small { font-size: 12px; }
.controls, .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.gap { gap: 8px; margin: 12px 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; min-width: 0; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.grid-2 > .card, .grid-3 > .card { margin-bottom: 0; }
.grid-2, .grid-3 { margin-bottom: 16px; }
.grid-2.tpl { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); }

input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { width: 100%; min-height: 420px; font: 12px/1.5 ui-monospace, Menlo, monospace; margin-top: 6px; }
label { font-size: 12px; color: var(--muted); display: inline-flex; flex-direction: column; gap: 4px; }
label.block { display: flex; margin-top: 10px; }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; margin-top: 10px; }
form { display: flex; gap: 8px; flex-wrap: wrap; }
form input:not([type=file]) { flex: 1; min-width: 160px; }

.btn { font: inherit; font-weight: 600; border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink); border-radius: 8px; padding: 7px 14px; cursor: pointer; text-decoration: none; display: inline-block; }
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.danger { border-color: var(--bad); color: var(--bad); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.icon { padding: 3px 9px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.3px; }
.kpi .s { font-size: 11px; color: var(--muted); }
.pos { color: var(--good); } .neg { color: var(--bad); }

.chart-box { position: relative; height: 260px; }

.mini { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.mini:last-child { border-bottom: 0; }
.mini img { width: 40px; height: 40px; object-fit: contain; background: var(--surface-2); border-radius: 8px; }
.mini .t { flex: 1; min-width: 0; }
.mini .t b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.mini .t span { font-size: 11px; color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini .n { font-weight: 700; font-variant-numeric: tabular-nums; font-size: 13px; }

.chip { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; white-space: nowrap; border: 1px solid currentColor; }
.chip.winner { color: var(--good); } .chip.money_burner { color: var(--bad); } .chip.rising { color: var(--info); }
.chip.zombie { color: var(--idle); } .chip.low_ctr { color: var(--warn); } .chip.average { color: var(--muted); }
.chip.no_data { color: var(--idle); }
.label-bar { display: flex; height: 30px; border-radius: 8px; overflow: hidden; gap: 2px; }
.label-bar div { display: flex; align-items: center; justify-content: center; color: #fff; font-size: 12px; font-weight: 600; min-width: 28px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .3px; color: var(--muted); padding: 8px; border-bottom: 1px solid var(--border); cursor: pointer; white-space: nowrap; position: sticky; top: 0; background: var(--surface); }
table.data td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:hover td { background: var(--surface-2); }
table.data img { width: 38px; height: 38px; object-fit: contain; border-radius: 6px; background: var(--surface-2); }
table.data .title { max-width: 260px; }
table.data .title b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
table.data .title span { color: var(--muted); font-size: 11px; }
input.cost { width: 80px; padding: 3px 6px; text-align: right; }
.reason { font-size: 11px; color: var(--muted); max-width: 220px; }
.thresholds summary { cursor: pointer; font-weight: 600; }

.feed-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; margin-bottom: 16px; }
.feed-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; cursor: pointer; }
.feed-card.sel { outline: 2px solid var(--accent); }
.feed-card .ch { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; }
.feed-card .ch.meta { color: var(--c-meta); } .feed-card .ch.google { color: var(--c-google); } .feed-card .ch.tiktok { color: var(--c-tiktok); }
.feed-card h4 { margin: 4px 0 8px; font-size: 15px; }
.url { display: flex; gap: 6px; }
.url input { flex: 1; font-size: 11px; font-family: ui-monospace, Menlo, monospace; padding: 4px 6px; min-width: 0; }

.rule { border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin-bottom: 10px; background: var(--surface-2); }
.rule-head { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.rule-head input { flex: 1; font-weight: 600; }
.rule .line { display: flex; gap: 6px; align-items: center; margin: 4px 0; flex-wrap: wrap; }
.rule .line .tag { font-size: 11px; font-weight: 700; color: var(--muted); width: 50px; }
.rule .line input { flex: 1; min-width: 120px; }
.rule .line select, .rule .line input { padding: 4px 8px; font-size: 12px; }

.stat { background: var(--surface-2); border-radius: 8px; padding: 6px 12px; font-size: 12px; }
.issues { font-size: 12px; color: var(--warn); margin: 6px 0; }
.result { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; font-size: 12px; white-space: pre-wrap; word-break: break-word; max-height: 360px; overflow: auto; }
.result:empty { display: none; }
#pushResult pre { margin-top: 8px; }

.tpl-preview { width: 100%; aspect-ratio: 1; object-fit: contain; border-radius: 10px; background: var(--surface-2); margin-top: 10px; display: block; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.thumbs img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; background: var(--surface-2); }
.error { color: var(--bad); font-size: 12px; }

.conn h3 { display: flex; align-items: center; gap: 8px; }
.conn form { flex-direction: column; }
.conn form input { min-width: 0; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--idle); display: inline-block; }
.dot.on { background: var(--good); }

#toast { position: fixed; bottom: 20px; right: 20px; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 10; }
#toast.show { opacity: 1; }

@media (max-width: 1250px) {
  .grid-3 { grid-template-columns: 1fr; }
}
@media (max-width: 1000px) {
  .grid-2, .grid-2.tpl { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  body { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; padding: 12px 16px; }
  nav { display: flex; overflow-x: auto; gap: 4px; }
  nav a { white-space: nowrap; }
  .logo { margin-bottom: 10px; }
  .sidebar-foot { display: none; }
  main { padding: 16px; }
}

/* ---------- auth + account ---------- */
body.auth-page { align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; padding: 28px; }
.auth-card .logo { margin: 0 0 18px; }
.auth-card h1 { font-size: 20px; }
.stack { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }
.stack label { display: flex; }
.stack input { width: 100%; }
.user-box { border-top: 1px solid var(--border); padding-top: 12px; margin-bottom: 10px; font-size: 13px; }
.user-box b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-box .row { margin-top: 8px; }
.status { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; border: 1px solid currentColor; white-space: nowrap; }
.status.ok { color: var(--good); } .status.error { color: var(--bad); } .status.warning { color: var(--warn); }
.status.running { color: var(--info); } .status.never { color: var(--idle); }
.msg { font-size: 11px; color: var(--muted); max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job-fields { display: contents; }
.notice { background: var(--surface-2); border-left: 3px solid var(--warn); padding: 10px 12px; border-radius: 6px; font-size: 12px; margin-bottom: 16px; }
.toggle { cursor: pointer; }
[data-admin][hidden], [data-editor][hidden] { display: none !important; }
.stack [hidden] { display: none; }

/* ---------- stores ---------- */
.store-pick { margin: -8px 8px 18px; display: flex; }
.store-pick select { width: 100%; font-weight: 600; }
.store-checks { border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin: 0; }
.store-checks legend { font-size: 12px; color: var(--muted); padding: 0 4px; }
.store-checks label { flex-direction: row; align-items: center; gap: 4px; color: var(--text); font-size: 13px; }
.badge { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); color: var(--muted); margin-left: 6px; }
@media (max-width: 760px) { .store-pick { margin: 0 0 10px; } }
