:root {
  --k: #1a1c2c; --plum: #5d275d; --red: #b13e53; --orange: #ef7d57;
  --gold: #ffcd75; --lime: #a7f070; --green: #38b764; --teal: #257179;
  --navy: #29366f; --royal: #3b5dc9; --blue: #41a6f6; --cyan: #73eff7;
  --white: #f4f4f4; --fog: #94b0c2; --steel: #566c86; --slate: #333c57;
  --head: 'Press Start 2P', 'Courier New', monospace;
  --body: 'VT323', 'Courier New', monospace;
  /* stepped corners: every box is cut on the 4px grid, never rounded */
  --notch: polygon(0 4px, 4px 4px, 4px 0, calc(100% - 4px) 0, calc(100% - 4px) 4px, 100% 4px,
    100% calc(100% - 4px), calc(100% - 4px) calc(100% - 4px), calc(100% - 4px) 100%,
    4px 100%, 4px calc(100% - 4px), 0 calc(100% - 4px));
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  background-color: var(--k);
  /* 2-colour dither, drawn on the 4px grid */
  background-image:
    linear-gradient(45deg, #20233a 25%, transparent 25%, transparent 75%, #20233a 75%),
    linear-gradient(45deg, #20233a 25%, transparent 25%, transparent 75%, #20233a 75%);
  background-size: 8px 8px;
  background-position: 0 0, 4px 4px;
  color: var(--white);
  font-family: var(--body);
  font-size: 22px;
  line-height: 1;
  -webkit-font-smoothing: none;
  font-smooth: never;
  text-rendering: optimizeSpeed;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

canvas, .px-img { image-rendering: pixelated; image-rendering: crisp-edges; display: block; }

.app {
  width: 100%;
  max-width: 480px;
  min-height: 100%;
  margin: 0 auto;
  padding: 16px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ---------- header ---------- */
.top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
#logo canvas { width: 155px; height: 45px; }
.sub { margin-top: 8px; font-family: var(--head); font-size: 8px; color: var(--fog); display: flex; gap: 8px; align-items: center; }
.tag { background: var(--navy); color: var(--cyan); padding: 4px 6px; }
.tag.d-hard { background: var(--plum); color: var(--gold); }
.tag.d-brutal { background: var(--red); color: var(--white); }
.nav { display: flex; gap: 8px; }

/* ---------- buttons ---------- */
.btn {
  font-family: var(--head);
  font-size: 12px;
  color: var(--k);
  background: var(--gold);
  border: 0;
  padding: 14px 18px 16px;
  clip-path: var(--notch);
  box-shadow: inset 0 -4px 0 var(--orange), inset 0 4px 0 #fff2c9;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:active, .btn.down { padding: 16px 18px 14px; box-shadow: inset 0 4px 0 var(--orange); }
.btn:focus-visible { outline: 4px solid var(--cyan); outline-offset: 0; clip-path: none; }
.btn.alt { background: var(--blue); box-shadow: inset 0 -4px 0 var(--royal), inset 0 4px 0 var(--cyan); }
.btn.alt:active { box-shadow: inset 0 4px 0 var(--royal); }
.btn.ghost { background: var(--slate); color: var(--fog); box-shadow: inset 0 -4px 0 #262b44, inset 0 4px 0 var(--steel); }
.btn.ghost:active { box-shadow: inset 0 4px 0 #262b44; }
.btn.ghost.off { color: var(--steel); text-decoration: line-through; }
.btn.small { font-size: 8px; padding: 10px 10px 12px; }
.btn.small:active { padding: 12px 10px 10px; }

/* ---------- hud ---------- */
.hud {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1.6fr;
  background: var(--k);
  border: 4px solid var(--slate);
  clip-path: var(--notch);
}
.hud .cell { padding: 8px 4px 8px 10px; display: flex; flex-direction: column; gap: 8px; border-left: 4px solid var(--slate); min-width: 0; }
.hud .cell:first-child { border-left: 0; }
.k { font-family: var(--head); font-size: 8px; color: var(--steel); }
.v { font-family: var(--head); font-size: 16px; color: var(--white); height: 16px; display: flex; gap: 2px; align-items: center; }
.v.gold { color: var(--gold); }
.v.over { color: var(--orange); }

/* ---------- board ---------- */
.stage { display: flex; justify-content: center; touch-action: none; }
#board { touch-action: none; cursor: pointer; }

.msg { font-family: var(--head); font-size: 8px; line-height: 1.6; text-align: center; color: var(--fog); min-height: 14px; }
.msg.warn { color: var(--orange); animation: blink 0.5s steps(1) 3; }
.msg.good { color: var(--lime); }
@keyframes blink { 50% { visibility: hidden; } }

.actions { display: flex; justify-content: center; gap: 12px; }
.actions .btn { min-width: 132px; }

.foot { margin-top: auto; padding-top: 6px; text-align: center; font-family: var(--head); font-size: 8px; color: var(--steel); }
.foot span { color: var(--fog); }

/* ---------- modal ---------- */
.veil {
  position: fixed; inset: 0; z-index: 10;
  background-color: rgba(26, 28, 44, 0.86);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.veil[hidden] { display: none; }
.modal {
  width: 100%; max-width: 400px; max-height: 100%;
  overflow-y: auto;
  background: var(--slate);
  border: 4px solid var(--k);
  outline: 4px solid var(--fog);
  outline-offset: -8px;
  padding: 26px 22px 22px;
  clip-path: var(--notch);
  animation: drop 0.18s steps(3);
}
@keyframes drop { from { transform: translateY(-24px); } to { transform: none; } }
.modal h2 { font-family: var(--head); font-size: 16px; line-height: 1.4; color: var(--gold); text-align: center; text-shadow: 2px 2px 0 var(--k); margin-bottom: 16px; }
.modal h2.big { font-size: 20px; color: var(--lime); }
.modal p { font-size: 22px; line-height: 1.05; color: var(--white); }
.modal .dim { color: var(--fog); }
.modal .row { display: flex; justify-content: center; gap: 12px; margin-top: 20px; flex-wrap: wrap; }

.rules { list-style: none; display: grid; gap: 12px; }
.rules li { display: grid; grid-template-columns: 40px 1fr; gap: 12px; align-items: center; font-size: 22px; line-height: 1.05; }
.rules .pic { width: 40px; height: 40px; background: var(--navy); border: 4px solid var(--k); display: flex; align-items: center; justify-content: center; font-family: var(--head); font-size: 12px; color: var(--cyan); }
.note { margin-top: 16px; padding-top: 14px; border-top: 4px dotted var(--steel); }

.stars { display: flex; justify-content: center; gap: 8px; margin-bottom: 16px; }
.modal p.line { font-family: var(--head); font-size: 8px; line-height: 2.2; text-align: center; color: var(--fog); }
.modal p.line b { color: var(--gold); font-weight: normal; }
.pips { display: flex; justify-content: center; flex-wrap: wrap; gap: 4px; margin: 14px 0; }
.pips i { width: 12px; height: 12px; background: var(--blue); box-shadow: inset -4px -4px 0 var(--royal); }
.pips i.over { background: var(--orange); box-shadow: inset -4px -4px 0 var(--red); }

.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 18px; }
.grid3 div { background: var(--k); padding: 10px 4px; text-align: center; clip-path: var(--notch); }
.grid3 b { display: block; font-family: var(--head); font-size: 16px; font-weight: normal; color: var(--white); margin-bottom: 8px; }
.grid3 span { font-family: var(--head); font-size: 6px; color: var(--fog); }
.hist { display: grid; gap: 6px; }
.hist .bar { display: grid; grid-template-columns: 44px 1fr; gap: 8px; align-items: center; font-family: var(--head); font-size: 8px; color: var(--fog); }
.hist .fill { height: 16px; min-width: 20px; background: var(--steel); color: var(--k); display: flex; align-items: center; justify-content: flex-end; padding-right: 4px; }
.hist .fill.me { background: var(--lime); }

@media (max-width: 460px) {
  .nav { display: grid; grid-template-columns: auto auto; gap: 6px; }
}
@media (max-width: 380px) {
  .app { padding: 12px 10px; gap: 10px; }
  #logo canvas { width: 124px; height: 36px; }
  .btn.small { padding: 10px 8px 12px; }
  .v { font-size: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .modal, .msg.warn { animation: none; }
}
