:root{
  --gold:#F2C14E;
  --cream:#EFE2C6;
  --ink:#2B2320;
  --hudw: clamp(150px, 16vw, 238px);
  --hud-top: 8px;
  --ctl: clamp(44px, 7.6vh, 82px);
  --ctl-gap: 8px;
  --safe-b: max(10px, env(safe-area-inset-bottom));
  --safe-r: max(10px, env(safe-area-inset-right));
  --safe-l: max(12px, env(safe-area-inset-left));
  --lv-top: 90px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden;
  background: #12203F;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  font-family: 'Barlow Semi Condensed', system-ui, sans-serif;
}
#stage { position: fixed; inset: 0; z-index: 0; background: linear-gradient(#12203F 0%, #3a2a4a 35%, #E4783C 78%, #F2C14E 100%); }
#stage canvas { display: block; }

#title {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: .16em;
  z-index: 22;
  pointer-events: none;
  font-family: 'Alfa Slab One', serif;
  font-weight: 400;
  font-size: clamp(26px, min(8.0vh, 5.0vw), 112px);
  line-height: .96;
  white-space: nowrap;
  filter: drop-shadow(0 2px 0 rgba(43,35,32,.95)) drop-shadow(0 8px 14px rgba(24,13,6,.5));
}
#title .line { display: inline-flex; align-items: flex-end; }
#title .ltr {
  display: inline-block;
  background-image: linear-gradient(180deg, #FFF4C4 0%, #F7D982 26%, #E7A92F 62%, #B87912 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  -webkit-text-stroke: .042em #2B2320;
  transform-origin: 50% 100%;
  will-change: transform;
}
body.stacked #title { flex-direction: column; align-items: center; gap: .02em; font-size: clamp(24px, 10.4vw, 48px); }

#hud {
  position: fixed;
  left: max(7px, env(safe-area-inset-left));
  top: var(--hud-top);
  width: var(--hudw);
  aspect-ratio: 905 / 682;
  z-index: 30;
  pointer-events: none;
  filter: drop-shadow(0 5px 12px rgba(20,12,8,.42));
}
#hud .plaque { position: absolute; inset: 0; width: 100%; height: 100%; }
#hud .crest {
  position: absolute; left: -8%; top: 2.5%; width: 42%;
  filter: drop-shadow(0 2px 3px rgba(20,10,4,.55));
  transition: filter .2s, transform .15s;
}
#hud.flash .crest { filter: drop-shadow(0 0 10px rgba(242,193,78,.95)) drop-shadow(0 2px 3px rgba(20,10,4,.55)); transform: scale(1.06); }
#hud .count {
  position: absolute; left: 35%; right: 11.5%; top: 12%;
  font-family: 'Chivo Mono', monospace; font-weight: 700;
  font-size: calc(var(--hudw) * .128);
  line-height: 1; text-align: right; white-space: nowrap;
  color: #2B2320;
  text-shadow: 0 1px 0 rgba(255,246,218,.45);
  letter-spacing: -.01em;
}
#hud .count.gain { color: #8a5c12; }
#hud .unbroken {
  position: absolute; left: 38%; right: 11%; top: 36%;
  font-family: 'Barlow Semi Condensed', sans-serif; font-weight: 800;
  font-size: calc(var(--hudw) * .074);
  letter-spacing: .15em; text-align: right;
  color: #5b3d24;
}
#hud .meter {
  position: absolute; left: 12.5%; right: 12%; top: 83.5%; height: 8%;
  background: rgba(32,20,10,.62);
  border-radius: 2px; overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0,0,0,.6);
}
#hud .fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #EFE2C6 0%, #F2C14E 100%);
  box-shadow: 0 0 7px rgba(242,193,78,.85);
  transition: width .12s linear;
}

#levelcard {
  position: fixed; left: 50%; top: var(--lv-top);
  transform: translateX(-50%) rotate(-.6deg) scale(.96);
  z-index: 24;
  padding: 5px 16px 7px;
  text-align: center;
  background: linear-gradient(180deg, #F5EAD1 0%, #E3CFA6 100%);
  border: 2px solid #2B2320;
  border-radius: 3px;
  box-shadow: 0 7px 18px rgba(24,14,8,.45), inset 0 0 0 1px rgba(255,255,255,.25);
  opacity: 0; pointer-events: none;
  transition: opacity .45s ease, transform .45s ease;
}
#levelcard.show { opacity: 1; transform: translateX(-50%) rotate(-.6deg) scale(1); }
#lvNum { display: block; font-family: 'Chivo Mono', monospace; font-size: 9px; letter-spacing: .18em; color: #7c5730; }
#lvName { display: block; font-family: 'Alfa Slab One', serif; font-size: clamp(15px, 2.4vw, 28px); line-height: 1.05; color: #2B2320; }
#lvLine { display: block; margin: 3px auto 0; max-width: 34ch; font-family: 'Barlow Semi Condensed', sans-serif; font-style: italic; font-weight: 600; font-size: clamp(9px, 1.35vw, 12px); line-height: 1.25; color: #62452a; }

#goal, #hint {
  position: fixed; left: var(--safe-l); z-index: 23; margin: 0;
  color: var(--cream);
  font-family: 'Barlow Semi Condensed', sans-serif; font-weight: 600;
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(18,10,5,.95), 0 0 10px rgba(18,10,5,.55);
}
#goal {
  bottom: calc(var(--safe-b) + 22px);
  font-size: clamp(11px, 1.5vh, 15px);
  letter-spacing: .04em;
  transition: opacity .6s ease;
}
#hint {
  bottom: var(--safe-b);
  max-width: calc(100vw - (3 * var(--ctl) + 2 * var(--ctl-gap)) - 42px);
  font-size: clamp(10px, 1.45vh, 13px);
  letter-spacing: .05em;
  line-height: 1.35;
  opacity: .92;
}
#hint .sep { opacity: .55; }

#controls, #actions {
  position: fixed; right: var(--safe-r);
  display: flex; gap: var(--ctl-gap);
  z-index: 30;
}
#controls { bottom: var(--safe-b); }
#actions { bottom: calc(var(--safe-b) + var(--ctl) + var(--ctl-gap)); }
.ctl, .act {
  display: block; padding: 0; margin: 0; border: 0; background: transparent;
  cursor: pointer; touch-action: manipulation;
  filter: drop-shadow(0 5px 7px rgba(20,12,8,.55));
  transition: transform .12s ease, filter .12s ease;
}
.ctl { width: var(--ctl); height: var(--ctl); }
.act { width: calc(var(--ctl) * .86); height: calc(var(--ctl) * .86); }
.ctl img, .act img { display: block; width: 100%; height: 100%; pointer-events: none; }
.ctl:hover, .act:hover { transform: translateY(-1px) scale(1.04); filter: drop-shadow(0 7px 10px rgba(20,12,8,.6)); }
.ctl:active, .act:active { transform: scale(.94); }
.ctl:focus-visible, .act:focus-visible { outline: 2px solid var(--cream); outline-offset: 3px; border-radius: 50%; }
.pulse { animation: pulse 1.9s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.07); } }
@media (prefers-reduced-motion: reduce) { .pulse { animation: none; } }

#bubble {
  position: fixed; left: 0; top: 0; z-index: 28;
  transform: translate(-50%, -100%);
  max-width: min(330px, 74vw);
  padding: 8px 12px;
  background: linear-gradient(180deg, #F6EAD0 0%, #E5D2AB 100%);
  border: 2px solid #2B2320;
  border-radius: 8px 8px 8px 4px;
  color: #2B2320;
  font-family: 'Barlow Semi Condensed', sans-serif; font-weight: 600;
  font-size: clamp(11px, 1.7vw, 15px); line-height: 1.25;
  box-shadow: 0 7px 16px rgba(20,12,8,.42);
  opacity: 0; pointer-events: none;
  transition: opacity .18s ease;
}
#bubble.show { opacity: 1; }
#bubble::after {
  content: ''; position: absolute; left: 50%; bottom: -9px;
  width: 14px; height: 14px; background: #E5D2AB;
  border-right: 2px solid #2B2320; border-bottom: 2px solid #2B2320;
  transform: translateX(-50%) rotate(45deg);
}
#bubble.enemy { background: linear-gradient(180deg, #DCCAA4 0%, #BFA57C 100%); color: #2A1D12; }
#bubble.enemy::after { background: #BFA57C; }

#overlay {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  background: rgba(18,32,63,.46);
  backdrop-filter: blur(1.5px) saturate(.85);
  -webkit-backdrop-filter: blur(1.5px) saturate(.85);
}
#overlay[hidden] { display: none; }
.panel {
  position: relative;
  width: min(560px, 88vw, calc(78vh * 1.327));
  aspect-ratio: 905 / 682;
  filter: drop-shadow(0 18px 36px rgba(12,8,4,.55));
  animation: panelIn .28s ease-out;
}
@keyframes panelIn { from { transform: scale(.94); opacity: .6; } to { transform: scale(1); opacity: 1; } }
.panel-bg { position: absolute; inset: 0; width: 100%; height: 100%; }
.panel-in {
  position: absolute; left: 13%; right: 13%; top: 9%; height: 67%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; color: var(--ink);
}
.panel h2 {
  margin: 0 0 .12em;
  font-family: 'Alfa Slab One', serif; font-weight: 400;
  font-size: clamp(19px, 4.1vw, 34px);
  line-height: .96; color: #2B2320;
  text-shadow: 0 1px 0 rgba(255,255,255,.4);
}
#ovKicker {
  margin: 0 0 .1em;
  font-family: 'Chivo Mono', monospace;
  font-size: clamp(8px, 1.1vw, 10px);
  letter-spacing: .18em; color: #7c5730;
  text-transform: uppercase;
}
#ovLead {
  margin: 0 0 .55em; max-width: 30ch;
  font-family: 'Barlow Semi Condensed', sans-serif; font-style: italic; font-weight: 600;
  font-size: clamp(10px, 1.7vw, 15px); color: #5c4028; line-height: 1.25;
}
#ovStats {
  display: flex; flex-direction: column; gap: 1px;
  font-family: 'Barlow Semi Condensed', sans-serif; font-weight: 700;
  font-size: clamp(9px, 1.4vw, 12px); letter-spacing: .1em; color: #4a3320;
  line-height: 1.35;
}
#ovStats .big {
  font-family: 'Chivo Mono', monospace; font-weight: 700;
  font-size: clamp(21px, 4.8vw, 34px); letter-spacing: .03em; color: #8a5c12;
  line-height: 1.1;
}
#ovStats .row { display: block; }
#ovStats .newbest { color: #9c6a10; letter-spacing: .16em; }
.panel-btn {
  position: absolute; left: 14%; right: 14%; top: 82.5%; height: 9.5%;
  background: transparent; border: 0; cursor: pointer;
  color: var(--gold);
  font-family: 'Barlow Semi Condensed', sans-serif; font-weight: 700;
  font-size: clamp(10px, 1.7vw, 14px); letter-spacing: .12em; text-transform: uppercase;
  text-shadow: 0 1px 2px rgba(0,0,0,.7);
  border-radius: 3px;
}
.panel-btn:hover { color: #FFF1B8; }
.panel-btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }

#vignette {
  position: fixed; inset: 0; z-index: 12; pointer-events: none;
  background:
    radial-gradient(ellipse at 50% 46%, rgba(0,0,0,0) 50%, rgba(30,18,10,.4) 100%),
    linear-gradient(to bottom, rgba(18,32,63,.16) 0%, rgba(18,32,63,0) 24%);
}
#grain {
  position: fixed; inset: -20%; z-index: 60; pointer-events: none;
  opacity: .14; mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/><feColorMatrix type='saturate' values='0'/></filter><rect width='180' height='180' filter='url(%23n)' opacity='.55'/></svg>");
  background-size: 180px 180px;
}
#boot {
  position: fixed; inset: 0; z-index: 70;
  display: grid; place-items: center;
  background: #12203F;
  color: var(--cream);
  font-family: 'Chivo Mono', monospace; font-size: 11px; letter-spacing: .32em;
  transition: opacity .5s ease;
}
#boot.hide { opacity: 0; pointer-events: none; }

body.stacked #hud { width: clamp(124px, 37vw, 186px); }
body.stacked #hint {
  left: var(--safe-l); right: auto;
  width: 44vw; max-width: none;
  bottom: var(--safe-b); font-size: 9.5px;
}
body.stacked #goal {
  left: var(--safe-l); right: auto;
  width: 44vw; max-width: none;
  bottom: calc(var(--safe-b) + 46px); font-size: 11px;
}
body.stacked .hi-extra { display: none; }
body.stacked #levelcard { max-width: 78vw; }
body.stacked .panel { width: min(560px, 92vw, calc(70vh * 1.327)); }

@media (max-height: 430px) {
  #title { font-size: clamp(22px, 9vh, 44px); }
  #goal { display: none; }
  #hint { font-size: 9px; }
}
