:root { --bg:#12162b; --panel:#1b2140; --fg:#f4f6ff; --dim:#aab2dc; --gold:#ffd75e; --mint:#2ee6a6; --bomb:#ff4d6d; --accent:#7c8cff; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #0a0d1c; color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none; touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
#stage {
  position: relative; height: 100vh; height: 100dvh; width: 100%; max-width: 520px; margin: 0 auto; overflow: hidden;
  display: flex; flex-direction: column; touch-action: none;
  background: radial-gradient(120% 70% at 50% 0%, #1f2650 0%, var(--bg) 60%);
}
#stage.shake { animation: shake .45s; }
@keyframes shake { 10%,50%,90% { transform: translate(-6px, 3px); } 30%,70% { transform: translate(6px, -3px); } }
#fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }
#flash { position: absolute; inset: 0; pointer-events: none; z-index: 4; background: var(--bomb); opacity: 0; }
#flash.on { animation: flash .5s; }
@keyframes flash { 0% { opacity: .45; } 100% { opacity: 0; } }

#hud { display: flex; align-items: flex-start; justify-content: space-between; padding: max(12px, env(safe-area-inset-top)) 14px 0; z-index: 2; }
#hud .best { flex: 1; font-size: 14px; font-weight: 700; letter-spacing: .08em; color: var(--dim); padding-top: 10px; }
#score { flex: 1; text-align: center; font-size: 52px; font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; text-shadow: 0 2px 18px rgba(255,215,94,.45); }
#hud .btns { flex: 1; display: flex; justify-content: flex-end; }
button { font: inherit; color: inherit; cursor: pointer; }
#btn-mute { width: 44px; height: 44px; border-radius: 12px; border: 1px solid rgba(255,255,255,.25); background: rgba(255,255,255,.08); font-size: 16px; padding: 0; }
#btn-mute:hover { background: rgba(255,255,255,.16); }
#btn-mute[aria-pressed="true"] { opacity: .55; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

#lives { display: flex; gap: 6px; justify-content: center; height: 22px; margin-top: 4px; z-index: 2; }
#lives svg { width: 22px; height: 22px; transition: transform .3s, opacity .3s; }
#lives svg.lost { opacity: .22; transform: scale(.8); }

#wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 8px 14px; z-index: 1; }
#board { display: grid; gap: 8px; height: 100%; max-height: calc(min(100vw, 520px) - 28px); aspect-ratio: 1 / 1; max-width: 100%; }

.tile { position: relative; padding: 0; border: 0; background: none; perspective: 600px; border-radius: 14px; animation: pop .35s backwards; animation-delay: calc(var(--i) * 18ms); }
@keyframes pop { from { transform: scale(.4); opacity: 0; } }
.tile .inner { position: absolute; inset: 0; transform-style: preserve-3d; transition: transform .32s cubic-bezier(.3,1.4,.5,1); border-radius: inherit; }
.tile.open .inner { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; border-radius: 14px; backface-visibility: hidden; -webkit-backface-visibility: hidden; display: flex; align-items: center; justify-content: center; flex-direction: column; overflow: hidden; }
.front { background: linear-gradient(160deg, #4a56a8, #2c3478); border: 2px solid #6f7ddb; box-shadow: 0 4px 0 #1b2152, inset 0 2px 0 rgba(255,255,255,.18); transition: filter .15s, transform .15s; }
.tile:not(.open):hover .front { filter: brightness(1.2); transform: translateY(-2px); }
.tile:not(.open):active .front { transform: translateY(2px); box-shadow: 0 1px 0 #1b2152; }
.tile:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }
.back { transform: rotateY(180deg); }
.back.safe { background: linear-gradient(160deg, #19d3a0, #0a8a72); border: 2px solid #7dffd9; box-shadow: inset 0 2px 0 rgba(255,255,255,.3); color: #fff; }
.back.bomb { background: radial-gradient(circle at 50% 55%, #6b1d36, #2a0d1c); border: 2px solid var(--bomb); box-shadow: 0 0 22px rgba(255,77,109,.7); }
.back.defused { background: linear-gradient(160deg, #46508f, #2b3266); border: 2px dashed #9aa6ff; }
.tile.boom .back.bomb { animation: boom .5s; }
@keyframes boom { 0% { transform: rotateY(180deg) scale(1); } 30% { transform: rotateY(180deg) scale(1.35); background: #fff; } 100% { transform: rotateY(180deg) scale(1); } }
.back svg { width: 46%; height: 46%; filter: drop-shadow(0 2px 3px rgba(0,0,0,.4)); }
.val { font-weight: 900; font-size: calc(var(--t, 60px) * .26); line-height: 1; margin-top: 2px; text-shadow: 0 1px 2px rgba(0,0,0,.55); }
.cnt { position: absolute; top: 5px; left: 5px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px; background: rgba(8,12,34,.75); color: #fff; font-size: calc(var(--t, 60px) * .2); font-weight: 800; display: flex; align-items: center; justify-content: center; }
.cnt.c0 { opacity: .55; }
.peek .tile.bomb-peek:not(.open) .front::after { content: ""; position: absolute; width: 22%; height: 22%; border-radius: 50%; background: var(--bomb); opacity: .85; }

#panel { padding: 6px 16px max(16px, env(safe-area-inset-bottom)); z-index: 2; display: flex; flex-direction: column; gap: 10px; }
.risk { display: flex; align-items: center; gap: 10px; font-size: 12px; font-weight: 800; letter-spacing: .08em; color: var(--dim); }
.risk .bar { flex: 1; height: 12px; border-radius: 6px; background: rgba(255,255,255,.12); overflow: hidden; }
.risk .bar i { display: block; height: 100%; width: 0; border-radius: 6px; background: var(--mint); transition: width .3s, background .3s; }
.risk b { min-width: 44px; text-align: right; font-size: 16px; color: var(--fg); font-variant-numeric: tabular-nums; }
#cash { display: flex; flex-direction: column; align-items: center; gap: 2px; width: 100%; padding: 10px 16px 12px; border: 0; border-radius: 16px;
  background: linear-gradient(180deg, #ffe27a, #f5b52e); color: #1a1530; box-shadow: 0 5px 0 #a9741a; transition: transform .1s, opacity .2s; }
#cash small { font-size: 13px; font-weight: 800; letter-spacing: .1em; }
#cash kbd { font: inherit; opacity: .6; border: 1px solid rgba(0,0,0,.35); border-radius: 4px; padding: 0 4px; margin-left: 4px; }
#cash strong { font-size: 28px; font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }
#cash:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 2px 0 #a9741a; }
#cash:disabled { opacity: .4; cursor: default; }
#cash.ready { animation: glow 1.2s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 5px 0 #a9741a, 0 0 24px rgba(255,215,94,.7); } }
#info { margin: 0; text-align: center; font-size: 13px; color: var(--dim); min-height: 1.3em; }

#toast { position: absolute; left: 0; right: 0; top: 24%; z-index: 6; text-align: center; pointer-events: none; opacity: 0; font-size: clamp(30px, 10vw, 46px); font-weight: 900; color: var(--gold); text-shadow: 0 4px 22px rgba(0,0,0,.7); }
#toast small { display: block; font-size: 16px; color: #fff; letter-spacing: .08em; margin-top: 4px; }
#toast.on { animation: toast 1.1s; }
@keyframes toast { 0% { opacity: 0; transform: scale(.6); } 18% { opacity: 1; transform: scale(1.1); } 30% { transform: scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translateY(-14px); } }

#overlay { position: absolute; inset: 0; z-index: 7; pointer-events: none; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  padding: 90px 28px 40px; background: rgba(8,10,24,.8); opacity: 0; visibility: hidden; transition: opacity .25s, visibility .25s; }
#overlay.show { opacity: 1; visibility: visible; }
#overlay h1 { margin: 0; font-size: clamp(36px, 12vw, 58px); text-shadow: 0 4px 24px rgba(255,215,94,.45); }
#overlay.over h1, #overlay.bomb h1 { color: var(--bomb); text-shadow: 0 4px 24px rgba(255,77,109,.5); }
#overlay.bomb { justify-content: flex-end; padding: 0 28px 6vh; background: linear-gradient(rgba(8,10,24,0), rgba(8,10,24,.92) 45%); }
#msg { margin: 0; font-size: 20px; font-weight: 700; min-height: 1.3em; }
#tip { margin: 0; max-width: 340px; font-size: 16px; line-height: 1.5; color: var(--dim); }
#tip:empty { display: none; }
#cta { margin: 16px 0 0; font-size: 20px; font-weight: 800; color: #1a1530; background: var(--gold); padding: 12px 28px; border-radius: 999px; animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
@media (prefers-reduced-motion: reduce) {
  #cta, #cash.ready, .tile { animation: none; } #overlay, .tile .inner { transition: none; } #stage.shake, #flash.on, #toast.on { animation: none; } #toast.on { opacity: 1; }
}
