:root {
  --ink: #0c0809;
  --panel: rgba(16, 10, 12, 0.78);
  --panel-edge: rgba(255, 220, 200, 0.12);
  --bone: #efe6d6;
  --muted: #b3a597;
  --blood: #d3172b;
  --blood-deep: #7d0a14;
  --gold: #f2c14e;
  --teal: #3fd6be;
  --hp: #e8323f;
  --focus: #ffd36b;
  --display: Impact, Haettenschweiler, 'Arial Narrow Bold', 'Franklin Gothic Bold', sans-serif;
  --ui: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--ink);
  color: var(--bone);
  font-family: var(--ui);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

.app {
  position: fixed;
  inset: 0;
  overflow: hidden;
  touch-action: none;
}

.stage {
  position: absolute;
  inset: 0;
  touch-action: none;
  cursor: none;
}
.game-canvas { display: block; width: 100%; height: 100%; }
.player-marker {
  position: absolute;
  left: 0; top: 0;
  width: 32px; height: 32px;
  margin: -16px 0 0 -16px;
  border-radius: 50%;
  pointer-events: none;
  will-change: transform;
}

.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ------------------------------------------------------------------ HUD */
.hud { position: absolute; left: 0; right: 0; pointer-events: none; z-index: 5; }
.hud-top {
  top: calc(10px + var(--safe-top));
  padding: 0 12px;
  display: grid;
  grid-template-columns: minmax(200px, 290px) 1fr minmax(190px, 270px);
  align-items: start;
  gap: 12px;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 10px;
  padding: 8px 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(3px);
}
.label {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
}
.label b { color: var(--bone); font-size: 14px; letter-spacing: 0; }
.stat + .stat { margin-top: 6px; }
.stat-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; white-space: nowrap; }
.value { font-weight: 800; font-size: 15px; }
.value.small { font-size: 12px; color: var(--muted); font-weight: 600; }

.bar {
  height: 8px;
  margin-top: 4px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}
.bar i {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: inherit;
  transform-origin: left center;
  transition: width 0.12s linear;
}
.bar.hp { height: 10px; }
.bar.hp i { background: linear-gradient(90deg, #a3101c, var(--hp)); width: 100%; }
.bar.xp i { background: linear-gradient(90deg, #1d9f8d, var(--teal)); }
.bar.blood i { background: linear-gradient(90deg, var(--blood-deep), var(--blood)); }
.bar.reload { height: 4px; }
.bar.reload i { background: var(--gold); transition: none; }
#health-display.low .value { color: #ff6b6b; animation: pulse 0.6s ease-in-out infinite alternate; }
#health-display.low .bar.hp { box-shadow: 0 0 0 1px rgba(255, 60, 60, 0.6); }

.panel.wave {
  justify-self: center;
  text-align: center;
  min-width: 190px;
  padding: 6px 16px 8px;
}
.wave-display { display: flex; flex-direction: column; align-items: center; line-height: 1; }
.wave-num { font-family: var(--display); font-size: 34px; font-weight: 400; color: var(--bone); letter-spacing: 0.02em; margin: 2px 0; }
.wave-sub { font-size: 12px; color: var(--muted); min-height: 15px; white-space: nowrap; }
.threat { margin-top: 4px; font-size: 13px; }
.threat b { font-size: 15px; }

.panel.score { text-align: right; }
.score-display { display: grid; grid-template-columns: auto 1fr; align-items: baseline; gap: 0 8px; }
.score-num { font-family: var(--display); font-weight: 400; font-size: 26px; text-align: right; color: var(--gold); }
.kills { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.blood-display { margin-top: 6px; text-align: left; }
.blood-display b { color: #ff5a64; font-size: 15px; }

.perks-taken {
  position: absolute;
  z-index: 5;
  top: calc(116px + var(--safe-top));
  left: 12px;
  max-width: 290px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  pointer-events: none;
}
.perk-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 7px;
  border-radius: 999px;
  background: rgba(16, 10, 12, 0.72);
  border: 1px solid rgba(63, 214, 190, 0.35);
  color: var(--bone);
}
.perk-chip .ico { color: var(--teal); }
.perks-taken.compact .perk-chip .pn { display: none; }
.perk-chip .cnt { color: var(--gold); }

.hud-bottom {
  bottom: calc(12px + var(--safe-bottom));
  padding: 0 12px;
  display: flex;
  align-items: flex-end;
  gap: 12px;
}
.panel.weapon { min-width: 220px; }
.weapon-name { font-family: var(--display); font-weight: 400; font-size: 22px; letter-spacing: 0.03em; color: var(--bone); }
.weapon-name.special { color: var(--gold); }
.ammo { font-size: 18px; font-weight: 800; color: var(--bone); margin-top: 2px; min-height: 22px; }
.ammo.reloading { color: var(--gold); }
.ammo.empty { color: #ff6b6b; }
.ammo .reserve { color: var(--muted); font-weight: 600; font-size: 14px; }

.effects { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.effect {
  --c: #fff;
  position: relative;
  min-width: 96px;
  padding: 6px 10px 9px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid color-mix(in srgb, var(--c) 55%, transparent);
  box-shadow: 0 0 14px color-mix(in srgb, var(--c) 30%, transparent);
  font-size: 12px;
  font-weight: 800;
  overflow: hidden;
}
.effect .eico { color: var(--c); margin-right: 4px; }
.effect .etime { float: right; margin-left: 8px; color: var(--muted); font-variant-numeric: tabular-nums; }
.effect .ebar { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--c); width: 100%; }
.effect.ending { animation: blink 0.25s steps(2) infinite; }

/* ------------------------------------------------------------- Toolbar */
.toolbar {
  position: absolute;
  z-index: 30;
  right: 12px;
  bottom: calc(12px + var(--safe-bottom));
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 60vw;
}
.status, .seed {
  font-size: 12px;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  white-space: nowrap;
}
.status { color: var(--teal); text-transform: uppercase; letter-spacing: 0.08em; min-width: 84px; text-align: center; }
.status[data-state="paused"] { color: var(--gold); }
.status[data-state="levelup"] { color: #9ff4ff; }
.status[data-state="dead"] { color: #ff5a64; }
.seed { color: var(--muted); }

button { font: inherit; color: inherit; }
.tbtn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px 11px;
  border-radius: 8px;
  border: 1px solid rgba(255, 230, 210, 0.2);
  background: rgba(26, 17, 19, 0.88);
  color: var(--bone);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.12s, transform 0.08s, border-color 0.12s;
}
.tbtn:hover { background: rgba(50, 30, 34, 0.95); border-color: rgba(255, 230, 210, 0.4); }
.tbtn:active { transform: translateY(1px) scale(0.97); }
.tbtn[aria-pressed="true"] { border-color: var(--gold); color: var(--gold); }
.tbtn.danger:hover { border-color: var(--blood); }
.tbtn:focus-visible, .btn:focus-visible, .perk-option:focus-visible, input:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* ------------------------------------------------------------- Overlays */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: radial-gradient(ellipse at center, rgba(12, 6, 8, 0.45), rgba(8, 3, 5, 0.82));
  animation: fadeIn 0.2s ease-out;
  overflow-y: auto;
}
.overlay.start { background: radial-gradient(ellipse at center, rgba(12, 6, 8, 0.25), rgba(8, 3, 5, 0.7)); }
.card {
  width: min(520px, 100%);
  background: linear-gradient(180deg, rgba(28, 16, 19, 0.95), rgba(14, 8, 10, 0.96));
  border: 1px solid rgba(255, 200, 180, 0.16);
  border-radius: 14px;
  padding: 22px 24px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.05);
}
.card.small { width: min(360px, 100%); }
.kicker { margin: 0; font-size: 11px; letter-spacing: 0.24em; text-transform: uppercase; color: var(--muted); }
.title {
  margin: 4px 0 0;
  font-family: var(--display);
  font-weight: 400;
  font-size: clamp(46px, 11vw, 78px);
  line-height: 0.95;
  letter-spacing: 0.04em;
  color: var(--bone);
  text-shadow: 0 3px 0 #3a0508, 0 0 30px rgba(211, 23, 43, 0.35);
}
.title span { color: var(--blood); }
.subtitle { margin: 2px 0 10px; font-size: 13px; letter-spacing: 0.4em; text-transform: uppercase; color: #e98b8b; }
.lore { margin: 0 auto 12px; max-width: 420px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.controls { list-style: none; padding: 0; margin: 0 0 14px; font-size: 13px; color: var(--bone); line-height: 1.9; }
.touch-only { display: none; }
body.touch-mode .touch-only, .is-coarse .touch-only { display: list-item; }
kbd {
  display: inline-block;
  min-width: 20px;
  padding: 1px 5px;
  margin: 0 1px;
  border-radius: 4px;
  border: 1px solid rgba(255, 230, 210, 0.3);
  border-bottom-width: 2px;
  background: rgba(255, 255, 255, 0.06);
  font: 700 11px var(--ui);
  color: var(--bone);
}
h2 { margin: 4px 0 10px; font-family: var(--display); font-weight: 400; font-size: 34px; letter-spacing: 0.04em; }
.muted { color: var(--muted); }
.hint { margin: 10px 0 0; font-size: 12px; color: var(--muted); }

.seed-form { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 0 0 4px; }
.seed-form label { font-size: 12px; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.seed-form input {
  width: 150px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 230, 210, 0.25);
  background: rgba(0, 0, 0, 0.35);
  color: var(--bone);
  font: 700 15px var(--ui);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-align: center;
  -webkit-user-select: text;
  user-select: text;
}
.seed-form input[aria-invalid="true"] { border-color: #ff5a64; }
.seed-error { min-height: 18px; margin: 0 0 8px; font-size: 12px; color: #ff7a82; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 10px 18px;
  border-radius: 10px;
  border: 1px solid rgba(255, 230, 210, 0.25);
  background: rgba(255, 255, 255, 0.06);
  color: var(--bone);
  font-weight: 800;
  font-size: 15px;
  cursor: pointer;
  transition: transform 0.08s, background 0.12s, box-shadow 0.12s;
}
.btn:hover { background: rgba(255, 255, 255, 0.12); }
.btn:active { transform: translateY(1px) scale(0.98); }
.btn.primary {
  background: linear-gradient(180deg, #d61a2e, #8e0c19);
  border-color: #ff6670;
  box-shadow: 0 6px 20px rgba(211, 23, 43, 0.35);
}
.btn.primary:hover { background: linear-gradient(180deg, #ea2a3e, #a10f1d); }
.btn.big { min-width: 220px; font-size: 18px; letter-spacing: 0.06em; text-transform: uppercase; }
.btn.ghost { background: transparent; }
.btn small { font-weight: 600; opacity: 0.75; }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }

/* Perk choice */
.perk-card { width: min(760px, 100%); }
.perk-choices { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 6px; }
.perk-option {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-height: 170px;
  padding: 16px 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(63, 214, 190, 0.35);
  background: linear-gradient(180deg, rgba(30, 50, 48, 0.55), rgba(16, 12, 14, 0.9));
  color: var(--bone);
  cursor: pointer;
  text-align: center;
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s, opacity 0.2s;
}
.perk-option:hover:not(:disabled), .perk-option:focus-visible {
  transform: translateY(-3px);
  border-color: var(--teal);
  box-shadow: 0 10px 28px rgba(63, 214, 190, 0.22);
}
.perk-option:active:not(:disabled) { transform: translateY(0) scale(0.98); }
.perk-option:disabled { opacity: 0.55; cursor: wait; }
.perk-option .key {
  position: absolute; top: 8px; left: 8px;
  width: 22px; height: 22px; line-height: 20px;
  border-radius: 6px; border: 1px solid rgba(255, 230, 210, 0.3);
  font-size: 12px; font-weight: 800; color: var(--muted);
}
.perk-option .pico { font-size: 30px; color: var(--teal); line-height: 1; margin-top: 6px; }
.perk-option .pname { font-family: var(--display); font-weight: 400; font-size: 21px; letter-spacing: 0.03em; }
.perk-option .pstack { font-size: 11px; color: var(--gold); font-weight: 800; letter-spacing: 0.1em; min-height: 14px; }
.perk-option .pdesc { font-size: 13px; color: var(--muted); line-height: 1.35; }
.perk-taken-list { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 4px; justify-content: center; min-height: 20px; }
.perk-taken-list .none { font-size: 12px; color: var(--muted); }

/* Death */
.death-title { font-size: clamp(48px, 12vw, 72px); color: var(--blood); text-shadow: 0 3px 0 #2a0306; margin: 0 0 8px; }
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0; }
.summary div { background: rgba(255, 255, 255, 0.04); border: 1px solid rgba(255, 230, 210, 0.08); border-radius: 8px; padding: 8px 6px; }
.summary dt { font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.summary dd { margin: 2px 0 0; font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.summary .hl dd { color: var(--gold); }
.summary .bl dd { color: #ff5a64; }
.best { margin: 10px 0 0; font-size: 13px; color: var(--gold); min-height: 18px; }

/* Wave banner */
.banner {
  position: absolute;
  z-index: 8;
  left: 50%;
  top: 30%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
  opacity: 0;
}
.banner b { display: block; font-family: var(--display); font-weight: 400; font-size: clamp(44px, 10vw, 84px); letter-spacing: 0.06em; color: var(--bone); text-shadow: 0 4px 0 #3a0508, 0 0 40px rgba(211, 23, 43, 0.6); }
.banner span { display: block; font-size: 14px; letter-spacing: 0.3em; text-transform: uppercase; color: #ff8b8b; }
.banner.show { animation: banner 2.2s ease-out forwards; }
.banner.levelup b { color: #8ff7e2; text-shadow: 0 4px 0 #0b3a33, 0 0 40px rgba(63, 214, 190, 0.6); }

/* Touch controls */
.touch-controls { display: none; }
body.touch-mode .touch-controls, .is-coarse .touch-controls { display: block; }
body.touch-mode .stage { cursor: default; }
.stick-zone {
  position: absolute;
  z-index: 6;
  bottom: 0;
  width: 50%;
  height: 46%;
  touch-action: none;
}
.stick-zone.left { left: 0; }
.stick-zone.right { right: 0; }
.stick-base {
  position: absolute;
  left: 50%; top: 60%;
  width: 116px; height: 116px;
  margin: -58px 0 0 -58px;
  border-radius: 50%;
  border: 2px solid rgba(255, 230, 210, 0.22);
  background: rgba(10, 5, 6, 0.25);
  opacity: 0.55;
  transition: opacity 0.12s;
}
.stick-zone.active .stick-base { opacity: 1; border-color: rgba(255, 230, 210, 0.5); }
.stick-zone.right .stick-base { border-color: rgba(255, 90, 100, 0.35); }
.stick-knob {
  position: absolute;
  left: 50%; top: 50%;
  width: 50px; height: 50px;
  margin: -25px 0 0 -25px;
  border-radius: 50%;
  background: rgba(239, 230, 214, 0.5);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
}
.stick-zone.right .stick-knob { background: rgba(230, 60, 72, 0.65); }
.stick-hint {
  position: absolute;
  bottom: calc(12px + var(--safe-bottom));
  left: 0; right: 0;
  text-align: center;
  font-size: 10px;
  letter-spacing: 0.2em;
  color: rgba(239, 230, 214, 0.4);
  font-weight: 800;
}
.touch-btn {
  position: absolute;
  z-index: 7;
  right: 18px;
  bottom: calc(46% + 8px);
  width: 52px; height: 52px;
  border-radius: 50%;
  border: 2px solid rgba(242, 193, 78, 0.6);
  background: rgba(20, 12, 14, 0.8);
  color: var(--gold);
  font-weight: 900;
  font-size: 18px;
  touch-action: manipulation;
}
.touch-btn:active { transform: scale(0.94); background: rgba(60, 40, 20, 0.9); }

.noscript { position: fixed; inset: 0; display: grid; place-items: center; color: var(--bone); }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.55; } }
@keyframes blink { 50% { opacity: 0.4; } }
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(1.4); }
  12% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(0.98); }
}

/* ------------------------------------------------------------- Responsive */
@media (max-width: 760px) {
  .hud-top { grid-template-columns: 1fr auto 1fr; gap: 6px; padding: 0 8px; top: calc(48px + var(--safe-top)); }
  .panel { padding: 6px 8px; border-radius: 8px; }
  .label { font-size: 9px; letter-spacing: 0.08em; }
  .value { font-size: 12px; }
  .value.small { font-size: 10px; }
  .panel.wave { min-width: 0; padding: 4px 10px 6px; }
  .wave-num { font-size: 24px; }
  .wave-sub { font-size: 10px; }
  .threat { font-size: 11px; }
  .threat b { font-size: 12px; }
  .score-num { font-size: 18px; }
  .kills { font-size: 10px; }
  .blood-display b { font-size: 12px; }
  .bar { height: 6px; }
  .bar.hp { height: 7px; }
  .perks-taken { top: calc(150px + var(--safe-top)); left: 8px; max-width: 60vw; }
  .perk-chip { font-size: 9px; padding: 2px 5px; }
  .perk-chip .pn { display: none; }
  .toolbar {
    top: calc(8px + var(--safe-top));
    bottom: auto;
    left: 8px; right: 8px;
    max-width: none;
    justify-content: space-between;
    flex-wrap: nowrap;
  }
  .tbtn { min-height: 34px; min-width: 38px; padding: 4px 9px; justify-content: center; }
  .tlabel { display: none; }
  .status, .seed { font-size: 10px; padding: 5px 8px; }
  .status { min-width: 0; margin-right: auto; }
  .hud-bottom { bottom: auto; top: calc(160px + var(--safe-top)); padding: 0 8px; flex-direction: column; align-items: flex-end; gap: 6px; right: 0; left: auto; }
  .panel.weapon { min-width: 0; text-align: right; }
  .weapon-name { font-size: 15px; }
  .ammo { font-size: 13px; min-height: 16px; }
  .ammo .reserve { font-size: 11px; }
  .effect { min-width: 80px; font-size: 10px; padding: 4px 7px 7px; }
  .effects { flex-direction: column; align-items: flex-end; gap: 4px; }
  .perk-choices { grid-template-columns: 1fr; gap: 8px; }
  .perk-option { min-height: 0; flex-direction: row; text-align: left; padding: 12px 12px 12px 40px; gap: 10px; flex-wrap: wrap; }
  .perk-option .key { top: 50%; margin-top: -11px; }
  .perk-option .pico { font-size: 24px; margin: 0; }
  .perk-option .pname { font-size: 18px; }
  .perk-option .pdesc { flex-basis: 100%; font-size: 12px; }
  .card { padding: 16px; }
  .summary { grid-template-columns: repeat(2, 1fr); }
  .controls { font-size: 12px; line-height: 1.7; }
  .lore { font-size: 13px; }
}

@media (max-width: 380px) {
  .tbtn { min-width: 34px; padding: 4px 7px; }
  .status, .seed { padding: 5px 6px; }
}

@media (pointer: coarse) {
  .touch-only { display: list-item; }
}

@media (prefers-reduced-motion: reduce) {
  .banner.show { animation-duration: 1.6s; }
  .overlay { animation: none; }
  .effect.ending { animation: none; opacity: 0.7; }
}
