:root {
    color-scheme: dark light;
    --bg: #091021;
    --panel: rgba(21, 31, 58, 0.68);
    --line: rgba(255, 255, 255, 0.12);
    --text: #f5f7ff;
    --muted: #9ba9c8;
    --cyan: #55e7ff;
    --violet: #a87cff;
    --gold: #ffd56a;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    touch-action: manipulation;
}

body {
    font-family: Inter, "Noto Sans TC", "Microsoft JhengHei", sans-serif;
    color: var(--text);
    background: radial-gradient(circle at 20% 10%, #25396e 0, transparent 35%), radial-gradient(circle at 85% 70%, #452b70 0, transparent 32%), linear-gradient(145deg, #07101f, #121932 60%, #07101f);
    background-attachment: fixed;
}

body::before {
    position: fixed;
    inset: 0;
    content: "";
    pointer-events: none;
    opacity: 0.28;
    background-image: linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.025) 1px, transparent 1px);
    background-size: 30px 30px;
}

button {
    border: 0;
    color: inherit;
    font: inherit;
    cursor: pointer;
}

.game-shell {
    width: min(100%, 500px);
    height: 100vh;
    height: 100svh;
    max-height: 100dvh;
    margin: 0 auto;
    padding: max(12px, env(safe-area-inset-top)) 12px max(18px, env(safe-area-inset-bottom));
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    gap: 10px;
    overflow: hidden;
}

.glass {
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--panel);
    box-shadow: 0 14px 45px rgba(0,0,0,.22), inset 0 1px rgba(255,255,255,.08);
    backdrop-filter: blur(20px) saturate(130%);
}

.topbar {
    padding: 10px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 12px; color: #081022; font-size: 24px; background: linear-gradient(145deg, var(--cyan), var(--violet)); box-shadow: 0 0 22px rgba(85,231,255,.35); }
h1, h2, p { margin: 0; }
h1 { font-size: 16px; letter-spacing: .08em; }
.brand p { margin-top: 2px; color: var(--muted); font-size: 9px; letter-spacing: .18em; }
.icon-button, .close-button { width: 38px; height: 38px; border-radius: 12px; background: rgba(255,255,255,.08); font-size: 20px; }
.icon-button.sound-on { color: var(--cyan); box-shadow: inset 0 0 0 1px rgba(85,231,255,.22), 0 0 18px rgba(85,231,255,.18); animation: music-pulse 1.8s ease-in-out infinite; }
.icon-button.sound-off { color: var(--muted); opacity: .72; animation: none; }

.status { padding: 9px 8px; display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; }
.status div { border-right: 1px solid var(--line); }
.status div:last-child { border: 0; }
.status small { display: block; color: var(--muted); font-size: 10px; }
.status strong { display: block; margin-top: 3px; font-size: 14px; }
.crown strong { color: var(--gold); text-shadow: 0 0 12px var(--gold); }
.crown.dim { opacity: .25; }

.board-frame { position: relative; min-width: 0; min-height: 0; overflow: hidden; display: grid; place-items: center; padding: 8px; }
#gameCanvas { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; touch-action: none; }
.toast { position: absolute; left: 50%; bottom: 14px; width: max-content; max-width: 90%; padding: 9px 15px; transform: translate(-50%, 18px); border: 1px solid rgba(255,122,122,.3); border-radius: 30px; background: rgba(48,12,24,.9); color: #ffb3bd; font-size: 13px; opacity: 0; pointer-events: none; transition: .25s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.controls { padding: 10px; }
.action-row { display: grid; grid-template-columns: 1fr 1fr 1.05fr 1.35fr; gap: 7px; }
.action-row button, .win-actions button { min-height: 42px; border: 1px solid var(--line); border-radius: 13px; background: rgba(255,255,255,.07); font-size: 13px; font-weight: 700; }
.action-row button:active, .dpad button:active { transform: scale(.94); }
.action-row .primary, .cta { color: #081022; background: linear-gradient(135deg, var(--cyan), #78a9ff 55%, var(--violet)); box-shadow: 0 8px 22px rgba(77,190,255,.2); }
.action-row .demo-button { border-color: rgba(255,255,255,.72); color: #241606; background: linear-gradient(135deg, #fff7a8, #ffd45f 55%, #ffad4d); box-shadow: inset 0 1px rgba(255,255,255,.8), 0 6px 18px rgba(255,181,67,.3); text-shadow: 0 1px rgba(255,255,255,.5); }
.action-row .demo-button:hover { filter: brightness(1.08) saturate(1.08); }
.ad-badge { display: inline-grid; min-width: 22px; height: 16px; place-items: center; margin-right: 2px; border: 1px solid rgba(255,255,255,.34); border-radius: 4px; color: #ffffff; background: #9b4d08; font-size: 8px; font-weight: 950; line-height: 1; text-shadow: 0 1px 2px rgba(0,0,0,.4); box-shadow: 0 2px 6px rgba(89,38,0,.25); }
.action-row .demo-button.running { color: #041b27; background: linear-gradient(135deg, #8affd6, #55e7ff); box-shadow: 0 0 20px rgba(85,231,255,.48); }

.dpad { width: 144px; height: 112px; margin: 10px auto 0; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); gap: 3px; }
.dpad button { border: 1px solid var(--line); background: linear-gradient(145deg, rgba(255,255,255,.13), rgba(255,255,255,.035)); box-shadow: inset 0 1px rgba(255,255,255,.12), 0 5px 12px rgba(0,0,0,.18); }
.dpad .up { grid-area: 1 / 2; border-radius: 11px 11px 5px 5px; }
.dpad .left { grid-area: 2 / 1; border-radius: 11px 5px 5px 11px; }
.dpad-core { grid-area: 2 / 2; display: grid; place-items: center; color: var(--cyan); background: rgba(0,0,0,.17); }
.dpad .right { grid-area: 2 / 3; border-radius: 5px 11px 11px 5px; }
.dpad .down { grid-area: 3 / 2; border-radius: 5px 5px 11px 11px; }
.hint { margin-top: 4px; color: var(--muted); text-align: center; font-size: 9px; letter-spacing: .08em; }

.modal { width: 100%; height: 100%; max-width: none; max-height: none; border: 0; padding: 16px; background: rgba(3,8,20,.62); color: var(--text); backdrop-filter: blur(12px); }
.modal[open] { display: grid; place-items: center; animation: fade-in .25s ease; }
.modal::backdrop { background: transparent; }
.modal-card { position: relative; width: min(100%, 440px); max-height: 90dvh; overflow: auto; padding: 24px; border: 1px solid var(--line); border-radius: 28px; text-align: center; background: linear-gradient(145deg, rgba(34,47,82,.94), rgba(12,20,41,.96)); box-shadow: 0 30px 90px rgba(0,0,0,.5), inset 0 1px rgba(255,255,255,.1); }
.start-card { overflow: hidden; padding: 36px 28px 30px; }
.start-card::before { position: absolute; inset: -60% 20% 50%; content: ""; background: radial-gradient(circle, rgba(84,231,255,.45), transparent 65%); }
.hero-crate { position: relative; width: 100px; height: 100px; margin: 0 auto 22px; display: grid; place-items: center; border: 4px solid #7b401e; border-radius: 18px; color: #fff3b0; font-size: 40px; background: repeating-linear-gradient(105deg, #bd7137 0, #bd7137 9px, #a95d2b 10px, #a95d2b 13px); box-shadow: inset 0 0 0 7px #d88b46, inset 0 12px rgba(255,255,255,.2), 0 18px 45px rgba(0,0,0,.4), 0 0 30px rgba(255,196,91,.22); transform: rotate(-4deg); animation: float 2.5s ease-in-out infinite; }
.eyebrow { color: var(--cyan); font-size: 10px; font-weight: 800; letter-spacing: .22em; }
.modal h2 { margin: 7px 0 10px; font-size: 27px; }
.modal-card > p:not(.eyebrow) { color: var(--muted); line-height: 1.7; font-size: 14px; }
.cta { width: 100%; min-height: 50px; margin-top: 22px; border-radius: 15px; font-weight: 900; }
.modal-head { display: flex; justify-content: space-between; text-align: left; }
.modal-head h2 { margin-bottom: 18px; }
.level-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; }
.level-grid button { min-height: 72px; border: 1px solid var(--line); border-radius: 15px; background: rgba(255,255,255,.06); }
.level-grid button strong, .level-grid button small { display: block; }
.level-grid button strong { font-size: 19px; }
.level-grid button small { margin-top: 5px; color: var(--muted); font-size: 9px; }
.level-grid button.current { border-color: var(--cyan); background: rgba(85,231,255,.14); box-shadow: 0 0 18px rgba(85,231,255,.12); }
.level-grid button.done::after { content: "★"; color: var(--gold); margin-left: 4px; }
.win-modal { overflow: hidden; }
#confettiCanvas { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.victory-card { overflow: visible; }
.victory-badge { width: 90px; height: 90px; margin: -68px auto 15px; display: grid; place-items: center; border-radius: 50%; color: #6c4611; font-size: 42px; background: radial-gradient(circle at 35% 25%, #fff5a9, #ffc64e 60%, #ef7f38); box-shadow: 0 0 45px rgba(255,205,81,.5); animation: pop .5s cubic-bezier(.2,1.6,.4,1); }
.win-actions { margin-top: 22px; display: grid; grid-template-columns: 1fr 1.3fr; gap: 10px; }
.win-actions .cta { margin: 0; }
.win-actions #nextButton {
    border: 1px solid rgba(255,255,255,.72);
    color: #061327;
    background: linear-gradient(120deg, #fff27a 0%, #58f5ff 38%, #75a7ff 68%, #dc83ff 100%);
    background-size: 180% 180%;
    box-shadow: 0 0 0 2px rgba(85,231,255,.16), 0 8px 26px rgba(85,231,255,.48), 0 0 34px rgba(168,124,255,.3);
    text-shadow: 0 1px rgba(255,255,255,.45);
    animation: next-glow 2.2s ease-in-out infinite;
}
.win-actions #nextButton:hover {
    filter: brightness(1.12) saturate(1.12);
}
.win-actions #nextButton:active {
    transform: scale(.96);
}

@keyframes float { 50% { transform: translateY(-8px) rotate(3deg); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop { from { transform: scale(.2) rotate(-30deg); } }
@keyframes next-glow {
    0%, 100% { background-position: 0% 50%; box-shadow: 0 0 0 2px rgba(85,231,255,.16), 0 8px 24px rgba(85,231,255,.42), 0 0 28px rgba(168,124,255,.24); }
    50% { background-position: 100% 50%; box-shadow: 0 0 0 3px rgba(255,242,122,.22), 0 10px 32px rgba(85,231,255,.62), 0 0 42px rgba(168,124,255,.42); }
}
@keyframes music-pulse {
    0%, 100% { transform: scale(1); text-shadow: 0 0 6px rgba(85,231,255,.3); }
    50% { transform: scale(1.06); text-shadow: 0 0 14px rgba(85,231,255,.8); }
}

@media (min-width: 700px) and (min-height: 700px) {
    .game-shell { padding-top: 18px; padding-bottom: 18px; }
}

@media (max-height: 690px) {
    .game-shell { gap: 5px; padding: max(5px, env(safe-area-inset-top)) 8px max(5px, env(safe-area-inset-bottom)); }
    .topbar { padding: 6px 12px; }
    .brand-mark { width: 32px; height: 32px; }
    .status { padding: 5px; }
    .controls { padding: 7px; }
    .dpad { width: 126px; height: 84px; margin-top: 6px; }
    .hint { display: none; }
}

@media (max-height: 560px) {
    .topbar { padding: 4px 10px; }
    .brand-mark { width: 28px; height: 28px; font-size: 18px; }
    h1 { font-size: 14px; }
    .brand p { display: none; }
    .icon-button { width: 30px; height: 30px; }
    .status { padding: 3px 5px; }
    .status small { font-size: 8px; }
    .status strong { margin-top: 1px; font-size: 12px; }
    .action-row button { min-height: 34px; }
    .dpad { width: 112px; height: 66px; margin-top: 4px; }
}

@media (max-width: 390px) {
    .action-row { gap: 5px; }
    .action-row button { padding-inline: 5px; font-size: 11px; }
    .ad-badge { min-width: 19px; height: 14px; font-size: 7px; }
}

@media (orientation: landscape) and (max-height: 500px) {
    .game-shell {
        grid-template-columns: minmax(0, 1fr) 154px;
        grid-template-rows: auto auto minmax(0, 1fr);
        width: min(100%, 720px);
        max-width: 720px;
    }
    .topbar { grid-column: 1 / -1; }
    .status { grid-column: 1; grid-row: 2; }
    .board-frame { grid-column: 1; grid-row: 3; }
    .controls { grid-column: 2; grid-row: 2 / 4; display: flex; flex-direction: column; justify-content: center; }
    .action-row { grid-template-columns: 1fr; }
    .action-row button { min-height: 31px; }
    .dpad { flex: 0 0 92px; width: 126px; height: 92px; }
}

@media (prefers-color-scheme: light) {
    :root { --bg: #eaf0ff; --panel: rgba(255,255,255,.68); --line: rgba(30,45,78,.12); --text: #17203a; --muted: #65718f; }
    body { background: radial-gradient(circle at 15% 12%, #bcecff 0, transparent 36%), radial-gradient(circle at 85% 75%, #d7c4ff 0, transparent 36%), #eef3ff; }
    .modal { color: #f5f7ff; }
}
