:root {
    --bg: #ffffff;
    --bg-splash: #FCFCFB;
    --text: #333333;
    --text-dark: #000000;
    --border: #cccccc;
    --input-bg: #ffffff;
    --modal-bg: #ffffff;
    --bar-empty: #e0e0e0;
    --letter-hidden: #333333;
    --letter-revealed: #000000;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1a1a1a;
        --bg-splash: #111111;
        --text: #cccccc;
        --text-dark: #f0f0f0;
        --border: #444444;
        --input-bg: #2a2a2a;
        --modal-bg: #2a2a2a;
        --bar-empty: #444444;
        --letter-hidden: #aaaaaa;
        --letter-revealed: #ffffff;
    }
}

/* Reset e base */
* {margin:0; padding:0; box-sizing:border-box; font-family: 'Segoe UI', Tahoma, sans-serif;}
body {display:flex; flex-direction:column; align-items:center; justify-content:center; min-height:100vh; color:var(--text); background: var(--bg);}
h1,h2,h3,p {text-align:center;}
button {cursor:pointer; border:none; border-radius:5px; padding:10px 20px; font-size:1rem; transition:0.2s; background:#333; color:#fff;}
input {padding:10px; font-size:1rem; border-radius:5px; border:1px solid #ccc; border-color: var(--border); background: var(--input-bg); color: var(--text-dark); width:80%; max-width:400px; margin:10px auto; display:block;}
pre {background:#e0e0e0; padding:10px; border-radius:5px; max-width:90%; word-wrap:break-word; white-space:pre-wrap; font-size:1rem;}

/* Splash page */
#splash {
	display:flex; flex-direction:column; align-items:center; justify-content:center; height:100vh; width:100%; text-align:center; background-color:var(--bg-splash);
}
#splash p {
    font-family: 'Google Sans Code', sans-serif;
    font-size:1rem;
    max-width:320px;
    margin:0 auto 30px auto;
    line-height:1.4;
}
#splash h1 {
    font-family: 'Tiny5', sans-serif;
    font-weight:bold;
    font-size:3rem;
    margin-bottom:0.5rem;
    text-align:center;
}
#splash img {width:150px; height:auto; margin-bottom:20px;}
#splash button {
    min-height:56px;
    font-size:1.35rem;
    line-height:1;
    padding:0 64px;
    border-radius:18px;
    font-weight:600;
    background: linear-gradient(180deg, #9148f2 0%, #7b2fe0 100%);
    color:#fff;
    box-shadow:
        inset 0 1px 0 rgba(255,255,255,0.22),
        inset 0 -2px 0 rgba(0,0,0,0.16),
        0 6px 14px rgba(0,0,0,0.16);
    margin-top:4px;
    margin-bottom:24px;
}
#splash small {
    display:block;
    margin-top:32px;
    font-size:16px;
    color: var(--text-dark);
}
#splash small span:first-child {font-weight:500;}

/* Game page */
#game {
    position: relative;
    display:none;
    flex-direction:column;
    align-items:center;
    justify-content:flex-start;
    width:100%;
    padding:20px;
    max-width:500px;
    background-color:var(--bg);
}

#game h2 {
    margin-top: 20px; 
    margin-bottom: 16px; 
    font-size: 1.8rem;
    font-weight: 700;
}

#attempts-indicator {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    margin-bottom: 4px;
}

.attempt-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background-color: var(--bar-empty, #e0e0e0);
    transition: background-color 0.3s;
}

.attempt-dot.used {
    background-color: #8338ec;
}

.attempt-dot.won {
    background-color: #22c55e;
}

#message {
    font-weight: 400; /* meno bold rispetto a #tentativi */
    font-size: 16px;
    color: var(--text-dark);
    margin-top: 10px;
}

/* Stessa larghezza dell’input standalone originario (80% / max 400px), sul form intero */
#guess-form {
    width: 80%;
    max-width: 400px;
    margin: 10px auto;
    box-sizing: border-box;
}

#input-wrapper {
    position: relative;
    width: 100%;
    margin: 0;
}

#input-wrapper input {
    width: 100%;
    margin: 0;
}

input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-results-button,
input[type="search"]::-webkit-search-results-decoration {
    display: none;
}

#autocomplete-list {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--modal-bg, #fff);
    border: 1px solid var(--border, #ccc);
    border-top: none;
    border-radius: 0 0 8px 8px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 1000;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.autocomplete-item {
    padding: 10px 14px;
    cursor: pointer;
    font-family: 'Roboto Mono', monospace;
    font-size: 0.9rem;
    color: var(--text-dark, #000);
    text-align: left;
    border-bottom: 1px solid var(--border, #eee);
}

.autocomplete-item:last-child {
    border-bottom: none;
}

.autocomplete-item:hover,
.autocomplete-item.active {
    background: #8338ec;
    color: #fff;
}

/* Stats button */
#statsBtn {position:fixed; top:15px; right:15px; background:none; padding:5px; z-index:50;}
#statsBtn img {width:30px; height:30px;}

/* Modal */
#modal {
    display: none;
    position: fixed;
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    background: rgba(0,0,0,0.4); 
    align-items: center; 
    justify-content: center;
}

#modalContent {
    background: var(--modal-bg); 
    padding: 20px 20px 30px 20px; 
    border-radius: 10px; 
    max-width: 90%; 
    width: 300px; 
    text-align: center; 
    position: relative;
}

#closeModal {
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer; 
    font-weight: bold;
    font-size: 1.2rem;
}

/* Responsive adjustments */
@media (max-width:600px){
    pre {font-size:0.9rem;}
    button {font-size:1rem; padding:10px 20px;}
    #statsBtn img {width:25px; height:25px;}
}

/* 1. Contenitore della frase, stile basato sullo screenshot */
#frase {
    background: var(--bg);
    border-radius: 5px;
    max-width: 90%;
    margin-bottom: 20px;
    padding: 10px;
    line-height: 1.5;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    /* primo valore = righe (più aria fra prima e seconda barra), secondo = colonne (parole sulla stessa riga) */
    gap: 1.35em 0.7em;
    user-select: none;
}

/* 2. Contenitore per le lettere di una parola */
.word {
    display: flex;
    gap: 0.11em;
}

.punctuation {
    /* Usa lo stesso stile delle lettere sbloccate per coerenza */
    font-family: 'Roboto Mono', monospace;
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-dark);
    
    /* Allineamento verticale con le lettere */
    display: flex;
    align-items: center;
}

.letter {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-family: 'Roboto Mono', monospace;
    font-size: 1rem;
    font-weight: 400;
    position: relative;
    perspective: 300px;
    width: 0.62em;
    height: 1.1em;
    line-height: 1;
    overflow: visible;
    background-color: transparent;
}

.letter-inner {
    position: relative;
    width: 100%;
    height: 100%;
    transform-style: preserve-3d;
    transition: transform 0.5s ease-in-out;
    background-color: transparent;
}

.letter.revealed .letter-inner {
    transform: rotateX(180deg);
}

.letter-front,
.letter-back {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    transform-style: preserve-3d;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Roboto Mono', monospace;
    font-size: 1rem;
    background-color: transparent;
    outline: none;
    box-shadow: none;
}

.letter-front {
    color: var(--letter-hidden, #333);
    transform: translateZ(0.05px);
}

.letter-back {
    color: var(--letter-revealed, #000);
    transform: rotateX(180deg) translateZ(0.05px);
}

/* Mobile: spaziatura parole/lettere cross-browser; flip stesso del desktop (vedi .letter-inner) */
@media (max-width: 768px) {
    #frase {
        column-gap: 0.75rem;
        row-gap: 1.1rem;
    }

    /*
     * Spazio tra lettere: su Firefox Android gap in em può apparire enorme perché il min-content
     * dei flex item tratta bene la monospace; clampiamo il gap in px + allineamo la cella alla larghezza ch.
     */
    .word {
        gap: clamp(3px, 0.085em, 5px);
    }

    .letter {
        min-width: 0;
        flex: 0 0 1ch;
        width: 1ch;
        height: 1.15em;
    }

    .punctuation {
        font-size: 0.9rem;
    }
}

/* Contenuto modale */
#modalContent, #modalText {
    background-color: var(--modal-bg);
}

/* Statistiche principali */
#statsSummary {
    display: flex;
    justify-content: space-between;
    margin: 20px 0 20px 0; /* più spazio sopra e sotto */
}

.statItem {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 22%;
}

.statNumber {
    font-weight: 600;
    font-size: 1.2rem;
    color: var(--text-dark);
}

.statLabel {
    font-weight: 500;
    font-size: 0.9rem;
    margin-top: 4px;
    text-align: center;
    text-transform: capitalize; /* Giocate, % Vittorie, etc. */
    color: var(--text-dark);
}

/* Distribuzione tentativi */
#distributionTitle {
    font-weight: 600;
    font-size: 1rem;
    margin: 20px 0 10px 0; /* spazio sopra come tra STATISTICHE e valori */
    text-align: center;
    text-transform: uppercase; /* DISTRIBUZIONE TENTATIVI */
    color: var(--text-dark);
}

.distributionRow {
    display: flex;
    align-items: center;
    margin-bottom: 6px;
}

.distributionLabel {
    width: 20px;
    font-size: 0.85rem;
    font-weight: 500;
}

.distributionBarContainer {
    flex: 1;
    background: transparent;
    border-radius: 4px;
    margin-left: 6px;
    position: relative;
    height: 18px;
}

.distributionBar {
    height: 100%;
    border-radius: 4px;
    background-color: var(--bar-empty);
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-right: 4px;
    color: #fff;
    font-size: 0.75rem;
    font-weight: 600;
    box-sizing: border-box;
    min-width: 0;
}

.distributionBar.current {
    background-color: #8338ec;
}

/* Bottoni */
.btn-large {
    min-height: 56px;
    font-size: 1.35rem;
    line-height: 1;
    padding: 0 48px;
    border-radius: 18px;
    font-weight: 700;
    margin-top: 8px;
    margin-bottom: 24px;
    cursor: pointer;
    transition: 0.2s;
    box-shadow:
        inset 0 1px 0 rgba(255,255,255,0.22),
        inset 0 -2px 0 rgba(0,0,0,0.16),
        0 6px 14px rgba(0,0,0,0.16);
}

.btn-black {
    background: linear-gradient(180deg, #151515 0%, #000 100%);
    color: #fff;
}

.btn-prova {
    background: linear-gradient(180deg, #9148f2 0%, #7b2fe0 100%);
    color: #fff;
}

#modalTitle {
    margin-bottom: 20px; /* aumenta lo spazio tra titolo e 4 valori */
    font-weight: 700;
    font-size: 1.5rem;
    color: var(--text-dark);
}

#leaderboardBtn {
    position: fixed;
    top: 15px;
    right: 55px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 5px;
    line-height: 0;
    z-index: 50;
}

#leaderboardBtn img {
    width: 30px;
    height: 30px;
    display: block;
}

@media (max-width:600px){
    #leaderboardBtn img {width:25px; height:25px;}
}

#leaderboardModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.4);
    align-items: center;
    justify-content: center;
    z-index: 100;
}

#leaderboardModal.open {
    display: flex;
}

#leaderboardContent {
    background: var(--modal-bg, #fff);
    padding: 20px;
    border-radius: 10px;
    max-width: 90%;
    width: 340px;
    max-height: 80vh;
    overflow-y: auto;
    text-align: center;
    position: relative;
}

#closeLeaderboard {
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
    font-weight: bold;
    font-size: 1.2rem;
    color: var(--text-dark, #000);
}

#leaderboardTitle {
    font-weight: 700;
    font-size: 1.5rem;
    margin-bottom: 16px;
    color: var(--text-dark, #000);
}

#leaderboardTabs {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-bottom: 16px;
}

.tab-btn {
    background: none;
    border: 2px solid #8338ec;
    color: #8338ec;
    border-radius: 10px;
    padding: 6px 18px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: 0.2s;
}

.tab-btn.active {
    background: #8338ec;
    color: #fff;
}

#nicknameSection {
    margin-bottom: 16px;
}

#nicknameSection p {
    font-size: 0.95rem;
    margin-bottom: 10px;
    color: var(--text-dark, #000);
}

#nicknameInput {
    width: 100%;
    margin-bottom: 10px;
}

.leaderboard-row {
    display: flex;
    align-items: center;
    padding: 10px 8px;
    border-bottom: 1px solid var(--border, #eee);
    gap: 10px;
    text-align: left;
}

.leaderboard-row:last-child {
    border-bottom: none;
}

.leaderboard-rank {
    flex: 0 0 24px;
    font-weight: 700;
    font-size: 1rem;
    width: 24px;
    text-align: center;
    color: var(--text-dark, #000);
}

.leaderboard-rank.gold { color: #FFD700; }
.leaderboard-rank.silver { color: #C0C0C0; }
.leaderboard-rank.bronze { color: #CD7F32; }

.leaderboard-info {
    flex: 1 1 auto;
    min-width: 0;
}

.leaderboard-name {
    font-weight: 500;
    font-size: 0.95rem;
    color: var(--text-dark, #000);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.leaderboard-name.is-you {
    color: #8338ec;
    font-weight: 700;
}

.leaderboard-score {
    flex: 0 0 auto;
    margin-left: auto;
    min-width: 56px;
    text-align: right;
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-dark, #000);
    white-space: nowrap;
}

.leaderboard-detail {
    font-size: 0.75rem;
    color: var(--text, #666);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.leaderboard-empty {
    padding: 30px;
    color: var(--text, #666);
    font-size: 0.95rem;
}
