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

[hidden] {
    display: none !important;
}

html,
body {
    height: 100%;
}

body {
    font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial, sans-serif;
    color: #111;
    background: #0d0f16 url(../assets/images/background-marker.png) center / cover no-repeat fixed;
    overflow: hidden;
}

button {
    font: inherit;
    color: inherit;
}

/* ---------- LAYOUT ----------
   Desktop / paysage : logo | avatar | bibliothèque
   Mobile portrait  : logo + actions / avatar / bibliothèque / crédits */
.app {
    --gap: clamp(12px, 2vw, 32px);
    height: 100vh;
    height: 100dvh;
    display: grid;
    gap: var(--gap);
    padding: var(--gap);
    padding-top: max(var(--gap), env(safe-area-inset-top));
    padding-bottom: max(var(--gap), env(safe-area-inset-bottom));
    grid-template-columns: clamp(110px, 15vw, 260px) minmax(0, 1fr) clamp(300px, 32vw, 500px);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas:
        "brand   stage library"
        "credits stage actions";
}

.brand {
    grid-area: brand;
}

.stage-area {
    grid-area: stage;
}

.library {
    grid-area: library;
}

.export {
    grid-area: actions;
}

.credits {
    grid-area: credits;
}

/* ---------- LOGO ---------- */
.logo {
    display: block;
    width: 100%;
    aspect-ratio: 180 / 138;
    background: url('../assets/images/logo.svg') left top / contain no-repeat;
    rotate: -4deg;
}

/* Petite note sous le logo (façon post-it) */
.note {
    margin-top: clamp(16px, 3dvh, 32px);
    padding: .9em .9em 1em;
    background: #f3f182;
    color: #111;
    font-size: clamp(11px, 1vw, 14px);
    font-weight: 600;
    line-height: 1.35;
    rotate: 2deg;
    box-shadow: 0 8px 16px rgba(0, 0, 0, .35);
}

/* ---------- STAGE / AVATAR ---------- */
.stage-area {
    container-type: size;
    min-width: 0;
    min-height: 0;
    display: grid;
    place-items: center;
}

.stage-wrap {
    /* carré le plus grand possible, avec de la marge pour la rotation et la punaise */
    width: calc(min(100cqw, 100cqh) * 0.9);
    aspect-ratio: 1 / 1;
    background: #fff;
    rotate: 3deg;
    position: relative;
    box-shadow: 0 18px 40px rgba(0, 0, 0, .45);
}

.punaise {
    position: absolute;
    left: 50%;
    top: 0;
    width: 12%;
    aspect-ratio: 600 / 564;
    translate: -50% -35%;
    background: url('../assets/images/punaise.png') center / contain no-repeat;
    z-index: 1;
    pointer-events: none;
}

#stage {
    width: 100%;
    height: 100%;
    display: block;
}

/* ---------- DRAG & DROP SUR L'AVATAR ---------- */
#stage {
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

#stage:focus-visible {
    outline: 3px solid #6ad8f3;
    outline-offset: 4px;
}

.stage-wrap.dragging #stage {
    cursor: grabbing !important;
}

.drag-outline {
    fill: rgba(106, 216, 243, .08);
    stroke: #111;
    stroke-width: 3;
    stroke-dasharray: 14 10;
    pointer-events: none;
}

.drag-hint {
    position: absolute;
    left: 50%;
    bottom: 0;
    translate: -50% 50%;
    max-width: 90%;
    padding: .45em 1em;
    border-radius: 999px;
    background: #111;
    color: #fff;
    font-size: clamp(11px, 1.6cqmin, 14px);
    font-weight: 700;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    opacity: 0;
    transition: opacity .3s ease;
}

body[data-draggable="true"]:not(.drag-learned) .drag-hint {
    opacity: 1;
}

/* ---------- BIBLIOTHÈQUE ---------- */
.library {
    container-type: inline-size;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* Le cahier : la feuille de papier qui contient les dessins */
.sheet {
    position: relative;
    z-index: 1;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    /* le PNG a une marge transparente : on l'agrandit pour que le papier colle aux bords du bloc */
    background: url(../assets/images/feuille.png) 60.7% 51.9% / 109.8% 110.4% no-repeat;
    filter: drop-shadow(0 12px 18px rgba(0, 0, 0, .4));
    padding: 4% 5% 5% 4%;
}

/* ---------- TABS (post-it qui monte) ---------- */
.tabs {
    --tab-h: 40px;
    --tab-h-hover: 58px;
    --tab-h-active: 66px;
    display: flex;
    flex-wrap: nowrap;
    align-items: flex-end;
    gap: 0;
    height: var(--tab-h-active);
    padding: 0 3%;
    /* les post-it passent sous la feuille */
    margin-bottom: -8px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    flex-shrink: 0;
}

.tabs::-webkit-scrollbar {
    display: none;
}

.tab {
    position: relative;
    flex: 1 0 auto;
    display: inline-flex;
    align-items: flex-start;
    justify-content: center;
    height: var(--tab-h);
    padding: .6em .4em 0;
    background: var(--tab-bg, #fff);
    color: #111;
    font-weight: 800;
    font-size: clamp(10.5px, 2.95cqi, 15px);
    white-space: nowrap;
    border: 0;
    border-radius: 8px 8px 0 0;
    box-shadow: inset 0 -10px 12px -8px rgba(0, 0, 0, .18);
    transform-origin: bottom center;
    transition: height .2s ease, background .2s ease;
    cursor: pointer;
}

/* les post-it se chevauchent un peu */
.tab + .tab {
    margin-left: -4px;
}

/* Hover : s'allonge et s'élève avec effet bounce (souris uniquement) */
@media (hover: hover) {
    .tab:hover {
        height: var(--tab-h-hover);
        z-index: 1;
        animation: bounceUp 0.45s cubic-bezier(.68, -0.55, .27, 1.55);
    }
}

/* Active : encore plus grand + rebond plus prononcé */
.tab[aria-selected="true"] {
    height: var(--tab-h-active);
    z-index: 2;
    animation: bounceActive 0.55s cubic-bezier(.68, -0.55, .27, 1.55);
}

.tab:focus-visible {
    outline: 3px solid #000;
    outline-offset: -3px;
}

/* --------- ANIMATIONS --------- */
@keyframes bounceUp {
    0% { transform: scaleY(1); }
    40% { transform: scaleY(1.1); }
    65% { transform: scaleY(0.95); }
    85% { transform: scaleY(1.03); }
    100% { transform: scaleY(1); }
}

@keyframes bounceActive {
    0% { transform: scaleY(1); }
    35% { transform: scaleY(1.15); }
    60% { transform: scaleY(0.95); }
    80% { transform: scaleY(1.05); }
    100% { transform: scaleY(1); }
}

/* ---------- PALETTE PAR CATÉGORIE ----------
     On affecte la couleur via des variables CSS par data-cat.
     Ajuste/ajoute selon tes IDs réels (ils viennent de index.json). */

/* Fond */
.tab[data-cat="Background"] {
    --tab-bg: #ffaa56;
}

/* Visage (formes) */
.tab[data-cat="face"] {
    --tab-bg: #ff7575;
}

/* Yeux */
.tab[data-cat="eyes"],
.tab[data-cat="eye"] {
    --tab-bg: #6ad8f3;
}

/* Sourcils */
.tab[data-cat="brows"],
.tab[data-cat="brow"] {
    --tab-bg: #b6f895;
}

/* Nez */
.tab[data-cat="nose"] {
    --tab-bg: #a2a6fc;
}

/* Bouche */
.tab[data-cat="mouth"] {
    --tab-bg: #fbb9ed;
}

/* Cheveux */
.tab[data-cat="hair"] {
    --tab-bg: #f3f182;
}

/* Motifs (catégorie multiple) */
.tab[data-cat="Motifs"] {
    --tab-bg: #ffe4cd;
}

/* Accessoires */
.tab[data-cat="accessories"],
.tab[data-cat="accessory"] {
    --tab-bg: #00ffb3;
}

/* ---------- PANES (contenu catégories) ---------- */
.panes {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, .25) transparent;
}

.panes::-webkit-scrollbar {
    width: 8px;
}

.panes::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, .25);
    border-radius: 8px;
}

.panes::-webkit-scrollbar-track {
    background: transparent;
}

.panes .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(clamp(58px, 20cqi, 96px), 1fr));
    gap: clamp(8px, 2.5cqi, 16px);
    /* de l'air pour la rotation aléatoire des cartes */
    padding: 10px;
}

.pane {
    display: none;
}

.pane[aria-hidden="false"] {
    display: block;
}

/* ---------- CARDS ---------- */
.card {
    position: relative;
    display: flex;
    flex-direction: column;
    padding: 2px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: outline .12s ease, box-shadow .12s ease, transform .06s ease;
}

.card img.thumb {
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
    object-fit: contain;
    display: block;
    transition: transform 0.2s ease;
}

/* Animation stop motion */
@media (hover: hover) {
    .card:hover img {
        animation: stopMotionRotate 0.4s infinite steps(2, end);
        transform: scale(1.2);
    }
}

@keyframes stopMotionRotate {
    0% { transform: rotate(-10deg) scale(1.2); }
    50% { transform: rotate(10deg) scale(1.2); }
    100% { transform: rotate(-10deg) scale(1.2); }
}

.card.active {
    background-color: rgb(255, 255, 250);
    border: 10px solid transparent;
    border-image-source: url('../assets/images/bordure-irreguliere.svg');
    border-image-slice: 50 fill;
    border-image-repeat: stretch;
    border-radius: 12px;
    /* compense la bordure pour que la carte garde la même taille */
    margin: -10px;
}

.card.disabled {
    opacity: .45;
}

.card.at-cap {
    opacity: .5;
    pointer-events: none;
    filter: grayscale(.2);
}

.card svg {
    background: #fff;
    width: 100%;
    height: auto;
    display: block;
}

.card label {
    font-size: 0;
}

/* Placeholder "— Aucun —" */
.none {
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1 / 1;
    border: 2px dashed rgba(0, 0, 0, .3);
    border-radius: 6px;
    color: rgba(0, 0, 0, .5);
    font-size: 11px;
    text-align: center;
}

/* Bouton remove (si activé côté JS) */
.card .remove {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    top: 6px;
    right: 6px;
    width: 22px;
    height: 22px;
    border: 1px solid #ccc;
    border-radius: 50%;
    background: #fff;
    font-size: 14px;
    cursor: pointer;
    opacity: 0;
    transition: opacity .15s ease-in-out, border-color .15s ease-in-out;
}

.card:hover .remove,
.card.active .remove {
    opacity: 1;
}

.card.disabled .remove {
    display: none;
}

/* ---------- TRANSFORM PANEL (X/Y + couleur) ---------- */
.transformers {
    flex-shrink: 0;
    font-size: 13px;
}

.transformers:has(.ctrl:not([hidden])) {
    padding-top: .75em;
    border-top: 1px dashed rgba(0, 0, 0, .25);
}

#transformPanel .row,
#colorLabel {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 4px 0;
    font-weight: 600;
}

#transformPanel .row label {
    width: 80px;
}

#transformPanel .row input[type="range"] {
    flex: 1;
    min-width: 0;
}

#transformPanel .row span {
    display: none;
}

#colorLabel input[type="color"] {
    inline-size: 36px;
    block-size: 28px;
    padding: 0;
    border: 1px solid #ccc;
    border-radius: 6px;
    background: #fff;
}

/* ---------- EXPORT / ACTIONS ---------- */
.export .row {
    display: flex;
    justify-content: center;
    gap: clamp(16px, 3vw, 48px);
}

.export button {
    display: grid;
    place-items: center;
    width: 48px;
    height: 48px;
    background: none;
    border: none;
    cursor: pointer;
    transition: transform .12s ease;
}

.export button img {
    width: 32px;
    height: 32px;
}

@media (hover: hover) {
    .export button:hover {
        transform: scale(1.2);
    }
}

.export button:active {
    transform: translateY(1px) scale(.95);
}

/* ---------- CRÉDITS ---------- */
.credits {
    align-self: end;
}

.hint {
    display: block;
    color: rgba(255, 255, 255, .5);
    font-size: 11px;
    line-height: 1.4;
}

/* ---------- TABLETTE (paysage étroit) ---------- */
@media (max-width: 1100px) and (orientation: landscape) {
    .app {
        grid-template-columns: clamp(90px, 12vw, 160px) minmax(0, 1fr) clamp(280px, 36vw, 420px);
    }

    .hint {
        font-size: 10px;
    }
}

/* Écrans très bas (téléphone en paysage) */
@media (max-height: 520px) and (orientation: landscape) {
    .app {
        --gap: 10px;
        grid-template-columns: clamp(80px, 11vw, 140px) minmax(0, 1fr) clamp(300px, 42vw, 440px);
    }

    .tabs {
        --tab-h: 30px;
        --tab-h-hover: 40px;
        --tab-h-active: 46px;
    }

    .export button {
        width: 38px;
        height: 38px;
    }

    .export button img {
        width: 26px;
        height: 26px;
    }

    .credits {
        display: none;
    }

    .note {
        margin-top: 10px;
        padding: .6em;
        font-size: 10px;
    }
}

/* ---------- MOBILE / PORTRAIT ---------- */
@media (orientation: portrait) and (max-width: 1100px) {
    .app {
        --gap: 12px;
        padding-inline: 16px;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-rows: auto auto minmax(0, 1fr) auto;
        grid-template-areas:
            "brand   actions"
            "stage   stage"
            "library library"
            "credits credits";
        row-gap: 10px;
    }

    .brand {
        align-self: center;
    }

    /* sur mobile, la bulle d'aide sous l'avatar suffit */
    .note {
        display: none;
    }

    .logo {
        width: auto;
        height: clamp(48px, 9dvh, 110px);
        justify-self: start;
        rotate: -3deg;
    }

    .stage-area {
        /* le carré prend la largeur dispo, mais laisse la place à la bibliothèque */
        height: min(calc(100vw - 32px), 44dvh);
    }

    .stage-wrap {
        width: calc(min(100cqw, 100cqh) * 0.92);
    }

    .library {
        min-height: 200px;
    }

    .tabs {
        --tab-h: 32px;
        --tab-h-hover: 32px;
        --tab-h-active: 48px;
        padding: 0;
        margin-bottom: -6px;
    }

    .sheet {
        padding: 3% 4% 4% 3%;
    }

    .export {
        align-self: center;
    }

    .export .row {
        gap: 4px;
    }

    .export button {
        width: 44px;
        height: 44px;
    }

    .export button img {
        width: 28px;
        height: 28px;
    }

    .hint {
        font-size: 9px;
        text-align: center;
    }
}

/* Réduit les animations si demandé par le système */
@media (prefers-reduced-motion: reduce) {
    .tab,
    .card img,
    .tab[aria-selected="true"] {
        animation: none !important;
    }
}
