/* Foldaway page chrome: HUD bar, stage, overlays, the stage-clear card and menus, in a bright
   daytime paper-craft look (cream paper, cut-paper panels with soft drop shadows, hand-cut
   buttons). The playfield itself is canvas. */

:root {
  --paper: #f6eedc;
  --paper-2: #efe3c8;
  --card: #fffaf0;
  --card-edge: #e3d3b1;
  --kraft: #c99a62;
  --kraft-dark: #8c6236;
  --ink: #2e2a25;
  --ink-soft: #5d554b;
  --post-red: #c8423a;
  --post-red-dark: #9a2e28;
  --sky: #3d6fa8;
  --leaf: #4f8c47;
  --gold: #c99318;
  --foil-1: #d9dee6;
  --foil-2: #f7f9fc;
  --foil-3: #a7b1bf;
  --glitter: #d14fa0;
  --glitter-2: #f2b33d;
  --boss-ink: #1d2436;
  --focus: #2c63b5;
  --danger: #b3261e;
  --shadow: rgba(80, 56, 24, 0.22);
  --shadow-strong: rgba(80, 56, 24, 0.34);
  --hud-height: 3rem;
  /* The playfield is landscape, 11:7. The renderer overwrites these with the fitted size. */
  --field-width: min(100vw, calc((100vh - var(--hud-height)) * 11 / 7));
  --field-height: calc(var(--field-width) * 7 / 11);
  --font-ui: "Trebuchet MS", "Gill Sans", "Segoe UI", system-ui, sans-serif;
  --font-title: "Rockwell", "Roboto Slab", "Bookman Old Style", Georgia, serif;
  --font-numbers: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  /* Slightly uneven corners give cut-paper edges. */
  --cut-a: 5px 11px 6px 9px / 9px 5px 11px 6px;
  --cut-b: 10px 4px 9px 6px / 5px 10px 6px 9px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  background:
    repeating-linear-gradient(97deg, rgba(140, 98, 54, 0.025) 0 2px, transparent 2px 7px),
    repeating-linear-gradient(8deg, rgba(140, 98, 54, 0.02) 0 1px, transparent 1px 5px),
    radial-gradient(ellipse at 50% 0%, #fdf8ec, transparent 70%),
    linear-gradient(180deg, var(--paper), var(--paper-2));
  color: var(--ink);
  font: 15px/1.4 var(--font-ui);
  overflow: hidden;
}

/* HUD */

.hud {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.3rem 0.95rem;
  flex-wrap: wrap;
  width: 100%;
  max-width: max(var(--field-width), 24rem);
  min-height: var(--hud-height);
  margin: 0 auto;
  padding: 0.35rem 0.75rem;
  font-variant-numeric: tabular-nums;
}

.hud-item {
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
}

.hud-label {
  color: var(--ink-soft);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  white-space: nowrap;
}

.hud-value {
  font: 700 1.05rem/1.2 var(--font-numbers);
  color: var(--ink);
}

.hud-value-soft { color: var(--ink-soft); }

.hud-clock { min-width: 4.4em; }

.hud-stage {
  flex: 1 0 auto;
  min-width: 0;
  align-items: center;
}

.hud-stage-label {
  padding: 0.05rem 0.45rem;
  border-radius: var(--cut-b);
  background: var(--sky);
  color: #fff;
  font: 700 0.85rem/1.3 var(--font-numbers);
  box-shadow: 1px 2px 0 var(--shadow);
  transform: rotate(-2deg);
}

.hud-stage-name {
  max-width: 12rem;
  overflow: hidden;
  color: var(--ink);
  font-family: var(--font-title);
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.hud-back {
  color: var(--ink-soft);
  font-size: 0.8rem;
  text-decoration: none;
}

.hud-back:hover { color: var(--ink); text-decoration: underline; }

/* Stamp slots: perforated postage-stamp outlines, filled red with a white star when collected. */
.hud-stamps {
  display: flex;
  gap: 0.25rem;
}

.stamp-slot {
  position: relative;
  width: 0.95rem;
  height: 1.15rem;
  border: 2px dotted var(--kraft);
  border-radius: 2px;
  background: rgba(255, 250, 240, 0.7);
}

.stamp-slot.is-filled {
  border-color: var(--card);
  background: var(--post-red);
  box-shadow: 0 0 0 1px var(--post-red-dark), 1px 2px 0 var(--shadow);
  transform: rotate(-4deg);
}

.stamp-slot.is-filled::after {
  content: "";
  position: absolute;
  inset: 22% 18%;
  background: var(--card);
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}

/* The foil badge keeps its place; it turns shiny while the hero is wrapped. */
.hud-foil {
  padding: 0.05rem 0.5rem;
  border: 1px dashed var(--card-edge);
  border-radius: var(--cut-a);
  color: #b4a68e;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.hud-foil.is-on {
  border: 1px solid var(--foil-3);
  background: linear-gradient(120deg, var(--foil-1), var(--foil-2) 45%, var(--foil-3) 60%, var(--foil-2));
  background-size: 220% 100%;
  color: #3e4652;
  box-shadow: 1px 2px 0 var(--shadow);
  animation: foil-shine 1.6s linear infinite;
}

@keyframes foil-shine {
  from { background-position: 100% 0; }
  to { background-position: -120% 0; }
}

.hud-lives {
  display: flex;
  gap: 0.3rem;
  min-height: 1rem;
}

/* A tiny folded-paper hare head: two ears and a muzzle cut from one sheet, with a fold shade. */
.life-icon {
  width: 1rem;
  height: 1rem;
  background: linear-gradient(115deg, #fffdf8 0 52%, #e6d6b8 52% 100%);
  clip-path: polygon(8% 100%, 14% 46%, 18% 0%, 36% 40%, 52% 2%, 58% 44%, 96% 70%, 88% 100%);
  filter: drop-shadow(1px 1px 0 var(--kraft-dark));
}

.hud-sound {
  padding: 0.2rem 0.6rem;
  border: 1px solid var(--card-edge);
  border-radius: var(--cut-a);
  background: var(--card);
  color: var(--ink);
  font: inherit;
  font-size: 0.78rem;
  box-shadow: 1px 2px 0 var(--shadow);
  cursor: pointer;
}

.hud-sound:hover { background: #fff; }
.hud-sound.is-muted { color: var(--ink-soft); text-decoration: line-through; }

@media (max-width: 560px) {
  .hud { gap: 0.2rem 0.6rem; padding: 0.3rem 0.5rem; }
  .hud-back-text { display: none; }
  .hud-value { font-size: 0.95rem; }
  .hud-label { font-size: 0.62rem; letter-spacing: 0.08em; }
  .hud-stage-name { max-width: 8rem; }
  .hud-sound { padding: 0.15rem 0.45rem; font-size: 0.72rem; }
}

/* Stage and playfield */

.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 0.25rem;
}

.playfield {
  position: relative;
  width: var(--field-width);
  height: var(--field-height);
  border-radius: 4px;
  background: #cfe6f2;
  box-shadow:
    0 0 0 3px var(--card),
    0 0 0 4px var(--card-edge),
    3px 6px 0 4px var(--shadow),
    0 14px 34px var(--shadow);
  overflow: hidden;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Panels over the top of the playfield (Glitter Rush timer, boss hit points). */

.field-hud {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  right: 0.5rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.4rem;
  pointer-events: none;
}

.rush-panel,
.boss-panel {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.25rem 0.6rem;
  border-radius: var(--cut-b);
  background: rgba(255, 250, 240, 0.92);
  box-shadow: 2px 3px 0 var(--shadow);
  font-size: 0.78rem;
  font-weight: 700;
}

.rush-label { color: var(--glitter); letter-spacing: 0.06em; text-transform: uppercase; }

.rush-meter {
  position: relative;
  width: 6.5rem;
  height: 0.55rem;
  border-radius: 999px;
  background: #eadfc9;
  overflow: hidden;
}

.rush-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 100%;
  background:
    radial-gradient(circle at 20% 50%, #fff 0 1px, transparent 2px) 0 0 / 0.6rem 0.55rem,
    linear-gradient(90deg, var(--glitter), var(--glitter-2));
  transition: width 0.1s linear;
}

.rush-seconds { font-family: var(--font-numbers); color: var(--ink); }

.boss-panel {
  margin-left: auto;
  background: rgba(29, 36, 54, 0.9);
  color: #f3ecdc;
}

.boss-name { font-family: var(--font-title); letter-spacing: 0.03em; white-space: nowrap; }

.boss-pips {
  display: flex;
  gap: 0.18rem;
}

.boss-pip {
  width: 0.55rem;
  height: 0.7rem;
  border: 1px solid rgba(243, 236, 220, 0.5);
  border-radius: 50% 50% 45% 45% / 60% 60% 40% 40%;
}

.boss-pip.is-filled {
  border-color: #0b0f1a;
  background: radial-gradient(circle at 35% 30%, #5a6788, #0b0f1a 70%);
}

/* Stage-clear card: an envelope-flap card in the middle of the field, under the menus. */

.card-layer {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem;
  background: rgba(246, 238, 220, 0.35);
}

.clear-card {
  position: relative;
  width: min(100%, 21rem);
  padding: 1.4rem 1.2rem 1rem;
  border-radius: var(--cut-a);
  background: var(--card);
  text-align: center;
  box-shadow: 0 0 0 1px var(--card-edge), 4px 6px 0 var(--shadow), 0 16px 30px var(--shadow);
  animation: card-unfold 0.35s ease-out both;
  transform-origin: 50% 0;
}

/* The envelope flap along the top edge. */
.clear-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1rem;
  background: linear-gradient(var(--post-red), var(--post-red-dark));
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  opacity: 0.9;
}

@keyframes card-unfold {
  from { opacity: 0; transform: perspective(600px) rotateX(-70deg); }
  to { opacity: 1; transform: none; }
}

.clear-heading {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 0.5rem;
  margin: 0.1rem 0 0.7rem;
  font: 700 1.35rem/1.15 var(--font-title);
}

.clear-stage-label {
  color: var(--sky);
  font-family: var(--font-numbers);
}

.clear-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem 1rem;
  margin: 0 0 0.5rem;
}

.clear-stats div { display: flex; flex-direction: column; }
.clear-stats dt { color: var(--ink-soft); font-size: 0.7rem; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; }
.clear-stats dd { margin: 0; font: 700 1.05rem/1.3 var(--font-numbers); }

.clear-mark {
  display: inline-block;
  padding: 0 0.35rem;
  border-radius: var(--cut-b);
  background: var(--leaf);
  color: #fff;
  font: 700 0.68rem/1.5 var(--font-ui);
  letter-spacing: 0.04em;
  vertical-align: middle;
}

.clear-mark.is-new {
  background: var(--post-red);
  transform: rotate(-4deg);
}

.clear-run {
  margin: 0.2rem 0 0;
  color: var(--post-red-dark);
  font-weight: 700;
}

/* Overlays */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem;
  overflow-y: auto;
  background: rgba(246, 238, 220, 0.72);
}

.panel {
  width: min(100%, 27rem);
  margin: auto;
  padding: 1rem 1.2rem 1.15rem;
  border-radius: var(--cut-a);
  background:
    repeating-linear-gradient(0deg, transparent 0 1.55rem, rgba(61, 111, 168, 0.06) 1.55rem calc(1.55rem + 1px)),
    var(--card);
  text-align: center;
  box-shadow: 0 0 0 1px var(--card-edge), 5px 7px 0 var(--shadow), 0 18px 40px var(--shadow);
}

.panel-narrow { width: min(100%, 17rem); }

.panel-wide {
  width: min(100%, 46rem);
  display: grid;
  gap: 0.6rem 1.4rem;
}

@media (min-width: 760px) {
  .panel-wide { grid-template-columns: 1.15fr 1fr; align-items: start; }
}

.title-scores { display: flex; flex-direction: column; align-items: stretch; }
.title-scores .button { margin-top: 0.6rem; }

.kicker {
  margin: 0;
  color: var(--post-red);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.24em;
  text-transform: uppercase;
}

.game-title {
  margin: 0.05rem 0 0.45rem;
  font: 700 clamp(2.1rem, 6vw, 3.1rem)/1.05 var(--font-title);
  color: var(--sky);
  letter-spacing: 0.03em;
  text-shadow: 2px 3px 0 var(--card-edge), 3px 5px 0 var(--shadow);
}

.screen-heading {
  margin: 0.1rem 0 0.65rem;
  font: 700 1.5rem/1.15 var(--font-title);
  color: var(--ink);
}

.lead {
  margin: 0 0 0.6rem;
  font-size: 0.88rem;
}

.hint {
  margin: 0 0 0.4rem;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.25rem 0.9rem;
  margin: 0 0 0.6rem;
  padding: 0;
  list-style: none;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

kbd {
  display: inline-block;
  min-width: 1.5em;
  margin: 0 0.08rem;
  padding: 0.05rem 0.3rem;
  border: 1px solid var(--card-edge);
  border-radius: 3px;
  background: #fff;
  color: var(--ink);
  font: 700 0.74rem/1.3 var(--font-ui);
  text-align: center;
  box-shadow: 0 2px 0 var(--card-edge);
}

.table-heading {
  margin: 0 0 0.35rem;
  color: var(--post-red-dark);
  font: 700 1rem/1.2 var(--font-title);
}

.scores { min-height: 1.5rem; }
/* Game over: the table sits directly above "Fold again"; keep the button (and its focus ring) off the last row. */
.panel > .scores { margin-bottom: 0.8rem; }

.score-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.score-table th {
  padding: 0.12rem 0.4rem;
  border-bottom: 2px dashed var(--card-edge);
  color: var(--ink-soft);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-align: left;
  text-transform: uppercase;
}

.score-table td { padding: 0.1rem 0.4rem; text-align: left; }
.score-table td:nth-child(3), .score-table td:nth-child(4), .score-table td:nth-child(5) { font-family: var(--font-numbers); }
.score-table td:nth-child(5) { white-space: nowrap; }
.score-table tbody tr:nth-child(odd) { background: rgba(201, 154, 98, 0.1); }

.score-table tr.is-player {
  background: rgba(200, 66, 58, 0.14);
  outline: 2px solid var(--post-red);
  font-weight: 700;
}

.score-message {
  margin: 0.3rem 0;
  color: var(--ink-soft);
  font-size: 0.85rem;
  font-style: italic;
}

.result { margin: 0 0 0.7rem; }
.result strong { color: var(--sky); font-size: 1.15rem; font-family: var(--font-numbers); }
.final-stage-name { font-family: var(--font-title); font-weight: 700; }

.name-form { margin: 0 0 0.6rem; }

.name-form label {
  display: block;
  margin-bottom: 0.25rem;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

.name-row {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}

.name-row input {
  width: 10rem;
  min-width: 0;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--kraft);
  border-radius: var(--cut-b);
  background: #fff;
  color: var(--ink);
  font: inherit;
}

.name-error {
  min-height: 1.2em;
  margin: 0.3rem 0 0;
  color: var(--danger);
  font-size: 0.8rem;
}

.status {
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
}

.status:empty { display: none; }

/* Buttons and focus: hand-cut paper tabs that press down when clicked. */

.menu {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.button {
  padding: 0.5rem 1rem;
  border: 1px solid var(--card-edge);
  border-radius: var(--cut-a);
  background: #fff;
  color: var(--ink);
  font: 700 0.92rem/1.2 var(--font-ui);
  box-shadow: 2px 3px 0 var(--shadow);
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s, background-color 0.15s;
}

.button:nth-child(even) { border-radius: var(--cut-b); }
.button:hover { background: #fffdf6; transform: translate(-1px, -1px); box-shadow: 3px 4px 0 var(--shadow); }
.button:active { transform: translate(2px, 3px); box-shadow: 0 0 0 var(--shadow); }
.button:disabled { opacity: 0.6; cursor: default; transform: none; }

.button-primary {
  border-color: var(--post-red-dark);
  background: var(--post-red);
  color: #fff;
  box-shadow: 2px 3px 0 var(--post-red-dark);
}

.button-primary:hover { background: #d34c43; box-shadow: 3px 4px 0 var(--post-red-dark); }

.button:focus-visible,
.hud-sound:focus-visible,
.hud-back:focus-visible,
.name-row input:focus-visible {
  outline: 3px dashed var(--focus);
  outline-offset: 3px;
}

/* Programmatic focus on menu buttons should also be visible for keyboard players. */
.overlay .button:focus {
  outline: 3px dashed var(--focus);
  outline-offset: 3px;
}

/* Small windows: keep the title readable without pushing anything off the field. The playfield
   can be much shorter than a menu panel here, so the overlays cover the whole window instead of
   just the field (still scrolling inside themselves when even that is too short). */
@media (max-height: 560px), (max-width: 560px) {
  .overlay { position: fixed; z-index: 30; }
  .panel { padding: 0.7rem 0.8rem 0.8rem; }
  .game-title { font-size: 2rem; }
  .lead { font-size: 0.8rem; }
  .hint { font-size: 0.74rem; }
  .score-table { font-size: 0.74rem; }
}

/* Touch devices (html.is-touch, shared/touch-controls.css): gestures, nothing reserved on screen.
   The pause button is cut from the same card as the menus, the steering marker is pencil ink.
   Landscape: one compact HUD line across the whole screen so the field keeps its height. */
:root {
  --tc-ink: var(--ink);
  --tc-fill-solid: var(--card);
  --tc-edge: var(--kraft-dark);
  --tc-pressed: rgba(200, 66, 58, 0.55);
  --tc-marker: var(--ink);
  --tc-marker-opacity: 0.32;
}

html.is-touch .tc-pause { border-color: var(--card-edge); box-shadow: 1px 2px 0 var(--shadow); }

@media (orientation: landscape) and (max-height: 560px) {
  /* Sound stays in the pause menu; the bar must fit one line without widening the page. */
  html.is-touch .hud-sound { display: none; }
  html.is-touch .hud {
    flex-wrap: nowrap;
    max-width: none;
    min-height: 0;
    gap: 0.75rem;
    padding: 0.1rem 0.6rem;
  }
  html.is-touch .hud-back-text { display: none; }
  html.is-touch .hud-stage-name { max-width: 9rem; }
  html.is-touch .stage { padding-bottom: 0; }
}

@media (orientation: landscape) and (max-height: 560px) and (max-width: 760px) {
  html.is-touch .hud { gap: 0.55rem; }
  html.is-touch .hud-stage-name { display: none; }
}

/* Portrait: the field sits under the HUD and the free space below it is where thumbs rest
   (nothing is reserved there; the field keeps its full width). */
@media (orientation: portrait) {
  html.is-touch .stage { align-items: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  .button { transition: none; }
  .button:hover, .button:active { transform: none; }
  .hud-foil.is-on { animation: none; }
  .clear-card { animation: none; }
  .rush-fill { transition: none; }
}
