/* ============================================================
   Dashboard Chacara Sonho Verde - dark verde natureza
============================================================ */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg-0: #0a1710;
  --bg-1: #0e2016;
  --bg-2: #142a1e;
  --bg-3: #1a3627;
  --line: #1e3d2c;
  --line-2: #2a5238;
  --text: #eaf5ec;
  --muted: #8ea99a;
  --muted-2: #6b8878;
  --accent: #4ade80;        /* verde vivido */
  --accent-2: #86efac;
  --accent-3: #22c55e;
  --green: #4ade80;
  --red: #f87171;
  --blue: #60a5fa;
  --purple: #a78bfa;
  --pink: #f472b6;
  --orange: #fb923c;
  --yellow: #fbbf24;
  --radius: 14px;
  --radius-sm: 10px;
}

html, body {
  height: 100%;
  font-family: 'Inter', -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-0);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
body { overflow-x: hidden; }
a { color: inherit; text-decoration: none; }

/* ====== SIDEBAR ====== */
.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: 240px;
  background: var(--bg-0);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 22px 16px;
  z-index: 10;
  overflow-y: auto;
}
.brand {
  display: flex; align-items: center; gap: 12px;
  padding: 4px 6px 22px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 18px;
}
.brand-mark {
  width: 40px; height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-3));
  color: #0a1710;
  font-weight: 800;
  font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  letter-spacing: 0.5px;
  box-shadow: 0 4px 14px rgba(74, 222, 128, 0.35);
}
.brand-mark.big { width: 64px; height: 64px; font-size: 22px; border-radius: 16px; margin: 0 auto 16px; }
.brand-name {
  font-size: 13px; font-weight: 700;
  letter-spacing: 1px; color: var(--text);
}
.brand-sub {
  font-size: 11px; color: var(--muted); letter-spacing: 0.4px;
}

.menu {
  display: flex; flex-direction: column;
  gap: 2px; flex: 1;
}
.menu-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-weight: 500; font-size: 14px;
  transition: background .15s, color .15s;
  position: relative;
}
.menu-item:hover { background: var(--bg-2); color: var(--text); }
.menu-item.active { background: var(--bg-2); color: var(--text); }
.menu-item.active::before {
  content: ''; position: absolute;
  left: -16px; top: 8px; bottom: 8px;
  width: 3px; background: var(--accent);
  border-radius: 0 3px 3px 0;
}
.menu-item .ic-svg { width: 18px; height: 18px; flex-shrink: 0; color: var(--muted); }
.menu-item:hover .ic-svg, .menu-item.active .ic-svg { color: var(--accent); }

.sidebar-footer {
  padding-top: 16px; border-top: 1px solid var(--line); margin-top: 16px;
}
.user { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--bg-2); border: 1px solid var(--line-2);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: var(--accent);
}
.user-name { font-size: 13px; font-weight: 600; color: var(--text); }
.logout-link { font-size: 11px; color: var(--muted); }
.logout-link:hover { color: var(--red); }

/* ====== MAIN ====== */
.main {
  margin-left: 240px;
  padding: 0 36px 32px;
  min-height: 100vh;
  background: var(--bg-1);
}

/* ============================================================
   PERIOD BAR (sticky no topo em todas as paginas)
   ============================================================ */
.period-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(14, 32, 22, 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  margin: 0 -36px 20px;
  padding: 14px 36px 12px;
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
.period-chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.period-chips .chip {
  background: var(--bg-3);
  color: var(--muted);
  border: 1px solid var(--line);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s, color .15s, border-color .15s;
  white-space: nowrap;
}
.period-chips .chip:hover {
  background: var(--bg-2);
  color: var(--text);
  border-color: var(--line-2);
}
.period-chips .chip.active {
  background: var(--accent);
  color: #0a1710;
  border-color: var(--accent);
  box-shadow: 0 3px 10px rgba(74, 222, 128, 0.25);
}
.period-dates {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.date-field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.date-field span {
  font-family: 'JetBrains Mono', 'Menlo', monospace;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--muted);
  font-weight: 600;
}
.date-field input[type="date"] {
  background: var(--bg-0);
  color: var(--text);
  border: 1px solid var(--line-2);
  padding: 6px 10px;
  border-radius: 8px;
  font-family: 'JetBrains Mono', 'Menlo', monospace;
  font-size: 12px;
  font-weight: 600;
  color-scheme: dark;
  transition: border-color .15s, opacity .15s;
}
.date-field input[type="date"]:focus {
  outline: none;
  border-color: var(--accent);
}
.date-field input[type="date"]:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.btn-apply {
  background: var(--accent);
  color: #0a1710;
  border: none;
  padding: 7px 14px;
  border-radius: 8px;
  font-weight: 800;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  letter-spacing: 0.3px;
  transition: filter .15s;
}
.btn-apply:hover { filter: brightness(1.08); }
.period-status {
  font-family: 'JetBrains Mono', 'Menlo', monospace;
  font-size: 11px;
  color: var(--muted);
  margin-left: auto;
  white-space: nowrap;
}

/* ============================================================
   BARRA DE PROGRESSO NO TOPO (fetch in-flight)
   ============================================================ */
#top-progress {
  position: fixed;
  top: 0; left: 0;
  height: 2px;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  z-index: 100;
  pointer-events: none;
  transition: width .3s ease, opacity .2s;
  opacity: 0;
}
#top-progress.loading {
  width: 70%;
  opacity: 1;
  transition: width 4s cubic-bezier(0.1, 0.9, 0.2, 1);
}
#top-progress.done {
  width: 100%;
  opacity: 1;
  transition: width .2s;
}

/* ============================================================
   TOAST
   ============================================================ */
.toast-wrap {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 340px;
  pointer-events: none;
}
.toast {
  background: var(--bg-2);
  border: 1px solid var(--line-2);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  opacity: 0;
  transform: translateX(20px);
  transition: opacity .28s, transform .28s;
  pointer-events: auto;
}
.toast.show { opacity: 1; transform: translateX(0); }
.toast-info  { border-color: var(--accent); }
.toast-warn  { border-color: var(--yellow); color: var(--yellow); }
.toast-error { border-color: var(--red); color: var(--red); }

/* topo respirando um pouco (compensa remocao do padding-top do main) */
.page-head { padding-top: 12px; }

.page-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 24px; margin-bottom: 24px; flex-wrap: wrap;
}
.eyebrow {
  font-size: 11px; text-transform: uppercase;
  letter-spacing: 2px; color: var(--accent);
  font-weight: 700; margin-bottom: 6px;
}
.page-head h1 {
  font-size: 28px; font-weight: 800; color: var(--text);
  letter-spacing: -0.5px; margin-bottom: 6px;
}
.muted { color: var(--muted); }
.center { text-align: center; }

.refresh-bar {
  display: flex; align-items: center; gap: 10px;
}
.refresh-bar .ts { font-size: 11px; color: var(--muted); }
.btn-refresh {
  background: var(--bg-3); color: var(--accent);
  border: 1px solid var(--line-2);
  padding: 8px 14px; border-radius: 8px;
  font-weight: 700; font-size: 12px;
  cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  font-family: inherit;
}
.btn-refresh:hover { background: var(--bg-2); border-color: var(--accent); }
.btn-refresh svg { width: 14px; height: 14px; }

/* ====== KPI CARDS COM DELTA ====== */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  transition: transform .15s, border-color .15s;
  position: relative;
}
.card:hover { transform: translateY(-2px); border-color: var(--line-2); }
.card .label {
  font-size: 10px; text-transform: uppercase;
  letter-spacing: 1.2px; color: var(--muted);
  font-weight: 600; margin-bottom: 8px;
}
.card .value {
  font-size: 26px; font-weight: 800; color: var(--text);
  line-height: 1.15; letter-spacing: -0.5px;
}
.card .delta {
  display: inline-block; margin-top: 8px;
  font-size: 11px; font-weight: 700;
  padding: 3px 8px; border-radius: 999px;
}
.delta.up { background: rgba(74, 222, 128, 0.12); color: var(--green); }
.delta.down { background: rgba(248, 113, 113, 0.12); color: var(--red); }
.delta.flat { background: rgba(142, 169, 154, 0.12); color: var(--muted); }
.card .delta-note { display: block; margin-top: 4px; font-size: 10px; color: var(--muted-2); }

/* Semantica invertida - CPL e CPM: subir é ruim, cair é bom */
.card.metric-inverse .delta.up { background: rgba(248, 113, 113, 0.12); color: var(--red); }
.card.metric-inverse .delta.down { background: rgba(74, 222, 128, 0.12); color: var(--green); }

/* ====== PANEL ====== */
.panel {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 24px;
  margin-bottom: 20px;
}
.panel h2 {
  font-size: 16px; font-weight: 700; color: var(--text);
  margin-bottom: 4px;
  display: flex; align-items: center; gap: 10px;
}
.panel h2::before {
  content: ''; width: 3px; height: 16px;
  background: var(--accent); border-radius: 2px;
}
.panel .desc { font-size: 12px; color: var(--muted); margin-bottom: 16px; }

/* ====== GRAFICOS ====== */
.grid-charts { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.chart-box {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 14px 16px 12px;
}
.chart-box.full { grid-column: 1 / -1; }
.chart-title {
  font-size: 11px; text-transform: uppercase;
  letter-spacing: 1px; color: var(--muted);
  font-weight: 600; margin-bottom: 10px;
}
.chart-wrap { position: relative; height: 240px; }
.chart-wrap.tall { height: 280px; }

/* ====== TABELA ====== */
.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
}
thead th {
  text-align: left; padding: 10px 12px;
  font-size: 10px; text-transform: uppercase;
  letter-spacing: 1px; color: var(--muted);
  font-weight: 600;
  border-bottom: 1px solid var(--line);
  background: var(--bg-3);
  white-space: nowrap;
}
tbody td {
  padding: 12px; border-bottom: 1px solid var(--line);
  font-size: 13px; vertical-align: top;
  color: var(--text);
}
tbody tr:hover { background: rgba(74, 222, 128, 0.05); }
tbody tr.highlight-danger { background: rgba(248, 113, 113, 0.08); }
tbody tr.highlight-danger:hover { background: rgba(248, 113, 113, 0.14); }
.num { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }

.tag {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 10px; font-weight: 700;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}
.tag-warn  { background: rgba(251, 191, 36, 0.12); color: var(--yellow); border: 1px solid rgba(251,191,36,0.25); }
.tag-danger{ background: rgba(248, 113, 113, 0.12); color: var(--red);    border: 1px solid rgba(248,113,113,0.25); }
.tag-ok    { background: rgba(74, 222, 128, 0.12);  color: var(--green);  border: 1px solid rgba(74,222,128,0.25); }
.tag-mut   { background: rgba(142, 169, 154, 0.12); color: var(--muted);  border: 1px solid rgba(142,169,154,0.25); }

/* ====== HEALTH ====== */
.health-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.health-card {
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px 20px;
}
.health-card .h-label { font-size: 10px; text-transform: uppercase; letter-spacing: 1.2px; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.health-card .h-value { font-size: 20px; font-weight: 700; color: var(--text); margin-bottom: 4px; }
.health-card .h-sub { font-size: 12px; color: var(--muted); }
.health-card.alert { border-color: var(--red); background: rgba(248, 113, 113, 0.06); }
.health-card.warn  { border-color: var(--yellow); background: rgba(251, 191, 36, 0.06); }
.health-card.ok    { border-color: var(--accent); background: rgba(74, 222, 128, 0.04); }
.health-card.alert .h-value { color: var(--red); }
.health-card.warn  .h-value { color: var(--yellow); }
.health-card.ok    .h-value { color: var(--accent); }

.alert-banner {
  background: rgba(248, 113, 113, 0.1);
  border: 1px solid rgba(248, 113, 113, 0.35);
  color: var(--red);
  padding: 14px 18px;
  border-radius: 10px;
  margin-bottom: 20px;
  font-size: 13px;
  font-weight: 600;
  display: flex; align-items: center; gap: 10px;
}
.alert-banner svg { width: 20px; height: 20px; flex-shrink: 0; }

/* ====== FLASH ====== */
.flashes { margin-bottom: 16px; }
.flash { padding: 10px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 6px; }
.flash-error { background: rgba(248, 113, 113, 0.1); border: 1px solid rgba(248, 113, 113, 0.25); color: var(--red); }
.flash-success { background: rgba(74, 222, 128, 0.1); border: 1px solid rgba(74, 222, 128, 0.25); color: var(--green); }

/* ====== LOGIN ====== */
.login-body {
  background: radial-gradient(circle at 30% 20%, #163d28 0%, #060d09 60%);
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.login-wrap { width: 100%; max-width: 420px; }
.login-card {
  background: var(--bg-1); border: 1px solid var(--line);
  border-radius: 20px; padding: 36px 32px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
}
.login-brand { text-align: center; margin-bottom: 28px; }
.login-brand h1 { font-size: 22px; font-weight: 800; letter-spacing: -0.3px; color: var(--text); margin-bottom: 4px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field span { font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--muted); font-weight: 600; }
.field input {
  background: var(--bg-0); border: 1px solid var(--line-2); color: var(--text);
  padding: 11px 14px; border-radius: 10px;
  font-size: 14px; font-family: inherit;
  transition: border-color .15s;
}
.field input:focus { outline: none; border-color: var(--accent); }
.btn-primary {
  width: 100%; margin-top: 8px;
  background: var(--accent); color: #0a1710;
  border: none; padding: 12px 18px; border-radius: 10px;
  font-weight: 800; font-size: 14px;
  cursor: pointer; letter-spacing: 0.3px;
  transition: filter .15s;
  font-family: inherit;
}
.btn-primary:hover { filter: brightness(1.08); }
.login-footer { text-align: center; font-size: 11px; margin-top: 22px; letter-spacing: 0.5px; }

/* ====== LOADING ====== */
.loading-banner {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 20px 24px;
  margin-bottom: 20px; color: var(--muted); font-size: 14px;
}
.spinner {
  width: 20px; height: 20px;
  border: 2px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error-banner {
  background: #2a1010; border: 1px solid #6b1f1f;
  border-radius: 10px; padding: 16px 20px;
  color: #f87171; font-size: 14px; margin-bottom: 20px;
}

/* ====== RESPONSIVE (mobile-first prioritario) ====== */
@media (max-width: 880px) {
  .sidebar {
    position: relative; width: 100%; height: auto;
    border-right: none; border-bottom: 1px solid var(--line);
    padding: 14px 16px;
  }
  .sidebar-footer { padding-top: 12px; margin-top: 12px; }
  .menu { flex-direction: row; overflow-x: auto; gap: 4px; }
  .menu-item { padding: 8px 12px; font-size: 12px; white-space: nowrap; }
  .menu-item.active::before { display: none; }
  .main { margin-left: 0; padding: 0 14px 18px; }

  /* period bar mobile */
  .period-bar {
    margin: 0 -14px 14px;
    padding: 12px 14px 10px;
    gap: 10px;
    flex-direction: column;
    align-items: stretch;
    min-width: 0;
    max-width: 100%;
  }
  .period-chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    min-width: 0;
    max-width: 100%;
  }
  .period-chips::-webkit-scrollbar { display: none; }
  .period-chips .chip { flex-shrink: 0; }
  .period-dates {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    min-width: 0;
    max-width: 100%;
    width: 100%;
  }
  .date-field { min-width: 0; }
  .date-field input[type="date"] {
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
  }
  .period-dates .btn-apply {
    grid-column: 1 / -1;
    width: 100%;
    padding: 10px;
  }
  /* quando preset != custom, colapsa datas pra economizar espaco */
  .period-bar:not(.mode-custom) .period-dates {
    display: none;
  }
  .period-bar:not(.mode-custom) .period-status {
    order: 2;
    margin-top: 4px;
  }
  .period-status {
    margin-left: 0;
    order: 3;
    width: 100%;
    text-align: right;
    min-width: 0;
  }

  .page-head { flex-direction: column; align-items: stretch; gap: 12px; padding-top: 4px; }
  .page-head h1 { font-size: 22px; }
  .refresh-bar { justify-content: flex-start; }
  .cards { grid-template-columns: 1fr 1fr; gap: 10px; }
  .card { padding: 14px 14px; }
  .card .value { font-size: 20px; }
  .grid-charts { grid-template-columns: 1fr; }
  .chart-wrap { height: 200px; }
  .panel { padding: 16px 14px; }
  table { font-size: 12px; }
  thead th, tbody td { padding: 8px 8px; }

  .toast-wrap { top: 12px; right: 12px; left: 12px; max-width: none; }
}

@media (max-width: 340px) {
  .period-dates { grid-template-columns: 1fr; }
}
