/* =========================================================
   VARIABILI TEMA
   ========================================================= */
:root {
    --bg: #edf2f7;
    --panel: rgba(255, 255, 255, 0.85);
    --text: #1a202c;
    --muted: #4a5568;
    --border: rgba(0, 0, 0, 0.08);
    --shadow: 0 10px 35px rgba(15, 23, 42, 0.08);
    --radius-lg: 1.2rem;
    --tooltip-bg: rgba(15, 23, 42, 0.9);
    --tooltip-text: #f7fafc;
    --accent: #6366f1;
}

body[data-theme='dark'] {
    --bg: radial-gradient(circle at 10% 20%, #1f2937 0%, #0f172a 50%, #020617 100%);
    --panel: rgba(15, 23, 42, 0.5);
    --text: #e2e8f0;
    --muted: #a0aec0;
    --border: rgba(226, 232, 240, 0.1);
    --shadow: 0 14px 40px rgba(0, 0, 0, 0.5);
    --tooltip-bg: rgba(15, 23, 42, 0.9);
    --tooltip-text: #e2e8f0;
    --accent: #a855f7;
}

/* =========================================================
   BODY E LAYOUT BASE
   ========================================================= */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    background: var(--bg);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
}

#app {
    width: 100%;
    display: flex;
    justify-content: center;
}

/* =========================================================
   CARD PRINCIPALE
   ========================================================= */
#container {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    width: min(100%, 1220px);
    margin: 0 1.1rem;
    padding: 1.1rem 1.3rem 1.4rem;
    backdrop-filter: blur(14px);
    position: relative;
}

.header-block {
    text-align: center;
    margin-bottom: 1.2rem;
}

#title {
    font-size: clamp(1.5rem, 2.1vw, 2rem);
    font-weight: 700;
    margin: 0;
}

#description {
    margin: 0.4rem 0 0;
    color: var(--muted);
    font-size: 0.9rem;
}

/* =========================================================
   TOGGLE TEMA
   ========================================================= */
#theme-toggle {
    position: fixed;
    top: 14px;
    right: 18px;
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    user-select: none;
    color: var(--text);
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 0;
    transition: transform 0.1s ease, opacity 0.2s ease;
    opacity: 0.75;
    z-index: 50;
}

.theme-toggle:hover {
    opacity: 1;
    transform: scale(1.1);
}

.theme-toggle:focus {
    outline: none;
}

/* =========================================================
   DATASET SWITCH
   ========================================================= */
#dataset-switch {
    display: inline-flex;
    gap: 0.35rem;
    margin-top: 0.75rem;
    flex-wrap: wrap;
    justify-content: center;
}

.ds-btn {
    border: 1px solid rgba(99, 102, 241, 0.1);
    background: rgba(99, 102, 241, 0.12);
    color: inherit;
    padding: 0.25rem 0.65rem 0.35rem;
    border-radius: 999px;
    font-size: 0.65rem;
    cursor: pointer;
    transition: 0.15s ease;
}

.ds-btn:hover {
    filter: brightness(1.03);
}

.ds-btn:focus-visible {
    outline: 2px solid rgba(99, 102, 241, 0.35);
    outline-offset: 2px;
}

/* stato attivo – LIGHT */
body[data-theme='light'] .ds-btn.active {
    background: var(--accent);
    color: #fff;
    border-color: transparent;
    box-shadow: 0 6px 18px rgba(99, 102, 241, 0.35);
}

/* stato base – DARK */
body[data-theme='dark'] .ds-btn {
    background: rgba(168, 85, 247, 0.16);
    border-color: rgba(168, 85, 247, 0.15);
    color: #e2e8f0;
}

/* stato attivo – DARK (più evidente) */
body[data-theme='dark'] .ds-btn.active {
    background: rgba(168, 85, 247, 0.65);
    border: 1px solid rgba(236, 252, 255, 0.9);
    color: #ffffff;
    box-shadow: 0 0 0 1px rgba(236, 252, 255, 0.45), 0 10px 28px rgba(0, 0, 0, 0.35);
}

/* focus dark */
body[data-theme='dark'] .ds-btn:focus-visible {
    outline: 2px solid rgba(236, 252, 255, 0.6);
    outline-offset: 2px;
}

/* =========================================================
   CONTENITORE GRAFICO (RESPONSIVE)
   ========================================================= */
#viz-wrapper {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    align-items: center;
}

#chart {
    width: 100%;
}

#chart svg {
    display: block;
    width: 100%;
    height: auto;
}

/* =========================================================
   TILE STYLING
   ========================================================= */
.tile {
    stroke: rgba(255, 255, 255, 0.5);
    stroke-width: 1;
    cursor: pointer;
    transition: opacity 0.16s ease, transform 0.16s ease;
}

.tile:hover {
    opacity: 0.9;
}

.tile-label {
    font-size: 0.58rem;
    pointer-events: none;
    fill: #0f172a;
}

body[data-theme='dark'] .tile-label {
    fill: #e2e8f0;
}

/* =========================================================
   TOOLTIP
   ========================================================= */
#tooltip {
    position: absolute;
    pointer-events: none;
    background: var(--tooltip-bg);
    color: var(--tooltip-text);
    padding: 0.4rem 0.5rem 0.5rem;
    border-radius: 0.5rem;
    font-size: 0.7rem;
    line-height: 1.2;
    min-width: 150px;
    z-index: 30;
    box-shadow: 0 10px 35px rgba(0, 0, 0, 0.25);
    z-index: 999;
}

#tooltip.hidden {
    display: none;
}

/* =========================================================
   LEGENDA
   ========================================================= */
#legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem 0.85rem;
    margin-top: 1.1rem;
    justify-content: center;
    align-items: center;
}

.legend-item {
    width: 14px;
    height: 14px;
    border-radius: 4px;
    margin-right: 0.35rem;
    flex: 0 0 14px;
}

.legend-entry {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.7rem;
    color: var(--muted);
    white-space: nowrap;
}


/* =========================================================
   RESPONSIVE
   ========================================================= */
@media (max-width: 820px) {
    #container {
        padding: 0.95rem 0.5rem 1.3rem;
    }

    #chart {
        max-width: 100%;
    }

    .tile-label {
        font-size: 0.5rem;
    }

    #legend {
        justify-content: flex-start;
    }

    #dataset-switch {
        justify-content: center;
    }
}