:root {
    --bg-1: #1a1a2e;
    --bg-2: #16213e;
    --card: rgba(255, 255, 255, 0.08);
    --card-strong: rgba(255, 255, 255, 0.14);
    --text: #fff;
    --muted: #a9b0c7;
    --accent: #e94560;
    --green-1: #11998e;
    --green-2: #38ef7d;
    --purple-1: #667eea;
    --purple-2: #764ba2;
    --radius: 14px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    min-height: 100%;
}

body {
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(160deg, var(--bg-1) 0%, var(--bg-2) 100%) fixed;
    background-color: var(--bg-1);
    color: var(--text);
    min-height: 100dvh;
    overscroll-behavior-y: none;
}

button, input {
    font: inherit;
    color: inherit;
}

button {
    touch-action: manipulation; /* pas de zoom au double tap */
    cursor: pointer;
    border: none;
    background: none;
    user-select: none;
    -webkit-user-select: none;
}

.hidden { display: none !important; }

/* ===== Écrans ===== */
.screen {
    max-width: 560px;
    margin: 0 auto;
    padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.app-title h1 {
    text-align: center;
    font-size: 1.8rem;
    padding: 8px 0;
}

.card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 16px;
}

h2 {
    font-size: 1rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--accent);
    margin-bottom: 12px;
}

h3 {
    text-align: center;
    font-size: 1.05rem;
    font-weight: 600;
}

.hint {
    color: var(--muted);
    font-size: 0.9rem;
    margin-top: 10px;
}

/* ===== Boutons ===== */
.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    padding: 12px 20px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 1rem;
    transition: transform 0.1s, opacity 0.2s;
}

.btn:active:not(:disabled) { transform: scale(0.97); }

.btn-big {
    width: 100%;
    min-height: 60px;
    font-size: 1.2rem;
}

.btn-primary {
    background: linear-gradient(135deg, var(--green-1) 0%, var(--green-2) 100%);
    color: #fff;
    box-shadow: 0 6px 20px rgba(56, 239, 125, 0.25);
}

.btn-primary:disabled {
    background: #444a5e;
    box-shadow: none;
    color: #9aa0b4;
}

.btn-accent { background: var(--accent); }

.btn-ghost {
    width: 100%;
    background: var(--card-strong);
}

.btn-danger { background: #e74c3c; }

.icon-btn {
    width: 48px;
    height: 48px;
    flex: none;
    border-radius: 12px;
    background: var(--card-strong);
    font-size: 1.4rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:active:not(:disabled) { transform: scale(0.92); }
.icon-btn:disabled { opacity: 0.35; }

.bottom-bar {
    position: sticky;
    bottom: 0;
    margin-top: auto;
    padding-top: 12px;
    padding-bottom: var(--safe-bottom);
    background: linear-gradient(to top, var(--bg-2) 60%, transparent);
}

.bottom-bar.stacked {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* ===== Configuration ===== */
.player-input {
    display: flex;
    gap: 8px;
}

.player-input input {
    flex: 1;
    min-width: 0;
    min-height: 48px;
    padding: 0 14px;
    border: 2px solid transparent;
    border-radius: 12px;
    font-size: 16px; /* évite le zoom auto sur iOS */
    background: rgba(255, 255, 255, 0.95);
    color: #222;
}

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

.players-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 12px;
}

.player-tag {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 6px 6px 12px;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--purple-1) 0%, var(--purple-2) 100%);
}

.player-index {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    font-weight: 700;
    flex: none;
}

.player-label {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
}

.mini-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.15);
    font-size: 0.9rem;
    flex: none;
}

.mini-btn.danger { background: rgba(0, 0, 0, 0.25); }
.mini-btn:active { background: rgba(255, 255, 255, 0.3); }

.segmented {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    background: rgba(0, 0, 0, 0.25);
    padding: 4px;
    border-radius: 12px;
}

.segmented button {
    min-height: 44px;
    border-radius: 9px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--muted);
}

.segmented button.active {
    background: var(--accent);
    color: #fff;
}

.switch-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-top: 14px;
    min-height: 44px;
}

.switch-row input {
    appearance: none;
    -webkit-appearance: none;
    width: 52px;
    height: 30px;
    border-radius: 15px;
    background: #444a5e;
    position: relative;
    flex: none;
    transition: background 0.2s;
    cursor: pointer;
}

.switch-row input::after {
    content: '';
    position: absolute;
    top: 3px;
    left: 3px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #fff;
    transition: transform 0.2s;
}

.switch-row input:checked { background: var(--green-1); }
.switch-row input:checked::after { transform: translateX(22px); }

/* ===== Jeu : barre du haut ===== */
.top-bar {
    position: sticky;
    top: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 12px;
    margin: calc(-16px - var(--safe-top)) -16px 0;
    padding: calc(10px + var(--safe-top)) 16px 10px;
    background: rgba(22, 33, 62, 0.92);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.stat {
    display: flex;
    flex-direction: column;
    line-height: 1.1;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 800;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--muted);
}

.top-actions {
    margin-left: auto;
    display: flex;
    gap: 8px;
}

/* ===== Jetons ===== */
.token-display {
    width: 140px;
    height: 140px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 4rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
    border: 5px solid rgba(255, 255, 255, 0.3);
    flex: none;
}

.token-display.empty {
    background: rgba(255, 255, 255, 0.06);
    border-style: dashed;
    color: var(--muted);
    font-size: 3rem;
    box-shadow: none;
}

.token-display.small {
    width: 96px;
    height: 96px;
    font-size: 2.8rem;
    border-width: 4px;
}

.token-display.mini {
    width: 22px;
    height: 22px;
    font-size: 0.7rem;
    border-width: 2px;
    box-shadow: none;
}

.token-white { background: linear-gradient(135deg, #f5f5f5 0%, #d6d6d6 100%); color: #333; }
.token-blue { background: linear-gradient(135deg, #2196f3 0%, #1565c0 100%); }
.token-red { background: linear-gradient(135deg, #f44336 0%, #c62828 100%); }
.token-yellow { background: linear-gradient(135deg, #ffeb3b 0%, #f9a825 100%); color: #333; }
.token-multicolor { background: conic-gradient(#f44336, #ffeb3b, #4caf50, #2196f3, #9c27b0, #f44336); }

/* ===== Phase tirage ===== */
.draw-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 24px;
    padding: 24px 0 8px;
}

/* ===== Phase recherche ===== */
.bidding-area {
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--card);
    border-radius: var(--radius);
    padding: 16px;
}

.round-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.timer {
    position: relative;
    width: 140px;
    height: 140px;
    flex: none;
    border-radius: 50%;
    --timer-color: var(--green-2);
}

.timer svg {
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
}

.timer-track {
    fill: rgba(0, 0, 0, 0.25);
    stroke: rgba(255, 255, 255, 0.1);
    stroke-width: 8;
}

.timer-progress {
    fill: none;
    stroke: var(--timer-color);
    stroke-width: 8;
    stroke-linecap: round;
    stroke-dasharray: 326.73;
    transition: stroke-dashoffset 0.25s linear, stroke 0.3s;
}

.timer-text {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

#timer-value {
    font-size: 2.8rem;
    font-weight: 800;
    line-height: 1;
    color: var(--timer-color);
    font-variant-numeric: tabular-nums;
}

.timer-state {
    font-size: 0.7rem;
    color: var(--muted);
    margin-top: 4px;
    max-width: 90px;
    text-align: center;
}

.timer.warning { --timer-color: #ffeb3b; }
.timer.danger { --timer-color: #f44336; }
.timer.danger:not(.paused):not(.finished) #timer-value { animation: pulse 0.5s infinite; }
.timer.paused #timer-value { opacity: 0.6; }
.timer.finished { animation: flash 0.6s 3; }

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.12); }
}

@keyframes flash {
    50% { background: rgba(244, 67, 54, 0.4); }
}

.winner-buttons {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.winner-btn {
    min-height: 60px;
    padding: 10px;
    border-radius: 12px;
    background: linear-gradient(135deg, var(--purple-1) 0%, var(--purple-2) 100%);
    font-size: 1.1rem;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.winner-btn:active { transform: scale(0.96); }

/* ===== Scores ===== */
.scores {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
}

.score-card {
    background: var(--card);
    border-radius: 12px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    border: 2px solid transparent;
}

.score-card.leader { border-color: rgba(255, 215, 0, 0.6); }

.score-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.score-card .player-name {
    font-weight: 600;
    color: var(--accent);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.score-card .player-score {
    font-size: 1.3rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    flex: none;
}

.score-bar {
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.1);
    overflow: hidden;
}

.score-bar span {
    display: block;
    height: 100%;
    background: linear-gradient(90deg, var(--green-1), var(--green-2));
    transition: width 0.3s;
}

.won-tokens {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    min-height: 22px;
}

/* ===== Résultats ===== */
.final-scores {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.final-score-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    background: var(--card);
    border-radius: 12px;
}

.final-score-item.winner {
    background: linear-gradient(135deg, #ffd700 0%, #ff8c00 100%);
    color: #2a2a2a;
    font-weight: 700;
}

.final-score-item .rank { font-size: 1.5rem; min-width: 32px; }
.final-score-item .name { flex: 1; font-size: 1.15rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.final-score-item .score { font-size: 1.15rem; font-weight: 700; flex: none; }

/* ===== Dialogue et notification ===== */
.dialog {
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 16px 16px calc(16px + var(--safe-bottom));
}

.dialog-box {
    width: 100%;
    max-width: 420px;
    background: #232b4a;
    border-radius: 18px;
    padding: 20px;
    animation: slide-up 0.2s ease-out;
}

.dialog-box p {
    font-size: 1.1rem;
    text-align: center;
    margin-bottom: 18px;
}

.dialog-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

@keyframes slide-up {
    from { transform: translateY(30px); opacity: 0; }
}

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(24px + var(--safe-bottom));
    transform: translateX(-50%);
    z-index: 30;
    background: rgba(0, 0, 0, 0.85);
    padding: 12px 20px;
    border-radius: 24px;
    font-weight: 600;
    white-space: nowrap;
    animation: slide-up 0.2s ease-out;
    pointer-events: none;
}

/* ===== Petits écrans ===== */
@media (max-width: 360px) {
    .timer { width: 120px; height: 120px; }
    #timer-value { font-size: 2.3rem; }
    .token-display.small { width: 80px; height: 80px; font-size: 2.3rem; }
    .round-row .icon-btn { width: 42px; height: 42px; }
}

/* ===== Écrans larges : un peu plus d'air ===== */
@media (min-width: 700px) {
    .screen { padding-top: calc(32px + var(--safe-top)); }
    .winner-buttons { grid-template-columns: repeat(3, 1fr); }
    .scores { grid-template-columns: repeat(3, 1fr); }
}

@media (hover: hover) {
    .btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
    .winner-btn:hover, .icon-btn:hover:not(:disabled), .btn-ghost:hover { filter: brightness(1.15); }
}
