/* ===========================================================
    Projeto: Plano Diretor - Estilos Dinâmicos
    Arquivo: style.css (versão refinada)
    =========================================================== */

/* 1) Tokens e Tema
   ----------------------------------------------------------- */
:root {
    /* Cores base (tema claro) */
    --bg: #f9fafb;
    --panel: #ffffff;
    --ink: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --accent: #0b5;
    --warn: #b45309;
    --err: #b00020;
    --ring: rgba(37, 99, 235, .35);
    --border: rgba(0, 0, 0, .06);
    --shadow-light: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-medium: 0 4px 16px rgba(0, 0, 0, .08);
    --shadow-glass: 0 4px 20px rgba(16, 24, 40, .1), 0 2px 8px rgba(16, 24, 40, .08);

    /* Complementos */
    --card: #fff;
    --line: #e5e7eb;
    --ok: #16a34a;
    --alert: #dc2626;

    /* Vidro (dash/controles) */
    --glass-bg: rgba(255, 255, 255, .7);
    --glass-brd: rgba(255, 255, 255, .8);
    --glass-shadow: 0 10px 30px rgba(16, 24, 40, .12), 0 2px 8px rgba(16, 24, 40, .10);
    --glass-blur: 16px;

    /* Dash compacto */
    --dash-font: 13px;
    --dash-lh: 1.25;
    --dash-vpad: 6px;
    --dash-gap: 6px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0c0c0c;
        --panel: #1a1a1a;
        --ink: #eaeaea;
        --muted: #a0a0a0;
        --border: rgba(255, 255, 255, .12);
        --shadow-light: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
        --shadow-medium: 0 4px 16px rgba(0, 0, 0, .45);
        --shadow-glass: 0 4px 20px rgba(0, 0, 0, .35), 0 2px 8px rgba(0, 0, 0, .25);

        --ring: rgba(99, 102, 241, .35);
        --glass-bg: rgba(20, 24, 34, .45);
        --glass-brd: rgba(255, 255, 255, .08);
        --glass-shadow: 0 10px 28px rgba(0, 0, 0, .35);
    }
}

/* 2) Base HTML e Responsividade
   ----------------------------------------------------------- */
html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: var(--ink);
    font: 14px / 1.45 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
    transition: background 0.3s ease;
}

/* 3) Painel de Ações (sólido)
   ----------------------------------------------------------- */
#painelAcoes {
    position: absolute;
    z-index: 999;
    top: 65px;
    left: 10px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: var(--shadow-medium);
    padding: 12px;
    display: flex;
    gap: 12px;
    align-items: center;
    transition: all 0.3s ease;
}

#painelAcoes .upload,
#painelAcoes button {
    appearance: none;
    border: 1px solid rgba(0, 0, 0, .12);
    background: #fff;
    color: #111;
    border-radius: 12px;
    padding: 10px 16px;
    cursor: pointer;
    font-weight: 600;
    transition: transform .1s ease, box-shadow .2s ease, background .2s ease, border-color .2s ease;
}

#painelAcoes .upload:hover {
    background: #f1f5f9;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

#painelAcoes .upload:active {
    transform: translateY(2px);
    box-shadow: none;
}

#painelAcoes .btn-primary,
#painelAcoes button.btn-primary {
    background: var(--primary);
    color: #fff;
    border-color: transparent;
    transition: all .2s ease;
}

#painelAcoes .btn-primary:hover,
#painelAcoes button.btn-primary:hover {
    background: #1d4ed8;
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2);
}

#painelAcoes .btn-primary:active,
#painelAcoes button.btn-primary:active {
    transform: translateY(2px);
    background: #1e40af;
    box-shadow: none;
}

#painelAcoes button:focus {
    outline: none;
    box-shadow: 0 0 0 3px var(--ring);
}

/* 4) Mapa e Controles Leaflet
   ----------------------------------------------------------- */
#map {
    width: 100%;
    height: 100vh;
    position: relative;
    isolation: isolate;
}

.leaflet-top .leaflet-control {
    margin-top: 140px;
}

.leaflet-left .leaflet-control {
    margin-left: 10px;
}

.leaflet-control-container .leaflet-control {
    z-index: 1000;
}

.leaflet-bar,
.leaflet-bar a,
.leaflet-control-layers,
.leaflet-control-layers-toggle {
    border: none !important;
}

.leaflet-bar a,
.leaflet-control-layers-toggle {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.5));
    border: 1px solid var(--glass-brd);
    -webkit-backdrop-filter: blur(16px) saturate(120%);
    backdrop-filter: blur(16px) saturate(120%);
    border-radius: 12px !important;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #222;
    transition: all .2s ease;
    box-shadow: var(--shadow-glass);
}

.leaflet-bar a:hover,
.leaflet-control-layers-toggle:hover {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.7));
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(16, 24, 40, .15);
}

.leaflet-bar a:active,
.leaflet-control-layers-toggle:active {
    transform: translateY(1px);
    box-shadow: var(--shadow-glass);
}

.leaflet-bar a:first-child {
    border-radius: 12px 12px 0 0 !important;
}

.leaflet-bar a:last-child {
    border-radius: 0 0 12px 12px !important;
}

.leaflet-control-layers {
    border-radius: 16px;
    overflow: hidden;
    box-shadow: var(--shadow-glass);
    background: linear-gradient(180deg, var(--glass-bg), rgba(255, 255, 255, .50));
    border: 1px solid var(--glass-brd);
    -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(120%);
    backdrop-filter: blur(var(--glass-blur)) saturate(120%);
}

.leaflet-control-layers .leaflet-control-layers-list {
    transition: max-height .25s ease;
}

.leaflet-control-layers:not(.leaflet-control-layers-expanded) .leaflet-control-layers-list {
    max-height: 0;
    overflow: hidden;
}

.leaflet-control-layers.leaflet-control-layers-expanded .leaflet-control-layers-list {
    max-height: 420px;
}

/* 5) Dash de Avisos (flutuante)
   ----------------------------------------------------------- */
#consoleAvisos {
    position: absolute;
    left: 14px;
    bottom: 14px;
    z-index: 998;
    width: min(520px, calc(100vw - 28px));
    padding: 10px 12px;
    border-radius: 16px;
    background: linear-gradient(180deg, var(--glass-bg), rgba(255, 255, 255, .50));
    border: 1px solid var(--glass-brd);
    box-shadow: var(--shadow-glass);
    -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(120%);
    backdrop-filter: blur(var(--glass-blur)) saturate(120%);
}

#consoleAvisos > div {
    display: block;
    max-height: calc(((var(--dash-font) * var(--dash-lh)) + (var(--dash-vpad) * 2)) * 3 + (var(--dash-gap) * 4));
    overflow-y: auto;
    padding-right: 2px;
    scrollbar-width: thin;
    scrollbar-color: rgba(17, 24, 39, .22) transparent;
}

#consoleAvisos > div::-webkit-scrollbar {
    width: 8px;
}

#consoleAvisos > div::-webkit-scrollbar-thumb {
    background: rgba(17, 24, 39, .12);
    border-radius: 6px;
}

#consoleAvisos > div:hover::-webkit-scrollbar-thumb {
    background: rgba(17, 24, 39, .22);
}

.msg {
    font-size: var(--dash-font);
    line-height: var(--dash-lh);
    padding: var(--dash-vpad) 10px;
    margin: var(--dash-gap) 0;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: #f9fafb80;
    color: #344054;
    box-shadow: 0 1px 0 rgba(16, 24, 40, .03) inset;
}

.msg.ok {
    background: rgba(16, 185, 129, .12);
    border-color: color-mix(in srgb, #10b981 30%, #0000);
}

.msg.warn {
    background: rgba(250, 176, 5, .12);
    border-color: color-mix(in srgb, #f59e0b 30%, #0000);
}

.msg.err {
    background: rgba(239, 68, 68, .12);
    border-color: color-mix(in srgb, #ef4444 30%, #0000);
}

/* 6) Leaflet.Draw - Ícones SVG Inline
   ----------------------------------------------------------- */
.leaflet-draw-toolbar a {
    background-image: none !important;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.leaflet-draw-toolbar a::before {
    content: "";
    display: block;
    width: 18px;
    height: 18px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    opacity: .95;
    transition: transform .08s ease, opacity .15s ease;
}

.leaflet-draw-toolbar a:hover::before {
    opacity: 1;
    transform: translateY(-1px);
}

.leaflet-draw-toolbar a:active::before {
    transform: translateY(0);
}

/* Polígono */
.leaflet-draw-draw-polygon::before {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"><path d="M8 3h8l5 5v8l-5 5H8l-5-5V8l5-5z" fill="%232563eb" fill-opacity="0.12" stroke="%232563eb" stroke-width="1.6"/><circle cx="8" cy="3" r="1.5" fill="%232563eb"/><circle cx="16" cy="3" r="1.5" fill="%232563eb"/><circle cx="21" cy="8" r="1.5" fill="%232563eb"/><circle cx="21" cy="16" r="1.5" fill="%232563eb"/><circle cx="16" cy="21" r="1.5" fill="%232563eb"/><circle cx="8" cy="21" r="1.5" fill="%232563eb"/><circle cx="3" cy="16" r="1.5" fill="%232563eb"/><circle cx="3" cy="8" r="1.5" fill="%232563eb"/></svg>');
}

/* Editar */
.leaflet-draw-edit-edit::before {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"><path d="M3 17.25V21h3.75L19.81 7.94l-3.75-3.75L3 17.25z" fill="%232563eb" fill-opacity="0.12" stroke="%232563eb" stroke-width="1.4"/><path d="M14.06 4.19l3.75 3.75" stroke="%232563eb" stroke-width="1.8" stroke-linecap="round"/></svg>');
}

/* Excluir */
.leaflet-draw-edit-remove::before {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"><path d="M6 7h12l-1 13H7L6 7z" fill="%23ef4444" fill-opacity="0.12" stroke="%23ef4444" stroke-width="1.4"/><path d="M9 7V5a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v2" stroke="%23222" stroke-width="1.4" fill="none"/><path d="M4 7h16" stroke="%23222" stroke-width="1.4"/><path d="M10 10v7M14 10v7" stroke="%23ef4444" stroke-width="1.6" stroke-linecap="round"/></svg>');
}

/* Estado ativo/selecionado */
.leaflet-draw-toolbar .leaflet-draw-toolbar-button-enabled,
.leaflet-draw-toolbar a.leaflet-disabled:focus {
    box-shadow: inset 0 0 0 2px rgba(37, 99, 235, .35);
}

/* 7) Estilos Reutilizáveis (Cards, Tabelas, etc.)
   ----------------------------------------------------------- */
.container {
    max-width: 1200px;
    margin: 24px auto;
    padding: 0 16px
}

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

.header h1 {
    margin: 0;
    font-size: 22px;
    font-weight: 700
}

.subtle {
    color: var(--muted);
    font-size: 14px
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 12px 0
}

.btn {
    border: 1px solid var(--line);
    background: #fff;
    padding: 8px 12px;
    border-radius: 10px;
    cursor: pointer
}

.btn.primary {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff
}

.btn:disabled {
    opacity: .5;
    cursor: not-allowed
}

.btn:focus {
    outline: 2px solid var(--primary);
    outline-offset: 2px
}

.map-wrap {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    overflow: hidden
}

#map+.map-note {
    padding: 8px 12px;
    border-top: 1px solid var(--line);
    font-size: 13px;
    color: var(--muted)
}

.map-snapshot {
    display: none;
    max-width: 100%
}

.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .06);
    padding: 16px;
    margin: 12px 0
}

.card h3 {
    margin: 0 0 6px 0;
    font-size: 18px
}

.meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 6px 0 10px
}

.badge {
    font-size: 12px;
    padding: 4px 8px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--muted)
}

.badge.ok {
    color: #065f46;
    border-color: #a7f3d0;
    background: #ecfdf5
}

.badge.warn {
    color: #92400e;
    border-color: #fde68a;
    background: #fffbeb
}

.badge.alert {
    color: #991b1b;
    border-color: #fecaca;
    background: #fef2f2
}

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

.legend-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: #fff;
    font-size: 13px;
    color: var(--ink)
}

.legend-chip .swatch {
    width: 28px;
    height: 18px;
    border-radius: 6px;
    border: 1px solid rgba(0, 0, 0, .2);
    background: repeating-linear-gradient(45deg, var(--swatch, #999) 0 6px, #ffffff 6px 12px)
}

.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px
}

.table th,
.table td {
    border-bottom: 1px solid var(--line);
    padding: 10px
}

.table th {
    position: sticky;
    top: 0;
    background: #fff;
    font-weight: 600
}

.num {
    text-align: right
}

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

@media (max-width:1024px) {
    .grid {
        grid-template-columns: 1fr
    }
}

.section {
    overflow: visible;
    max-height: unset
}

@media print {
    .toolbar {
        display: none !important
    }

    #map {
        display: none
    }

    .map-snapshot {
        display: block;
        margin: 0;
        border: 1px solid var(--line);
        border-radius: 6px
    }

    .container {
        max-width: 100%;
        margin: 0;
        padding: 0 10mm
    }

    .card {
        page-break-inside: avoid
    }
}

/* 8) Estilos Específicos da Página de Análise
   ----------------------------------------------------------- */
.page-analise .grid {
    grid-template-columns: 1fr !important;
    grid-auto-flow: row !important;
}

.page-analise .grid>* {
    grid-column: 1 / -1 !important;
}

.page-analise #map {
    min-height: calc(100vh - 180px) !important;
}

.page-analise .map-wrap {
    border-radius: 18px;
}

.page-analise .legend {
    gap: 12px;
}

.page-analise .legend-chip {
    padding: 10px 14px;
    border-radius: 14px;
    border: 1px solid var(--line);
    min-width: 160px;
    height: 40px;
    align-items: center;
}

.page-analise .legend-chip .swatch {
    width: 36px;
    height: 22px;
    border-radius: 8px;
    border: 1px solid rgba(0, 0, 0, .2);
    background: repeating-linear-gradient(45deg, var(--swatch, #999) 0 8px, #fff 8px 16px);
}

.page-analise .legend-chip span {
    font-size: 14px;
    font-weight: 600;
}

/* 9) Responsividade e Acessibilidade Finais
   ----------------------------------------------------------- */
@media (max-width: 520px) {
    #painelAcoes {
        left: 10px;
        right: 10px;
        top: 62px;
        flex-wrap: wrap;
    }

    .leaflet-top .leaflet-control {
        margin-top: 140px;
    }

    #consoleAvisos {
        left: 10px;
        right: 10px;
        width: auto;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
        animation: none !important;
    }
}
