:root {
  --bg: #f6f7fb; --surface: #fff; --surface-2: #f1f3f9; --border: #e4e7f0;
  --text: #151a2d; --muted: #6b7390; --accent: #4f46e5; --accent-soft: #eceafd;
  --good: #0f9d63; --bad: #d93a4a;
  --meta: #3b82f6; --tiktok: #ec4899; --google: #f59e0b;
  --shadow: 0 1px 2px rgba(20,25,50,.05), 0 4px 16px rgba(20,25,50,.04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1120; --surface: #171b30; --surface-2: #1e2340; --border: #2a3053;
    --text: #e8ebf7; --muted: #8e97bb; --accent: #8b85ff; --accent-soft: #25295a;
    --good: #34d399; --bad: #fb7185; --shadow: none;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 650; }
.muted { color: var(--muted); }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 32px; box-shadow: var(--shadow); }
.login h1 { font-size: 22px; margin-bottom: 4px; }
.login form { display: grid; gap: 14px; margin-top: 24px; }
label.field { display: grid; gap: 6px; font-size: 12px; color: var(--muted); font-weight: 600; }
input, select { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; min-width: 0; }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 10px 16px; font-weight: 600; cursor: pointer; }
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--bad); }
.btn.small { padding: 6px 10px; font-size: 12px; }
.error { color: var(--bad); font-size: 13px; min-height: 18px; }

/* Layout */
.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px; padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 10px; }
.dot { width: 12px; height: 12px; border-radius: 4px; background: var(--accent); display: inline-block; }
.tabs { display: flex; gap: 4px; }
.tab { background: none; border: 0; padding: 7px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--accent-soft); color: var(--accent); }
.spacer { flex: 1; }
.who { color: var(--muted); font-size: 13px; }
main { max-width: 1280px; margin: 0 auto; padding: 24px; display: grid; gap: 20px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; }

.filters { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { background: none; border: 0; padding: 6px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 600; }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.filters input[type=date] { padding: 6px 8px; }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 26px; font-weight: 700; margin: 6px 0 4px; font-variant-numeric: tabular-nums; }
.delta { font-size: 12px; font-weight: 600; }
.delta.good { color: var(--good); } .delta.bad { color: var(--bad); } .delta.flat { color: var(--muted); }

/* Cards / charts */
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.card h2 { font-size: 15px; margin-bottom: 14px; display: flex; align-items: center; gap: 10px; }
.card h2 .spacer { flex: 1; }
.chart { position: relative; height: 280px; }

/* Creatives */
.creatives { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.creative { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--surface); }
.thumb { height: 130px; position: relative; display: flex; align-items: flex-end; padding: 10px; color: #fff; }
.thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.thumb .rank { position: absolute; top: 8px; left: 8px; background: rgba(0,0,0,.45); border-radius: 6px; padding: 1px 8px; font-weight: 700; font-size: 12px; }
.thumb .fmt { position: relative; font-size: 11px; font-weight: 600; background: rgba(0,0,0,.4); padding: 2px 8px; border-radius: 99px; }
.c-body { padding: 12px; }
.c-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-sub { font-size: 12px; color: var(--muted); margin-bottom: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.c-stats div span { display: block; font-size: 11px; color: var(--muted); }
.c-stats div b { font-variant-numeric: tabular-nums; }
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--c, var(--muted)); }
.pill.meta { --c: var(--meta); } .pill.tiktok { --c: var(--tiktok); } .pill.google { --c: var(--google); }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: right; padding: 9px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child, th.l, td.l { text-align: left; }
th { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
tr.click { cursor: pointer; } tr.click:hover td { background: var(--surface-2); }
.empty { padding: 40px; text-align: center; color: var(--muted); }

/* Admin */
.admin-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; align-items: end; }
.swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 8px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600; z-index: 50; }

@media (max-width: 640px) {
  main { padding: 14px; } .topbar { padding: 10px 14px; }
  .grid2 { grid-template-columns: 1fr; }
}
[hidden] { display: none !important; }
.pill.instagram { --c: #a855f7; }
