:root {
  --bg: #f3f6fb;
  --card: #ffffff;
  --text: #101828;
  --muted: #667085;
  --blue: #2563eb;
  --blue-soft: #eaf2ff;
  --danger: #c81e1e;
  --border: #d9e1ec;
  --soft-border: #edf1f7;
  --shadow: 0 18px 45px rgba(16, 24, 40, .08);
  --radius: 18px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: radial-gradient(circle at top left, #ffffff 0, var(--bg) 42%, #edf2fa 100%);
  color: var(--text);
}
button, input, select { font: inherit; }
button { cursor: pointer; border: 0; }
button:disabled { opacity: .65; cursor: not-allowed; }

.app { width: min(1620px, calc(100% - 56px)); margin: 0 auto; padding: 28px 0 50px; }
.header { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; margin-bottom: 22px; }
.eyebrow { display: inline-block; margin-bottom: 8px; color: var(--blue); font-size: 12px; line-height: 1; font-weight: 900; letter-spacing: .14em; text-transform: uppercase; }
h1 { margin: 0; font-size: clamp(38px, 4vw, 60px); line-height: .95; letter-spacing: -0.065em; }
.subtitle { margin: 12px 0 0; color: var(--muted); font-size: 17px; }
.header-actions { display: flex; align-items: center; gap: 10px; }
#refreshBtn, .login-button, .date-popover-actions #applyDatePicker {
  height: 42px;
  padding: 0 18px;
  border-radius: 999px;
  background: #111827;
  color: #fff;
  font-weight: 850;
}
#refreshBtn:hover, .login-button:hover, .date-popover-actions #applyDatePicker:hover { background: #0b1220; }
.logout-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 42px;
  padding: 0 16px;
  border-radius: 999px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--border);
  text-decoration: none;
  font-weight: 800;
}
.logout-link:hover { background: #f8fafc; }

.card { background: rgba(255,255,255,.93); border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: var(--radius); }
.filters {
  display: grid;
  grid-template-columns: minmax(220px, 1.4fr) repeat(7, minmax(135px, 1fr));
  gap: 14px;
  padding: 16px;
  margin-bottom: 18px;
  overflow: visible;
}
label { font-size: 12px; color: var(--muted); font-weight: 850; }
input, select {
  display: block;
  width: 100%;
  margin-top: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 13px;
  background: #fff;
  color: var(--text);
  outline: none;
  font-size: 14px;
}
input:focus, select:focus, .date-range-button:focus { border-color: #93c5fd; box-shadow: 0 0 0 4px rgba(37, 99, 235, .11); }
.date-range-button {
  display: flex; align-items: center; gap: 10px; width: 100%; height: 43px; margin-top: 6px;
  border: 1px solid var(--border); background: #fff; color: var(--text); border-radius: 12px; padding: 0 13px;
  text-align: left; font-weight: 800;
}
.calendar-icon { width: 18px; height: 18px; font-size: 11px; color: var(--muted); }
.filter-actions-inline { display: flex; align-items: end; }
.secondary-button { width: 100%; height: 43px; margin-top: 20px; color: var(--text); background: #fff; border: 1px solid var(--border); border-radius: 12px; font-weight: 800; }
.secondary-button:hover { background: #f8fafc; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi { padding: 18px; min-height: 124px; }
.kpi span { color: var(--muted); font-size: 13px; font-weight: 900; }
.kpi strong { display: block; margin-top: 9px; font-size: 31px; line-height: 1; letter-spacing: -0.05em; }
.kpi small { display: block; margin-top: 8px; color: var(--muted); font-size: 11px; font-weight: 850; }
.accent-kpi { border-color: #fca5a5; background: linear-gradient(180deg, #fff, #fff7f7); }
.drill-btn { background: transparent; color: inherit; padding: 0; font-weight: 900; text-decoration: none; }
.drill-btn:hover { color: var(--blue); text-decoration: underline; text-underline-offset: 3px; }
.kpi-link { font-size: inherit; letter-spacing: inherit; }
.rate-link { color: #1d4ed8; }
.danger-link { color: var(--danger); }

.tabs { display: flex; gap: 8px; padding: 8px; margin-bottom: 18px; overflow-x: auto; box-shadow: 0 12px 30px rgba(16,24,40,.05); }
.tab-btn { flex: 0 0 auto; height: 42px; padding: 0 16px; border-radius: 12px; background: transparent; color: var(--muted); font-weight: 900; }
.tab-btn:hover { background: #f3f6fb; color: var(--text); }
.tab-btn.active { background: #111827; color: #fff; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.grid.two { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-bottom: 18px; }
.chart-card { padding: 18px; min-height: 420px; }
.chart-card h2, .card-heading h2 { margin: 0; font-size: 18px; letter-spacing: -0.03em; }
.chart-card h2 { margin-bottom: 16px; }
canvas { height: 340px !important; }
.table-card { padding: 18px; min-height: 380px; }
.card-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.card-heading span { color: var(--muted); font-size: 12px; font-weight: 850; text-align: right; }
.table-scroll { overflow: auto; max-height: 480px; border: 1px solid var(--soft-border); border-radius: 14px; background: #fff; }
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; }
th, td { padding: 12px 12px; border-bottom: 1px solid var(--soft-border); white-space: nowrap; text-align: right; }
th:first-child, td:first-child { text-align: left; }
th { position: sticky; top: 0; z-index: 1; background: #f8fafc; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .035em; }
tbody tr:hover { background: #f8fafc; }
tbody tr:last-child td { border-bottom: 0; }
.name-cell { max-width: 290px; overflow: hidden; text-overflow: ellipsis; font-weight: 900; color: var(--text); }
.rate-cell { font-weight: 900; color: #1d4ed8; }
.danger-rate { color: var(--danger); }
.empty-table { text-align: center !important; color: var(--muted); padding: 28px !important; }

/* Calendário light, sem visual preto */
.date-popover {
  position: absolute;
  z-index: 99;
  display: none;
  grid-template-columns: 170px 1fr;
  width: min(620px, calc(100vw - 24px));
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 24px 70px rgba(16, 24, 40, .20);
  overflow: hidden;
}
.date-popover.open { display: grid; }
.date-presets { padding: 12px 10px; border-right: 1px solid var(--soft-border); background: #f8fafc; }
.date-presets button { width: 100%; height: 36px; display: block; margin: 3px 0; padding: 0 10px; text-align: left; color: #475467; background: transparent; border-radius: 8px; font-weight: 750; }
.date-presets button:hover, .date-presets button.active { background: #eaf2ff; color: #1d4ed8; }
.date-calendar-wrap { padding: 12px 14px 10px; background: #fff; }
.date-popover-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 10px; }
.date-popover-actions button { height: 36px; border-radius: 10px; padding: 0 16px; font-weight: 850; }
.ghost-button { background: #fff; color: var(--muted); border: 1px solid transparent; }
.ghost-button:hover { background: #f8fafc; color: var(--text); border-color: var(--border); }
.date-popover .flatpickr-calendar { background: transparent !important; box-shadow: none !important; border: 0 !important; width: 360px !important; }
.date-popover .flatpickr-months, .date-popover .flatpickr-innerContainer, .date-popover .flatpickr-rContainer, .date-popover .flatpickr-days, .date-popover .dayContainer { background: transparent !important; box-shadow: none !important; }
.date-popover .flatpickr-current-month, .date-popover .flatpickr-monthDropdown-months, .date-popover .numInputWrapper input { color: var(--text) !important; font-weight: 900 !important; }
.date-popover .flatpickr-prev-month, .date-popover .flatpickr-next-month { fill: #98a2b3 !important; color: #98a2b3 !important; }
.date-popover .flatpickr-weekday { color: #667085 !important; font-weight: 800 !important; }
.date-popover .flatpickr-day { color: #344054 !important; border-radius: 10px !important; border-color: transparent !important; font-weight: 750 !important; }
.date-popover .flatpickr-day:hover { background: #eff6ff !important; border-color: transparent !important; }
.date-popover .flatpickr-day.prevMonthDay, .date-popover .flatpickr-day.nextMonthDay { color: #98a2b3 !important; }
.date-popover .flatpickr-day.today { border-color: #60a5fa !important; }
.date-popover .flatpickr-day.selected, .date-popover .flatpickr-day.startRange, .date-popover .flatpickr-day.endRange { background: #60a5fa !important; border-color: #60a5fa !important; color: #fff !important; }
.date-popover .flatpickr-day.inRange { background: #dbeafe !important; border-color: #dbeafe !important; box-shadow: -5px 0 0 #dbeafe, 5px 0 0 #dbeafe !important; }
.date-popover .flatpickr-calendar.arrowTop:before, .date-popover .flatpickr-calendar.arrowTop:after { display: none !important; }

/* Modal leads */
.modal { display: none; position: fixed; inset: 0; z-index: 120; }
.modal.open { display: block; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, .48); backdrop-filter: blur(3px); }
.modal-panel { position: relative; width: min(1080px, calc(100vw - 36px)); max-height: min(760px, calc(100vh - 40px)); margin: 20px auto; background: #fff; border: 1px solid var(--border); border-radius: 18px; box-shadow: 0 30px 90px rgba(15, 23, 42, .28); display: flex; flex-direction: column; overflow: hidden; }
.modal-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; padding: 20px 22px; border-bottom: 1px solid var(--soft-border); }
.modal-header h2 { margin: 0 0 6px; font-size: 22px; letter-spacing: -0.04em; }
.modal-header p { margin: 0; color: var(--muted); font-size: 13px; font-weight: 750; }
.modal-close { width: 38px; height: 38px; border-radius: 999px; background: #f3f4f6; color: var(--text); font-size: 24px; line-height: 1; }
.modal-close:hover { background: #e5e7eb; }
.modal-table-scroll { border: 0; border-radius: 0; max-height: 620px; }
.lead-list-table th, .lead-list-table td { text-align: left; }
.copy-phone { background: #f8fafc; color: var(--blue); padding: 6px 9px; border-radius: 8px; font-weight: 850; }
.copy-phone:hover { background: #eaf2ff; }

/* Login clean/profissional */
.login-page { min-height: 100vh; background: #f6f8fc; display: grid; place-items: center; padding: 24px; }
.login-shell { width: min(100%, 410px); }
.login-panel { background: #fff; border: 1px solid var(--border); border-radius: 18px; padding: 32px; box-shadow: 0 18px 55px rgba(16, 24, 40, .10); }
.login-brand { display: inline-flex; align-items: center; justify-content: center; height: 34px; padding: 0 12px; margin: 0 0 18px; border-radius: 999px; background: #f1f5f9; color: #475467; font-size: 12px; font-weight: 900; letter-spacing: .04em; text-transform: uppercase; }
.login-panel h1 { font-size: 28px; line-height: 1.06; letter-spacing: -0.045em; margin: 0 0 8px; }
.login-panel p { margin: 0 0 24px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.login-form { display: grid; gap: 15px; }
.login-form input { padding: 14px; border-radius: 12px; font-size: 15px; }
.login-button { width: 100%; margin-top: 4px; height: 48px; border-radius: 12px; font-size: 15px; }
.login-error { margin: 0 0 18px; padding: 12px 14px; border-radius: 12px; background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; font-weight: 800; font-size: 14px; }
.login-footnote { margin-top: 16px; color: var(--muted); font-size: 12px; text-align: center; }

@media (max-width: 1200px) {
  .filters { grid-template-columns: repeat(3, 1fr); }
  .grid.two { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app { width: min(100% - 24px, 100%); padding-top: 18px; }
  .header { flex-direction: column; }
  .filters { grid-template-columns: 1fr; }
  .date-popover { grid-template-columns: 1fr; }
  .date-presets { border-right: 0; border-bottom: 1px solid var(--soft-border); display: grid; grid-template-columns: 1fr 1fr; }
  .modal-panel { width: calc(100vw - 20px); margin: 10px auto; }
}

/* v7 refinements */
.kpis {
  grid-template-columns: repeat(5, minmax(0, 1fr));
  align-items: stretch;
}
.kpi {
  min-width: 0;
  min-height: 112px;
  padding: 16px;
}
.kpi strong {
  font-size: clamp(24px, 2.05vw, 31px);
  word-break: keep-all;
}
.kpi small { line-height: 1.25; }
.grid.one { display: grid; grid-template-columns: 1fr; gap: 18px; margin-bottom: 18px; }
.chart-card { position: relative; overflow: hidden; }
.chart-empty {
  position: absolute;
  inset: 58px 18px 18px;
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  background: rgba(248, 250, 252, .82);
  color: var(--muted);
  font-weight: 900;
  pointer-events: none;
}
.sort-btn {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 2px;
  width: 100%;
  background: transparent;
  color: inherit;
  padding: 0;
  border: 0;
  text-transform: inherit;
  letter-spacing: inherit;
  font-size: inherit;
  font-weight: inherit;
  cursor: pointer;
}
th:first-child .sort-btn { justify-content: flex-start; }
.sort-btn:hover { color: var(--blue); }
.sort-btn span { min-width: 16px; color: var(--blue); }
.lead-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 76px;
  height: 32px;
  padding: 0 11px;
  border-radius: 9px;
  background: #111827;
  color: #fff;
  font-weight: 900;
  text-decoration: none;
  white-space: nowrap;
}
.lead-link:hover { background: #2563eb; color: #fff; }
.lead-list-table th:last-child,
.lead-list-table td:last-child { text-align: center; }
@media (max-width: 1500px) {
  .kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 620px) {
  .kpis { grid-template-columns: 1fr; }
}

/* v9 emergency layout fixes: keep overflow inside cards, never on the page */
html,
body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

.app {
  width: min(1500px, calc(100vw - 48px));
  max-width: calc(100vw - 48px);
}

.card,
.filters,
.kpis,
.tabs,
.tab-panel,
.grid,
.grid > *,
.table-card,
.chart-card,
.table-scroll,
.card-heading,
.modal-panel {
  min-width: 0;
  max-width: 100%;
}

.grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid.one {
  grid-template-columns: minmax(0, 1fr);
}

.table-card {
  overflow: hidden;
}

.table-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
}

.table-scroll table {
  width: max-content;
  min-width: 100%;
  max-width: none;
}

th,
td {
  padding: 10px 11px;
}

.name-cell {
  max-width: 230px;
  min-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-heading h2 {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-heading span {
  flex: 0 1 auto;
  min-width: 0;
}

.chart-card {
  overflow: hidden;
  min-height: 360px;
}

.chart-card canvas {
  display: block;
  width: 100% !important;
  max-width: 100% !important;
  height: 300px !important;
}

#tab-ads .chart-card,
#tab-channels .chart-card {
  min-height: 340px;
}

#tab-ads .grid.one,
#tab-channels .grid.two:last-child {
  margin-top: 0;
}

.kpis {
  grid-template-columns: repeat(5, minmax(150px, 1fr));
}

.kpi {
  min-width: 0;
}

.kpi strong {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tabs {
  max-width: 100%;
  overflow-x: auto;
}

/* make the big tables breathe without forcing the full page wider */
#campaignTable .name-cell,
#adTable .name-cell,
#adSetTable .name-cell,
#channelTable .name-cell {
  max-width: 260px;
}

/* desktop fallback: when the viewport is not very wide, stack the table cards */
@media (max-width: 1350px) {
  .grid.two,
  .performance-grid {
    grid-template-columns: 1fr;
  }
  .app {
    width: min(1180px, calc(100vw - 32px));
    max-width: calc(100vw - 32px);
  }
}

@media (max-width: 980px) {
  .kpis {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  h1 {
    font-size: clamp(34px, 8vw, 46px);
  }
}

@media (max-width: 620px) {
  .app {
    width: calc(100vw - 20px);
    max-width: calc(100vw - 20px);
  }
  .kpis {
    grid-template-columns: 1fr;
  }
  .chart-card canvas {
    height: 260px !important;
  }
}

/* v10 wide desktop layout: more room for KPIs/cards, fewer internal scrollbars */
@media (min-width: 1360px) {
  .app {
    width: min(1880px, calc(100vw - 56px));
    max-width: calc(100vw - 56px);
  }

  .kpis {
    grid-template-columns: repeat(10, minmax(120px, 1fr));
    gap: 12px;
  }

  .kpi {
    min-height: 106px;
    padding: 15px 14px;
  }

  .kpi span {
    font-size: 12px;
  }

  .kpi strong {
    font-size: clamp(24px, 1.45vw, 31px);
  }

  .kpi small {
    font-size: 10.5px;
  }

  .grid.two,
  .performance-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .table-card {
    padding: 16px;
  }

  .table-scroll {
    overflow-x: auto;
  }

  .table-scroll table {
    width: 100%;
    min-width: 860px;
  }

  th,
  td {
    padding: 10px 9px;
    font-size: 12.5px;
  }

  th {
    font-size: 10.5px;
  }

  .name-cell {
    max-width: 220px;
    min-width: 130px;
  }

  #campaignTable .name-cell,
  #adTable .name-cell,
  #adSetTable .name-cell,
  #channelTable .name-cell {
    max-width: 250px;
  }
}

@media (min-width: 1700px) {
  .app {
    width: min(1960px, calc(100vw - 72px));
    max-width: calc(100vw - 72px);
  }

  .kpis {
    grid-template-columns: repeat(10, minmax(135px, 1fr));
    gap: 14px;
  }

  .kpi {
    min-height: 112px;
    padding: 16px;
  }

  .kpi span {
    font-size: 13px;
  }

  .kpi small {
    font-size: 11px;
  }

  .table-scroll table {
    min-width: 920px;
  }

  th,
  td {
    padding: 11px 10px;
  }

  .name-cell {
    max-width: 260px;
    min-width: 150px;
  }

  #campaignTable .name-cell,
  #adTable .name-cell,
  #adSetTable .name-cell,
  #channelTable .name-cell {
    max-width: 300px;
  }
}

@media (min-width: 2000px) {
  .app {
    width: min(2100px, calc(100vw - 96px));
    max-width: calc(100vw - 96px);
  }

  .table-scroll table {
    min-width: 0;
  }
}

/* Keep older fallback only for genuinely small screens */
@media (max-width: 1180px) {
  .grid.two,
  .performance-grid {
    grid-template-columns: 1fr;
  }
}
