:root {
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --ink: #f4efe6;
  --accent: #ffd54a;
  --danger: #ff5a4a;
  --stam: #8ee06a;
  --font: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  background: #1a1420; color: var(--ink); font-family: var(--font);
  overflow: hidden; touch-action: none; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  position: fixed; inset: 0;
}
canvas#game { position: fixed; inset: 0; display: block; }
#touch-layer { position: fixed; inset: 0; z-index: 5; touch-action: none; }
.hidden { display: none !important; }

/* ---------------- HUD ---------------- */
#hud {
  position: fixed; left: 0; right: 0; top: 0; z-index: 10; pointer-events: none;
  display: flex; justify-content: space-between; align-items: flex-start;
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) 0 calc(12px + var(--safe-l));
}
#stam-wrap { width: min(46vw, 260px); }
#stam-label { font-size: 12px; letter-spacing: 1.5px; font-weight: 700; opacity: .9; text-shadow: 0 1px 2px #000; margin-bottom: 4px; }
#stam-bar {
  height: 20px; border-radius: 10px; background: rgba(0,0,0,.55);
  border: 2px solid rgba(255,255,255,.55); overflow: hidden; box-shadow: 0 2px 6px rgba(0,0,0,.5);
}
#stam-fill {
  height: 100%; width: 100%; border-radius: 8px;
  background: linear-gradient(180deg, #b9f59a, var(--stam) 60%, #5cb648);
  transition: width .08s linear, background .3s;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.2);
}
#stam-wrap.low #stam-fill { background: linear-gradient(180deg, #ff9a8a, var(--danger) 60%, #c23a2c); }
#stam-wrap.low #stam-bar { animation: lowpulse .6s infinite; border-color: var(--danger); }
#stam-wrap.pulse #stam-bar { transform: scale(1.06); transition: transform .12s; }
@keyframes lowpulse { 0%,100% { box-shadow: 0 0 0 0 rgba(255,90,74,0); } 50% { box-shadow: 0 0 0 5px rgba(255,90,74,.5); } }
#score { text-align: right; padding-right: 52px; }
#score-val { font-size: 30px; font-weight: 900; line-height: 1; text-shadow: 0 2px 0 #000, 0 0 12px rgba(0,0,0,.6); }
#best-val { font-size: 12px; letter-spacing: 1.5px; font-weight: 700; opacity: .85; text-shadow: 0 1px 2px #000; margin-top: 3px; }

#mute-btn {
  position: fixed; z-index: 40; right: calc(10px + var(--safe-r)); top: calc(8px + var(--safe-t));
  width: 44px; height: 44px; border-radius: 12px; border: 2px solid rgba(255,255,255,.5);
  background: rgba(0,0,0,.55); color: #fff; font-size: 20px; cursor: pointer; touch-action: manipulation;
}
#mute-btn.muted { opacity: .7; }
#mute-btn:active { transform: scale(.94); }

/* ---------------- Warning banner ---------------- */
#warn-banner {
  position: fixed; z-index: 12; left: 50%; top: calc(84px + var(--safe-t)); transform: translateX(-50%);
  width: min(92vw, 420px); pointer-events: none; text-align: center;
  background: rgba(20,10,10,.8); border: 3px solid var(--accent); border-radius: 14px; padding: 8px 12px 10px;
  animation: warnflash .5s infinite alternate; box-shadow: 0 0 24px rgba(255,213,74,.35);
}
#warn-banner.active { border-color: #7fd0ff; animation: none; box-shadow: 0 0 30px rgba(127,208,255,.5); }
#warn-text { font-weight: 900; font-size: clamp(15px, 4vw, 20px); letter-spacing: 1px; color: var(--accent); text-shadow: 0 1px 0 #000; }
#warn-banner.active #warn-text { color: #bfe6ff; }
#warn-bar { height: 8px; background: rgba(255,255,255,.15); border-radius: 4px; margin-top: 6px; overflow: hidden; }
#warn-fill { height: 100%; width: 100%; background: var(--accent); transition: width .1s linear; }
#warn-banner.active #warn-fill { background: #7fd0ff; }
@keyframes warnflash { from { border-color: var(--accent); } to { border-color: #fff; } }

#safe-tag {
  position: fixed; z-index: 12; left: 50%; top: calc(160px + var(--safe-t)); transform: translateX(-50%);
  pointer-events: none; font-weight: 900; letter-spacing: 1px; color: #bfe6ff;
  background: rgba(10,30,50,.75); border: 2px solid #7fd0ff; padding: 4px 12px; border-radius: 10px;
}
#hint {
  position: fixed; z-index: 12; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%);
  max-width: min(88vw, 480px); text-align: center; pointer-events: none;
  background: rgba(0,0,0,.6); border-radius: 12px; padding: 8px 14px; font-size: 15px; font-weight: 700;
  text-shadow: 0 1px 2px #000; animation: fadein .3s;
}
body.touch #hint { bottom: calc(190px + var(--safe-b)); }
@keyframes fadein { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------------- Joystick ---------------- */
#joy {
  position: fixed; z-index: 8; width: 140px; height: 140px; border-radius: 50%;
  transform: translate(-50%, -50%); pointer-events: none; display: none;
  opacity: .8; transition: opacity .15s;
}
body.touch #joy { display: block; }
#joy.active { opacity: 1; }
#joy-ring { position: absolute; inset: 0; border-radius: 50%; background: rgba(0,0,0,.35); border: 3px solid rgba(255,255,255,.7); box-shadow: 0 2px 10px rgba(0,0,0,.5); }
#joy-knob {
  position: absolute; left: 50%; top: 50%; width: 62px; height: 62px; border-radius: 50%;
  transform: translate(-50%, -50%); background: radial-gradient(circle at 40% 35%, #fff, #d8d0c8 70%);
  box-shadow: 0 3px 8px rgba(0,0,0,.6); border: 2px solid rgba(0,0,0,.25);
}
.joy-arrow { position: absolute; color: rgba(255,255,255,.75); font-size: 13px; text-shadow: 0 1px 2px #000; }
.joy-arrow.up { top: 8px; left: 50%; transform: translateX(-50%); }
.joy-arrow.down { bottom: 8px; left: 50%; transform: translateX(-50%); }
.joy-arrow.left { left: 10px; top: 50%; transform: translateY(-50%); }
.joy-arrow.right { right: 10px; top: 50%; transform: translateY(-50%); }
body.touch .touch-only { display: inline; }
.touch-only { display: none; }

/* ---------------- Overlays ---------------- */
.overlay {
  position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center;
  background: rgba(10, 8, 16, .55); padding: calc(12px + var(--safe-t)) 12px calc(12px + var(--safe-b));
  animation: fadein .3s; overflow: auto;
}
.card {
  width: min(92vw, 440px); background: rgba(24, 18, 30, .92); border: 3px solid rgba(255,255,255,.18);
  border-radius: 20px; padding: 22px 22px 16px; text-align: center; box-shadow: 0 12px 40px rgba(0,0,0,.6);
  max-height: 96vh; overflow: auto;
}
h1 { margin: 0 0 6px; font-size: clamp(30px, 8vw, 44px); line-height: .95; letter-spacing: -1px; font-weight: 900; }
h1 span { color: var(--accent); }
h2 { margin: 0 0 6px; font-size: clamp(28px, 7vw, 38px); color: var(--danger); font-weight: 900; }
.tag { margin: 0 0 14px; opacity: .85; font-size: 15px; }
.legend { list-style: none; padding: 0; margin: 0 0 16px; text-align: left; font-size: 14px; line-height: 1.35; }
.legend li { display: flex; gap: 10px; align-items: center; padding: 6px 8px; border-radius: 10px; background: rgba(255,255,255,.05); margin-bottom: 5px; }
.legend .ico { font-size: 22px; width: 30px; text-align: center; flex: none; }
.legend b { color: #fff; }
kbd { display: inline-block; background: #333; border: 1px solid #666; border-bottom-width: 3px; border-radius: 5px; padding: 0 6px; font: 700 12px var(--font); margin: 0 1px; color: #fff; }
.big-btn {
  display: block; width: 100%; padding: 16px; font: 900 20px var(--font); letter-spacing: 1px;
  background: linear-gradient(180deg, #ffe27a, var(--accent)); color: #2a1e08; border: none; border-radius: 14px;
  cursor: pointer; box-shadow: 0 5px 0 #b98d12, 0 8px 20px rgba(0,0,0,.5); touch-action: manipulation;
}
.big-btn:active { transform: translateY(3px); box-shadow: 0 2px 0 #b98d12; }
.small { margin: 12px 0 0; font-size: 12px; opacity: .7; }
.reason { margin: 4px 0 12px; font-size: 15px; opacity: .9; }
.result { margin: 8px 0 18px; }
.score-big { font-size: 56px; font-weight: 900; line-height: 1; text-shadow: 0 3px 0 #000; }
.best { font-size: 15px; letter-spacing: 1px; opacity: .85; margin-top: 6px; font-weight: 700; }
.best.new { color: var(--accent); animation: bestpop .6s ease-out; }
@keyframes bestpop { 0% { transform: scale(.7); } 60% { transform: scale(1.15); } 100% { transform: scale(1); } }

@media (max-height: 600px) {
  .card { padding: 14px 16px 12px; }
  .legend li { padding: 4px 8px; margin-bottom: 3px; font-size: 13px; }
  .legend .ico { font-size: 18px; }
  h1 { font-size: 28px; }
  .tag { margin-bottom: 8px; }
}

/* ---------------- Storm meter ---------------- */
#storm-wrap { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 14px; filter: drop-shadow(0 1px 2px #000); }
#storm-bar { flex: 1; height: 8px; border-radius: 4px; background: rgba(0,0,0,.5); border: 1.5px solid rgba(255,255,255,.4); overflow: hidden; }
#storm-fill { height: 100%; width: 0%; background: linear-gradient(90deg, #6f8fb0, #9fc4e8); transition: width .15s linear; }
#storm-wrap.soon #storm-fill { background: linear-gradient(90deg, #d9b13a, var(--accent)); }
#storm-wrap.soon .ico { animation: shakeico .4s infinite; }
#storm-wrap.now #storm-fill { background: #7fd0ff; }
@keyframes shakeico { 0%,100% { transform: translateX(0); } 50% { transform: translateX(2px); } }
