:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e3e6eb;
  --ink: #141a22;
  --ink-2: #4a5566;
  --muted: #8590a0;
  --accent: #0f8b7d;
  --accent-soft: #e1f3f0;
  --c-leads: #0f8b7d;
  --c-spend: #8fa6bf;
  --c-cpl: #d9822b;
  --c-google: #c9a227;
  --c-organic: #6b7fd7;
  --c-unknown: #b8bec8;
  --c-groups: #4f6bd8;
  --good: #11865f;
  --good-soft: #e4f5ee;
  --bad: #c8463d;
  --bad-soft: #fbeceb;
  --warn: #a96a00;
  --warn-soft: #fdf3df;
  --shadow: 0 1px 2px rgba(20, 26, 34, 0.04), 0 1px 1px rgba(20, 26, 34, 0.03);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116;
    --surface: #161a21;
    --surface-2: #1d222b;
    --border: #2a303b;
    --ink: #e9edf2;
    --ink-2: #b4bdc9;
    --muted: #7d8796;
    --accent: #2fb5a4;
    --accent-soft: #15302d;
    --c-leads: #2fb5a4;
    --c-spend: #5d7590;
    --c-cpl: #f0a050;
    --c-google: #d9b54a;
    --c-organic: #8a9bf0;
    --c-unknown: #59616e;
    --c-groups: #7d93f0;
    --good: #3cc58f;
    --good-soft: #14302a;
    --bad: #f07a70;
    --bad-soft: #3a1d1c;
    --warn: #e6ac4a;
    --warn-soft: #33281a;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0e1116;
  --surface: #161a21;
  --surface-2: #1d222b;
  --border: #2a303b;
  --ink: #e9edf2;
  --ink-2: #b4bdc9;
  --muted: #7d8796;
  --accent: #2fb5a4;
  --accent-soft: #15302d;
  --c-leads: #2fb5a4;
  --c-spend: #5d7590;
  --c-cpl: #f0a050;
  --c-google: #d9b54a;
  --c-organic: #8a9bf0;
  --c-unknown: #59616e;
  --c-groups: #7d93f0;
  --good: #3cc58f;
  --good-soft: #14302a;
  --bad: #f07a70;
  --bad-soft: #3a1d1c;
  --warn: #e6ac4a;
  --warn-soft: #33281a;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

a { color: inherit; }

button, input, select { font: inherit; color: inherit; }

.num { font-variant-numeric: tabular-nums; }

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  font-size: 15px;
}

.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), var(--c-groups));
}

.topbar-actions { display: flex; align-items: center; gap: 6px; }

.user-chip {
  font-size: 12px;
  color: var(--ink-2);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 4px 10px;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mode-banner {
  margin: 0;
  padding: 9px 24px;
  font-size: 12.5px;
  color: var(--warn);
  background: var(--warn-soft);
  border-bottom: 1px solid var(--border);
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s;
}

.btn:hover { background: var(--surface-2); }

.btn:focus-visible, .seg button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--surface); }
.btn.primary:hover { opacity: 0.9; background: var(--ink); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--bad); }
.btn.icon { width: 34px; padding: 0; }
.btn.sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn.sm.icon { width: 28px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ---------- Layout ---------- */

.app { max-width: 1440px; margin: 0 auto; padding: 24px; }

/* ---------- Dashboard com categorias ---------- */

.dash-shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 28px; align-items: start; }
.dash-main { min-width: 0; }

.side {
  position: sticky;
  top: 76px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.side-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 4px 10px 8px;
}
.side-nav { display: grid; gap: 2px; }
.side-nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink-2);
  font-weight: 500;
  font-size: 13.5px;
}
.side-nav a:hover { background: var(--surface-2); color: var(--ink); }
.side-nav a.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); border: 1px solid var(--border); padding: 8px 9px; }
.side-nav a.active svg { color: var(--accent); }
.side-back { margin-top: 14px; padding: 6px 10px; font-size: 12.5px; color: var(--muted); text-decoration: none; }
.side-back:hover { color: var(--ink); }

.kpi-sm .kpi-value { font-size: 24px; margin: 6px 0 4px; }
.kpi-sm .kpi-foot { font-size: 11.5px; }

.funnel { display: grid; gap: 4px; }
.funnel-step { padding: 6px 0; }
.funnel-rate { font-size: 12px; color: var(--muted); padding-left: 8px; }
.funnel-rate strong { color: var(--ink); }

.survey-frame { width: 100%; height: 640px; border: 1px solid var(--border); border-radius: 10px; background: #fff; }

.lib-group { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 14px 0 8px; }
.lib-group:first-child { margin-top: 0; }

.check-list { margin: 0; padding-left: 18px; display: grid; gap: 6px; font-size: 13px; color: var(--ink-2); }

.goal-bar { display: inline-block; width: 70px; height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; vertical-align: middle; margin-right: 6px; }
.goal-bar > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.widget .pace-progress { margin: 8px 0 0; height: 6px; }

.cov-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.cov-tile { background: var(--surface-2); border-radius: 10px; padding: 14px; min-width: 0; }
.cov-tile .kpi-value { font-size: 26px; }

.q-controls { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: -4px 0 14px; }
.seg.seg-sm button { padding: 4px 9px; font-size: 12px; }
.q-channel { height: 30px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); padding: 0 8px; font-size: 12px; }

td.camp { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.camp, th:first-child { position: sticky; left: 0; background: var(--surface); z-index: 1; }
thead th:first-child, tfoot td.camp { background: var(--surface-2); }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.eyebrow {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

h1 { font-size: 26px; letter-spacing: -0.02em; margin: 4px 0 2px; font-weight: 700; }

.sub { color: var(--muted); font-size: 13px; }

.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.seg {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 3px;
  gap: 2px;
  flex-wrap: wrap;
}

.seg button {
  border: 0;
  background: transparent;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 500;
}

.seg button[aria-pressed="true"] { background: var(--ink); color: var(--surface); }

.custom-range { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }

.custom-range input {
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 0 8px;
}

.freshness { font-size: 12px; color: var(--muted); display: flex; gap: 14px; flex-wrap: wrap; }
.freshness .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 5px; background: var(--good); }
.freshness .dot.warn { background: var(--warn); }

/* ---------- Widget grid ---------- */

.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.widget {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  min-width: 0;
}

.w-1 { grid-column: span 1; }
.w-2 { grid-column: span 2; }
.w-3 { grid-column: span 3; }
.w-4 { grid-column: span 4; }

.widget h3 { margin: 0; font-size: 14px; font-weight: 600; letter-spacing: -0.01em; }
.widget .w-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.widget-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 14px; }

.kpi-label { font-size: 12.5px; color: var(--ink-2); font-weight: 500; }
.kpi-value { font-size: 30px; font-weight: 700; letter-spacing: -0.03em; margin: 8px 0 6px; line-height: 1.1; }
.kpi-foot { font-size: 12px; color: var(--muted); }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
}
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.neutral { background: var(--surface-2); color: var(--ink-2); }

.chart-box { position: relative; height: 300px; }
.chart-box.sm { height: 240px; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.legend i.line { height: 3px; border-radius: 2px; vertical-align: 2px; width: 14px; }
.legend i.dash { height: 0; border-top: 2px dashed; border-radius: 0; vertical-align: 3px; width: 14px; background: none !important; }

/* source bars */
.bars { display: grid; gap: 12px; }
.bar-row-top { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 5px; gap: 8px; }
.bar-row-top span:last-child { color: var(--ink-2); }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 99px; }
.divider { height: 1px; background: var(--border); margin: 14px 0; }

/* pace */
.pace-progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 10px 0 6px; }
.pace-progress span { display: block; height: 100%; background: var(--ink); border-radius: 99px; }
.stat-list { display: grid; gap: 10px; margin-top: 14px; }
.stat-list div { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; }
.stat-list dt { color: var(--ink-2); }
.stat-list dd { margin: 0; font-weight: 600; text-align: right; }

/* table */
.table-wrap { overflow-x: auto; margin: 0 -18px -18px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 14px; text-align: right; white-space: nowrap; border-top: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; padding-left: 18px; }
th:last-child, td:last-child { padding-right: 18px; }
thead th { border-top: 0; font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; background: var(--surface-2); }
tfoot td { font-weight: 700; background: var(--surface-2); }
td.partial { color: var(--muted); }
td .tag { font-size: 10.5px; color: var(--muted); margin-left: 6px; font-weight: 500; }
.cpl-bad { color: var(--bad); }
.cpl-good { color: var(--good); }

.empty { color: var(--muted); font-size: 13px; padding: 30px 0; text-align: center; }

.note { font-size: 11.5px; color: var(--muted); margin-top: 12px; }

/* ---------- Edit mode ---------- */

.edit-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 14px;
  margin-bottom: 14px;
  border: 1px dashed var(--accent);
  background: var(--accent-soft);
  border-radius: 10px;
  font-size: 13px;
}

.editing .widget { outline: 1px dashed var(--border); outline-offset: -1px; cursor: grab; }
.editing .widget:active { cursor: grabbing; }

.widget-tools {
  position: absolute;
  top: 8px;
  right: 8px;
  display: none;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
  z-index: 3;
}
.editing .widget-tools { display: flex; }
.sortable-ghost { opacity: 0.35; }

.library { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.library button {
  text-align: left;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
}
.library button:hover { border-color: var(--accent); }
.library strong { display: block; font-size: 13px; }
.library span { font-size: 12px; color: var(--muted); }

/* ---------- Manager ---------- */

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }

.dash-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.dash-card h3 { margin: 0; font-size: 16px; }
.dash-card .meta { font-size: 12.5px; color: var(--muted); display: grid; gap: 3px; }
.dash-card .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: auto; }
.dash-card code { font-size: 12px; background: var(--surface-2); padding: 1px 6px; border-radius: 5px; }

/* ---------- Modal ---------- */

.modal {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
.modal::backdrop { background: rgba(14, 17, 22, 0.45); }
.modal-inner { display: flex; flex-direction: column; max-height: calc(100vh - 50px); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px 8px; }
.modal-head h2 { font-size: 17px; margin: 0; }
.modal-body { padding: 8px 18px 12px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 16px; border-top: 1px solid var(--border); }
.modal-foot:empty { display: none; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.field { display: grid; gap: 5px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12.5px; font-weight: 500; color: var(--ink-2); }
.field input, .field select {
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 0 10px;
  width: 100%;
}
.field .hint { font-size: 11.5px; color: var(--muted); }
.form-error { color: var(--bad); font-size: 12.5px; margin-top: 10px; }
.section-title { grid-column: 1 / -1; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-top: 6px; }

.entries-list { display: grid; gap: 6px; margin-top: 12px; max-height: 260px; overflow-y: auto; }
.entries-list div { display: grid; grid-template-columns: 1fr 110px; align-items: center; gap: 10px; font-size: 13px; }
.entries-list input { height: 32px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface); padding: 0 8px; text-align: right; }

/* ---------- Toast & states ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--surface);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 50;
  max-width: calc(100vw - 32px);
}

.loading { color: var(--muted); padding: 60px 0; text-align: center; }
.error-box { background: var(--bad-soft); color: var(--bad); border-radius: 10px; padding: 14px 16px; font-size: 13px; }

/* ---------- Responsive ---------- */

@media (max-width: 1024px) {
  .dash-shell { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .side { position: static; }
  .side-title, .side-back { display: none; }
  .side-nav { display: flex; overflow-x: auto; gap: 4px; padding-bottom: 4px; scrollbar-width: none; }
  .side-nav a { white-space: nowrap; flex: none; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .w-3, .w-4 { grid-column: span 2; }
}

@media (max-width: 900px) {
  .cov-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 640px) {
  .app { padding: 16px; }
  .topbar { padding: 10px 16px; }
  .mode-banner { padding: 9px 16px; }
  h1 { font-size: 22px; }
  .grid { gap: 10px; }
  .w-2, .w-3, .w-4 { grid-column: span 2; }
  .widget { padding: 14px; }
  .kpi-value { font-size: clamp(18px, 5.4vw, 24px); }
  .chart-box { height: 260px; }
  .table-wrap { margin: 0 -14px -14px; }
  .form-grid { grid-template-columns: 1fr; }
  .library { grid-template-columns: 1fr; }
  .user-chip { display: none; }
}
