:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --pos-text: #006300;
  --neg-text: #d03b3b;
  --chart-pos: #2a78d6;
  --chart-neg: #e34948;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --pos-text: #0ca30c;
    --neg-text: #f07a7a;
    --chart-pos: #3987e5;
    --chart-neg: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-wash: rgba(57, 135, 229, 0.16);
  --pos-text: #0ca30c;
  --neg-text: #f07a7a;
  --chart-pos: #3987e5;
  --chart-neg: #e66767;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 15px/1.45 var(--font);
}

h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 16px; margin-bottom: 12px; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.muted { color: var(--text-2); }
.pos { color: var(--pos-text); }
.neg { color: var(--neg-text); }

/* Top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.brand { font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.topbar-actions form { margin: 0; }
.buyer-picker { display: flex; align-items: center; gap: 8px; }

select, input[type="date"], input[type="text"], input[type="password"] {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  min-height: 36px;
}

.link {
  font: inherit;
  color: var(--accent);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.link:hover { text-decoration: underline; }

.button {
  display: inline-block;
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 8px;
  padding: 10px 16px;
  cursor: pointer;
}
.button:hover { text-decoration: none; filter: brightness(1.05); }

/* Layout */
.container { max-width: 1200px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
}

/* Filters */
.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.presets, .custom-range { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.custom-range { margin: 0; }

.pill {
  font: inherit;
  font-size: 14px;
  color: var(--text-2);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}
.pill:hover { color: var(--text); background: var(--accent-wash); text-decoration: none; }
.pill.is-active { color: var(--accent); background: var(--accent-wash); border-color: var(--accent); font-weight: 600; }

.caption { margin: 0 0 16px; font-size: 14px; }

/* Stat tiles */
.tiles {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.tile-label { font-size: 13px; color: var(--text-2); }
.tile-value { font-size: 22px; font-weight: 600; }
.tile-hero { grid-row: span 2; justify-content: center; }
.tile-hero .tile-value { font-size: 40px; line-height: 1.15; }

@media (max-width: 720px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tile-hero { grid-column: 1 / -1; grid-row: auto; }
  .tile-hero .tile-value { font-size: 32px; }
  .tile-value { font-size: 20px; }
  .filters { flex-direction: column; align-items: stretch; }
  .custom-range { display: grid; grid-template-columns: 1fr 1fr; }
  .custom-range > span { display: none; }
  .custom-range input[type="date"] { width: 100%; min-width: 0; }
  .custom-range .pill { grid-column: 1 / -1; justify-content: center; }
}

/* Chart */
.chart-box { position: relative; height: 300px; }

.day-table { margin-top: 12px; }
.day-table summary { cursor: pointer; color: var(--accent); font-size: 14px; }
.day-table .table-wrap { margin-top: 8px; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { font-weight: 600; color: var(--text-2); font-size: 13px; }
th a { color: inherit; }
th a:hover { color: var(--text); text-decoration: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.name { white-space: normal; min-width: 200px; }
tfoot td { font-weight: 600; border-bottom: 0; border-top: 1px solid var(--axis); }
tbody tr:hover { background: var(--accent-wash); }

.count { font-weight: 400; font-size: 14px; margin-left: 4px; }

.badge {
  display: inline-block;
  font-size: 12px;
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0 8px;
  margin-left: 6px;
}

.alert {
  background: rgba(208, 59, 59, 0.08);
  border: 1px solid rgba(208, 59, 59, 0.35);
  border-radius: 12px;
  padding: 12px 16px;
  margin-bottom: 16px;
}

.empty { margin: 0; }
.empty-state { text-align: center; padding: 40px 16px; }
.empty-state p { margin: 8px 0 20px; }
.footnote { font-size: 13px; margin: 8px 0 24px; }

/* ===== CRM layout with sidebar ===== */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 210px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.side-brand { font-weight: 700; font-size: 20px; padding: 18px 20px; letter-spacing: .5px; }
.side-nav { display: flex; flex-direction: column; padding: 8px; gap: 2px; flex: 1; }
.side-link {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px;
  color: var(--text-2); font-size: 15px;
}
.side-link:hover { background: var(--accent-wash); color: var(--text); text-decoration: none; }
.side-link.is-active { background: var(--accent-wash); color: var(--accent); font-weight: 600; }
.side-ico { font-size: 15px; }
.side-foot { padding: 14px 16px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.side-user { font-size: 13px; color: var(--muted); }
.side-logout { font: inherit; color: var(--accent); background: none; border: 0; padding: 0; text-align: left; cursor: pointer; }
.side-logout:hover { text-decoration: underline; }

.main { flex: 1; min-width: 0; padding: 20px 24px; max-width: 1280px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; }

.messages { margin-bottom: 14px; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 10px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); }
.toast.success { background: rgba(12,163,12,0.10); border-color: rgba(12,163,12,0.35); }
.toast.error { background: rgba(208,59,59,0.08); border-color: rgba(208,59,59,0.35); }

/* new-campaign form */
.geo-form { display: flex; align-items: flex-end; gap: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
.form-actions { margin: 4px 0 28px; }
.w-input { width: 72px; text-align: right; }
.hint { font-size: 13px; margin: 10px 0 0; }
.badge.rec { color: var(--pos-text); border-color: rgba(12,163,12,0.4); }
table.offers td.name { min-width: 280px; }
table.offers input[type="checkbox"] { width: 18px; height: 18px; }

@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; height: auto; position: static; flex-direction: row; align-items: center; flex-wrap: wrap; }
  .side-nav { flex-direction: row; flex: 1; }
  .side-foot { border-top: 0; flex-direction: row; align-items: center; }
  .main { padding: 16px; }
}

/* Loading overlay (shown on navigation / form submit, like Binom/Keitaro) */
.load-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, 0.38); backdrop-filter: blur(1px);
  display: none; place-items: center;
}
.load-overlay.on { display: grid; }
.load-card { display: flex; flex-direction: column; align-items: center; gap: 12px; color: #fff; font-weight: 600; }

/* Scoped overlay: covers only the data region below the filters. */
.loadable { position: relative; }
.load-overlay-local {
  position: absolute; inset: 0; z-index: 5;
  display: none; place-items: start center;
  background: color-mix(in srgb, var(--page) 72%, transparent);
  backdrop-filter: blur(1px);
}
.loadable.loading .load-overlay-local { display: grid; }
.load-overlay-local .load-card { color: var(--text-2); margin-top: 48px; }
.load-overlay-local .spinner { border-color: var(--border); border-top-color: var(--accent); }
.spinner {
  width: 44px; height: 44px; border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.3); border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; padding: 28px; }
.login-card p { margin: 0; }
.login-card .alert { margin: 0; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--text-2); }
.field input { width: 100%; }
