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

body {
    overflow: hidden;
    background: #1a1a1a;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    -webkit-user-select: none;
    user-select: none;
    overscroll-behavior: none;
    touch-action: none;
}

#canvas {
    display: block;
    width: 100vw;
    height: 100vh;
    touch-action: none;
}

#settingsToggle {
    position: fixed;
    top: 20px;
    left: 20px;
    z-index: 20;
    width: 44px;
    height: 44px;
    padding: 0;
    font-size: 1.4rem;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.7);
    color: #e0c060;
    border: 1px solid rgba(224, 192, 96, 0.4);
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, background 0.2s ease;
}

#settingsToggle:hover {
    background: rgba(0, 0, 0, 0.85);
    transform: scale(1.08);
}

.settings-panel {
    position: fixed;
    top: 74px;
    left: 20px;
    z-index: 20;
    background: rgba(0, 0, 0, 0.6);
    padding: 0;
    border-radius: 15px;
    backdrop-filter: blur(10px);
    max-width: 320px;
    width: calc(100vw - 40px);
    overflow: hidden;
    transition: max-height 0.35s ease, opacity 0.25s ease, padding 0.35s ease;
    max-height: 400px;
    opacity: 1;
}

.settings-panel.hidden {
    max-height: 0;
    opacity: 0;
    padding: 0;
    pointer-events: none;
}

.settings-panel h2 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #e0c060;
    padding: 12px 15px 0;
    margin-bottom: 10px;
}

.settings-body {
    padding: 0 15px 15px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.control-row {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

label {
    font-size: 1rem;
}

select {
    padding: 6px 12px;
    border-radius: 8px;
    border: 2px solid #e0c060;
    background: #2a2a2a;
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
    outline: none;
}

select:focus {
    border-color: #f0d070;
}

.settings-body input[type="text"] {
    padding: 6px 10px;
    border-radius: 8px;
    border: 2px solid rgba(255, 255, 255, 0.25);
    background: #2a2a2a;
    color: #fff;
    font-size: 0.95rem;
    outline: none;
    max-width: 150px;
    touch-action: auto;
}

.settings-body input[type="text"]:focus {
    border-color: #e0c060;
}

button {
    padding: 8px 16px;
    border-radius: 8px;
    border: none;
    background: #e0c060;
    color: #1a1a1a;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

button:hover {
    background: #f0d070;
    transform: scale(1.05);
}

button:active {
    transform: scale(0.98);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.colors-row {
    flex-wrap: wrap;
    gap: 10px;
}

.color-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.color-group label {
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.7);
}

input[type="color"] {
    -webkit-appearance: none;
    appearance: none;
    width: 36px;
    height: 36px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    cursor: pointer;
    background: none;
    padding: 2px;
}

input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 5px;
}

input[type="color"]::-moz-color-swatch {
    border: none;
    border-radius: 5px;
}

.presets {
    display: flex;
    gap: 4px;
}

.preset-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    border-radius: 6px;
    font-size: 1rem;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.preset-btn:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: scale(1.1);
}

#results {
    position: fixed;
    bottom: 20px;
    left: 20px;
    z-index: 10;
    background: rgba(0, 0, 0, 0.6);
    padding: 12px 15px;
    border-radius: 15px;
    backdrop-filter: blur(10px);
    min-height: 40px;
    pointer-events: auto;
    color: #fff;
}

.result-dice {
    display: flex;
    gap: 10px;
    justify-content: flex-start;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.die-result {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: #f5f5f0;
    border-radius: 6px;
    overflow: hidden;
    animation: popIn 0.3s ease-out;
    box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.4), inset 0 0 0 1px rgba(0,0,0,0.08);
}

.die-face-svg {
    width: 100%;
    height: 100%;
}

.result-total {
    font-size: 1.3rem;
    color: #e0c060;
    font-weight: bold;
}

.hint {
    position: fixed;
    bottom: 6px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.4);
    white-space: nowrap;
    pointer-events: none;
}

#rollBtnWrap {
    position: fixed;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
}

#rollBtnWrap #rollBtn {
    padding: 14px 32px;
    font-size: 1.1rem;
    border-radius: 14px;
    box-shadow: 0 4px 20px rgba(224, 192, 96, 0.3);
}

@keyframes popIn {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    70% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ── Bouton 🏆 + panneau scores ─────────────────────── */

#scoresToggle {
    position: fixed;
    top: max(20px, calc(env(safe-area-inset-top) + 10px));
    left: max(74px, calc(env(safe-area-inset-left) + 64px));
    z-index: 20;
    width: 44px;
    height: 44px;
    padding: 0;
    font-size: 1.3rem;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.7);
    color: #e0c060;
    border: 1px solid rgba(224, 192, 96, 0.4);
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, background 0.2s ease;
}

#scoresToggle:hover {
    background: rgba(0, 0, 0, 0.85);
    transform: scale(1.08);
}

.scores-panel {
    position: fixed;
    top: max(74px, calc(env(safe-area-inset-top) + 64px));
    left: max(74px, calc(env(safe-area-inset-left) + 64px));
    z-index: 20;
    background: rgba(0, 0, 0, 0.75);
    border-radius: 15px;
    backdrop-filter: blur(10px);
    min-width: 280px;
    max-width: 340px;
    overflow: hidden;
    transition: max-height 0.35s ease, opacity 0.25s ease;
    max-height: 520px;
    opacity: 1;
}

.scores-panel.hidden {
    max-height: 0;
    opacity: 0;
    pointer-events: none;
}

.scores-panel h2 {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #e0c060;
    padding: 10px 12px 6px;
    margin: 0;
}

.scores-tabs {
    display: flex;
    gap: 4px;
    padding: 0 10px 6px;
}

.scores-terrain {
    display: flex;
    gap: 4px;
    padding: 0 10px 8px;
    flex-wrap: wrap;
}

.terrain-btn {
    padding: 3px 8px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: rgba(255, 255, 255, 0.05);
    color: rgba(255, 255, 255, 0.5);
    font-size: 0.72rem;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
}

.terrain-btn:hover {
    background: rgba(255, 255, 255, 0.12);
    color: #e0c060;
}

.terrain-btn.active {
    background: rgba(100, 180, 100, 0.2);
    border-color: rgba(100, 200, 100, 0.5);
    color: #aaffaa;
    font-weight: bold;
}

.score-tab {
    flex: 1;
    padding: 5px 0;
    border-radius: 8px;
    border: 1px solid rgba(224, 192, 96, 0.25);
    background: rgba(255, 255, 255, 0.06);
    color: rgba(255, 255, 255, 0.5);
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.15s ease;
    text-align: center;
}

.score-tab:hover {
    background: rgba(255, 255, 255, 0.12);
    color: #e0c060;
}

.score-tab.active {
    background: rgba(224, 192, 96, 0.2);
    border-color: #e0c060;
    color: #e0c060;
    font-weight: bold;
}

#scoresList {
    padding: 0 12px 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-height: 340px;
    overflow-y: auto;
}

.score-row {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    color: #fff;
    padding: 2px 0;
}

.score-rank {
    min-width: 28px;
    text-align: center;
    font-size: 0.85rem;
}

.score-time {
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
    color: #aaffaa;
    min-width: 64px;
    text-align: right;
    margin-left: auto;
}

.score-name {
    font-size: 0.82rem;
    color: #aad4ff;
    font-weight: bold;
    max-width: 72px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.score-date {
    font-size: 0.68rem;
    color: rgba(255, 255, 255, 0.4);
    white-space: nowrap;
    min-width: 68px;
    text-align: right;
}

.score-empty-msg {
    color: rgba(255, 255, 255, 0.35);
    font-size: 0.85rem;
    text-align: center;
    padding: 12px 0;
}

/* ── Modal saisie prénom ─────────────────────────────── */

.name-modal {
    position: fixed;
    inset: 0;
    z-index: 300;
    background: rgba(0, 0, 0, 0.72);
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(4px);
}

.name-modal.hidden {
    display: none;
}

.name-modal-box {
    background: rgba(15, 15, 28, 0.97);
    border: 1px solid rgba(224, 192, 96, 0.55);
    border-radius: 18px;
    padding: 28px 32px 24px;
    text-align: center;
    min-width: 270px;
    max-width: 90vw;
    box-shadow: 0 8px 40px rgba(0,0,0,0.6);
}

.name-modal-box p {
    color: #e0c060;
    font-size: 1.05rem;
    font-weight: bold;
    margin: 0 0 10px;
    line-height: 1.55;
}

.name-time {
    font-family: 'Courier New', Courier, monospace;
    font-size: 1.6rem;
    font-weight: bold;
    color: #aaffaa;
    margin-bottom: 14px;
    letter-spacing: 2px;
}

.name-modal-box input[type="text"] {
    width: 100%;
    padding: 10px 14px;
    border-radius: 10px;
    border: 2px solid rgba(224, 192, 96, 0.5);
    background: #1a1a2e;
    color: #fff;
    font-size: 1.25rem;
    font-weight: bold;
    text-align: center;
    letter-spacing: 3px;
    outline: none;
    touch-action: auto;
    margin-bottom: 16px;
    box-sizing: border-box;
}

.name-modal-box input[type="text"]:focus {
    border-color: #e0c060;
}

.name-modal-actions {
    display: flex;
    justify-content: center;
}

.name-modal-box button {
    padding: 10px 30px;
    font-size: 1rem;
}

/* ── Bannière nouveau record ─────────────────────────── */

#record-banner {
    position: fixed;
    top: calc(50% + 72px);
    left: 50%;
    transform: translateX(-50%) scale(0);
    z-index: 101;
    pointer-events: none;
    font-size: clamp(1.1rem, 3.5vw, 1.8rem);
    font-weight: 800;
    letter-spacing: 3px;
    white-space: nowrap;
    color: #fff;
    text-shadow:
        0 0 15px rgba(170, 255, 170, 0.9),
        0 0 35px rgba(100, 255, 100, 0.6),
        2px 2px 0 #1a5c1a;
    opacity: 0;
}

#record-banner.active {
    animation: recordBanner 2.8s ease-out forwards;
}

@keyframes recordBanner {
    0%   { transform: translateX(-50%) scale(0);    opacity: 0; }
    15%  { transform: translateX(-50%) scale(1.15); opacity: 1; }
    28%  { transform: translateX(-50%) scale(0.95); opacity: 1; }
    45%  { transform: translateX(-50%) scale(1);    opacity: 1; }
    72%  { transform: translateX(-50%) scale(1);    opacity: 1; }
    100% { transform: translateX(-50%) scale(1);    opacity: 0; }
}

/* ── Yahtzee flash ──────────────────────────────────── */

#yahtzee-flash {
    position: fixed;
    inset: 0;
    z-index: 100;
    pointer-events: none;
    background: radial-gradient(ellipse at center,
        rgba(255, 215, 0, 0.55) 0%,
        rgba(255, 140, 0, 0.28) 50%,
        transparent 72%);
    opacity: 0;
}

#yahtzee-flash.active {
    animation: yahtzeeFlash 1.6s ease-out forwards;
}

#yahtzee-banner {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    z-index: 101;
    pointer-events: none;
    font-size: clamp(2.5rem, 8vw, 5rem);
    font-weight: 900;
    letter-spacing: 6px;
    white-space: nowrap;
    color: #ffd700;
    text-shadow:
        0 0 25px rgba(255, 215, 0, 0.9),
        0 0 55px rgba(255, 140, 0, 0.7),
        3px 3px 0 #7a5500;
    opacity: 0;
}

#yahtzee-banner.active {
    animation: yahtzeeBanner 2.8s ease-out forwards;
}

@keyframes yahtzeeFlash {
    0%   { opacity: 0; }
    8%   { opacity: 1; }
    28%  { opacity: 0.45; }
    48%  { opacity: 0.9; }
    68%  { opacity: 0.35; }
    100% { opacity: 0; }
}

@keyframes yahtzeeBanner {
    0%   { transform: translate(-50%, -50%) scale(0);    opacity: 0; }
    12%  { transform: translate(-50%, -50%) scale(1.25); opacity: 1; }
    22%  { transform: translate(-50%, -50%) scale(0.92); opacity: 1; }
    32%  { transform: translate(-50%, -50%) scale(1.06); opacity: 1; }
    45%  { transform: translate(-50%, -50%) scale(1);    opacity: 1; }
    72%  { transform: translate(-50%, -50%) scale(1);    opacity: 1; }
    100% { transform: translate(-50%, -50%) scale(1);    opacity: 0; }
}

/* ── Chronomètre de lancer ───────────────────────────── */

#chrono {
    position: fixed;
    top: max(20px, calc(env(safe-area-inset-top) + 10px));
    right: max(20px, calc(env(safe-area-inset-right) + 10px));
    z-index: 15;
    font-size: 1.35rem;
    font-family: 'Courier New', Courier, monospace;
    font-weight: bold;
    letter-spacing: 1px;
    color: #e0c060;
    background: rgba(0, 0, 0, 0.65);
    padding: 6px 14px;
    border-radius: 10px;
    backdrop-filter: blur(5px);
    border: 1px solid rgba(224, 192, 96, 0.3);
    pointer-events: none;
    min-width: 108px;
    text-align: center;
    transition: color 0.3s ease, border-color 0.3s ease;
}

#chrono.hidden {
    display: none;
}

#chrono.stopped {
    color: #aaffaa;
    border-color: rgba(170, 255, 170, 0.4);
}

/* En paysage mobile, laisser la place au bouton roll (bas-droit) */
@media (orientation: landscape) and (max-height: 500px) {
    #chrono {
        top: max(20px, calc(env(safe-area-inset-top) + 10px));
        right: max(140px, calc(env(safe-area-inset-right) + 130px));
        font-size: 1.1rem;
        padding: 4px 10px;
        min-width: 90px;
    }
}

#fps {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 15;
    font-size: 0.8rem;
    font-family: monospace;
    color: #0f0;
    background: rgba(0, 0, 0, 0.5);
    padding: 4px 10px;
    border-radius: 6px;
    pointer-events: none;
}

/* ── Safe-area (notch / home indicator) ───────────────── */

#settingsToggle {
    top: max(20px, calc(env(safe-area-inset-top) + 10px));
    left: max(20px, calc(env(safe-area-inset-left) + 10px));
}

.settings-panel {
    top: max(74px, calc(env(safe-area-inset-top) + 64px));
    left: max(20px, calc(env(safe-area-inset-left) + 10px));
}

#rollBtnWrap {
    bottom: max(40px, calc(env(safe-area-inset-bottom) + 20px));
}

#results {
    bottom: max(20px, calc(env(safe-area-inset-bottom) + 10px));
    left: max(20px, calc(env(safe-area-inset-left) + 10px));
}

.hint {
    bottom: max(6px, calc(env(safe-area-inset-bottom) + 4px));
}

/* ── Portrait mobile (max 430px) ──────────────────────── */

@media (max-width: 430px) {
    /* Éviter que les résultats chevauchent le bouton roll */
    #results {
        bottom: max(106px, calc(env(safe-area-inset-bottom) + 96px));
    }

    /* La hint peut déborder sur petits écrans */
    .hint {
        white-space: normal;
        text-align: center;
        width: calc(100% - 40px);
        font-size: 0.68rem;
        line-height: 1.4;
    }

    /* Dés dans les résultats légèrement plus petits */
    .die-result {
        width: 30px;
        height: 30px;
    }
}

/* ── Paysage mobile (hauteur ≤ 500px) ────────────────── */

@media (orientation: landscape) and (max-height: 500px) {
    /* Panneau settings : hauteur limitée + scroll interne */
    .settings-panel {
        max-height: calc(100dvh - max(80px, calc(env(safe-area-inset-top) + 70px)));
        overflow-y: auto;
    }

    /* Bouton roll : coin bas-droit pour libérer l'espace central */
    #rollBtnWrap {
        left: auto;
        right: max(20px, calc(env(safe-area-inset-right) + 10px));
        bottom: max(14px, calc(env(safe-area-inset-bottom) + 8px));
        transform: none;
    }

    #rollBtnWrap #rollBtn {
        padding: 10px 20px;
        font-size: 0.95rem;
    }

    /* Résultats : coin bas-gauche, plus compact */
    #results {
        bottom: max(14px, calc(env(safe-area-inset-bottom) + 8px));
        max-width: calc(50vw - 20px);
        padding: 8px 10px;
    }

    .die-result {
        width: 28px;
        height: 28px;
    }

    .result-total {
        font-size: 1rem;
    }

    /* Hint : masquée en paysage mobile */
    .hint {
        display: none;
    }
}
