:root {
  --parchment-1: #f0dfae;
  --parchment-2: #d6b577;
  --parchment-3: #b8924c;
  --ink: #2a1a0a;
  --ink-2: #4a2e14;
  --sepia: #6e431a;
  --sigil: #8a2a18;
  --gold: #c2902f;
  --filch: #6f635a;
  --norris: #b86a2a;
  --snape: #1a1410;
  --umbridge: #c97a9a;
  --cloak: #345a8a;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: #1a1308;
  color: var(--ink);
  font-family: "IM Fell English", "Iowan Old Style", Georgia, serif;
  touch-action: none;
  overscroll-behavior: none;
}

#frame {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  background:
    radial-gradient(ellipse at 30% 20%, #f3e3b6 0%, #e1c889 35%, #c19a52 75%, #8a6628 100%);
  background-attachment: fixed;
  position: relative;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* Parchment texture — pure CSS layered gradients with tea stains, inkblots,
   and fold creases. */
#frame::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    /* Inkblots / stains */
    radial-gradient(ellipse 18px 12px at 8% 24%, rgba(70, 35, 5, 0.30) 0, transparent 70%),
    radial-gradient(ellipse 14px 10px at 92% 18%, rgba(70, 35, 5, 0.22) 0, transparent 70%),
    radial-gradient(ellipse 22px 14px at 12% 88%, rgba(70, 35, 5, 0.26) 0, transparent 70%),
    radial-gradient(ellipse 16px 11px at 86% 78%, rgba(70, 35, 5, 0.22) 0, transparent 70%),
    radial-gradient(ellipse 10px 8px at 50% 6%, rgba(70, 35, 5, 0.18) 0, transparent 70%),
    /* Tea-stain blobs */
    radial-gradient(circle at 12% 18%, rgba(120, 70, 20, 0.20) 0, transparent 35%),
    radial-gradient(circle at 78% 30%, rgba(80, 40, 10, 0.16) 0, transparent 30%),
    radial-gradient(circle at 30% 78%, rgba(60, 30, 5, 0.18) 0, transparent 40%),
    radial-gradient(circle at 88% 88%, rgba(110, 60, 15, 0.20) 0, transparent 35%),
    /* Fine fibre noise */
    repeating-linear-gradient(
      45deg,
      rgba(80, 50, 20, 0.030) 0 2px,
      transparent 2px 6px
    ),
    /* Fold creases — vertical centre + two horizontal thirds */
    linear-gradient(to right,
      transparent calc(50% - 1px),
      rgba(80, 40, 10, 0.18) calc(50% - 0.5px),
      rgba(80, 40, 10, 0.10) 50%,
      rgba(80, 40, 10, 0.18) calc(50% + 0.5px),
      transparent calc(50% + 1px)
    ),
    linear-gradient(to bottom,
      transparent calc(33% - 1px),
      rgba(80, 40, 10, 0.14) 33%,
      transparent calc(33% + 1px)
    ),
    linear-gradient(to bottom,
      transparent calc(67% - 1px),
      rgba(80, 40, 10, 0.14) 67%,
      transparent calc(67% + 1px)
    );
  mix-blend-mode: multiply;
  opacity: 0.92;
}

#frame::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    /* Vignette / page edge darkening */
    radial-gradient(ellipse at center, transparent 50%, rgba(40, 20, 5, 0.55) 100%),
    /* Subtle warm haze */
    radial-gradient(ellipse at 50% 30%, rgba(255, 220, 150, 0.08) 0, transparent 60%);
}

/* HUD */
#hud {
  flex: 0 0 auto;
  padding: 10px 14px 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  position: relative;
  z-index: 2;
  border-bottom: 1px solid rgba(60, 30, 5, 0.35);
}

.hud-left .hud-dedication {
  font-family: "IM Fell English", "Iowan Old Style", Georgia, serif;
  font-style: italic;
  font-size: 10px;
  letter-spacing: 0.04em;
  color: var(--sepia);
  line-height: 1.1;
  margin-bottom: 1px;
}

.hud-left .hud-title {
  font-family: "IM Fell English SC", "IM Fell English", serif;
  font-size: 20px;
  letter-spacing: 0.08em;
  color: var(--ink);
  line-height: 1;
  text-shadow: 0 1px 0 rgba(255, 230, 180, 0.5);
}

.hud-left .hud-sub {
  font-style: italic;
  font-size: 11px;
  color: var(--sepia);
  margin-top: 2px;
  line-height: 1.1;
}

.hud-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hud-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: "IM Fell English SC", serif;
  line-height: 1;
}

.hud-stat .lbl {
  font-size: 9px;
  letter-spacing: 0.1em;
  color: var(--sepia);
  text-transform: uppercase;
}

.hud-stat span:not(.lbl) {
  font-size: 16px;
  color: var(--ink);
  margin-top: 2px;
}

.hud-stat.lives {
  flex-direction: row;
  gap: 3px;
  margin-top: 8px;
}
.hud-stat.lives span {
  width: 8px;
  height: 14px;
  background: linear-gradient(to bottom, var(--gold) 0%, var(--ink-2) 100%);
  border-radius: 2px 2px 1px 1px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.3);
}
.hud-stat.lives span.lost {
  opacity: 0.15;
  filter: grayscale(1);
}

#pause-btn {
  background: transparent;
  border: 1px solid rgba(60, 30, 5, 0.4);
  color: var(--ink);
  width: 32px;
  height: 32px;
  border-radius: 16px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  font-family: serif;
}
#pause-btn:active {
  background: rgba(60, 30, 5, 0.15);
}

/* Stage */
#stage {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 2;
  padding: 8px 8px calc(14px + var(--safe-bottom));
  min-height: 0;
}

#game {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 21 / 27;
  image-rendering: auto;
  filter: drop-shadow(0 4px 8px rgba(40, 20, 0, 0.4));
  touch-action: none;
}

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  padding: 20px;
  z-index: 3;
}

.overlay.show {
  display: flex;
  animation: fade-in 0.3s ease-out;
}

.overlay::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center, rgba(240, 220, 170, 0.86) 0%, rgba(140, 90, 30, 0.78) 100%);
  z-index: -1;
}

.overlay h1 {
  font-family: "IM Fell English SC", serif;
  font-size: 28px;
  margin: 0 0 12px;
  color: var(--ink);
  letter-spacing: 0.06em;
}

.overlay .quote {
  font-family: "IM Fell English", serif;
  font-style: italic;
  font-size: 18px;
  color: var(--ink-2);
  margin-bottom: 24px;
  max-width: 320px;
  line-height: 1.4;
}

.overlay .quote em {
  color: var(--sigil);
  font-weight: 600;
}

.overlay .btn {
  background: rgba(40, 20, 5, 0.85);
  color: var(--parchment-1);
  border: 2px solid var(--ink);
  padding: 12px 28px;
  font-family: "IM Fell English SC", serif;
  font-size: 16px;
  letter-spacing: 0.1em;
  cursor: pointer;
  border-radius: 4px;
  margin: 6px 0;
  min-width: 200px;
}
.overlay .btn:active {
  transform: translateY(1px);
}
.overlay .btn.ghost {
  background: transparent;
  color: var(--ink);
}

.overlay .small {
  font-size: 12px;
  color: var(--sepia);
  margin-top: 18px;
  font-style: italic;
}

.overlay .stat-row {
  display: flex;
  gap: 24px;
  margin: 8px 0 18px;
}
.overlay .stat-row .col {
  display: flex;
  flex-direction: column;
}
.overlay .stat-row .col .v {
  font-family: "IM Fell English SC", serif;
  font-size: 24px;
  color: var(--ink);
}
.overlay .stat-row .col .k {
  font-size: 11px;
  letter-spacing: 0.1em;
  color: var(--sepia);
  text-transform: uppercase;
}

/* Name entry */
.name-entry {
  display: flex;
  gap: 10px;
  margin: 12px 0 18px;
}
.name-entry .slot {
  width: 44px;
  height: 56px;
  border: 2px solid var(--ink);
  background: rgba(255, 240, 200, 0.4);
  font-family: "IM Fell English SC", serif;
  font-size: 32px;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}
.name-entry .slot.active {
  background: rgba(255, 220, 140, 0.9);
  box-shadow: 0 0 0 3px rgba(194, 144, 47, 0.5);
}
.name-keys {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  max-width: 320px;
  margin: 0 auto 12px;
}
.name-keys button {
  background: rgba(40, 20, 5, 0.7);
  color: var(--parchment-1);
  border: 1px solid var(--ink);
  padding: 8px 0;
  font-family: "IM Fell English SC", serif;
  font-size: 14px;
  border-radius: 3px;
  cursor: pointer;
}
.name-keys button:active {
  transform: translateY(1px);
}
.name-keys .wide {
  grid-column: span 2;
}

/* Leaderboard */
.lb {
  width: 100%;
  max-width: 340px;
  margin: 6px 0 18px;
  border-collapse: collapse;
  font-family: "IM Fell English SC", serif;
  color: var(--ink);
}
.lb th,
.lb td {
  padding: 6px 8px;
  border-bottom: 1px dashed rgba(60, 30, 5, 0.35);
  text-align: left;
  font-size: 14px;
}
.lb th {
  font-size: 11px;
  letter-spacing: 0.1em;
  color: var(--sepia);
  text-transform: uppercase;
  border-bottom: 1px solid rgba(60, 30, 5, 0.5);
}
.lb tr.you td {
  background: rgba(194, 144, 47, 0.25);
  font-weight: 600;
}
.lb td.rank {
  font-family: "IM Fell English", serif;
  font-style: italic;
  width: 32px;
  color: var(--sepia);
}
.lb td.score {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* In-game center text (level intro) */
.center-banner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: "IM Fell English SC", serif;
  font-size: 22px;
  color: var(--sigil);
  background: rgba(240, 220, 170, 0.85);
  padding: 8px 18px;
  border: 1px solid var(--ink);
  border-radius: 4px;
  pointer-events: none;
  text-align: center;
  z-index: 3;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes flicker {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

@media (max-height: 700px) {
  .hud-left .hud-title { font-size: 17px; }
  .hud-left .hud-sub { font-size: 11px; }
}
