:root {
    --bg-void: #060d1a;
    --bg-deep: #0f172a;
    --bg-panel: rgba(10, 20, 40, 0.72);
    --border-ice: rgba(148, 163, 184, 0.12);
    --text-dim: #64748b;
    --text-soft: #94a3b8;
    --text-base: #cbd5e1;
    --text-hi: #f1f5f9;
    --neon-safe: #10ffb0;
    --neon-warn: #ffea00;
    --neon-high: #ff8c00;
    --neon-crit: #ff4060;
    --accent: #38bdf8;
    --accent-dim: rgba(56, 189, 248, 0.15);
}

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

body {
    font-family: 'DM Mono', monospace;
    background: var(--bg-void);
    color: var(--text-base);
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

/* ─── MAP ─── */
#map {
    position: fixed;
    inset: 0;
    z-index: 0;
}

/* ─── OVERLAYS ─── */
body::before {
    content: '';
    position: fixed;
    inset: 0;
    z-index: 1;
    pointer-events: none;
    background: repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0, 0, 0, 0.04) 2px, rgba(0, 0, 0, 0.04) 4px);
}

/* ─── TOP BAR ─── */
#topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 20;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    background: linear-gradient(180deg, rgba(6, 13, 26, 0.9) 0%, transparent 100%);
    backdrop-filter: blur(8px);
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 16px;
    min-width: 0;
}

.topbar-divider {
    width: 1px;
    height: 20px;
    background: var(--border-ice);
    flex: 0 0 auto;
}

.topbar-tagline {
    font-size: 0.65rem;
    color: var(--text-dim);
    letter-spacing: 0.06em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.logo-text {
    font-family: 'Syne', sans-serif;
    font-weight: 800;
    font-size: 1.25rem;
    color: var(--text-hi);
    white-space: nowrap;
    flex: 0 0 auto;
}

.logo-text span {
    color: var(--neon-safe);
}

.status-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(16, 255, 176, 0.08);
    border: 1px solid rgba(16, 255, 176, 0.25);
    border-radius: 99px;
    padding: 4px 12px;
    font-size: 0.68rem;
    color: var(--neon-safe);
    white-space: nowrap;
    flex: 0 0 auto;
}

.status-text-short {
    display: none;
}

.pulse-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--neon-safe);
    animation: pulse-glow 1.6s ease-in-out infinite;
}

@keyframes pulse-glow {

    0%,
    100% {
        opacity: 1;
        box-shadow: 0 0 0 0 rgba(16, 255, 176, 0.5);
    }

    50% {
        opacity: 0.6;
        box-shadow: 0 0 0 5px rgba(16, 255, 176, 0);
    }
}

/* ─── MAIN PANEL (desktop) ─── */
#analytics-panel {
    position: fixed;
    top: 72px;
    right: 16px;
    bottom: 16px;
    z-index: 20;
    width: 340px;
    background: var(--bg-panel);
    border: 1px solid var(--border-ice);
    border-radius: 18px;
    backdrop-filter: blur(24px) saturate(1.4);
    -webkit-backdrop-filter: blur(24px) saturate(1.4);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.05);

    transform: translateX(400px);
    opacity: 0;
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.3s ease-out;
}

#analytics-panel.open {
    transform: translateX(0);
    opacity: 1;
}

/* Handle (visibile soprattutto su mobile) */
.panel-handle {
    height: 28px;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    user-select: none;
}

.panel-handle:active {
    cursor: grabbing;
}

.panel-handle-bar {
    width: 48px;
    height: 6px;
    border-radius: 99px;
    background: rgba(255, 255, 255, 0.25);
}

.sheet-backdrop {
    display: none;
}

/* ─── MOBILE: Bottom sheet trascinabile ─── */
@media (max-width: 640px) {
    #topbar {
        padding: 0 12px;
    }

    .topbar-left {
        gap: 10px;
    }

    .logo-text {
        font-size: 1.1rem;
    }

    .topbar-divider {
        display: none;
    }

    .topbar-tagline {
        display: none;
    }

    .status-pill {
        padding: 4px 10px;
        font-size: 0.62rem;
        max-width: 40vw;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .status-text-full {
        display: none;
    }

    .status-text-short {
        display: inline;
    }

    #analytics-panel {
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;

        /* Altezza massima raggiungibile con drag manuale */
        height: 85vh;
        max-height: 85vh;

        border-radius: 18px 18px 0 0;
        border-bottom: none;

        /* sheet: controllato da JS */
        --sheet-translate-y: 100%;
        transform: translate3d(0, var(--sheet-translate-y), 0);

        opacity: 1;
        transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
        pointer-events: none;
    }

    .sheet-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.6);
        z-index: 19;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.35s ease;
    }

    .sheet-backdrop.active {
        pointer-events: auto;
    }

    #analytics-panel.open,
    #analytics-panel.collapsed,
    #analytics-panel.expanded {
        transform: translate3d(0, var(--sheet-translate-y), 0);
        pointer-events: auto;
    }

    /* lascia visibile solo “testa + maniglia” (fallback CSS) */
    #analytics-panel.collapsed {
        --sheet-translate-y: calc(100% - 96px);
    }

    #analytics-panel.dragging {
        transition: none !important;
    }

    .panel-handle,
    .panel-header {
        touch-action: none;
    }

    /* Nascondi elementi non presenti */
    #trail-selector {
        display: none !important;
    }

    #coord-bar {
        display: none !important;
    }
}

/* Classe CSS da applicare temporaneamente per la demo */
.map-clean-mode .leaflet-tile {
    filter: grayscale(1) opacity(0.15) !important;
}

.map-clean-mode .leaflet-overlay-pane {
    opacity: 1 !important;
}

.panel-header {
    padding: 16px 18px 12px;
    border-bottom: 1px solid var(--border-ice);
    flex-shrink: 0;
    user-select: none;
}

.panel-title {
    font-family: 'Syne', sans-serif;
    font-weight: 700;
    font-size: 0.78rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-dim);
}

.trail-name {
    font-family: 'Syne', sans-serif;
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text-hi);
    margin-top: 4px;
}

/* ─── RISK SCORE ─── */
.risk-section {
    padding: 18px 18px 14px;
    border-bottom: 1px solid var(--border-ice);
    display: flex;
    align-items: center;
    gap: 18px;
    flex-shrink: 0;
}

.risk-ring-wrap {
    position: relative;
    flex-shrink: 0;
}

.risk-ring-svg {
    transform: rotate(-90deg);
}

.ring-bg {
    fill: none;
    stroke: rgba(255, 255, 255, 0.05);
    stroke-width: 6;
}

.ring-fill {
    fill: none;
    stroke-width: 6;
    stroke-linecap: round;
    transition: stroke-dashoffset 1.2s cubic-bezier(0.34, 1.56, 0.64, 1), stroke 0.6s ease;
}

.risk-value {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.risk-num {
    font-family: 'Syne', sans-serif;
    font-weight: 800;
    font-size: 1.5rem;
    line-height: 1;
    transition: color 0.6s;
}

.risk-unit {
    font-size: 0.55rem;
    color: var(--text-dim);
}

.risk-details {
    flex: 1;
}

.risk-label {
    font-family: 'Syne', sans-serif;
    font-weight: 700;
    font-size: 1.1rem;
    line-height: 1;
    transition: color 0.6s;
}

.risk-sub {
    font-size: 0.65rem;
    color: var(--text-dim);
    margin-top: 4px;
}

.risk-bar-track {
    height: 4px;
    background: rgba(255, 255, 255, 0.06);
    border-radius: 99px;
    margin-top: 10px;
}

.risk-bar-fill {
    height: 100%;
    border-radius: 99px;
    transition: width 1.2s, background 0.6s;
}

/* ─── DATA CARDS ─── */
.cards-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 12px 18px 16px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
}

.cards-scroll::-webkit-scrollbar {
    display: none;
}

.section-label {
    font-size: 0.58rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--text-dim);
    margin-bottom: 8px;
}

.data-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-ice);
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 8px;
    position: relative;
    overflow: hidden;
}

.data-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 3px;
    height: 100%;
    border-radius: 99px 0 0 99px;
}

.card-accent-red::before {
    background: var(--neon-crit);
}

.card-accent-blue::before {
    background: var(--accent);
}

.card-accent-green::before {
    background: var(--neon-safe);
}

.card-label {
    font-size: 0.62rem;
    color: var(--text-dim);
    text-transform: uppercase;
}

.card-value {
    font-family: 'Syne', sans-serif;
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text-hi);
    margin-top: 2px;
}

.card-sub {
    font-size: 0.65rem;
    color: var(--text-soft);
    margin-top: 3px;
}

.badge {
    display: inline-flex;
    padding: 3px 8px;
    border-radius: 6px;
    font-size: 0.6rem;
    font-weight: 500;
    margin-top: 6px;
}

.badge-red {
    background: rgba(255, 64, 96, 0.15);
    color: var(--neon-crit);
    border: 1px solid rgba(255, 64, 96, 0.3);
}

.badge-blue {
    background: rgba(56, 189, 248, 0.12);
    color: var(--accent);
    border: 1px solid rgba(56, 189, 248, 0.25);
}

.badge-green {
    background: rgba(16, 255, 176, 0.1);
    color: var(--neon-safe);
    border: 1px solid rgba(16, 255, 176, 0.25);
}

/* ─── LOADING OVERLAY ─── */
#loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(6, 13, 26, 0.88);
    backdrop-filter: blur(8px);
    z-index: 30;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    transition: opacity 0.4s;
}

#loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.loader-ring {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 2px solid rgba(56, 189, 248, 0.12);
    border-top: 2px solid var(--accent);
    animation: spin 1s linear infinite;
}

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

.load-text {
    font-size: 0.7rem;
    color: var(--text-soft);
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

/* ─── INSTRUCTION OVERLAY ─── */
#instruction {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 10;
    font-family: 'Syne';
    font-size: 1.2rem;
    color: rgba(255, 255, 255, 0.5);
    pointer-events: none;
    text-align: center;
}

.leaflet-control-zoom {
    transform: scale(1.15);
    transform-origin: top left;
    margin-top: 10px !important;
}

.leaflet-control-container {
    z-index: 5 !important;
}

.leaflet-control-attribution {
    display: none;
}

.leaflet-top.leaflet-left {
    top: 60px;
}

/* Stile Custom Marker Critico */
.critical-pulse {
    width: 24px;
    height: 24px;
    background: rgba(255, 64, 96, 0.4);
    border-radius: 50%;
    animation: pulse-crit 1.5s infinite;
    display: flex;
    align-items: center;
    justify-content: center;
}

.critical-core {
    width: 12px;
    height: 12px;
    background: #ff4060;
    border-radius: 50%;
    border: 2px solid #fff;
}

@keyframes pulse-crit {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    100% {
        transform: scale(2.5);
        opacity: 0;
    }
}