@font-face { font-family: 'Silkscreen'; src: url('fonts/Silkscreen-Regular.ttf'); font-display: block; }
@font-face { font-family: 'SilkscreenB'; src: url('fonts/Silkscreen-Bold.ttf'); font-display: block; }
@font-face { font-family: 'Pixelify'; src: url('fonts/PixelifySans.ttf'); font-weight: 400 700; font-display: block; }

:root {
  --ink: #0e0810; --panel: #2b1f33; --panel2: #3a2a44; --hi: #5a4468; --text: #f4ead8; --dim: #b8a8c0;
  --gold: #ffd040; --gold2: #c88a10; --red: #ff4a5a; --green: #6ee87a; --blue: #4fb4ff;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #120c16; color: var(--text); font-family: 'Pixelify', system-ui, sans-serif; user-select: none; -webkit-user-select: none; touch-action: none; overscroll-behavior: none; }
canvas#game { position: fixed; inset: 0; display: block; image-rendering: pixelated; image-rendering: crisp-edges; }
img { image-rendering: pixelated; }
.hidden { display: none !important; }

/* ---------- pixel panels & buttons ---------- */
.px { background: var(--panel); box-shadow: 0 0 0 3px var(--ink), inset 0 3px 0 var(--hi), inset 0 -3px 0 #1a1220, 0 6px 0 3px rgba(0,0,0,.35); border-radius: 2px; }
.btn { font-family: 'SilkscreenB', monospace; font-size: 16px; color: var(--text); background: linear-gradient(#5a4468, #3a2a44); border: 0; padding: 12px 18px; cursor: pointer; letter-spacing: 1px;
  box-shadow: 0 0 0 3px var(--ink), inset 0 3px 0 #7a6088, inset 0 -4px 0 #24182c, 0 5px 0 3px rgba(0,0,0,.4); transition: transform .06s, filter .1s; border-radius: 2px; text-transform: uppercase; }
.btn:hover { filter: brightness(1.15); }
.btn:active, .btn.pressed { transform: translateY(3px); box-shadow: 0 0 0 3px var(--ink), inset 0 3px 0 #7a6088, inset 0 -2px 0 #24182c, 0 2px 0 3px rgba(0,0,0,.4); }
.btn.gold { background: linear-gradient(#ffe070, #e8a820); color: #3a2208; box-shadow: 0 0 0 3px var(--ink), inset 0 3px 0 #fff4b0, inset 0 -4px 0 #a86a10, 0 5px 0 3px rgba(0,0,0,.4); }
.btn.green { background: linear-gradient(#7ef08a, #38b048); color: #0e2a12; box-shadow: 0 0 0 3px var(--ink), inset 0 3px 0 #c8ffc8, inset 0 -4px 0 #207030, 0 5px 0 3px rgba(0,0,0,.4); }
.btn.red { background: linear-gradient(#ff7080, #c8303a); box-shadow: 0 0 0 3px var(--ink), inset 0 3px 0 #ffb0b8, inset 0 -4px 0 #801820, 0 5px 0 3px rgba(0,0,0,.4); }
.btn.small { font-size: 12px; padding: 8px 12px; }
.btn:disabled { filter: grayscale(.8) brightness(.7); cursor: default; }
.btn.big { font-size: 26px; padding: 16px 36px; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; padding: calc(8px + var(--safe-t)) 10px calc(8px + var(--safe-b)); }
.hud-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.hud-left { display: flex; flex-direction: column; gap: 6px; }
#hearts { display: flex; flex-wrap: wrap; gap: 2px; max-width: 46vw; }
#hearts img { width: 26px; height: 26px; filter: drop-shadow(0 2px 0 rgba(0,0,0,.6)); }
#hearts img.half { clip-path: inset(0 50% 0 0); }
#hearts .hw { position: relative; width: 26px; height: 26px; }
#hearts .hw img { position: absolute; left: 0; top: 0; }
#hearts img.pop { animation: pop .35s; }
@keyframes pop { 0% { transform: scale(1.6); } 100% { transform: scale(1); } }
#xpwrap { display: flex; align-items: center; gap: 6px; }
#xpbar { width: min(40vw, 180px); height: 10px; background: #1a1220; box-shadow: 0 0 0 2px var(--ink), inset 0 2px 0 #000; overflow: hidden; }
#xpfill { height: 100%; width: 0; background: linear-gradient(#a0f0ff, #3aa0e8); box-shadow: inset 0 2px 0 rgba(255,255,255,.5); transition: width .15s; }
#lvl { font-family: 'SilkscreenB', monospace; font-size: 12px; text-shadow: 0 2px 0 #000; color: #a0f0ff; }
.hud-center { text-align: center; }
#depth { font-family: 'SilkscreenB', monospace; font-size: 24px; text-shadow: 0 3px 0 #000, 0 0 12px rgba(0,0,0,.6); }
#crushwarn { font-family: 'SilkscreenB', monospace; font-size: 12px; color: var(--red); text-shadow: 0 2px 0 #000; opacity: 0; transition: opacity .2s; }
#crushwarn.on { opacity: 1; animation: blink .4s infinite alternate; }
@keyframes blink { to { color: #ffd0d0; } }
.hud-right { text-align: right; }
#gold { font-family: 'SilkscreenB', monospace; font-size: 20px; color: var(--gold); text-shadow: 0 3px 0 #5a3000; display: flex; align-items: center; gap: 4px; justify-content: flex-end; }
#gold img { width: 20px; height: 20px; }
#gold.bump { animation: bump .2s; }
@keyframes bump { 50% { transform: scale(1.15); } }
#score { font-family: 'Silkscreen', monospace; font-size: 12px; color: var(--dim); text-shadow: 0 2px 0 #000; }
#skills { position: fixed; left: 10px; bottom: calc(10px + var(--safe-b)); display: flex; gap: 8px; }
.sk { width: 46px; height: 46px; position: relative; background: #1a1220; box-shadow: 0 0 0 3px var(--ink), inset 0 0 0 2px var(--skc, #888); border-radius: 4px; overflow: hidden; }
.sk img { position: absolute; inset: 6px; width: 34px; height: 34px; object-fit: contain; }
.sk .cd { position: absolute; inset: 0; background: conic-gradient(rgba(10,6,14,.72) var(--p, 0%), transparent 0); }
.sk .lv { position: absolute; right: 2px; bottom: 0; font-family: 'SilkscreenB', monospace; font-size: 10px; text-shadow: 0 1px 0 #000, 1px 0 0 #000; }
.sk.fire { animation: skfire .35s; }
@keyframes skfire { 0% { box-shadow: 0 0 0 3px var(--ink), inset 0 0 0 2px #fff, 0 0 18px var(--skc); transform: scale(1.12); } }
#itembar { position: fixed; right: 10px; bottom: calc(10px + var(--safe-b)); display: flex; flex-wrap: wrap-reverse; justify-content: flex-end; gap: 3px; max-width: 46vw; }
#itembar img { width: 22px; height: 22px; object-fit: contain; background: rgba(10,6,14,.6); border-radius: 3px; padding: 2px; box-shadow: inset 0 0 0 1px var(--rc, #888); }
#pausebtn { pointer-events: auto; position: fixed; right: 10px; top: calc(64px + var(--safe-t)); font-size: 12px; padding: 6px 9px; font-family: monospace; color: var(--text); background: rgba(20,12,24,.7); border: 0; box-shadow: 0 0 0 2px var(--ink); border-radius: 3px; cursor: pointer; }

/* ---------- toasts / banner ---------- */
#toasts { position: fixed; left: 50%; top: calc(90px + var(--safe-t)); transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; width: min(92vw, 420px); }
.toast { display: flex; align-items: center; gap: 10px; padding: 8px 12px; width: 100%; animation: tin .25s ease-out, tout .4s 2.6s forwards; box-shadow: 0 0 0 3px var(--ink), inset 0 0 0 2px var(--rc), 0 6px 0 3px rgba(0,0,0,.35); background: rgba(34,24,40,.94); }
.toast img { width: 34px; height: 34px; object-fit: contain; }
.toast b { font-family: 'SilkscreenB', monospace; font-size: 13px; color: var(--rc); display: block; }
.toast span { font-size: 14px; color: var(--text); }
@keyframes tin { from { transform: translateY(-14px) scale(.9); opacity: 0; } }
@keyframes tout { to { transform: translateY(-10px); opacity: 0; } }
#banner { position: fixed; left: 0; right: 0; top: 30%; text-align: center; pointer-events: none; font-family: 'SilkscreenB', monospace; font-size: clamp(22px, 6vw, 44px); color: #fff; text-shadow: 0 4px 0 #000, 0 0 24px rgba(160,120,255,.7); opacity: 0; }
#banner.on { animation: banner 2.4s ease-out forwards; }
#banner small { display: block; font-size: .4em; color: var(--dim); letter-spacing: 3px; }
@keyframes banner { 0% { opacity: 0; transform: scale(.8); letter-spacing: 12px; } 15% { opacity: 1; transform: scale(1); letter-spacing: 2px; } 75% { opacity: 1; } 100% { opacity: 0; } }

body.modal #toasts { opacity: 0; }
/* ---------- joystick ---------- */
#joy { position: fixed; width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%; background: radial-gradient(rgba(255,255,255,.08), rgba(255,255,255,.02)); box-shadow: 0 0 0 3px rgba(255,255,255,.25), inset 0 0 20px rgba(0,0,0,.3); pointer-events: none; opacity: 0; transition: opacity .12s; }
#joy.on { opacity: 1; }
#joy::before, #joy::after { content: '◀'; position: absolute; top: 50%; left: 10px; transform: translateY(-50%); color: rgba(255,255,255,.4); font-size: 14px; }
#joy::after { content: '▶'; left: auto; right: 10px; }
#knob { position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #fff8d0, #ffd040 45%, #c88a10); box-shadow: 0 0 0 3px var(--ink), 0 4px 0 2px rgba(0,0,0,.4); }

/* ---------- screens ---------- */
#screen { position: fixed; inset: 0; overflow-x: hidden; display: flex; align-items: center; justify-content: center; padding: calc(12px + var(--safe-t)) 12px calc(12px + var(--safe-b)); background: rgba(12,6,16,.55); backdrop-filter: blur(1px); overflow-y: auto; }
#screen.clear { background: transparent; backdrop-filter: none; }
.wrap { width: min(100%, 760px); min-width: 0; max-height: 100%; display: flex; flex-direction: column; gap: 14px; align-items: center; }
.title-h { font-family: 'SilkscreenB', monospace; font-size: clamp(20px, 5vw, 30px); text-shadow: 0 4px 0 #000; text-align: center; margin: 0; }
.sub { color: var(--dim); text-align: center; font-size: 16px; margin: 0; }
.logo { font-family: 'SilkscreenB', monospace; font-size: clamp(40px, 15vw, 140px); line-height: .9; margin: 0; letter-spacing: 4px;
  background: linear-gradient(#fff6b0 0%, #ffd040 40%, #f0a020 60%, #c86010 100%); -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 4px 0 #5a2808) drop-shadow(0 8px 0 #1a0a06) drop-shadow(0 0 30px rgba(255,180,60,.35)); animation: float 3s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-6px); } }
.tagline { font-family: 'Silkscreen', monospace; font-size: 14px; color: #fff4d8; text-shadow: 0 2px 0 #000, 0 0 8px #000, 0 0 3px #000; letter-spacing: 2px; text-align: center; }
.menu { display: flex; flex-direction: column; gap: 14px; align-items: stretch; width: min(80vw, 320px); }
.row { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.bank { font-family: 'SilkscreenB', monospace; color: var(--gold); font-size: 20px; text-shadow: 0 3px 0 #5a3000; display: flex; align-items: center; gap: 6px; }
.bank img { width: 22px; }
.hero-pre { width: 120px; height: 120px; animation: herobob 1s ease-in-out infinite; filter: drop-shadow(0 6px 0 rgba(0,0,0,.4)); }
@keyframes herobob { 50% { transform: translateY(-4px) scaleY(1.02); } }
.panel { padding: 16px; width: 100%; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; width: 100%; }
.card { position: relative; padding: 12px; display: flex; flex-direction: column; gap: 6px; cursor: pointer; background: var(--panel2); box-shadow: 0 0 0 3px var(--ink), inset 0 0 0 2px var(--rc, var(--hi)), 0 5px 0 3px rgba(0,0,0,.35); transition: transform .1s, filter .1s; border-radius: 2px; }
.card:hover { transform: translateY(-3px); filter: brightness(1.1); }
.card.sel { box-shadow: 0 0 0 3px var(--ink), inset 0 0 0 3px var(--gold), 0 0 0 6px var(--gold), 0 5px 0 6px rgba(0,0,0,.35); }
.card.locked { filter: grayscale(.4) brightness(.8); }
.card .hd { display: flex; align-items: center; gap: 10px; }
.card .hd img { width: 44px; height: 44px; object-fit: contain; }
.card .nm { font-family: 'SilkscreenB', monospace; font-size: 14px; color: var(--rc, var(--text)); }
.card .rar { font-size: 11px; font-family: 'Silkscreen', monospace; color: var(--rc); opacity: .85; }
.card .ds { font-size: 15px; line-height: 1.25; }
.card .up { color: var(--green); font-size: 14px; }
.card .down { color: var(--red); font-size: 14px; }
.card .cost { font-family: 'SilkscreenB', monospace; color: var(--gold); font-size: 13px; display: flex; gap: 4px; align-items: center; }
.card .cost img { width: 14px; }
.card .tag { position: absolute; right: 8px; top: 8px; font-family: 'SilkscreenB', monospace; font-size: 10px; background: var(--gold); color: #2a1a00; padding: 2px 5px; }
.card .key { position: absolute; left: 6px; top: 4px; font-family: 'SilkscreenB', monospace; font-size: 10px; color: var(--dim); }
.lvcards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; width: 100%; }
.lvcards .card { padding: 16px 12px; align-items: center; text-align: center; animation: cardin .35s backwards; }
.lvcards .card .hd { flex-direction: column; }
.lvcards .card .hd img { width: 56px; height: 56px; }
@keyframes cardin { from { transform: translateY(30px) rotate(-3deg); opacity: 0; } }
.lvtitle { font-family: 'SilkscreenB', monospace; font-size: clamp(32px, 9vw, 54px); color: #a0f0ff; text-shadow: 0 4px 0 #0a3050, 0 0 26px rgba(120,220,255,.6); margin: 0; animation: lvl .5s; }
@keyframes lvl { from { transform: scale(1.6); opacity: 0; } }
.tabs { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.statgrid { display: grid; grid-template-columns: 1fr auto; gap: 6px 18px; font-size: 18px; width: min(90vw, 340px); }
.statgrid b { font-family: 'SilkscreenB', monospace; color: var(--gold); text-align: right; }
table.lb { border-collapse: collapse; width: 100%; font-size: 16px; }
table.lb td, table.lb th { padding: 6px 8px; text-align: left; }
table.lb th { font-family: 'Silkscreen', monospace; color: var(--dim); font-size: 12px; }
table.lb tr:nth-child(even) td { background: rgba(255,255,255,.04); }
table.lb tr.me td { background: rgba(255,208,64,.18); }
table.lb td.n { font-family: 'SilkscreenB', monospace; color: var(--gold); text-align: right; }
table.lb img { width: 22px; vertical-align: middle; }
input.name { font-family: 'SilkscreenB', monospace; font-size: 18px; padding: 10px; background: #1a1220; color: var(--text); border: 0; box-shadow: 0 0 0 3px var(--ink), inset 0 0 0 2px var(--hi); width: 200px; text-align: center; text-transform: uppercase; user-select: text; -webkit-user-select: text; }
canvas.castle { width: min(92vw, 560px); height: auto; image-rendering: pixelated; box-shadow: 0 0 0 3px var(--ink), 0 6px 0 3px rgba(0,0,0,.35); }
.stages { display: flex; flex-direction: column; gap: 6px; width: 100%; max-height: 34vh; overflow-y: auto; padding: 4px; }
.stage { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel2); box-shadow: 0 0 0 2px var(--ink); font-size: 15px; }
.stage.done { opacity: .6; }
.stage.next { box-shadow: 0 0 0 2px var(--ink), inset 0 0 0 2px var(--gold); }
.stage b { font-family: 'SilkscreenB', monospace; font-size: 13px; }
.slots { display: flex; gap: 10px; }
.slot { width: 56px; height: 56px; background: #1a1220; box-shadow: 0 0 0 3px var(--ink), inset 0 0 0 2px var(--hi); display: flex; align-items: center; justify-content: center; }
.slot img { width: 40px; height: 40px; object-fit: contain; }
.slot.lock { opacity: .4; font-size: 20px; }
.itemlist { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; width: 100%; max-height: 45vh; overflow-y: auto; padding: 4px; }
.itemlist .it { display: flex; gap: 8px; align-items: center; padding: 6px 8px; background: var(--panel2); box-shadow: 0 0 0 2px var(--ink), inset 0 0 0 1px var(--rc); font-size: 13px; }
.itemlist .it img { width: 28px; height: 28px; object-fit: contain; }
.itemlist .it b { font-family: 'SilkscreenB', monospace; font-size: 11px; color: var(--rc); display: block; }
.toggle { display: flex; gap: 10px; align-items: center; justify-content: space-between; width: 100%; font-size: 18px; }
.help { font-size: 15px; color: var(--dim); text-align: center; line-height: 1.4; }
.help kbd { font-family: 'SilkscreenB', monospace; background: var(--panel2); padding: 2px 6px; box-shadow: 0 0 0 2px var(--ink); color: var(--text); font-size: 12px; }
.newbest { font-family: 'SilkscreenB', monospace; color: var(--gold); font-size: 20px; animation: blink .5s infinite alternate; }
#loading { position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; background: #120c16; z-index: 50; }
.ld-title { font-family: 'SilkscreenB', monospace; font-size: 64px; color: var(--gold); }
.ld-sub { color: var(--dim); font-family: 'Silkscreen', monospace; }
.hurt-pulse { animation: hurtp .3s; }
@keyframes hurtp { 0% { transform: translateX(-4px); } 25% { transform: translateX(4px); } 50% { transform: translateX(-3px); } 100% { transform: none; } }
@media (max-width: 520px) {
  .btn.big { font-size: 22px; padding: 14px 28px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .grid .card { min-width: 0; overflow-wrap: anywhere; }
  .logo { font-size: 12.5vw !important; letter-spacing: .04em !important; max-width: 100%; white-space: nowrap; }
  .tagline { max-width: 100%; text-align: center; letter-spacing: .08em; font-size: 12px; }
  .wrap > .row .btn.small { padding-left: 10px; padding-right: 10px; }
  .menu, .wrap > .row { max-width: 100%; flex-wrap: wrap; justify-content: center; }
  .grid .card { padding: 9px; }
  .card .hd img { width: 34px; height: 34px; }
  .card .nm { font-size: 11px; }
  .card .up, .card .down, .card .ds { font-size: 12px; }
  .lvcards { grid-template-columns: 1fr; gap: 10px; }
  .lvcards .card { flex-direction: row; text-align: left; padding: 10px 12px; }
  .lvcards .card .hd { flex-direction: row; }
  .lvcards .card .hd img { width: 40px; height: 40px; }
  #depth { font-size: 20px; }
}
