:root {
  color-scheme: light;
  --surface: #fcfcfb;
  --surface-2: #ffffff;
  --border: #e4e3df;
  --grid: #ecebe7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6f6e69;
  --series-1: #2a78d6;
  /* one colour per account, one for deals, one for the alert: never shared */
  --acc-1: #2a78d6; --acc-2: #1baf7a;
  --deal: #4a3aa7; --no-deal: #d9d8d2; --alert: #eda100;
  --good: #0f7b3e;
  --good-bg: #e6f4ec;
  --bad: #c4312f;
  --bad-bg: #fbeaea;
  --warn: #8a5a00;
  --warn-bg: #fdf3dc;
  --accent-bg: #e8f1fc;
  --bar: #c9c8c2;
  --shade-1: #e3eefc; --shade-2: #cde2fb; --shade-3: #b7d3f6; --shade-4: #9ec5f4; --shade-5: #86b6ef;
  --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236f6e69' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface: #1a1a19; --surface-2: #232322; --border: #3a3a37; --grid: #2e2e2c;
    --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #9b9a92;
    --series-1: #3987e5; --acc-1: #3987e5; --acc-2: #199e70;
    --deal: #9085e9; --no-deal: #4a4a46; --alert: #c98500;
    --good: #4cc38a; --good-bg: #173326; --bad: #f07272; --bad-bg: #3a1d1d;
    --warn: #e3b341; --warn-bg: #3a2f12; --accent-bg: #1c2f48; --bar: #55554f;
    --shade-1: #16283d; --shade-2: #10325a; --shade-3: #104281; --shade-4: #184f95; --shade-5: #1c5cab;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface: #1a1a19; --surface-2: #232322; --border: #3a3a37; --grid: #2e2e2c;
  --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #9b9a92;
  --series-1: #3987e5; --acc-1: #3987e5; --acc-2: #199e70;
  --deal: #9085e9; --no-deal: #4a4a46; --alert: #c98500;
  --good: #4cc38a; --good-bg: #173326; --bad: #f07272; --bad-bg: #3a1d1d;
  --warn: #e3b341; --warn-bg: #3a2f12; --accent-bg: #1c2f48; --bar: #55554f;
  --shade-1: #16283d; --shade-2: #10325a; --shade-3: #104281; --shade-4: #184f95; --shade-5: #1c5cab;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--surface); color: var(--text-primary);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.top, .tabs, main { max-width: 1120px; margin: 0 auto; padding: 0 16px; }
.top { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: end; justify-content: space-between; padding-top: 24px; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0; color: var(--text-secondary); font-weight: 600; }
.muted { color: var(--text-muted); margin: 2px 0 0; font-size: 13px; }
.icon { display: inline-flex; width: 1em; height: 1em; flex: none; }
.icon svg { width: 100%; height: 100%; }

/* Filters: toggle and selects share one look (height, border, radius) */
.filters { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface-2); color: var(--text-secondary); padding: 0 12px; height: 34px; font: inherit; font-size: 14px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent-bg); color: var(--text-primary); font-weight: 600; }
.field { display: inline-flex; gap: 6px; align-items: center; color: var(--text-secondary); font-size: 13px; }
select, .chip, .btn { font: inherit; font-size: 14px; color: var(--text-primary); background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; height: 34px; padding: 0 10px; }
select { appearance: none; -webkit-appearance: none; padding-right: 30px; cursor: pointer;
  background: var(--surface-2) var(--chevron) no-repeat right 8px center / 16px; }
select:hover, .seg button:hover { border-color: var(--series-1); }
select:focus-visible, .seg button:focus-visible, .btn:focus-visible, .chip:focus-visible { outline: 2px solid var(--series-1); outline-offset: 1px; }
#campaign { max-width: 240px; text-overflow: ellipsis; }
.chip, .btn { cursor: pointer; }
.chip { background: var(--accent-bg); }
.btn:hover, .chip:hover { border-color: var(--series-1); }
.switch { display: inline-flex; gap: 8px; align-items: center; font-size: 14px; color: var(--text-secondary); cursor: pointer; }
.switch input { accent-color: var(--series-1); width: 16px; height: 16px; margin: 0; }
.error { background: var(--surface-2); border: 1px solid var(--bad); border-radius: 8px; padding: 12px; margin-top: 16px; }

.tabs { display: flex; gap: 4px; margin-top: 20px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tabs button { border: 0; background: none; font: inherit; color: var(--text-secondary); padding: 10px 14px; cursor: pointer; border-bottom: 3px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--text-primary); font-weight: 600; border-bottom-color: var(--series-1); }
.tabs button:focus-visible { outline: 2px solid var(--series-1); outline-offset: -2px; }
[role="tabpanel"] { padding-top: 24px; animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
main { padding-bottom: 48px; }
.block { margin-top: 28px; }
.block h2 { margin-bottom: 12px; }

/* The one card style */
.kpi, .card, .funnel li { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { padding: 14px 16px; }
.kpi-label { color: var(--text-secondary); font-size: 13px; display: flex; align-items: center; }
.kpi-value { font-size: 30px; font-weight: 700; font-variant-numeric: tabular-nums; margin: 2px 0; letter-spacing: -0.01em; }
.kpi-delta { font-size: 12px; color: var(--text-muted); }
.kpi-delta.good { color: var(--good); }
.kpi-delta.bad { color: var(--bad); }

/* The one alert style */
.warning { margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: var(--warn-bg); border: 1px solid color-mix(in srgb, var(--alert) 45%, transparent); color: var(--text-primary); display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; }
.warning-icon { color: var(--warn); font-size: 18px; }
.warning > span:nth-child(2) { flex: 1 1 260px; }

/* Funnel: one row, conversion rate under each step */
.funnel { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 16px; }
.funnel li { position: relative; padding: 12px 14px; }
.funnel li + li::before { content: ""; position: absolute; left: -13px; top: 50%; width: 10px; height: 10px; transform: translateY(-50%);
  background: var(--chevron) no-repeat center / 14px; rotate: -90deg; }
.fn-label { font-size: 13px; color: var(--text-secondary); display: flex; align-items: center; }
.fn-value { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.fn-rate { font-size: 12px; color: var(--text-muted); min-height: 1.4em; }

.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 28px; }
@media (max-width: 720px) { .charts { grid-template-columns: 1fr; } }
.card { margin: 0; padding: 12px 14px; min-width: 0; }
.card figcaption { color: var(--text-secondary); font-size: 13px; margin-bottom: 8px; }
.plot { position: relative; height: 220px; }
.card.wide { grid-column: 1 / -1; }

.panel-head { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.sheet-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.empty { color: var(--text-muted); text-align: center; padding: 32px 0; }
#sheet-empty { padding: 0 0 12px; text-align: left; }
.table-head { margin-top: 28px; }

.link { all: unset; cursor: pointer; color: var(--series-1); font-size: 13px; font-weight: 600; display: inline-flex; gap: 4px; align-items: center; }
.link:hover, .link:focus-visible { text-decoration: underline; }

/* Tables */
.sheet-wrap { overflow: auto; max-height: 70vh; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); }
.datatable { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
.datatable th, .datatable td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; background: var(--surface-2); }
.datatable .col-date, .datatable .col-label, .datatable .col-status, .datatable .col-verdict, .datatable .col-pipeline { text-align: left; }
.datatable .col-label { white-space: normal; min-width: 180px; }
.datatable thead { position: sticky; top: 0; z-index: 2; }
.datatable thead th { color: var(--text-secondary); font-weight: 600; }
.datatable .totals td { font-weight: 700; background: var(--accent-bg); }
.datatable .col-date { position: sticky; left: 0; z-index: 1; }
.datatable thead .col-date { z-index: 3; }
.datatable tbody tr:hover td { background: var(--accent-bg); }
.datatable td.shade-1 { background: var(--shade-1); } .datatable td.shade-2 { background: var(--shade-2); }
.datatable td.shade-3 { background: var(--shade-3); } .datatable td.shade-4 { background: var(--shade-4); }
.datatable td.shade-5 { background: var(--shade-5); }
.datatable th .info-tip { bottom: auto; top: calc(100% + 6px); left: auto; right: 0; }
.datatable td.zero { color: var(--text-muted); }
.datatable td.strong { font-weight: 700; }
.datatable td.alert-text { color: var(--warn); font-weight: 600; }
.sort { all: unset; cursor: pointer; display: inline-flex; gap: 4px; align-items: center; }
.sort:hover, .sort:focus-visible { color: var(--text-primary); text-decoration: underline; }
.sort-arrow { font-size: 9px; }

/* Ads table */
.ad-title { all: unset; cursor: pointer; font-weight: 600; font-size: 14px; color: var(--text-primary); }
.ad-title:hover, .ad-title:focus-visible { color: var(--series-1); text-decoration: underline; }
.ad-sub { font-size: 12px; color: var(--text-muted); margin-top: 1px; }
.ads-table tr.inactive td { color: var(--text-muted); }
.ads-table tr.inactive .ad-title { color: var(--text-secondary); font-weight: 500; }
.ads-table tr.inactive .spark { color: var(--text-muted); }
.col-spark { width: 110px; color: var(--series-1); }
.spark { width: 96px; height: 24px; display: block; margin-left: auto; }
.pill { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--grid); color: var(--text-secondary); }
.pill-running { background: var(--good-bg); color: var(--good); }
.badge { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 6px; background: var(--grid); color: var(--text-secondary); }
.badge-good { background: var(--good-bg); color: var(--good); }
.badge-bad { background: var(--bad-bg); color: var(--bad); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }

/* Leads table: cost per deal with an in-cell bar */
.col-bar { position: relative; min-width: 140px; }
.cell-bar { position: absolute; left: 10px; top: 50%; height: 8px; max-width: calc(100% - 80px); transform: translateY(-50%); border-radius: 4px; background: var(--bar); }
.cell-value { position: relative; }
.col-pipeline { color: var(--text-secondary); }

.info { position: relative; display: inline-flex; margin-left: 4px; }
.info-btn { all: unset; cursor: help; color: var(--text-muted); font-size: 13px; line-height: 1; padding: 2px; display: inline-flex; }
.info-btn:hover { color: var(--text-secondary); }
.info-btn:focus-visible { outline: 2px solid var(--series-1); border-radius: 4px; }
.info-tip { display: none; position: absolute; z-index: 20; bottom: calc(100% + 6px); left: -8px; width: max-content; max-width: 260px; padding: 8px 10px; border-radius: 8px; background: var(--text-primary); color: var(--surface); font-size: 12px; font-weight: 400; line-height: 1.4; white-space: normal; text-align: left; box-shadow: 0 4px 12px rgb(0 0 0 / .15); }
.info:hover .info-tip, .info:focus-within .info-tip { display: block; }

.skeleton { display: block; border-radius: 8px; background: linear-gradient(90deg, var(--grid) 25%, var(--surface-2) 50%, var(--grid) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; }
.skeleton.tile { height: 92px; border-radius: 12px; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton, [role="tabpanel"] { animation: none; } }

@media (max-width: 760px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 24px; }
  .kpi:nth-child(2n) .info-tip { left: auto; right: -8px; }  /* right column: open leftwards */
  .funnel { grid-template-columns: repeat(3, 1fr); }
  .funnel li:nth-child(3n + 1)::before { display: none; }
}
@media (max-width: 480px) {
  .funnel { grid-template-columns: 1fr 1fr; gap: 12px 16px; }
  .funnel li:nth-child(3n + 1)::before { display: block; }
  .funnel li:nth-child(odd)::before { display: none; }
  .info-tip { max-width: min(240px, 70vw); }
}
