:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --border: #e5e9f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-soft: #dbeafe;
  --primary-text: #1d4ed8;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --err: #dc2626;
  --err-soft: #fee2e2;
  --sidebar: #0f172a;
  --sidebar-text: #cbd5e1;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .04);
  --radius: 12px;
  --font: "Segoe UI", "Inter", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --panel: #111827;
    --panel-2: #0f172a;
    --border: #1f2937;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --primary: #3b82f6;
    --primary-soft: #1e3a8a55;
    --primary-text: #93c5fd;
    --ok-soft: #14532d55;
    --warn-soft: #78350f55;
    --err-soft: #7f1d1d55;
    --sidebar: #020617;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; }
[x-cloak] { display: none !important; }
a { color: var(--primary-text); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 236px; flex: none; background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { padding: 22px 20px 18px; display: flex; gap: 10px; align-items: center; color: #fff; font-weight: 700; font-size: 17px; }
.brand-logo { width: 32px; height: 32px; border-radius: 9px; background: linear-gradient(135deg, #3b82f6, #22d3ee); display: grid; place-items: center; }
.nav { display: flex; flex-direction: column; gap: 2px; padding: 4px 10px; }
.nav button { all: unset; cursor: pointer; display: flex; gap: 11px; align-items: center; padding: 10px 12px; border-radius: 9px; color: var(--sidebar-text); font-size: 14px; }
.nav button:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav button.active { background: rgba(59,130,246,.18); color: #fff; }
.nav svg { width: 18px; height: 18px; flex: none; }
.nav .count { margin-left: auto; font-size: 11px; background: rgba(255,255,255,.1); padding: 1px 7px; border-radius: 99px; }
.sidebar-foot { margin-top: auto; padding: 16px 20px; font-size: 12px; color: #94a3b8; border-top: 1px solid rgba(255,255,255,.06); }
.sidebar-foot .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }

.main { flex: 1; min-width: 0; padding: 24px 28px 48px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .brand { padding: 14px 16px 6px; }
  .nav { flex-direction: row; overflow-x: auto; padding: 6px 10px 10px; }
  .nav button { white-space: nowrap; }
  .sidebar-foot { display: none; }
  .main { padding: 16px; }
}

/* ---------- Building blocks ---------- */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.grid > .card + .card { margin-top: 0; }
.grid + .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 p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: Consolas, "Cascadia Mono", monospace; font-size: 12.5px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.hint { background: var(--panel-2); border: 1px dashed var(--border); border-radius: 10px; padding: 10px 12px; color: var(--muted); font-size: 13px; }
.hint ol { margin: 6px 0 0; padding-left: 18px; }

.btn { all: unset; box-sizing: border-box; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 7px; height: 36px; padding: 0 14px; border-radius: 9px; font-weight: 600; font-size: 13.5px; background: var(--panel); border: 1px solid var(--border); color: var(--text); white-space: nowrap; }
.btn:hover { border-color: #c7d0dd; background: var(--panel-2); }
.btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--err); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { height: 30px; padding: 0 10px; font-size: 12.5px; border-radius: 8px; }
.btn[disabled], .btn.loading { opacity: .55; pointer-events: none; }
.btn svg { width: 16px; height: 16px; }

.field { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; font-weight: 600; color: var(--muted); }
input[type=text], input[type=password], input[type=date], input[type=time], input[type=search], select, textarea {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 9px; height: 36px; padding: 0 11px; min-width: 0; }
textarea { height: auto; padding: 9px 11px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.check { display: inline-flex; gap: 8px; align-items: center; cursor: pointer; font-weight: 500; color: var(--text); font-size: 13.5px; }
.check input { width: 16px; height: 16px; accent-color: var(--primary); }

.switch { display: inline-block; position: relative; width: 38px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: #cbd5e1; border-radius: 99px; transition: .15s; cursor: pointer; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .15s; }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::after { transform: translateX(16px); }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.err { background: var(--err-soft); color: var(--err); border-color: transparent; }
.badge.info { background: var(--primary-soft); color: var(--primary-text); border-color: transparent; }
.chip { all: unset; cursor: pointer; padding: 5px 12px; border-radius: 99px; border: 1px solid var(--border); background: var(--panel); font-size: 12.5px; font-weight: 600; color: var(--muted); }
.chip.active { background: var(--primary-soft); color: var(--primary-text); border-color: transparent; }
.chip b { color: var(--text); margin-left: 4px; }

.banner { display: flex; gap: 10px; align-items: center; padding: 11px 14px; border-radius: 10px; margin-bottom: 16px; font-size: 13.5px; }
.banner.err { background: var(--err-soft); color: var(--err); }
.banner.warn { background: var(--warn-soft); color: var(--warn); }

/* ---------- KPI ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12.5px; font-weight: 600; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.kpi.accent { background: linear-gradient(135deg, #1d4ed8, #2563eb); border-color: transparent; color: #fff; }
.kpi.accent .label { color: #dbeafe; }

/* ---------- Tables ---------- */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: 10px; max-height: 640px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { position: sticky; top: 0; background: var(--panel-2); font-size: 12px; color: var(--muted); font-weight: 700; z-index: 1; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--panel-2); }
tr.total td { font-weight: 700; background: var(--primary-soft); position: sticky; bottom: 0; }
td.name { max-width: 340px; overflow: hidden; text-overflow: ellipsis; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.empty svg { width: 36px; height: 36px; opacity: .5; margin-bottom: 6px; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: #f59e0b; }
.status-dot.ACTIVE { background: var(--ok); }
.status-dot.PAUSED, .status-dot.CAMPAIGN_PAUSED, .status-dot.ADSET_PAUSED { background: #94a3b8; }

/* ---------- Filter bar ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filters .field { min-width: 150px; }

/* ---------- Account picker ---------- */
.picker { position: relative; }
.picker-btn { min-width: 230px; justify-content: space-between; font-weight: 500; }
.picker-panel { position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; width: min(440px, 92vw); background: var(--panel); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 12px 32px rgba(15,23,42,.18); padding: 10px; }
.picker-list { max-height: 320px; overflow: auto; margin-top: 8px; display: flex; flex-direction: column; }
.picker-list label { display: flex; gap: 9px; align-items: center; padding: 7px 8px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.picker-list label:hover { background: var(--panel-2); }
.picker-list input { accent-color: var(--primary); }

/* ---------- Chart ---------- */
.chart-box { position: relative; height: 380px; }

/* ---------- Modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15, 23, 42, .5); z-index: 50; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--panel); border-radius: 14px; width: min(680px, 100%); max-height: 92vh; overflow: auto; box-shadow: 0 24px 64px rgba(0,0,0,.3); }
.modal.wide { width: min(1000px, 100%); }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--panel); z-index: 2; }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; position: sticky; bottom: 0; background: var(--panel); }
.preview-img { width: 100%; border: 1px solid var(--border); border-radius: 10px; }

/* ---------- Rules ---------- */
.rule-card { display: grid; grid-template-columns: 1fr auto; gap: 12px; }
.rule-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.times { display: flex; gap: 6px; flex-wrap: wrap; }
.time-chip { display: inline-flex; gap: 6px; align-items: center; padding: 4px 6px 4px 10px; border-radius: 8px; background: var(--primary-soft); color: var(--primary-text); font-weight: 700; font-variant-numeric: tabular-nums; }
.time-chip button { all: unset; cursor: pointer; padding: 0 4px; opacity: .7; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg button { all: unset; cursor: pointer; padding: 7px 12px; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--primary); color: #fff; }

/* ---------- Toast ---------- */
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 80; max-width: min(420px, 92vw); }
.toast { padding: 11px 14px; border-radius: 10px; background: #0f172a; color: #fff; box-shadow: 0 10px 28px rgba(0,0,0,.25); font-size: 13.5px; display: flex; gap: 8px; align-items: flex-start; white-space: pre-line; }
.toast.err { background: #b91c1c; }
.toast.ok { background: #15803d; }

/* ---------- Auth pages ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background: radial-gradient(circle at 20% 10%, #1e3a8a33, transparent 40%), var(--bg); }
.auth-card { width: min(420px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 20px 50px rgba(15,23,42,.12); padding: 28px; display: flex; flex-direction: column; gap: 16px; }
.auth-brand { color: var(--text); padding: 0; }
.auth-card .btn.primary { height: 42px; }
.center { text-align: center; }
.page-narrow { max-width: 1000px; margin: 0 auto; padding: 24px 16px 48px; }
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.user-box { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: #334155; color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px; flex: none; }
.sidebar-foot a { color: #cbd5e1; text-decoration: none; }
.sidebar-foot a:hover { color: #fff; }

.spin { width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { height: 14px; border-radius: 6px; background: linear-gradient(90deg, var(--panel-2), var(--border), var(--panel-2)); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .spin, .skeleton { animation: none; } }
