/* style.css */

/* --- Stili Preloader --- */
#preloader {
    position: fixed; left: 0; top: 0; width: 100vw; height: 100vh;
    background-color: #333; z-index: 9999; display: flex;
    justify-content: center; align-items: center; opacity: 1; visibility: visible;
    transition: opacity 0.5s ease-out, visibility 0s linear 0.5s;
}
#preloader.hidden { opacity: 0; visibility: hidden; transition: opacity 0.5s ease-out, visibility 0s linear 0.5s; }
.preloader-content { text-align: center; color: white; }
.preloader-content i { display: block; margin-bottom: 15px; }
.preloader-content p { font-size: 1.2em; margin: 0; }
/* --- Fine Stili Preloader --- */


html, body {
    margin: 0; padding: 0; width: 100vw; height: 100vh; /* Consider using 100% instead of 100vh if mobile viewport issues arise */
    overflow: hidden; font-family: sans-serif; background-color: #333; position: relative;
    /* Improve text rendering */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
.canvas-container { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; display: flex; justify-content: center; align-items: center; }
#mapCanvas { display: block; width: 100%; height: 100%; cursor: grab; background-color: #00539b; touch-action: none; /* Disable default touch actions like scrolling/zooming handled by JS */ }
#mapCanvas:active { cursor: grabbing; }

h1 {
    position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 10;
    margin: 0; padding: 5px 15px; background-color: rgba(0, 0, 0, 0.7); /* Slightly more opaque */
    color: white; border-radius: 5px; font-size: 1.2em; text-align: center;
    max-width: calc(100vw - 120px); /* Prevent overlap with corner buttons */
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; /* Handle long titles */
}

.toggle-button {
    position: absolute; z-index: 11; padding: 8px 10px; cursor: pointer;
    background-color: rgba(0, 0, 0, 0.6); color: white; border: none; border-radius: 5px;
    font-size: 1.1em; line-height: 1; transition: background-color 0.2s ease;
    /* Ensure minimum tap area */
    min-width: 40px;
    min-height: 40px;
    display: inline-flex; /* Align icon nicely */
    justify-content: center;
    align-items: center;
}
.toggle-button:hover { background-color: rgba(0, 0, 0, 0.8); }
.toggle-button.top-left-toggle { top: 10px; left: 10px; }
.toggle-button.top-right-toggle { top: 10px; right: 10px; }

.controls {
    position: absolute; z-index: 10; background-color: rgba(255, 255, 255, 0.9); /* More opaque */
    padding: 15px; /* Increased padding */
    border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.3); /* Stronger shadow */
    display: flex; gap: 10px; /* Increased gap */
    transition: transform 0.4s ease-in-out, opacity 0.3s ease-in-out; /* Added opacity transition */
    width: 280px; max-height: calc(100vh - 80px); /* Limit height */
    overflow-y: auto; /* Enable scroll */
    flex-direction: column; align-items: stretch; box-sizing: border-box;
    /* Hide scrollbar visually but keep functionality */
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;  /* Firefox */
}
.controls::-webkit-scrollbar { display: none; /* Chrome, Safari, Opera */ }

.controls.left-panel { top: 60px; left: 10px; transform: translateX(0); max-height: calc(100vh - 130px); } /* Adjusted top/max-height */
.controls.left-panel.collapsed { transform: translateX(calc(-100% - 20px)); opacity: 0; }
.controls.left-panel h2 { margin-top: 0; font-size: 1.1em; text-align: center; border-bottom: 1px solid #ccc; padding-bottom: 8px; margin-bottom: 12px; }
.controls.left-panel p { font-size: 0.9em; color: #333; margin: 6px 0; }

.controls.top-controls { top: 60px; right: 10px; transform: translateX(0); max-height: calc(100vh - 130px); } /* Adjusted top/max-height */
.controls.top-controls.collapsed { transform: translateX(calc(100% + 20px)); opacity: 0; }

/* Control Elements Styling */
.controls label {
    display: flex; align-items: center; justify-content: space-between; gap: 8px; /* Increased gap */
    font-size: 0.95em; /* Slightly larger font */
    background-color: rgba(240, 240, 240, 0.9); padding: 8px 10px; /* Increased padding */
    border-radius: 4px; width: 100%; box-sizing: border-box; flex-wrap: wrap; /* Allow wrapping on small controls */
}
.controls label > *:first-child:not(span.range-container):not(input):not(select):not(span.seed-container) {
    white-space: nowrap; flex-shrink: 0; margin-right: 5px; font-weight: bold; /* Make label text bold */
}
.controls span:not(.range-container span):not(.seed-container span) { /* Target value spans */
    font-weight: normal; /* Value spans normal weight */
    min-width: 40px; /* Ensure space for values */
    text-align: right;
    background-color: #fff; /* Background for value */
    padding: 2px 4px;
    border-radius: 3px;
    font-size: 0.9em;
}
.controls input[type="number"] { width: 60px; padding: 4px; } /* Slightly wider */
.controls input[type="range"] { flex-grow: 1; min-width: 80px; cursor: pointer; }
.controls input[type="checkbox"] { width: 18px; height: 18px; /* Larger checkboxes */ margin-left: 10px; flex-shrink: 0; cursor: pointer; }

.range-container { display: flex; align-items: center; gap: 8px; flex-grow: 1; min-width: 0; }
.range-container input[type="range"] { flex-grow: 1; min-width: 50px; }
.range-container span { /* Value display next to range */
    flex-shrink: 0;
    min-width: 35px; /* Ensure space for range value */
    text-align: right;
    background-color: #fff; padding: 2px 4px; border-radius: 3px; font-size: 0.9em;
}

.seed-container { /* Container for seed input and button */
    display: flex; flex-grow: 1; gap: 5px; align-items: center;
}
.seed-container input[type="text"] {
    flex-grow: 1; padding: 4px; min-width: 50px; /* Allow shrinking */
}
.seed-container button {
    padding: 4px 8px; flex-shrink: 0; line-height: 1; /* Align icon */
    font-size: 0.9em;
}

.controls button:not(.seed-container button):not(.toggle-button) { /* Style main buttons */
    padding: 10px 15px; cursor: pointer; font-size: 1em;
    background-color: #e9ecef; border: 1px solid #ced4da; border-radius: 4px;
    transition: background-color 0.2s ease;
}
.controls button:not(.seed-container button):not(.toggle-button):hover {
    background-color: #dee2e6;
}
#generateBtn { /* Make generate button stand out */
    background-color: #007bff; color: white; border-color: #007bff;
}
#generateBtn:hover { background-color: #0056b3; border-color: #0056b3; }


.controls label.checkbox-label { justify-content: flex-start; cursor: pointer; } /* Make whole label clickable */
.controls label.checkbox-label input[type="checkbox"] { margin-left: 0; order: 1; margin-right: 10px;} /* Checkbox first */
.controls label.checkbox-label > *:first-child { order: 0; margin-right: auto;} /* Text takes remaining space */

.controls label select { flex-grow: 1; min-width: 100px; padding: 6px; cursor: pointer; } /* Improved select padding */


/* Stili Controlli Inferiori (Zoom/Reset) */
.controls.bottom-controls {
    position: absolute; bottom: 10px; left: 10px; z-index: 10;
    background-color: rgba(255, 255, 255, 0.9); padding: 8px; /* Slightly more padding */
    border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    display: flex; flex-direction: row; gap: 8px; /* Increased gap */
    align-items: center;
}
.controls.bottom-controls button {
    padding: 6px 10px; /* More padding */
    font-size: 1em; /* Slightly larger font */
    min-width: 35px; /* Minimum tap width */
    min-height: 35px; /* Minimum tap height */
    cursor: pointer; line-height: 1.2; flex-shrink: 0;
    display: inline-flex; justify-content: center; align-items: center; /* Center content */
    background-color: #e9ecef; border: 1px solid #ced4da; border-radius: 4px;
    transition: background-color 0.2s ease;
}
.controls.bottom-controls button:hover {
     background-color: #dee2e6;
}
/* No specific style needed for reset button unless it wraps */


/* Stile per le immagini nel pannello */
.capital-detail-image, .state-detail-image {
    display: block; max-width: 100%; height: auto; margin-top: 10px;
    margin-bottom: 10px; border-radius: 4px; border: 1px solid #ccc; background-color: #eee;
}

#offscreenCanvas { position: absolute; left: -9999px; top: -9999px; visibility: hidden; }


/* Stili Footer Modificati */
footer {
    width: 100%; background-color: transparent; color: #aaa;
    padding: 5px 10px; /* Reduced padding */
    font-size: 0.8em; text-align: center;
    position: absolute; bottom: 0; left: 0; z-index: 5;
    box-sizing: border-box;
}
footer p { margin: 0; padding: 0; }
footer a {
    color: #ccc; text-decoration: underline; cursor: pointer;
    margin: 0 8px; transition: color 0.2s ease;
    /* Add padding for better touch target */
    padding: 5px 0;
}
footer a:hover { color: #fff; text-decoration: none; }


/* Stili per precaricare FontAwesome */
body::before {
    content: '\f21c\f100\f101\f110\f013\f447\f554\f083\f521\f024\f0b2\f00d\f2f1\f500'; /* Added fa-angles-left/right, fa-times, seed, dice */
    font-family: "Font Awesome 6 Free"; font-weight: 900; position: absolute;
    left: -9999px; top: -9999px; visibility: hidden; display: none; /* Ensure it doesn't affect layout */
}

/* Stili per Info Mondo nel Pannello Sinistro */
#worldInfo {
    padding: 10px; margin-bottom: 15px; background-color: rgba(0,0,0, 0.05);
    border-radius: 4px; border: 1px solid #ccc;
}
#worldInfo h3 {
    margin-top: 0; margin-bottom: 8px; font-size: 1.05em; /* Slightly smaller */
    text-align: center;
}
#worldInfo p {
    font-size: 0.9em; color: #333; margin: 5px 0; line-height: 1.4;
}
#worldInfo p strong { color: #111; }
#worldInfo span#currentSeedDisplay { /* Style seed display */
    word-break: break-all; /* Allow long seeds to wrap */
    background-color: #e9ecef;
    padding: 2px 5px;
    border-radius: 3px;
    font-family: monospace; /* Use monospace for seed */
    font-size: 0.9em;
}


/* Stile per Dettagli Elemento nel Pannello Sinistro */
#elementDetailContent {
    font-size: 0.9em; color: #333; margin: 0; /* Remove margin */ padding: 10px 5px; /* Adjust padding */
    border-top: 1px solid #ddd; margin-top: 10px;
}
#elementDetailContent h4 {
    margin-top: 0; margin-bottom: 8px; font-size: 1.05em; color: #111;
    text-align: center;
}


/* Stili per Overlay e Pannelli Info (About/Roadmap) */
.panel-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(0, 0, 0, 0.6); z-index: 15;
    opacity: 0; visibility: hidden;
    transition: opacity 0.3s ease, visibility 0s linear 0.3s;
    cursor: pointer;
}
.panel-overlay.active {
    opacity: 1; visibility: visible;
    transition: opacity 0.3s ease, visibility 0s;
}

.info-panel {
    position: fixed; bottom: 0; left: 0; width: 100%;
    max-height: 80vh; /* Allow slightly more height */
    background-color: #f8f9fa; color: #333; z-index: 20;
    transform: translateY(100%); transition: transform 0.4s ease-in-out;
    box-shadow: 0 -3px 15px rgba(0, 0, 0, 0.2);
    display: flex; flex-direction: column; box-sizing: border-box;
    border-top-left-radius: 12px; /* Larger radius */
    border-top-right-radius: 12px;
}
.info-panel.active { transform: translateY(0); }

.info-panel-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 20px; /* Adjusted padding */
    border-bottom: 1px solid #dee2e6; background-color: #e9ecef;
    flex-shrink: 0;
    border-top-left-radius: 12px; border-top-right-radius: 12px;
}
.info-panel-header h2 { margin: 0; font-size: 1.3em; color: #212529; }
.close-panel-btn {
    background: none; border: none; font-size: 1.8em; /* Larger close icon */
    color: #6c757d; cursor: pointer; padding: 0 5px; line-height: 1;
    /* Ensure good tap area */
    min-width: 40px;
    min-height: 40px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
}
.close-panel-btn:hover { color: #343a40; }

.info-panel-content {
    padding: 15px 20px 20px 20px; /* Add more bottom padding */
    overflow-y: auto; flex-grow: 1; line-height: 1.6;
    /* Style scrollbar for browsers that support it */
    scrollbar-width: thin;
    scrollbar-color: #adb5bd #f8f9fa;
}
.info-panel-content::-webkit-scrollbar { width: 8px; }
.info-panel-content::-webkit-scrollbar-track { background: #f8f9fa; }
.info-panel-content::-webkit-scrollbar-thumb { background-color: #adb5bd; border-radius: 4px; border: 2px solid #f8f9fa; }

.info-panel-content h3 { margin-top: 0.5em; margin-bottom: 0.8em; color: #343a40; }
.info-panel-content h4 { margin-top: 1.2em; margin-bottom: 0.6em; color: #495057; }
.info-panel-content ul { padding-left: 25px; list-style: disc; margin-bottom: 1em;}
.info-panel-content li { margin-bottom: 10px; } /* More spacing */
.info-panel-content p { margin-bottom: 1.2em; } /* More spacing */
.info-panel-content strong { color: #212529; }
.info-panel-content em { color: #6c757d; font-style: italic; }


/* =========================================== */
/* --- Mobile Optimizations (<= 768px) --- */
/* =========================================== */
@media (max-width: 768px) {

    h1 {
        font-size: 1.1em; /* Slightly smaller title */
        padding: 4px 10px;
        max-width: calc(100vw - 100px); /* Adjust max-width for smaller buttons */
    }

    .toggle-button {
        padding: 6px 8px;
        font-size: 1em; /* Slightly smaller icon */
        min-width: 36px; /* Adjust tap area slightly */
        min-height: 36px;
    }
    .toggle-button.top-left-toggle { top: 8px; left: 8px; }
    .toggle-button.top-right-toggle { top: 8px; right: 8px; }

    .controls {
        /* Make panels take most of the width */
        width: calc(100vw - 20px); /* Leave 10px margin on each side */
        max-width: 320px; /* Prevent panels from becoming too wide on ~tablet portrait */
        padding: 10px;
        gap: 8px;
        /* Adjust max-height considering smaller viewport */
        max-height: calc(100vh - 110px); /* Adjust based on button/header positions */
    }

    .controls.left-panel {
        top: 50px; /* Adjust position */
        left: 10px;
        /* Make sure collapse transform works with new width */
        /* transform: translateX(0); /* Already default */
    }
    .controls.left-panel.collapsed {
        transform: translateX(calc(-100% - 15px)); /* Adjust collapse distance */
    }

    .controls.top-controls {
        top: 50px; /* Adjust position */
        right: 10px;
         /* Make sure collapse transform works with new width */
        /* transform: translateX(0); /* Already default */
    }
    .controls.top-controls.collapsed {
         transform: translateX(calc(100% + 15px)); /* Adjust collapse distance */
    }

    /* Adjust control element spacing and size */
    .controls label {
        padding: 6px 8px;
        font-size: 0.9em;
        gap: 5px;
    }
     .controls input[type="number"] { width: 50px; padding: 3px; }
     .controls input[type="checkbox"] { width: 16px; height: 16px; }
     .range-container span { min-width: 30px; } /* Value display next to range */
     .controls button:not(.seed-container button):not(.toggle-button) {
        padding: 8px 12px;
        font-size: 0.95em;
    }
    .seed-container input[type="text"] { padding: 3px; }
    .seed-container button { padding: 3px 6px; }
    .controls label select { padding: 5px; }


    .controls.bottom-controls {
        bottom: 5px; /* Closer to edge */
        left: 5px;
        padding: 5px;
        gap: 5px;
        /* Allow wrapping if buttons don't fit */
        flex-wrap: wrap;
    }
    .controls.bottom-controls button {
        padding: 5px 8px;
        font-size: 0.9em;
        min-width: 32px;
        min-height: 32px;
    }

    /* Footer adjustments */
    footer {
        font-size: 0.75em;
        padding: 3px 5px;
    }
     footer a {
        margin: 0 5px; /* Reduce space between links */
        padding: 3px 0;
    }

    /* Info Panel Adjustments */
    .info-panel {
        max-height: 85vh; /* Allow slightly more height on mobile */
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
    }
    .info-panel-header {
        padding: 10px 15px;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
    }
    .info-panel-header h2 { font-size: 1.15em; }
    .close-panel-btn {
        font-size: 1.6em;
        min-width: 36px;
        min-height: 36px;
    }
    .info-panel-content { padding: 10px 15px 15px 15px; }
    .info-panel-content li { margin-bottom: 8px; }
    .info-panel-content p { margin-bottom: 1em; }

}

/* Optional: Further adjustments for very small screens */
@media (max-width: 480px) {
    h1 {
        font-size: 1em;
        max-width: calc(100vw - 80px);
    }

    .controls {
         /* Ensure it fits even very narrow screens */
        width: calc(100vw - 10px);
        left: 5px; /* Adjust position based on new width */
        right: 5px; /* Adjust position based on new width */
        /* Override specific left/right positioning if using width: calc(...) */
        margin-left: auto;
        margin-right: auto;
    }
     .controls.left-panel { left: 5px; right: auto; }
     .controls.top-controls { right: 5px; left: auto; }

     /* Further reduce font size in controls if needed */
     .controls label { font-size: 0.85em; }

     .controls.bottom-controls {
        /* Stack controls if they wrap badly */
        /* flex-direction: column; */
        /* align-items: flex-start; */
        /* Keep row layout, wrap should handle most cases */
     }
}

/* Stili per la Legenda dei Biomi */
.biome-legend {
    position: absolute;
    bottom: 45px;
    right: 15px;
    background-color: rgba(40, 40, 40, 0.85);
    color: #eee;
    padding: 10px 15px;
    border-radius: 5px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    font-size: 0.8em;
    z-index: 10;
    max-height: 30vh; /* Limita altezza */
    /* Rimuovi overflow-y da qui se lo vuoi solo quando non è nascosto */
    box-shadow: 0 2px 5px rgba(0,0,0,0.3);
    transition: opacity 0.3s ease, transform 0.3s ease; /* Aggiungi transizioni */
    transform-origin: bottom right; /* Punto di origine per animazione */
}

/* NUOVO: Stile per l'header della legenda (titolo + bottone) */
.legend-header {
    display: flex;
    justify-content: space-between; /* Allinea titolo a sx, bottone a dx */
    align-items: center;
    margin-bottom: 8px; /* Spazio sotto l'header */
    padding-bottom: 5px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.biome-legend h4 {
    margin: 0; /* Rimuovi margini dal titolo h4 */
    padding: 0; /* Rimuovi padding dal titolo h4 */
    border-bottom: none; /* Rimuovi bordo dal titolo h4 */
    font-size: 0.9em;
    color: #fff;
}

/* NUOVO: Stile per il bottone toggle */
.toggle-legend-btn {
    background: none;
    border: none;
    color: #ccc; /* Colore icona */
    cursor: pointer;
    padding: 0 5px; /* Leggero padding */
    font-size: 1em; /* Dimensione icona relativa */
    line-height: 1; /* Allineamento verticale icona */
    transition: color 0.2s ease;
}
.toggle-legend-btn:hover {
    color: #fff; /* Icona più chiara all'hover */
}

/* Stili lista (invariati) */
.biome-legend ul {
    list-style: none;
    padding: 0;
    margin: 0;
    overflow-y: auto; /* Sposta overflow qui, si applica solo alla lista */
    max-height: calc(30vh - 50px); /* Sottrai altezza approx header+padding */
    /* Nascondi la lista quando la legenda è collassata */
    transition: max-height 0.3s ease, opacity 0.3s ease; /* Animazione per la lista */

}

.biome-legend li {
    display: flex;
    align-items: center;
    margin-bottom: 4px;
    white-space: nowrap; /* Evita che il nome vada a capo facilmente */
}

.legend-color-box {
    display: inline-block;
    width: 14px;
    height: 14px;
    margin-right: 8px;
    border: 1px solid #555; /* Bordo leggero per distinguere colori chiari */
    flex-shrink: 0; /* Non far restringere il quadratino */
}


.legend-text {
    opacity: 0.9;
}

/* NUOVO: Classe per nascondere la legenda */
.biome-legend.hidden {
    /* Opzione 1: Opacità e Transform (più smooth ma occupa spazio) */
    /* opacity: 0; */
    /* transform: scale(0.95) translateY(10px); */
    /* pointer-events: none; */

    /* Opzione 2: Nascondi completamente (più semplice) */
     padding-top: 5px; /* Riduce padding quando nascosto */
     padding-bottom: 5px;
     max-height: 30px; /* Altezza minima per mostrare solo header */
     border: 1px solid rgba(255, 255, 255, 0.1); /* Bordo più leggero */
     background-color: rgba(40, 40, 40, 0.7); /* Più trasparente */
}

.biome-legend.hidden ul {
    max-height: 0; /* Nasconde la lista */
    opacity: 0;
    overflow: hidden; /* Necessario per max-height 0 */
    pointer-events: none;
}
