/* ═══════════════════════════════════════
   DESIGN TOKENS
   ═══════════════════════════════════════ */

:root {
    --bg-page: #0d1117;
    --bg-card: #161b22;
    --bg-card-hover: #1c2129;
    --bg-input: #0d1117;
    --border: #30363d;
    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --text-muted: #6e7681;
    --accent-green: #3fb950;
    --accent-red: #f85149;
    --accent-blue: #58a6ff;
    --accent-yellow: #d29922;
    --accent-purple: #bc8cff;
    --accent-teal: #5eead4;
    --font-sans: 'Inter', -apple-system, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 2px 8px rgba(0,0,0,.3);
}

[data-theme="light"] {
    --bg-page: #f6f8fa; --bg-card: #ffffff; --bg-card-hover: #f3f4f6;
    --bg-input: #f6f8fa; --border: #d0d7de;
    --text-primary: #1f2328; --text-secondary: #656d76; --text-muted: #8b949e;
    --accent-green: #1a7f37; --accent-red: #cf222e; --accent-blue: #0969da;
    --accent-yellow: #9a6700; --accent-purple: #8250df; --accent-teal: #0d9488;
    --shadow: 0 2px 8px rgba(0,0,0,.08);
}

/* ═══════════════════════════════════════
   BASE
   ═══════════════════════════════════════ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: var(--font-sans); background: var(--bg-page);
    color: var(--text-primary); line-height: 1.6; min-height: 100vh;
}

.loading {
    display: flex; flex-direction: column; align-items: center;
    justify-content: center; min-height: 60vh; gap: 16px; color: var(--text-secondary);
}

.spinner {
    width: 36px; height: 36px; border: 3px solid var(--border);
    border-top-color: var(--accent-teal); border-radius: 50%;
    animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ═══════════════════════════════════════
   HEADER
   ═══════════════════════════════════════ */

.header {
    border-bottom: 1px solid var(--border); padding: 14px 24px;
    display: flex; align-items: center; justify-content: space-between;
    flex-wrap: wrap; gap: 12px;
}

.header-left { display: flex; align-items: center; gap: 12px; }

.header-logo { font-family: var(--font-mono); font-size: 1.1rem; font-weight: 700; }
.header-logo span { color: var(--accent-teal); }

.header-badge {
    font-size: .65rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: .06em; background: var(--accent-teal); color: var(--bg-page);
    padding: 2px 8px; border-radius: 4px;
}

.header-right { display: flex; align-items: center; gap: 12px; }
.header-timestamp { font-size: .78rem; color: var(--text-muted); }

.theme-toggle {
    background: var(--bg-input); border: 1px solid var(--border);
    color: var(--text-primary); width: 34px; height: 34px;
    border-radius: var(--radius-sm); cursor: pointer; font-size: 1rem;
    display: flex; align-items: center; justify-content: center; transition: background .2s;
}

.theme-toggle:hover { background: var(--bg-card-hover); }

/* ═══════════════════════════════════════
   NAVIGATION
   ═══════════════════════════════════════ */

.nav {
    display: flex; gap: 2px; padding: 6px 24px;
    border-bottom: 1px solid var(--border); overflow-x: auto;
}

.nav-btn {
    background: none; border: none; color: var(--text-muted);
    font-family: var(--font-sans); font-size: .78rem; font-weight: 500;
    padding: 8px 14px; border-radius: var(--radius-sm);
    cursor: pointer; white-space: nowrap; transition: all .15s;
}

.nav-btn:hover { color: var(--text-primary); background: var(--bg-card); }
.nav-btn.active { color: var(--accent-teal); background: rgba(94,234,212,.08); font-weight: 600; }

/* ═══════════════════════════════════════
   FILTER TOOLBAR
   ═══════════════════════════════════════ */

.filter-toolbar {
    background: var(--bg-card); border-bottom: 1px solid var(--border);
    padding: 14px 24px; display: flex; gap: 20px; align-items: flex-end;
    flex-wrap: wrap; position: sticky; top: 0; z-index: 10;
}

.filter-field { display: flex; flex-direction: column; gap: 4px; }

.filter-field label {
    font-size: .62rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: .08em; color: var(--text-muted);
}

.filter-select {
    appearance: none; -webkit-appearance: none;
    background: var(--bg-input); border: 1px solid var(--border);
    color: var(--text-primary); font-family: var(--font-sans);
    font-size: .82rem; font-weight: 500; padding: 7px 32px 7px 10px;
    border-radius: 6px; cursor: pointer; min-width: 160px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238b949e' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 10px center;
    transition: border-color .15s;
}

.filter-select:hover { border-color: var(--accent-teal); }
.filter-select:focus { outline: none; border-color: var(--accent-teal); box-shadow: 0 0 0 2px rgba(94,234,212,.15); }

.filter-divider { width: 1px; height: 30px; background: var(--border); align-self: flex-end; margin-bottom: 2px; }

/* ═══════════════════════════════════════
   KPI CARDS
   ═══════════════════════════════════════ */

.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; padding: 20px 24px; position: relative; z-index: 20; }

.kpi-card {
    background: var(--bg-card); border: 1px solid var(--border);
    border-left: 3px solid var(--accent-teal); border-radius: var(--radius);
    padding: 18px 20px; transition: all .2s;
}

.kpi-card:hover { background: var(--bg-card-hover); transform: translateY(-1px); box-shadow: var(--shadow); }

.kpi-label {
    font-size: .68rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: .06em; color: var(--text-muted); margin-bottom: 6px;
    display: flex; align-items: center; gap: 6px;
}

.kpi-value {
    font-family: var(--font-mono); font-size: 1.5rem; font-weight: 700; line-height: 1.2;
    display: flex; align-items: baseline; gap: 8px;
}
.kpi-detail { font-size: .75rem; color: var(--text-secondary); margin-top: 6px; }

.delta-up { font-size: .7rem; font-weight: 600; color: var(--accent-green); }
.delta-down { font-size: .7rem; font-weight: 600; color: var(--accent-red); }

/* ═══════════════════════════════════════
   VIEW TOGGLE
   ═══════════════════════════════════════ */

.view-toggle {
    display: flex; gap: 4px; padding: 10px 24px;
    border-bottom: 1px solid var(--border); background: var(--bg-page);
}

.view-btn {
    background: none; border: 1px solid var(--border);
    color: var(--text-muted); font-family: var(--font-sans);
    font-size: .78rem; font-weight: 600; padding: 7px 20px;
    border-radius: 20px; cursor: pointer; transition: all .2s;
}

.view-btn:hover { color: var(--text-primary); border-color: var(--accent-teal); }
.view-btn.active {
    background: var(--accent-teal); color: var(--bg-page);
    border-color: var(--accent-teal);
}

/* ═══════════════════════════════════════
   SECTIONS & CARDS
   ═══════════════════════════════════════ */

.section { padding: 20px 24px; display: none; }
.section.active { display: block; }

.section-title {
    font-size: 1.05rem; font-weight: 700; margin-bottom: 18px;
    display: flex; align-items: center; gap: 10px;
    position: relative; z-index: 20;
}

.section-title::before {
    content: ''; width: 4px; height: 18px;
    background: var(--accent-teal); border-radius: 2px;
}

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

.card-title {
    font-size: .88rem; font-weight: 600; margin-bottom: 14px;
    display: flex; align-items: center; gap: 6px;
    position: relative; z-index: 20;
}

.chart-container { position: relative; height: 380px; }
.chart-container-sm { position: relative; height: 280px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* ═══════════════════════════════════════
   INFO TOOLTIPS (?)
   ═══════════════════════════════════════ */

.info-tip {
    position: relative; cursor: help; display: inline-flex; align-items: center;
    justify-content: center; width: 16px; height: 16px; border-radius: 50%;
    background: var(--border); color: var(--text-muted); font-size: .6rem;
    font-weight: 700; flex-shrink: 0;
}

.info-tip:hover .info-box { display: block; }

.info-box {
    display: none; position: absolute; bottom: calc(100% + 8px); left: 50%;
    transform: translateX(-50%); background: var(--bg-page);
    border: 1px solid var(--border); padding: 14px 18px; border-radius: 10px;
    font-size: .78rem; font-weight: 400; color: var(--text-secondary);
    width: 320px; max-width: 90vw; z-index: 300; box-shadow: 0 8px 24px rgba(0,0,0,.4);
    line-height: 1.6; text-transform: none; letter-spacing: 0;
    white-space: normal; word-wrap: break-word;
    pointer-events: none;
}
.info-box.below { bottom: auto; top: calc(100% + 8px); }

.info-box strong { color: var(--text-primary); }

.note-box {
    background: rgba(94,234,212,.06); border: 1px solid rgba(94,234,212,.15);
    border-radius: 8px; padding: 12px 16px; font-size: .78rem;
    color: var(--text-secondary); margin-bottom: 16px; line-height: 1.6;
}

/* ═══════════════════════════════════════
   TABLES (SORTABLE)
   ═══════════════════════════════════════ */

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

table { width: 100%; border-collapse: collapse; font-size: .82rem; }

thead th {
    text-align: left; font-size: .65rem; font-weight: 700;
    text-transform: uppercase; letter-spacing: .05em;
    color: var(--text-secondary); padding: 8px 10px;
    border-bottom: 2px solid var(--border); white-space: nowrap;
}

th.sortable { cursor: pointer; user-select: none; transition: color .15s; }
th.sortable:hover { color: var(--accent-teal); }
th.sortable::after { content: ' \u21C5'; opacity: .3; font-size: .7rem; }
th.sort-asc::after { content: ' \u25B2'; opacity: 1; color: var(--accent-teal); }
th.sort-desc::after { content: ' \u25BC'; opacity: 1; color: var(--accent-teal); }

tbody td { padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody tr:hover { background: var(--bg-card-hover); }

.td-name { max-width: 200px; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.td-mono { font-family: var(--font-mono); font-size: .8rem; }
.td-right { text-align: right; }

.td-seg {
    font-size: .65rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: .04em; padding: 2px 8px; border-radius: 4px; display: inline-block;
}

.seg-grande { background: rgba(88,166,255,.12); color: var(--accent-blue); }
.seg-cooperativa { background: rgba(94,234,212,.12); color: var(--accent-teal); }
.seg-outro { background: rgba(210,153,34,.12); color: var(--accent-yellow); }

.badge-positive { color: var(--accent-green); }
.badge-negative { color: var(--accent-red); }

/* ═══════════════════════════════════════
   MAP LEGEND
   ═══════════════════════════════════════ */

/* ─── MAPA ──────────────────────────────── */

.geo-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.geo-left { display: flex; flex-direction: column; gap: 12px; }
.geo-right { display: flex; flex-direction: column; }

.geo-map-card { padding: 16px; overflow: hidden; }
.geo-table-card { padding: 16px; }
.geo-table { font-size: .78rem; }
.geo-table td { padding: 6px 8px; }
.geo-table th { padding: 6px 8px; }
.td-muted { color: var(--text-muted); font-size: .72rem; }

.geo-filters {
    display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px;
}
.geo-reg-btn {
    background: none; border: 1px solid var(--border); color: var(--text-muted);
    font-family: var(--font-sans); font-size: .7rem; font-weight: 600;
    padding: 4px 12px; border-radius: 14px; cursor: pointer; transition: all .15s;
}
.geo-reg-btn:hover { border-color: var(--accent-teal); color: var(--text-primary); }
.geo-reg-btn.active { background: var(--accent-teal); color: var(--bg-page); border-color: var(--accent-teal); }

.geo-pager {
    display: flex; align-items: center; gap: 4px; margin-top: 12px; padding-top: 10px;
    border-top: 1px solid var(--border);
}
.geo-page-btn {
    background: none; border: 1px solid var(--border); color: var(--text-muted);
    font-family: var(--font-mono); font-size: .72rem; font-weight: 600;
    width: 28px; height: 28px; border-radius: 6px; cursor: pointer; transition: all .15s;
}
.geo-page-btn:hover { border-color: var(--accent-teal); color: var(--text-primary); }
.geo-page-btn.active { background: var(--accent-teal); color: var(--bg-page); border-color: var(--accent-teal); }

#mapa-container { position: relative; width: 100%; min-height: 380px; }
#mapa-container svg { display: block; width: 100%; }

.map-tooltip {
    position: absolute; background: var(--bg-page); border: 1px solid var(--border);
    padding: 12px 16px; border-radius: 10px; color: var(--text-primary);
    pointer-events: none; z-index: 200;
    box-shadow: 0 8px 28px rgba(0,0,0,.5); line-height: 1.4;
    white-space: nowrap;
}
.mtt-uf { font-size: 1.1rem; font-weight: 800; color: var(--accent-teal); display: inline; }
.mtt-nome { font-size: .78rem; color: var(--text-muted); display: inline; margin-left: 6px; }
.mtt-row { display: flex; justify-content: space-between; gap: 20px; font-size: .78rem; padding: 3px 0; border-top: 1px solid var(--border); }
.mtt-row span { color: var(--text-secondary); white-space: nowrap; }
.mtt-row strong { font-family: var(--font-mono); font-size: .76rem; color: var(--text-primary); white-space: nowrap; }

.map-scale {
    display: flex; align-items: center; gap: 10px; padding: 14px 20px 0;
    font-size: .72rem; color: var(--text-muted); max-width: 400px; margin: 0 auto;
}
.map-scale-bar {
    flex: 1; height: 6px; border-radius: 3px;
    background: linear-gradient(to right, #162032, #5eead4);
}

/* ═══════════════════════════════════════
   FOOTER
   ═══════════════════════════════════════ */

.footer {
    border-top: 1px solid var(--border); padding: 18px 24px;
    text-align: center; font-size: .75rem; color: var(--text-muted); line-height: 1.8;
}

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

/* ═══════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════ */

@media (max-width: 768px) {
    .kpi-grid { grid-template-columns: repeat(2, 1fr); padding: 16px; }
    .kpi-value { font-size: 1.2rem; }
    .header { padding: 10px 16px; }
    .section { padding: 16px; }
    .chart-container { height: 300px; }
    .chart-container-sm { height: 240px; }
    .grid-2 { grid-template-columns: 1fr; }
    .geo-layout { grid-template-columns: 1fr; }
    .geo-table-card { max-height: none; }
    .chart-container-tall { height: 550px; }
    .nav { padding: 6px 16px; }
    .filter-toolbar { padding: 10px 16px; gap: 12px; }
    .filter-select { min-width: 130px; }
}

@media (max-width: 480px) {
    .kpi-grid { grid-template-columns: 1fr; }
    .nav-btn { font-size: .72rem; padding: 6px 10px; }
    .filter-toolbar { flex-direction: column; align-items: stretch; }
    .filter-select { width: 100%; }
    .view-toggle { padding: 8px 16px; }
    .view-btn { padding: 6px 14px; font-size: .72rem; }
}

/* Tooltip overflow fix */
.info-tip { position: relative; }
@media (max-width: 768px) {
    .info-box { left: auto; right: 0; transform: none; }
}
