:root {
    --sky: #9cddff;
    --sky2: #e7f8ff;
    --ink: #23415a;
    --muted: #69849a;
    --panel: rgba(255,255,255,.94);
    --yellow: #ffd75e;
    --blue: #4fb6f5;
    --green: #69d28a;
    --pink: #ff8eae;
    --purple: #a88cf5;
    --border: rgba(72,127,164,.18);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: "Trebuchet MS", "Segoe UI", Arial, sans-serif;
    color: var(--ink);
    background: linear-gradient(180deg, var(--sky) 0, var(--sky2) 67%, #f6fcff 100%);
    overflow-x: hidden;
}
button, a { font: inherit; }
button { color: inherit; }

.sky { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: -1; }
.sun { position: absolute; width: 92px; height: 92px; border-radius: 50%; background: #ffe66f; right: 5vw; top: 42px; box-shadow: 0 0 0 14px rgba(255,230,111,.17); }

/* CHOIX DE DIFFICULTÉ */
.choice-page { min-height: 100vh; display: grid; place-items: center; padding: 28px 16px; }
.difficulty-card {
    width: min(820px, 100%);
    background: rgba(255,255,255,.95);
    border: 3px solid rgba(255,255,255,.75);
    border-radius: 34px;
    padding: 28px;
    text-align: center;
    box-shadow: 0 18px 55px rgba(57,122,165,.18);
}
.difficulty-icon { font-size: 62px; line-height: 1; }
.difficulty-card h1 { margin: 10px 0 4px; font-size: clamp(29px, 5vw, 46px); color: #28628a; }
.difficulty-intro { margin: 0 0 24px; color: var(--muted); font-size: 18px; }
.difficulty-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.level {
    min-height: 145px;
    padding: 18px 10px;
    border-radius: 24px;
    text-decoration: none;
    color: var(--ink);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 5px;
    border: 2px solid rgba(35,65,90,.07);
    transition: transform .16s ease, filter .16s ease;
}
.level-emoji { font-size: 35px; }
.level strong { font-size: 17px; }
.level span:last-child { font-weight: 700; opacity: .72; }
.level-6 { background: #fff3a8; }
.level-12 { background: #c9f3d6; }
.level-20 { background: #cfeeff; }
.level-42 { background: #e1d5ff; }
.level-56 { background: #ffd1df; }
@media (hover:hover) and (pointer:fine) { .level:hover { transform: translateY(-4px) rotate(-1deg); filter: saturate(1.08); } }
.home-link { display: inline-block; margin-top: 24px; color: #39769e; text-decoration: none; font-weight: 700; }

/* JEU */
.game-page { height: 100vh; overflow: hidden; }
.puzzle-app { height: 100vh; display: grid; grid-template-rows: auto minmax(0,1fr); padding: 10px; gap: 10px; }
.game-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    background: var(--panel);
    border-radius: 22px;
    box-shadow: 0 8px 28px rgba(48,112,154,.15);
    border: 1px solid var(--border);
    z-index: 20;
    min-width: 0;
}
.round-button, .tool-button {
    border: 0;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
}
.round-button { flex: 0 0 44px; height: 44px; border-radius: 50%; background: #e8f6ff; color: #2d709d; font-size: 24px; }
.game-title { display: flex; align-items: center; gap: 9px; min-width: 0; margin-right: auto; }
.game-title > span { font-size: 32px; }
.game-title strong { display: block; font-size: 21px; line-height: 1; color: #306c94; }
.game-title small { display: block; color: var(--muted); margin-top: 3px; white-space: nowrap; }
.status-pill { height: 42px; padding: 0 13px; border-radius: 15px; background: #eef9ff; display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; }
.timer-pill { background: #fff7d8; }
.toolbar-actions { display: flex; gap: 8px; }
.tool-button { min-height: 42px; padding: 0 13px; border-radius: 15px; background: #f1edff; }
#shuffleBtn { background: #ebf9ee; }
.toggle-button { background: #eef6ff; transition: opacity .15s ease, background .15s ease, transform .15s ease; }
.toggle-button.is-active { background: #dff3ff; box-shadow: inset 0 0 0 2px rgba(61,153,211,.18); }
.toggle-button:not(.is-active) { background: #f2f4f5; opacity: .58; }
@media (hover:hover) and (pointer:fine) { .toggle-button:hover, #shuffleBtn:hover { transform: translateY(-1px); } }

.play-area {
    position: relative;
    min-height: 0;
    overflow: hidden;
    border-radius: 28px;
    background: rgba(255,255,255,.30);
    border: 2px solid rgba(255,255,255,.56);
    box-shadow: inset 0 0 40px rgba(255,255,255,.22);
    touch-action: none;
    user-select: none;
}
.board {
    position: absolute;
    border-radius: 18px;
    overflow: hidden;
    background: #fff;
    box-shadow: 0 8px 22px rgba(38,88,121,.16);
    border: 3px solid rgba(255,255,255,.96);
    z-index: 1;
}
.board img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; opacity: 0; display: none; pointer-events: none; user-select: none; }
.board canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: none; pointer-events: none; }
.pieces-layer { position: absolute; inset: 0; z-index: 5; pointer-events: none; }
.puzzle-piece {
    position: absolute;
    pointer-events: auto;
    touch-action: none;
    cursor: grab;
    filter: drop-shadow(0 4px 4px rgba(32,67,90,.22));
    transition: filter .12s ease;
}
.puzzle-piece.dragging { cursor: grabbing; filter: drop-shadow(0 9px 8px rgba(32,67,90,.32)); z-index: 9999 !important; }
.puzzle-piece.placed { cursor: default; filter: none; pointer-events: none; }

.loading-card, .error-card {
    position: absolute;
    left: 50%; top: 50%; transform: translate(-50%,-50%);
    z-index: 30;
    background: rgba(255,255,255,.94);
    padding: 24px 28px;
    border-radius: 25px;
    text-align: center;
    box-shadow: 0 10px 35px rgba(45,103,143,.16);
}
.spinner { width: 44px; height: 44px; margin: 0 auto 10px; border-radius: 50%; border: 6px solid #dff3ff; border-top-color: #4fb6f5; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-card > div { font-size: 48px; }
.error-card h1 { margin: 5px 0; }
.error-card p { margin: 6px 0 15px; }
.error-card a { display: inline-block; padding: 10px 14px; background: #dff3ff; border-radius: 12px; color: #286c99; font-weight: 800; text-decoration: none; }
.mobile-help { position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%); z-index: 15; padding: 7px 12px; border-radius: 999px; background: rgba(255,255,255,.89); font-size: 12px; font-weight: 700; white-space: nowrap; color: #527288; }

.model-overlay, .finish-overlay { position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center; padding: 18px; background: rgba(30,64,88,.57); backdrop-filter: blur(5px); }
.model-overlay[hidden], .finish-overlay[hidden] { display: none; }
.model-dialog { position: relative; width: min(720px, 94vw); max-height: 90vh; padding: 16px 16px 14px; border-radius: 26px; background: white; text-align: center; box-shadow: 0 18px 60px rgba(0,0,0,.24); }
.model-dialog img { display: block; max-width: 100%; max-height: calc(90vh - 70px); margin: auto; border-radius: 15px; object-fit: contain; }
.model-dialog strong { display: block; margin-top: 8px; color: #47748f; }
.model-close { position: absolute; right: 8px; top: 8px; z-index: 2; width: 42px; height: 42px; border: 0; border-radius: 50%; background: white; box-shadow: 0 3px 12px rgba(0,0,0,.18); font-size: 28px; cursor: pointer; }
.finish-card { width: min(500px, 94vw); padding: 28px; background: #fff; border-radius: 30px; text-align: center; box-shadow: 0 20px 65px rgba(0,0,0,.25); }
.finish-stars { font-size: 34px; letter-spacing: 5px; }
.finish-card h1 { font-size: 46px; margin: 7px 0 4px; color: #2d7cab; }
.finish-card p { font-size: 18px; }
.finish-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; }
.finish-button { padding: 13px 12px; border-radius: 16px; text-decoration: none; font-weight: 900; color: #26455a; }
.replay { background: #dff1ff; }
.another { background: #d8f5df; }
.confetti { position: fixed; z-index: 1001; width: 10px; height: 18px; border-radius: 3px; pointer-events: none; animation: fall 1.8s ease-in forwards; }
@keyframes fall { from { transform: translateY(-60px) rotate(0deg); opacity: 1; } to { transform: translateY(110vh) rotate(560deg); opacity: .8; } }

@media (max-width: 820px) {
    .difficulty-grid { grid-template-columns: repeat(2, 1fr); }
    .difficulty-grid .level:last-child { grid-column: 1 / -1; min-height: 112px; }
    .difficulty-card { padding: 20px 16px; border-radius: 28px; }

    .puzzle-app { padding: 6px; gap: 6px; }
    .game-bar { border-radius: 18px; padding: 7px 8px; gap: 6px; flex-wrap: wrap; }
    .round-button { flex-basis: 40px; height: 40px; }
    .game-title { flex: 1 1 180px; }
    .game-title > span { font-size: 27px; }
    .game-title strong { font-size: 18px; }
    .game-title small { font-size: 11px; }
    .status-pill { height: 36px; padding: 0 9px; font-size: 13px; }
    .toolbar-actions { margin-left: auto; }
    .tool-button { min-height: 36px; padding: 0 10px; font-size: 13px; }
    .play-area { border-radius: 20px; }
    .mobile-help { display: block !important; }
}

/* Le paysage mobile reprend la logique PC : barre compacte en haut, grand jeu dessous. */
@media (max-height: 520px) and (orientation: landscape) {
    .puzzle-app { padding: 4px; gap: 4px; }
    .game-bar { flex-wrap: nowrap; min-height: 48px; padding: 4px 7px; border-radius: 14px; }
    .round-button { flex-basis: 36px; height: 36px; font-size: 20px; }
    .game-title { flex: 1 1 auto; }
    .game-title > span { display: none; }
    .game-title strong { font-size: 16px; }
    .game-title small { font-size: 10px; }
    .status-pill { height: 34px; }
    .tool-button { min-height: 34px; }
    .tool-button span { display: none; }
    .mobile-help { bottom: 4px; }
}

@media (max-width: 460px) and (orientation: portrait) {
    .choice-page { padding: 14px 10px; }
    .difficulty-card h1 { font-size: 31px; }
    .difficulty-icon { font-size: 48px; }
    .difficulty-intro { font-size: 15px; }
    .level { min-height: 116px; padding: 10px 6px; }
    .level-emoji { font-size: 28px; }
    .level strong { font-size: 15px; }
    .game-title small { display: none; }
    .timer-pill span { display: none; }
    .tool-button span { display: none; }
}
