/* =========================================================
   Habillage moderne du jeu de flood-fill 2 joueurs (multijoueur)
   ========================================================= */

:root {
    --bg-1: #0f0f23;
    --bg-2: #1a1a3a;
    --panel: rgba(255, 255, 255, 0.05);
    --panel-border: rgba(255, 255, 255, 0.1);
    --text: #f1f5f9;
    --text-dim: #94a3b8;
    --accent: #818cf8;
    --accent-2: #f472b6;
    --gradient: linear-gradient(135deg, var(--accent), var(--accent-2));
    --radius-lg: 24px;
    --radius-md: 16px;
    --radius-sm: 8px;

    /* Taille de reference du plateau : carree, plafonnee a 1080px, et qui
       retrecit avec la fenetre. Reutilisee par #j1/#j2 pour rester
       proportionnels au plateau. */
    --plateau-size: min(1080px, 92vw, 70vh);
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    font-family: 'Poppins', system-ui, -apple-system, 'Segoe UI', sans-serif;
    color: var(--text);
    background: radial-gradient(circle at 20% 20%, #2a1e4d, transparent 45%),
        radial-gradient(circle at 80% 80%, #1e2a4d, transparent 45%),
        linear-gradient(160deg, var(--bg-1), var(--bg-2));
    background-attachment: fixed;
    overflow-x: hidden;
}

.container {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.2rem;
}

.game-title {
    margin: 0;
    text-align: center;
    font-size: 2rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text);
}

.game-title span {
    background: var(--gradient);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

/* Le layout d'origine est une <table> ; on la transforme en flexbox
   sans toucher au HTML.
   IMPORTANT : on utilise le combinateur ">" (enfant direct) et non
   l'espace (descendant). Le plateau de jeu est lui-meme une <table
   class="gamegrid"> imbriquee dans un <td> de cette table. Avec un
   simple selecteur descendant ("table.maintable tr"), les regles
   flex/block s'appliquaient AUSSI aux <tr>/<td> du plateau, ce qui
   cassait completement la grille sur Safari iOS (tous les pions
   s'empilaient dans une seule colonne). Le child combinator evite
   ce chevauchement. */
table.maintable,
table.maintable > tbody {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 1.2rem;
}

table.maintable > tbody > tr {
    display: flex;
    width: 100%;
    align-items: flex-start;
    justify-content: center;
    gap: .5rem;
    flex-wrap: wrap;
}

table.maintable > tbody > tr > td {
    display: block;
}

/* ---------- Badge code de salle (mode reseau) ---------- */

.room-code-badge {
    position: fixed;
    top: 16px;
    left: 16px;
    padding: 8px 16px;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 999px;
    backdrop-filter: blur(12px);
    font-size: 0.85rem;
    color: var(--text-dim);
    z-index: 10;
}

.room-code-badge strong {
    color: var(--text);
    letter-spacing: 0.08em;
}

/* ---------- Panneaux joueurs ---------- */

/* Largeur = 50% du plateau ; hauteur libre (auto), basee sur le contenu. */
#j1,
#j2 {
    width: calc(var(--plateau-size) * 0.5);
    height: auto;
    min-width: 160px;
}

#j1 .middle,
#j2 .middle {
    width: 100%;
    height: 100%;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-lg);
    padding: 24px 18px;
    backdrop-filter: blur(12px);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
    transition: transform 0.25s ease, box-shadow 0.25s ease, border-color 0.25s ease;
}

/* Le joueur dont c'est le tour est mis en valeur (classe ajoutee en JS) */
#j1.active .middle,
#j2.active .middle {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(129, 140, 248, 0.5), 0 12px 34px rgba(129, 140, 248, 0.35);
    transform: translateY(-4px);
}

/* Joueur deconnecte / en attente de reconnexion */
#j1.disconnected .middle,
#j2.disconnected .middle {
    opacity: 0.45;
}

.photo {
    width: 96px;
    height: 96px;
    margin: 0 auto 12px;
    border-radius: 50%;
    padding: 4px;
    background: var(--gradient);
}

.photoframe {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: var(--bg-2);
}

.name {
    margin-top: 4px;
}

h1.namejoueur {
    font-size: 1.1rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-dim);
    margin: 0;
}

.score {
    margin-top: 10px;
    font-size: 2.4rem;
    font-weight: 800;
    line-height: 1;
    background: var(--gradient);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

/* ---------- Barre de temps ---------- */

.speed-time {
    margin-top: 6px;
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    color: var(--accent);
    min-height: 1.1em;
    display: none;
}

#j1.speed-mode .speed-time,
#j2.speed-mode .speed-time {
    display: block;
}

.ai-thinking {
    margin-top: 6px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-dim);
    min-height: 1.1em;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.ai-thinking.visible {
    opacity: 1;
    animation: ai-thinking-pulse 1.1s ease-in-out infinite;
}

@keyframes ai-thinking-pulse {

    0%,
    100% {
        opacity: 0.55;
    }

    50% {
        opacity: 1;
    }
}

.timer-bar {
    margin-top: 14px;
    width: 150px;
    height: 12px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--panel-border);
    box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.35);
    overflow: hidden;
}

.timer-bar-fill {
    height: 100%;
    width: 0%;
    border-radius: 999px;
    background: var(--gradient);
    transition: width 0.9s linear, background 0.3s ease;
}

.timer-bar.urgent .timer-bar-fill {
    background: linear-gradient(135deg, #fb923c, #ef4444);
    animation: timer-pulse 0.6s ease-in-out infinite;
}

@keyframes timer-pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.55;
    }
}

/* ---------- Plateau de jeu ---------- */

.plateau {
    width: var(--plateau-size);
    height: calc(var(--plateau-size) / 1.16);
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-lg);
    padding: 16px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(12px);
}

table.gamegrid {
    border-collapse: separate;
    border-spacing: 3px;
}

table.gamegrid td {
    border-radius: var(--radius-sm);
    background: rgba(255, 255, 255, 0.03);
}

table.gamegrid img {
    display: block;
    border-radius: 6px;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

table.gamegrid img:hover {
    transform: scale(1.1);
    position: relative;
    z-index: 2;
}

table.gamegrid td.not-my-turn img {
    cursor: not-allowed;
}

/* Case qui vient d'etre effacee par un coup (diff calcule cote client
   entre l'ancien et le nouveau plateau recu du serveur). */
.cell-clearing {
    animation: cell-clear 0.35s ease forwards;
}

@keyframes cell-clear {
    from {
        transform: scale(1);
        opacity: 1;
    }

    to {
        transform: scale(0);
        opacity: 0;
    }
}

/* ---------- Ecran de fin de partie / attente / erreur ---------- */

.gameover-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(10, 10, 25, 0.75);
    backdrop-filter: blur(6px);
    z-index: 9999;
    animation: fadeIn 0.3s ease;
}

.gameover-card {
    background: var(--bg-2);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-lg);
    padding: 40px 56px;
    text-align: center;
    box-shadow: 0 25px 60px rgba(0, 0, 0, 0.55);
    animation: popIn 0.35s ease;
    max-width: 90vw;
}

.gameover-card h1 {
    margin: 0 0 12px;
    font-size: 1.8rem;
    background: var(--gradient);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.gameover-winner {
    font-size: 1.2rem;
    color: var(--text);
    margin: 0 0 16px;
}

.gameover-scores {
    color: var(--text-dim);
    margin: 0;
}

.gameover-card .share-code {
    margin-top: 18px;
    padding: 10px 18px;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 999px;
    display: inline-block;
    color: var(--text-dim);
    font-size: 0.9rem;
}

.gameover-card .share-code strong {
    color: var(--text);
    letter-spacing: 0.1em;
}

.gameover-card .home-link {
    display: inline-block;
    margin-top: 20px;
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
}

.gameover-card .home-link:hover {
    text-decoration: underline;
}

/* ---------- Boutons generiques (mode/home) ---------- */

.mode-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    min-width: 150px;
    padding: 20px 28px;
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-md);
    background: var(--panel);
    color: var(--text);
    font-family: inherit;
    font-size: 1.1rem;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.mode-btn span {
    font-size: 0.8rem;
    font-weight: 400;
    color: var(--text-dim);
}

.mode-btn:hover,
.mode-btn:focus-visible {
    border-color: var(--accent);
    transform: translateY(-3px);
    box-shadow: 0 10px 24px rgba(129, 140, 248, 0.35);
    outline: none;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes popIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* ---------- Page d'accueil (creer / rejoindre) ---------- */

.home-container {
    width: 100%;
    display: flex;
    justify-content: center;
}

.home-card {
    width: 100%;
    max-width: 640px;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius-lg);
    padding: 40px 36px;
    text-align: center;
    backdrop-filter: blur(12px);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
}

.home-title {
    margin: 0 0 8px;
    font-size: 2.2rem;
    font-weight: 800;
}

.home-title span {
    background: var(--gradient);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.home-subtitle {
    margin: 0 0 28px;
    color: var(--text-dim);
}

.home-error {
    margin: 0 0 20px;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    background: rgba(239, 68, 68, 0.15);
    border: 1px solid rgba(239, 68, 68, 0.4);
    color: #fecaca;
    font-size: 0.9rem;
}

.home-forms {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
    flex-wrap: wrap;
}

.home-form {
    flex: 1 1 220px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    text-align: left;
}

.home-form h2 {
    margin: 0 0 4px;
    font-size: 1rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.home-form input {
    width: 100%;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--panel-border);
    background: rgba(255, 255, 255, 0.04);
    color: var(--text);
    font-family: inherit;
    font-size: 0.95rem;
}

.home-form input::placeholder {
    color: var(--text-dim);
}

.home-form input:focus {
    outline: none;
    border-color: var(--accent);
}

.home-form .mode-btn {
    width: 100%;
    min-width: 0;
}

.game-mode-select {
    display: flex;
    gap: 8px;
}

.game-mode-option {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 8px;
    border-radius: 999px;
    border: 1px solid var(--panel-border);
    background: rgba(255, 255, 255, 0.03);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-dim);
    transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}

.game-mode-option input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.game-mode-option:has(input:checked) {
    border-color: var(--accent);
    color: var(--text);
    background: rgba(129, 140, 248, 0.15);
}

.mode-badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    border: 1px solid var(--panel-border);
    color: var(--text-dim);
    background: rgba(255, 255, 255, 0.03);
    white-space: nowrap;
}

.mode-badge-speed {
    color: var(--accent);
    border-color: var(--accent);
    background: rgba(129, 140, 248, 0.15);
}

.mode-badge-solo {
    margin-left: 4px;
}

.history-times {
    font-size: 0.75rem;
    color: var(--text-dim);
    margin-top: 2px;
}

.game-mode-hint {
    margin: -4px 0 0;
    font-size: 0.75rem;
    color: var(--text-dim);
    line-height: 1.4;
}

.home-divider {
    color: var(--text-dim);
    font-size: 0.85rem;
    text-transform: uppercase;
}

.home-history-link {
    display: inline-block;
    margin-top: 28px;
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.9rem;
}

.home-history-link:hover {
    text-decoration: underline;
}

/* ---------- Page historique ---------- */

.history-card {
    max-width: 780px;
    text-align: left;
}

.history-card .home-title,
.history-card .home-subtitle {
    text-align: center;
}

.history-empty {
    text-align: center;
    color: var(--text-dim);
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.history-table th,
.history-table td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--panel-border);
}

.history-table th {
    color: var(--text-dim);
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
}

.replay-link {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.85rem;
    white-space: nowrap;
}

.replay-link:hover {
    text-decoration: underline;
}

/* ---------- Page de replay ---------- */

.replay-back-link {
    position: fixed;
    top: 16px;
    right: 16px;
    margin: 0;
    z-index: 10;
}

.replay-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
    padding: 10px 18px;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 999px;
    backdrop-filter: blur(12px);
}

.replay-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--panel-border);
    background: rgba(255, 255, 255, 0.05);
    color: var(--text);
    font-size: 1rem;
    cursor: pointer;
    transition: transform 0.15s ease, border-color 0.15s ease;
}

.replay-btn:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
}

.replay-btn-play {
    background: var(--gradient);
    border-color: transparent;
    color: #0f0f23;
}

.replay-counter {
    margin-left: 8px;
    color: var(--text-dim);
    font-size: 0.9rem;
    min-width: 110px;
    text-align: center;
}

/* ---------- Tablette / petit ecran ---------- */

@media (max-width: 900px) {
    table.maintable > tbody > tr {
        gap: 1.2rem;
    }

    #j1 .middle,
    #j2 .middle {
        padding: 16px 12px;
        min-width: 140px;
    }

    .score {
        font-size: 1.8rem;
    }
}

/* ---------- Telephone mobile ---------- */

@media (max-width: 700px) {
    body {
        padding: 12px;
        align-items: flex-start;
    }

    .game-title {
        font-size: 1.3rem;
    }

    .container {
        max-width: 100%;
    }

    table.maintable > tbody > tr {
        flex-direction: column;
        align-items: center;
        gap: 0.6rem;
    }

    table.maintable > tbody > tr > td {
        display: flex;
        justify-content: center;
        width: 100%;
    }

    #j1,
    #j2 {
        min-width: 0;
        width: 100%;
        max-width: 320px;
    }

    #j1 .middle,
    #j2 .middle {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 14px;
        padding: 10px 16px;
    }

    #j1.active .middle,
    #j2.active .middle {
        transform: none;
    }

    .photo {
        width: 48px;
        height: 48px;
        margin: 0;
        flex-shrink: 0;
    }

    .name {
        margin-top: 0;
    }

    h1.namejoueur {
        font-size: 0.8rem;
    }

    .score {
        margin-top: 0;
        font-size: 1.5rem;
    }

    .timer-bar {
        width: 90px;
        height: 10px;
        margin: 0;
    }

    .plateau {
        width: min(1080px, 94vw, 56vh);
        height: min(1080px, 94vw, 56vh);
        padding: 8px;
        border-radius: var(--radius-md);
    }

    table.gamegrid {
        border-spacing: 2px;
    }

    table.gamegrid td {
        border-radius: 4px;
    }

    .gameover-card {
        padding: 28px 24px;
        margin: 0 16px;
    }

    .home-card {
        padding: 28px 20px;
    }

    .home-forms {
        flex-direction: column;
    }

    .room-code-badge {
        top: 8px;
        left: 8px;
        font-size: 0.75rem;
        padding: 6px 12px;
    }
}
