:root {
    --text: #ece8df;
    --pale: #b5afa2;
    --ember: #d8d2c3;
    --btn1: #8a8579;
    --btn2: #55514a;
    --btn-glow: rgba(170, 165, 150, .3);
    --soft: rgba(225, 220, 205, .1);
    --soft2: rgba(225, 220, 205, .22);
    --danger: #d7322b;
    --line: rgba(225, 220, 205, .16);
    --hud: #181715;
    --bg1: #1d1c1a;
    --bg2: #0b0a09;
    --glow-bg: rgba(170, 165, 150, .2);
    --f1: #b3b3ae;
    --f2: #75756f;
    --side: #44443f;
    --glow: rgba(210, 210, 200, .3);
    --num: #ddd8cb;
    --crit: #ff6b35;
    --font: 'Trebuchet MS', 'Segoe UI', system-ui, -apple-system, sans-serif;
    --primary: #667eea;
    --secondary: #764ba2;
    --transition: all 0.35s cubic-bezier(0.16, 1, 0.3, 1);
    --st: env(safe-area-inset-top, 0px);
    --sb: env(safe-area-inset-bottom, 0px);
}

body[data-tier="1"] { --hud: #231509; --bg1: #2a1a10; --bg2: #0f0a06; --glow-bg: rgba(255, 120, 30, .3); --f1: #e8aa70; --f2: #a8602c; --side: #6d3a14; --glow: rgba(255, 140, 50, .45); --num: #f2b883; --crit: #ffe066; --ember: #ff9a4a; --pale: #d0a98a; --line: rgba(255, 170, 100, .18); --btn1: #d4803a; --btn2: #8a4a1c; --btn-glow: rgba(255, 120, 30, .3); --soft: rgba(255, 150, 70, .1); --soft2: rgba(255, 150, 70, .22); }
body[data-tier="2"] { --hud: #171a21; --bg1: #1c212b; --bg2: #090b0f; --glow-bg: rgba(140, 180, 255, .2); --f1: #f4f6fa; --f2: #a7b2c4; --side: #66708a; --glow: rgba(190, 220, 255, .5); --num: #e2ebf8; --crit: #ffb347; --ember: #cfe0f5; --pale: #a9b6c8; --line: rgba(190, 215, 255, .18); --btn1: #8d9bb3; --btn2: #5a6783; --btn-glow: rgba(140, 180, 255, .3); --soft: rgba(190, 215, 255, .1); --soft2: rgba(190, 215, 255, .22); }
body[data-tier="3"] { --hud: #231a07; --bg1: #2c2008; --bg2: #100b03; --glow-bg: rgba(255, 190, 40, .3); --f1: #ffe27a; --f2: #d9a21b; --side: #8a5f08; --glow: rgba(255, 196, 0, .55); --num: #ffd54a; --crit: #ff5a36; --ember: #ffcf4a; --pale: #d6bd7c; --line: rgba(255, 200, 70, .2); --btn1: #d49a10; --btn2: #8f5f06; --btn-glow: rgba(255, 190, 40, .35); --soft: rgba(255, 200, 70, .1); --soft2: rgba(255, 200, 70, .22); }
body[data-tier="4"] { --hud: #0c1d15; --bg1: #0f2a1d; --bg2: #040d09; --glow-bg: rgba(40, 255, 150, .22); --f1: #78f5b8; --f2: #17a366; --side: #0b5c39; --glow: rgba(44, 255, 154, .5); --num: #86ffc6; --crit: #fff275; --ember: #4ff0a2; --pale: #8fcfb0; --line: rgba(70, 240, 160, .18); --btn1: #1fb374; --btn2: #0b6b44; --btn-glow: rgba(40, 255, 150, .3); --soft: rgba(70, 240, 160, .1); --soft2: rgba(70, 240, 160, .22); }
body[data-tier="5"] { --hud: #0a1b25; --bg1: #0e2837; --bg2: #040b10; --glow-bg: rgba(120, 230, 255, .26); --f1: #eafdff; --f2: #6fd8f0; --side: #2a7e96; --glow: rgba(139, 233, 255, .6); --num: #bdf5ff; --crit: #ff9ad5; --ember: #8be9ff; --pale: #9ccbd8; --line: rgba(139, 233, 255, .2); --btn1: #3fb6d6; --btn2: #1b6f8c; --btn-glow: rgba(120, 230, 255, .3); --soft: rgba(139, 233, 255, .1); --soft2: rgba(139, 233, 255, .22); }

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    font-family: var(--font);
    color: var(--text);
    background: radial-gradient(ellipse 80% 55% at 50% 100%, var(--glow-bg), transparent 70%), linear-gradient(var(--bg1), var(--bg2));
    background-attachment: fixed;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

button { font-family: inherit; color: inherit; }

.hud {
    position: fixed; top: 0; left: 0; right: 0; z-index: 100;
    height: calc(56px + var(--st));
    padding: var(--st) calc(10px + env(safe-area-inset-right, 0px)) 0 calc(10px + env(safe-area-inset-left, 0px));
    background: var(--hud);
    border-bottom: 1px solid var(--line);
    display: flex; align-items: center; justify-content: space-between; gap: 6px;
}
.hud-left, .hud-right { display: flex; align-items: center; gap: 6px; }
.hud-right { justify-content: flex-end; }
.hud-center { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; line-height: 1.1; }
.hud-val { font-size: 24px; font-weight: 700; color: var(--ember); white-space: nowrap; font-variant-numeric: tabular-nums; }
.hud-val i { font-style: normal; font-size: 11px; font-weight: 400; color: var(--pale); }
.hud-pps { font-size: 12px; color: var(--pale); white-space: nowrap; font-variant-numeric: tabular-nums; }
.hud-buffs { display: flex; gap: 8px; font-size: 11px; font-weight: 700; color: var(--ember); white-space: nowrap; font-variant-numeric: tabular-nums; }

.icon-btn {
    width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line);
    background: var(--soft); color: var(--text); cursor: pointer;
    display: flex; align-items: center; justify-content: center; flex-shrink: 0; text-decoration: none; font-size: 18px;
    transition: background .2s;
}
.icon-btn:hover { background: var(--soft2); }
.icon-btn svg { pointer-events: none; }

.reset-btn {
    min-height: 44px; padding: 0 12px; border-radius: 12px; border: 1px solid var(--line);
    background: linear-gradient(135deg, var(--btn1), var(--btn2)); color: #fff; font-weight: 700; font-size: 14px; cursor: pointer;
    box-shadow: 0 4px 12px var(--btn-glow); text-shadow: 0 1px 2px rgba(0, 0, 0, .45);
}
.reset-btn:hover { filter: brightness(1.12); }

.app {
    position: fixed; inset: calc(56px + var(--st)) 0 0 0;
    display: grid; grid-template-columns: minmax(0, 1fr);
}
.stage {
    position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
    padding: 12px 12px calc(84px + var(--sb)); min-height: 0; overflow: hidden;
}
.zone {
    position: relative; width: 100%; max-width: 640px; flex: 1; min-height: 220px; max-height: 560px;
    display: flex; align-items: center; justify-content: center; cursor: pointer; outline: none; touch-action: manipulation;
}
.info-line { font-size: 15px; color: var(--pale); text-align: center; }
.info-line span:first-child { font-weight: 700; color: var(--num); }

.mis {
    flex-shrink: 0; width: 100%; max-width: 640px; display: flex; flex-direction: column; gap: 1px;
    padding: 7px 12px; border-radius: 12px; border: 1px solid var(--line); color: var(--text); text-align: left;
    background: linear-gradient(90deg, var(--soft2) var(--p, 0%), var(--soft) var(--p, 0%));
    touch-action: manipulation; overflow: hidden;
}
.mis.tap { cursor: pointer; }
.mis.tap:hover { border-color: var(--soft2); }
.mis:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }
.mis-top { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.mis-name { min-width: 0; font-size: 14px; color: var(--num); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mis-prog { flex-shrink: 0; font-style: normal; font-size: 13px; font-weight: 700; color: var(--ember); white-space: nowrap; font-variant-numeric: tabular-nums; }
.mis-rew { font-size: 12px; line-height: 1.3; color: var(--pale); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mis.show { background: var(--soft2); }
.mis.ok { background: var(--soft2); border-color: var(--ember); }
.mis.ok .mis-name, .mis.ok .mis-rew { color: var(--ember); }

.ingot {
    --u: 1.4px;
    --u: max(.9px, min(calc(100vw / 255), calc((100vh - 220px) / 170), 2.1px));
    --u: max(.9px, min(calc(100vw / 255), calc((100dvh - 220px) / 170), 2.1px));
    position: relative; display: block; flex-shrink: 0; padding: 0; border: 0; background: none; cursor: pointer;
    width: calc(var(--u) * 210); height: calc(var(--u) * 170); touch-action: manipulation;
}
.ingot:focus-visible { outline: 2px solid var(--ember); outline-offset: 4px; border-radius: 20px; }
.ingot > *, .ingot span, .ingot svg { pointer-events: none; }
.press { position: absolute; inset: 0; display: block; animation: breathe 4s ease-in-out infinite; }
@keyframes breathe { 0%, 100% { transform: scale(.985); } 50% { transform: scale(1.015); } }
@media (prefers-reduced-motion: reduce) { .press { animation: none; } }
.shade {
    position: absolute; left: 50%; top: calc(50% + var(--u) * 40); width: calc(var(--u) * 150); height: calc(var(--u) * 22);
    margin-left: calc(var(--u) * -75); background: radial-gradient(ellipse at center, rgba(0, 0, 0, .5), transparent 68%);
}
.ingot[data-tier="emeraude"] .shade, .ingot[data-tier="diamant"] .shade { display: none; }

.gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }
.ingot[data-tier="diamant"] .gl { filter: drop-shadow(0 calc(var(--u) * 4) calc(var(--u) * 7) rgba(120, 190, 255, .5)); }

.float {
    position: absolute; z-index: 5; pointer-events: none; font-weight: 800; font-size: 22px; color: var(--num);
    text-shadow: 0 2px 4px rgba(0, 0, 0, .8); white-space: nowrap; transform: translate(-50%, -50%); will-change: transform, opacity;
}
.float.crit { font-size: 36px; color: var(--crit); }

.panel {
    position: fixed; z-index: 220;
    inset: calc(var(--st) + 10px) 10px calc(var(--sb) + 10px) 10px;
    display: flex; flex-direction: column; min-height: 0;
    background: linear-gradient(170deg, var(--bg1), var(--bg2));
    border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 24px 64px rgba(0, 0, 0, .7);
    transform: translateY(110%); visibility: hidden; transition: transform .28s ease, visibility 0s .28s;
}
body.panel-open .panel { transform: none; visibility: visible; transition: transform .28s ease; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 6px 16px; }
.panel-head h2 { font-size: 20px; color: var(--ember); }
.panel-body { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 4px 12px calc(14px + var(--sb)); }
.panel h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--pale); margin: 12px 2px 8px; }

.scrim { display: none; position: fixed; inset: 0; z-index: 210; background: rgba(0, 0, 0, .6); }
body.panel-open .scrim { display: block; }

.burger {
    position: fixed; z-index: 90; left: 50%; transform: translateX(-50%); bottom: calc(16px + var(--sb));
    min-height: 52px; padding: 0 26px; border-radius: 26px; border: 1px solid var(--line);
    background: linear-gradient(135deg, var(--btn1), var(--btn2)); color: #fff; font-size: 17px; font-weight: 700; cursor: pointer;
    display: flex; align-items: center; gap: 10px; box-shadow: 0 8px 22px var(--btn-glow);
}

.row {
    display: flex; align-items: center; gap: 10px; padding: 8px 10px; margin-bottom: 8px; min-height: 60px;
    border-radius: 12px; background: rgba(255, 255, 255, .05); border: 1px solid rgba(255, 255, 255, .05);
}
.row.locked { opacity: .55; }
.row.hl { animation: rowhl 1.7s ease-out; }
@keyframes rowhl { 0%, 40% { box-shadow: 0 0 0 2px var(--ember); background: var(--soft2); } 100% { box-shadow: 0 0 0 2px transparent; } }
@media (prefers-reduced-motion: reduce) { .row.hl { animation: none; box-shadow: 0 0 0 2px var(--ember); } }
.row .info { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.row .info > b, .row .lvl { white-space: nowrap; }
.row b { font-size: 15px; }
.row .lvl { margin-left: 6px; font-size: 12px; color: var(--ember); font-weight: 700; }
.row small { display: block; font-size: 12px; line-height: 1.35; color: var(--pale); }
.buy {
    flex-shrink: 0; min-width: 88px; min-height: 44px; padding: 0 10px; border-radius: 10px; border: none; cursor: pointer;
    font-size: 15px; font-weight: 700; color: #fff; font-variant-numeric: tabular-nums;
    background: linear-gradient(135deg, var(--btn1), var(--btn2)); box-shadow: 0 4px 12px var(--btn-glow); text-shadow: 0 1px 2px rgba(0, 0, 0, .45);
}
.buy:hover:not(:disabled) { filter: brightness(1.12); }
.buy:active:not(:disabled) { transform: translateY(1px); }
.row .btns { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.buy:disabled { cursor: default; opacity: .4; background: rgba(255, 255, 255, .12); box-shadow: none; }


.overlay {
    display: none; position: fixed; inset: 0; z-index: 300; background: rgba(8, 5, 3, .86);
    align-items: center; justify-content: center; padding: 20px;
}
.overlay.open { display: flex; }
.modal {
    position: relative; background: linear-gradient(160deg, var(--bg1), var(--bg2)); border: 1px solid var(--line);
    border-radius: 22px; padding: 30px 26px; max-width: 380px; width: 100%; max-height: 85vh; overflow-y: auto;
    box-shadow: 0 24px 64px rgba(0, 0, 0, .7);
}
.modal-close {
    position: absolute; top: 8px; right: 8px; width: 44px; height: 44px; background: none; border: none;
    color: var(--pale); font-size: 20px; cursor: pointer; border-radius: 8px;
}
.modal-close:hover { background: rgba(255, 255, 255, .1); }
.modal h2 { font-size: 22px; color: var(--ember); margin-bottom: 18px; padding-right: 30px; }
.modal ul { padding-left: 18px; }
.modal li { font-size: 14px; line-height: 1.6; margin-bottom: 8px; color: var(--pale); }
.modal li strong { color: var(--text); }
#offOverlay { z-index: 350; }
#tierOverlay { z-index: 360; }
.modal.center { text-align: center; }
.modal.center h2 { padding-right: 0; margin-bottom: 14px; }
.m-emoji { font-size: 48px; line-height: 1.1; margin-bottom: 4px; }
.stats { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; text-align: left; }
.stat { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 12px; border-radius: 10px; background: rgba(255, 255, 255, .05); font-size: 14px; }
.stat b { color: var(--ember); text-align: right; font-variant-numeric: tabular-nums; }
.modal .sub { font-size: 14px; line-height: 1.5; color: var(--pale); margin-bottom: 14px; }
.reset-text { font-size: 15px; line-height: 1.6; color: var(--pale); margin-bottom: 20px; }
.pause-menu { display: flex; flex-direction: column; gap: 12px; }
.btn-forge {
    width: 100%; min-height: 48px; padding: 12px; border-radius: 14px; border: 1px solid rgba(255, 255, 255, .14);
    font-size: 17px; font-weight: 700; color: #fff; cursor: pointer; background: rgba(255, 255, 255, .08);
}
.btn-forge:hover { filter: brightness(1.15); }
.btn-forge.danger { background: linear-gradient(135deg, #d7322b, #8f1b16); border-color: transparent; }

@media (max-width: 420px) { .hud-val i { display: none; } .reset-btn { padding: 0 9px; font-size: 13px; } }

@media (min-width: 900px) {
    :root { --pw: clamp(460px, 34vw, 500px); }
    .app { grid-template-columns: minmax(0, 1fr) var(--pw); }
    .stage { padding-bottom: 12px; }
    .burger, .scrim, .panel-close { display: none !important; }
    .panel {
        position: static; inset: auto; z-index: auto; transform: none; visibility: visible; transition: none;
        border-radius: 0; border-width: 0 0 0 1px; box-shadow: none; background: color-mix(in srgb, var(--bg2) 80%, transparent);
    }
    .ingot {
        --u: max(.9px, min(calc((100vw - var(--pw)) / 255), calc((100vh - 190px) / 170), 2.1px));
        --u: max(.9px, min(calc((100vw - var(--pw)) / 255), calc((100dvh - 190px) / 170), 2.1px));
    }
}

@media (max-height: 480px) {
    .modal { padding: 18px 20px; max-height: 92vh; }
    .overlay { padding: 8px; }
}

/* Scrollbar du menu */
.panel-body, .modal { scrollbar-width: thin; scrollbar-color: var(--soft2) transparent; }
@supports selector(::-webkit-scrollbar) {
    .panel-body, .modal { scrollbar-width: auto; scrollbar-color: auto; }
    .panel-body::-webkit-scrollbar, .modal::-webkit-scrollbar { width: 6px; }
    .panel-body::-webkit-scrollbar-track, .modal::-webkit-scrollbar-track { margin: 6px 0; border-radius: 3px; background: var(--soft); }
    .panel-body::-webkit-scrollbar-thumb, .modal::-webkit-scrollbar-thumb { border-radius: 3px; background: var(--soft2); }
    .panel-body::-webkit-scrollbar-thumb:hover, .modal::-webkit-scrollbar-thumb:hover { background: var(--ember); }
}

/* Pub récompense (couleurs suivant la matière du lingot) */
.toast {
    position: fixed; top: max(20px, env(safe-area-inset-top)); left: 50%; z-index: 10000;
    display: flex; align-items: center; gap: 12px; width: max-content; max-width: calc(100vw - 32px);
    padding: 13px 20px; color: #fff; font-size: 14px; font-weight: 600; line-height: 1.4; text-align: left;
    background: linear-gradient(135deg, var(--bg1), var(--hud));
    border: 1px solid var(--glow); border-radius: 18px;
    box-shadow: 0 12px 36px rgba(0, 0, 0, .5), 0 0 22px var(--glow-bg);
    backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
    transform: translateX(-50%); animation: toastEnter .35s cubic-bezier(0.16, 1, 0.3, 1) both;
}
.toast::before {
    content: '↻'; display: grid; place-items: center; flex: 0 0 30px; width: 30px; height: 30px;
    color: #fff; font-size: 19px; font-weight: 700; border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--secondary)); box-shadow: 0 3px 12px rgba(102, 126, 234, .35);
}
.toast-success::before { content: '✓'; background: linear-gradient(135deg, #51cf66, #20a85a); box-shadow: 0 3px 12px rgba(81, 207, 102, .35); }
@keyframes toastEnter {
    from { opacity: 0; transform: translate(-50%, -14px) scale(.96); }
    to { opacity: 1; transform: translate(-50%, 0) scale(1); }
}
@media (max-width: 480px) {
    .toast { top: max(12px, env(safe-area-inset-top)); max-width: calc(100vw - 24px); padding: 11px 16px; border-radius: 16px; font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }

.mg-ad-overlay {
    position: fixed; inset: 0; z-index: 10050; background: rgba(8, 5, 3, .9);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    display: flex; align-items: center; justify-content: center; padding: 16px;
}
.mg-ad-video-dialog {
    width: 100%; max-width: 720px; padding: 18px; color: var(--text); border-radius: 20px;
    background: linear-gradient(160deg, var(--bg1), var(--bg2));
    border: 1px solid var(--glow);
    box-shadow: 0 24px 60px rgba(0, 0, 0, .75), 0 0 40px var(--glow-bg);
}
.mg-ad-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.mg-ad-title { margin: 0 0 4px; font-size: 1.08rem; color: var(--num); }
.mg-ad-desc { margin: 0; font-size: .86rem; color: var(--pale); }
.mg-ad-close-btn {
    background: rgba(255, 255, 255, .08); border: 1px solid var(--line); color: #fff;
    width: 34px; height: 34px; border-radius: 10px; cursor: pointer; flex-shrink: 0;
}
.mg-ad-close-btn:hover { background: rgba(255, 255, 255, .18); }
.mg-ad-player-wrap { position: relative; width: 100%; aspect-ratio: 16 / 9; background: #000; border-radius: 12px; overflow: hidden; }
#ad-content-element, #ad-container { position: absolute; inset: 0; width: 100%; height: 100%; }
.mg-ad-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: .92rem; color: var(--pale); pointer-events: none; }

.btn-boost-ad {
    flex-shrink: 0; min-width: 88px; min-height: 44px; padding: 0 10px; border-radius: 10px;
    border: 1px solid rgba(255, 212, 59, .55); background: rgba(255, 212, 59, .12); color: #ffd43b;
    font-size: 15px; font-weight: 700; cursor: pointer; white-space: nowrap; transition: var(--transition);
}
.btn-boost-ad:hover { background: rgba(255, 212, 59, .24); transform: translateY(-1px); }
.btn-boost-ad:active { transform: translateY(1px); }
.btn-boost-ad.hidden, .boost-or.hidden { display: none; }

/* Fix mobile : .app (position: fixed) crée un contexte d'empilement, le panneau passait sous le scrim */
@media (max-width: 899px) {
    body.panel-open .app { z-index: 220; pointer-events: none; }
    body.panel-open .panel { pointer-events: auto; }
}
