:root {
    --blue: #2187d8;
    --blue-dark: #1267ae;
    --sky: #8ed8ff;
    --yellow: #ffd84d;
    --pink: #ff7fad;
    --green: #76cf70;
    --ink: #24445f;
    --panel: rgba(255,255,255,.95);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    min-height: 100dvh;
    overflow: hidden;
    color: var(--ink);
    background: linear-gradient(#8ed8ff 0%, #dff5ff 62%, #eefadf 100%);
    font-family: "Trebuchet MS", Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; }
button { cursor: pointer; }

.sky-decoration { position: fixed; inset: 0; overflow: hidden; pointer-events: none; }
.sun {
    position: absolute; right: 4vw; top: 4vh; width: 86px; height: 86px;
    background: #ffe469; border: 8px solid rgba(255,255,255,.45); border-radius: 50%;
    box-shadow: 0 0 0 12px rgba(255,228,105,.18);
}

.coloring-app {
    position: relative; z-index: 1; height: 100dvh; min-height: 560px;
    display: grid; grid-template-columns: 285px minmax(0,1fr); gap: 14px; padding: 14px;
}

.kids-panel {
    min-height: 0; overflow: auto; scrollbar-width: thin;
    padding: 15px; background: var(--panel); border: 4px solid #fff;
    border-radius: 28px; box-shadow: 0 10px 25px rgba(38,112,154,.18);
}
.back-link {
    display: inline-block; margin-bottom: 9px; color: var(--blue-dark); text-decoration: none;
    font-size: 13px; font-weight: 800;
}
.title-block { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; }
.pencil-badge {
    width: 54px; height: 54px; flex: 0 0 54px; display: grid; place-items: center;
    border-radius: 18px; background: var(--yellow); font-size: 30px; transform: rotate(-4deg);
    box-shadow: 0 5px 0 #e5b92d;
}
h1 { margin: 0; color: #1773bd; font-size: 21px; line-height: 1.05; white-space: nowrap; }
.title-block p { margin: 5px 0 0; font-size: 12px; color: #648097; }

.palette-card, .size-card {
    padding: 12px; margin-top: 10px; background: #f8fcff; border: 2px solid #d8eefb; border-radius: 20px;
}
.palette-card h2, .size-card label { display: block; margin: 0 0 10px; font-size: 14px; font-weight: 900; color: #34769e; }
.palette {
    display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px;
}
.color-button {
    position: relative; width: 100%; aspect-ratio: 1; border: 3px solid white; border-radius: 50%;
    box-shadow: 0 2px 0 rgba(53,101,133,.18), 0 0 0 1px rgba(47,89,117,.12);
    transition: transform .1s ease;
}
.color-button:hover { transform: scale(1.08); }
.color-button.active { transform: scale(1.12); box-shadow: 0 0 0 3px white, 0 0 0 6px #2b89cc; }
.color-button.active::after {
    content: "✓"; position: absolute; inset: 0; display: grid; place-items: center; color: white;
    font-weight: 900; text-shadow: 0 1px 2px rgba(0,0,0,.55); font-size: 17px;
}
.color-button[data-light="1"].active::after { color: #53616d; text-shadow: none; }

.size-row { display: grid; grid-template-columns: 16px 1fr 32px; align-items: center; gap: 8px; }
.size-row input { width: 100%; accent-color: #ff759d; }
.dot { display: block; border-radius: 50%; background: #31485b; justify-self: center; }
.small-dot { width: 7px; height: 7px; }
.big-dot { width: 24px; height: 24px; }

.big-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin-top: 12px; }
.kid-button {
    min-height: 66px; border: 0; border-radius: 19px; color: white; font-weight: 900; font-size: 12px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    box-shadow: 0 5px 0 rgba(0,0,0,.13); transition: transform .08s ease;
}
.kid-button:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.13); }
.kid-button.active { outline: 5px solid rgba(255,255,255,.9); box-shadow: 0 0 0 5px #3b82bd, 0 5px 0 rgba(0,0,0,.13); }
.button-icon { font-size: 25px; }
.eraser-button { background: #63c77b; }
.undo-button { background: #6da8e8; }
.clear-button { background: #ff8e6d; }
.kid-button:disabled { opacity: .42; cursor: default; box-shadow: 0 3px 0 rgba(0,0,0,.08); transform: none; }

.bottom-actions { display: grid; gap: 8px; margin-top: 13px; }
.little-action, .download-action, .empty-message button {
    border: 0; border-radius: 15px; min-height: 43px; font-weight: 900;
}
.little-action { color: #3b718f; background: #e6f5ff; border: 2px solid #c8eafb; }
.download-action { color: white; background: #2589d8; box-shadow: 0 4px 0 #1768a9; }

.drawing-zone { min-width: 0; min-height: 0; position: relative; display: flex; flex-direction: column; }
.tip {
    align-self: center; margin-bottom: 7px; padding: 7px 18px; color: #5b7181; background: rgba(255,255,255,.8);
    border: 2px solid white; border-radius: 999px; font-size: 13px; font-weight: 900;
}
.paper-holder {
    flex: 1 1 auto; min-height: 0; position: relative; display: grid; place-items: center;
    padding: 12px; overflow: hidden;
}
.paper {
    position: relative; background: white; border: 8px solid white; border-radius: 22px;
    box-shadow: 0 12px 26px rgba(45,99,133,.24), 0 0 0 2px rgba(96,144,171,.12);
    overflow: hidden; touch-action: none; user-select: none;
}
.paper canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#paintCanvas { z-index: 1; background: white; cursor: crosshair; touch-action: none; }
#outlineCanvas { z-index: 2; pointer-events: none; }
.brush-cursor {
    position: absolute; z-index: 3; width: 22px; height: 22px; border: 2px solid rgba(0,0,0,.55); border-radius: 50%;
    box-shadow: 0 0 0 2px rgba(255,255,255,.8); pointer-events: none; transform: translate(-50%,-50%); display: none;
}

.loading, .empty-message {
    position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center; padding: 25px; color: #49718b;
}
.loading[hidden], .empty-message[hidden], .paper[hidden] { display: none !important; }
.rainbow-spinner {
    width: 52px; height: 52px; margin-bottom: 12px; border-radius: 50%;
    border: 7px solid #ffd85e; border-top-color: #ff799e; border-right-color: #58c779; border-bottom-color: #5eb8ee;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.empty-picture { font-size: 52px; }
.empty-message h2 { margin: 7px 0 5px; color: #267fba; }
.empty-message p { margin: 0 0 15px; max-width: 420px; }
.empty-message button { padding: 0 18px; background: #ff8cb0; color: white; box-shadow: 0 4px 0 #db6488; }

.toast {
    position: fixed; z-index: 50; left: 50%; bottom: 20px; transform: translate(-50%, 15px);
    padding: 11px 17px; border-radius: 999px; background: #24445f; color: white; font-weight: 900; font-size: 13px;
    opacity: 0; transition: .2s ease; pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%,0); }

@media (max-width: 820px) {
    body { overflow: auto; }
    .sun { width: 62px; height: 62px; }
    .coloring-app { min-height: 100dvh; height: auto; display: flex; flex-direction: column; padding: 8px; gap: 8px; }
    .kids-panel { overflow: visible; padding: 10px; border-radius: 22px; }
    .back-link { display: none; }
    .title-block { margin: 0 0 8px; padding-right: 150px; }
    .pencil-badge { width: 40px; height: 40px; flex-basis: 40px; font-size: 23px; border-radius: 13px; box-shadow: 0 3px 0 #e5b92d; }
    h1 { font-size: 18px; }
    .title-block p { display: none; }
    .palette-card { margin-top: 0; padding: 8px 9px; }
    .palette-card h2 { display: none; }
    .palette { grid-template-columns: repeat(12, 1fr); gap: 5px; }
    .color-button { border-width: 2px; }
    .size-card { margin-top: 7px; padding: 8px 10px; }
    .size-card label { display: none; }
    .big-actions { position: absolute; right: 12px; top: 11px; display: flex; gap: 5px; margin: 0; }
    .kid-button { min-height: 42px; width: 44px; border-radius: 13px; box-shadow: 0 3px 0 rgba(0,0,0,.13); font-size: 0; }
    .button-icon { font-size: 22px; }
    .bottom-actions { grid-template-columns: 1fr 1.15fr; gap: 7px; margin-top: 7px; }
    .little-action, .download-action { min-height: 39px; }
    .tip { display: none; }
    .drawing-zone { min-height: 68vh; }
    .paper-holder { min-height: 68vh; padding: 5px; }
    .paper { border-width: 5px; border-radius: 15px; }
}

@media (max-width: 520px) {
    .title-block { padding-right: 145px; }
    .palette { grid-template-columns: repeat(8, 1fr); }
    .color-button:nth-child(n+17) { display: none; }
    .drawing-zone, .paper-holder { min-height: 64vh; }
}


/* Téléphone tenu en paysage : même logique que sur PC, simplement réduite.
   Le panneau reste sur toute la hauteur et le coloriage occupe tout le reste. */
@media (orientation: landscape) and (max-height: 620px) {
    html, body { height: 100%; }
    body { overflow: hidden; }

    .sun { width: 48px; height: 48px; top: 12px; right: 16px; border-width: 5px; }

    .coloring-app {
        height: 100dvh;
        min-height: 0;
        display: grid;
        grid-template-columns: clamp(205px, 27vw, 235px) minmax(0, 1fr);
        align-items: stretch;
        gap: 8px;
        padding: 7px;
    }

    .kids-panel {
        min-height: 0;
        height: 100%;
        max-height: none;
        align-self: stretch;
        overflow-y: auto;
        padding: 9px;
        border-width: 3px;
        border-radius: 20px;
    }

    .drawing-zone { min-height: 0; height: 100%; align-self: stretch; }
    .back-link { display: none; }

    .title-block {
        display: flex;
        margin: 0 0 7px;
        padding-right: 0;
        gap: 7px;
        align-items: center;
    }
    .pencil-badge {
        width: 34px; height: 34px; flex-basis: 34px;
        border-radius: 10px; font-size: 19px; box-shadow: 0 2px 0 #e5b92d;
    }
    h1 { font-size: 15px; white-space: nowrap; }
    .title-block p { display: none; }

    .palette-card { margin-top: 0; padding: 6px 7px 7px; border-radius: 14px; }
    .palette-card h2 { display: block; margin-bottom: 5px; font-size: 11px; }
    .palette { grid-template-columns: repeat(6, 1fr); gap: 4px; }
    .color-button { border-width: 2px; }
    .color-button:nth-child(n+17) { display: block; }

    .size-card { margin-top: 6px; padding: 6px 7px; border-radius: 13px; }
    .size-card label { display: block; margin-bottom: 4px; font-size: 11px; }
    .size-row { grid-template-columns: 10px 1fr 24px; gap: 5px; }
    .big-dot { width: 19px; height: 19px; }

    .big-actions {
        position: static;
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 5px;
        margin-top: 6px;
    }
    .kid-button {
        width: auto;
        min-height: 43px;
        border-radius: 12px;
        box-shadow: 0 2px 0 rgba(0,0,0,.13);
        font-size: 0;
    }
    .button-icon { font-size: 19px; }

    .bottom-actions { grid-template-columns: 1fr 1fr; gap: 5px; margin-top: 6px; }
    .little-action, .download-action {
        min-height: 35px;
        border-radius: 11px;
        font-size: 11px;
        padding-inline: 4px;
    }

    .tip { display: none; }
    .paper-holder { min-height: 0; height: 100%; padding: 3px; }
    .paper { border-width: 4px; border-radius: 13px; }
}

/* Sur les téléphones très bas, on garde le même dessin d'interface,
   on resserre seulement les espacements au lieu de supprimer le titre. */
@media (orientation: landscape) and (max-height: 410px) {
    .coloring-app { grid-template-columns: clamp(195px, 26vw, 220px) minmax(0, 1fr); padding: 5px; gap: 6px; }
    .kids-panel { padding: 6px; border-radius: 17px; }
    .title-block { margin-bottom: 4px; }
    .pencil-badge { width: 30px; height: 30px; flex-basis: 30px; font-size: 17px; }
    h1 { font-size: 14px; }
    .palette-card { padding: 4px 6px 5px; }
    .palette-card h2, .size-card label { margin-bottom: 3px; font-size: 10px; }
    .palette { gap: 3px; }
    .size-card { margin-top: 4px; padding: 4px 6px; }
    .big-actions, .bottom-actions { margin-top: 4px; }
    .kid-button { min-height: 34px; }
    .button-icon { font-size: 17px; }
    .little-action, .download-action { min-height: 31px; font-size: 10px; }
}

/* ---- v6 : modèle couleur + zoom tactile ---- */
.paper {
    transform-origin: center center;
    will-change: transform;
}

.model-button,
.zoom-reset {
    position: absolute;
    z-index: 12;
    border: 3px solid #fff;
    border-radius: 999px;
    min-height: 40px;
    padding: 6px 12px;
    font-weight: 900;
    color: #2d668b;
    background: rgba(255,255,255,.94);
    box-shadow: 0 4px 10px rgba(44,95,128,.18);
}
.model-button { left: 8px; top: 3px; }
.zoom-reset { right: 8px; bottom: 7px; color: #fff; background: #2a8bd4; }
.model-button:active,
.zoom-reset:active { transform: translateY(2px); }

.model-overlay {
    position: fixed;
    z-index: 100;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(26,61,84,.72);
    backdrop-filter: blur(3px);
}
.model-overlay[hidden] { display: none !important; }
.model-dialog {
    position: relative;
    max-width: min(92vw, 900px);
    max-height: 92dvh;
    padding: 12px 12px 9px;
    background: #fff;
    border: 5px solid #fff;
    border-radius: 24px;
    box-shadow: 0 18px 50px rgba(0,0,0,.28);
    display: flex;
    flex-direction: column;
    align-items: center;
}
.model-dialog img {
    display: block;
    max-width: 100%;
    max-height: calc(92dvh - 72px);
    object-fit: contain;
    border-radius: 15px;
}
.model-close {
    position: absolute;
    z-index: 2;
    top: -14px;
    right: -14px;
    width: 44px;
    height: 44px;
    border: 4px solid #fff;
    border-radius: 50%;
    color: #fff;
    background: #ff6179;
    font-size: 28px;
    font-weight: 900;
    line-height: 1;
    box-shadow: 0 4px 10px rgba(0,0,0,.18);
}
.model-caption {
    padding-top: 7px;
    color: #37799f;
    font-size: 14px;
    font-weight: 900;
}
body.model-open { overflow: hidden; }

@media (max-width: 820px) {
    .model-button,
    .zoom-reset {
        min-height: 36px;
        padding: 5px 9px;
        border-width: 2px;
        font-size: 12px;
    }
    .model-button { top: 2px; left: 2px; }
    .zoom-reset { right: 3px; bottom: 3px; }
}

@media (orientation: landscape) and (max-height: 620px) {
    .model-button,
    .zoom-reset {
        min-height: 32px;
        padding: 4px 8px;
        font-size: 11px;
    }
    .model-dialog {
        max-width: 88vw;
        max-height: 92dvh;
        padding: 8px;
        border-width: 3px;
        border-radius: 18px;
    }
    .model-dialog img { max-height: calc(92dvh - 48px); }
    .model-caption { display: none; }
    .model-close {
        top: -9px;
        right: -9px;
        width: 36px;
        height: 36px;
        border-width: 3px;
        font-size: 23px;
    }
}
