:root {
  --paper: #FBF9F5;
  --surface: #FFFFFF;
  --surface-sunken: #F3EFE7;
  --ink: #1A1712;
  --ink-muted: #6B6459;
  --line: #E7E0D4;
  --line-strong: #D8CFBE;
  --brand: #B7833A;
  --brand-strong: #8E6326;
  --brand-soft: #F0E4CE;
  --accent-red: #E83F3F;
  --accent-mint: #21B573;
  --accent-teal: #18A9C7;
  --accent-gold: #F5C542;
  --font-serif: Fraunces, Georgia, "Times New Roman", serif;
  --font-sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(26, 23, 18, .05), 0 1px 1px rgba(26, 23, 18, .04);
  --shadow-md: 0 14px 38px rgba(26, 23, 18, .13), 0 3px 8px rgba(26, 23, 18, .07);
  --ease: cubic-bezier(.2, .7, .2, 1);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 16% 0%, rgba(240, 228, 206, .8), transparent 28rem),
    linear-gradient(180deg, var(--paper), #F7F1E8 100%);
  color: var(--ink);
  font-family: var(--font-sans);
}

a, button { -webkit-tap-highlight-color: transparent; }
a { color: inherit; }
button {
  min-height: 44px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  font: 800 .94rem/1 var(--font-sans);
  transition: transform 160ms var(--ease), border-color 160ms var(--ease), box-shadow 160ms var(--ease), background 160ms var(--ease);
}
button:hover { transform: translateY(-1px); border-color: var(--brand); box-shadow: var(--shadow-sm); }
button:active, button.is-held { transform: translateY(0); background: var(--brand-soft); }
button:focus-visible, a:focus-visible { outline: 2px solid var(--brand); outline-offset: 3px; }
button.hidden { display: none; }

.shell {
  width: min(1160px, calc(100vw - clamp(14px, 4vw, 48px)));
  min-height: 100svh;
  margin: 0 auto;
  padding: 16px 0 calc(18px + env(safe-area-inset-bottom));
  display: grid;
  gap: 12px;
}

.brandbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  min-height: 58px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  box-shadow: var(--shadow-sm);
}
.wordmark {
  color: var(--ink);
  font-family: var(--font-serif);
  font-size: clamp(1.35rem, 3vw, 1.85rem);
  font-weight: 500;
  line-height: 1;
  text-decoration: none;
  white-space: nowrap;
}
.wordmark span { color: var(--brand); }
.product-lockup {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: var(--ink-muted);
  font-family: var(--font-mono);
  font-size: .8125rem;
  line-height: 1;
}
.product-lockup span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip {
  width: 12px;
  height: 12px;
  flex: 0 0 auto;
  border-radius: 4px;
  background: linear-gradient(135deg, var(--accent-gold), var(--accent-red) 48%, var(--accent-teal));
  box-shadow: inset 0 0 0 1px rgba(26, 23, 18, .16);
}
.brandbar p { margin: 0; color: var(--ink-muted); font-size: .875rem; line-height: 1.2; white-space: nowrap; }

.promo-banner {
  margin: 0 0 12px;
  padding: 12px 14px;
  border: 2px solid #f2cf00;
  border-radius: 14px;
  background: #fff7bf;
  color: #211900;
  text-align: center;
  box-shadow: 0 10px 24px rgba(0, 0, 0, .12);
}
.promo-banner p { margin: 0 auto 10px; max-width: 760px; font-size: .95rem; line-height: 1.35; font-weight: 700; }
.promo-banner a { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.promo-banner img { display: block; width: min(260px, 100%); height: auto; filter: drop-shadow(0 8px 14px rgba(0, 0, 0, .18)); }

.hud {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 8px;
}
.hud div {
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.label {
  display: block;
  color: var(--ink-muted);
  font-family: var(--font-mono);
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: lowercase;
}
.hud strong {
  display: block;
  margin-top: 4px;
  color: var(--ink);
  font-size: clamp(1rem, 1.8vw, 1.4rem);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stage-wrap {
  position: relative;
  width: min(100%, calc((100svh - 250px) * 15 / 16));
  max-width: 860px;
  justify-self: center;
  overflow: hidden;
  aspect-ratio: 15 / 16;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg);
  background: #050608;
  box-shadow: var(--shadow-md), 0 0 34px rgba(24, 169, 199, .22), inset 0 0 0 1px rgba(255, 255, 255, .1);
}
.stage-wrap::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  border-radius: inherit;
  box-shadow: inset 0 0 0 2px rgba(183, 131, 58, .45), inset 0 -64px 120px rgba(11, 30, 34, .35);
}
canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #050608;
  overscroll-behavior: contain;
  touch-action: none;
  user-select: none;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 13px;
  padding: 24px;
  text-align: center;
  /* On short viewports the overlay's own content (icon, title, text, buttons, key
     hints) can be taller than the stage box, which clips overflow — without this,
     that content silently became unreachable instead of just scrollable. */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background:
    linear-gradient(180deg, rgba(251,249,245,.94), rgba(251,249,245,.78)),
    radial-gradient(circle at 50% 34%, rgba(245,197,66,.22), transparent 20rem),
    radial-gradient(circle at 42% 70%, rgba(232,63,63,.14), transparent 18rem);
}
.overlay.hidden { display: none; }
.overlay-tile {
  width: 68px;
  height: 68px;
  border: 1px solid rgba(26,23,18,.12);
  border-radius: var(--radius-lg);
  background:
    radial-gradient(circle at 44% 30%, #FFF3C4 0 16%, transparent 17%),
    radial-gradient(ellipse at 50% 52%, #F5C542 0 40%, transparent 41%),
    radial-gradient(ellipse at 50% 78%, #E83F3F 0 30%, transparent 31%),
    #FBF9F5;
  box-shadow: var(--shadow-md);
}
.overlay h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: clamp(2.4rem, 8vw, 5rem);
  font-weight: 520;
  line-height: .92;
}
.overlay p { max-width: 540px; margin: 0; color: var(--ink-muted); font-size: 1rem; line-height: 1.45; }
.overlay-buttons { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.overlay button {
  min-width: 120px;
  border-color: var(--brand-strong);
  background: var(--brand-strong);
  color: #fff;
}
.overlay button#modeButton,
.overlay button#settingsButton {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line-strong);
}

.charity-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(480px, 100%);
  max-height: 42vh;
  overflow-y: auto;
  padding: 4px 2px;
}
.charity-list.hidden { display: none; }
.charity-remaining {
  text-align: center;
  font-weight: 700;
  color: var(--ink);
  padding: 4px 0 2px;
}
.charity-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 10px;
  padding: 8px 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.charity-name { flex: 1 1 160px; min-width: 0; font-size: .92rem; color: var(--ink); text-align: left; }
.charity-controls { display: flex; align-items: center; gap: 8px; flex-shrink: 0; margin-left: auto; }
.charity-count { min-width: 1.4em; text-align: center; color: var(--ink); }
.charity-step {
  min-width: 48px;
  min-height: 48px;
  padding: 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--brand-strong);
  background: var(--brand-strong);
  color: #fff;
  font-size: 1.3rem;
  line-height: 1;
}
.keys {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
  max-width: 560px;
}
.keys span {
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: rgba(255,255,255,.65);
  color: var(--ink-muted);
  font-family: var(--font-mono);
  font-size: .72rem;
}

.controls {
  display: grid;
  grid-template-columns: auto repeat(2, minmax(88px, 1fr));
  align-items: center;
  gap: 10px;
  width: min(860px, 100%);
  justify-self: center;
  touch-action: none;
}
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 48px);
  grid-template-rows: repeat(2, 48px);
  gap: 8px;
}
.pad { min-width: 48px; min-height: 48px; padding: 0; font-size: 1.2rem; }
.pad.up { grid-column: 2; grid-row: 1; }
.pad.left { grid-column: 1; grid-row: 2; }
.pad.down { grid-column: 2; grid-row: 2; }
.pad.right { grid-column: 3; grid-row: 2; }
.action { min-height: 56px; text-transform: lowercase; font-size: 1.02rem; }
.action.pepper { background: var(--accent-red); color: #FFF6EA; border-color: #B21F1F; box-shadow: 0 0 20px rgba(232,63,63,.28); }
.action.pepper.is-held { background: #B21F1F; }
.action.pause { background: var(--surface); color: var(--ink); border-color: var(--line-strong); }

.game-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  color: var(--ink-muted);
  font-size: .84rem;
  padding: 4px 2px;
}
.game-footer nav { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.game-footer a { text-decoration: none; }

@media (max-width: 720px) {
  .shell { width: calc(100vw - 12px); padding-top: 8px; gap: 6px; }
  .brandbar { grid-template-columns: 1fr; gap: 5px; min-height: 0; border-radius: var(--radius-md); }
  .brandbar p { display: none; }
  .promo-banner { order: 8; margin: 10px 0 0; padding: 8px 10px; }
  .promo-banner p { font-size: .8rem; margin-bottom: 6px; }
  .promo-banner img { width: min(180px, 100%); }
  .hud { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
  .hud div { padding: 8px 9px; }
  .stage-wrap {
    width: min(100%, calc((100svh - 224px - env(safe-area-inset-bottom)) * 15 / 16));
    max-height: calc(100svh - 224px - env(safe-area-inset-bottom));
    border-radius: var(--radius-md);
  }
  .controls {
    grid-template-columns: auto repeat(2, minmax(76px, 1fr));
    gap: 6px;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .dpad { justify-self: start; gap: 6px; }
  .action { align-self: stretch; min-height: 48px; font-size: .86rem; }
  .game-footer { align-items: flex-start; flex-direction: column; }
  .overlay { padding: 14px; gap: 9px; }
  .overlay-tile { width: 44px; height: 44px; }
  .overlay h1 { font-size: clamp(1.7rem, 9vw, 3rem); }
  .overlay p { font-size: .88rem; line-height: 1.3; }
  .keys { display: none; }
}

@media (max-width: 360px) {
  .shell { width: calc(100% - 12px); }
  .hud { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .hud div { padding: 7px 6px; }
  .hud .label { font-size: .62rem; }
  .hud strong { font-size: .86rem; }
  .controls { grid-template-columns: auto repeat(2, minmax(66px, 1fr)); }
  .overlay { padding: 14px; }
  .overlay p { font-size: .9rem; }
  .keys { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 1ms !important; scroll-behavior: auto !important; }
}

.game-guide {
  display: grid;
  gap: 12px;
  margin-top: 22px;
  padding: clamp(18px, 3vw, 28px);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  box-shadow: var(--shadow-sm);
  color: var(--ink-muted);
  line-height: 1.65;
}

.game-guide h2 {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-serif);
  font-size: clamp(1.35rem, 3vw, 2rem);
  font-weight: 500;
  letter-spacing: 0;
}

.game-guide p { margin: 0; }

.game-guide table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: var(--radius-sm);
  font-size: 0.92rem;
}

.game-guide th,
.game-guide td {
  padding: 10px 12px;
  border: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.game-guide th {
  color: var(--ink);
  background: color-mix(in srgb, var(--surface-sunken) 78%, transparent);
}

.game-guide details {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--surface-sunken) 70%, transparent);
}

.game-guide summary { color: var(--ink); cursor: pointer; font-weight: 700; }
.game-guide details p { margin-top: 8px; }

@media (max-width: 720px) {
  .game-guide { margin-top: 18px; padding: 16px; }
  .game-guide table, .game-guide thead, .game-guide tbody, .game-guide tr, .game-guide th, .game-guide td { display: block; }
  .game-guide th { display: none; }
  .game-guide td { border-top: 0; }
  .game-guide td:first-child { border-top: 1px solid var(--line); color: var(--ink); font-weight: 700; }
}

.game-guide,
.related-games {
  display: grid;
  gap: 12px;
  margin-top: 18px;
  padding: clamp(16px, 3vw, 24px);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--surface-sunken) 72%, transparent);
  box-shadow: var(--shadow-sm);
}

.related-games h2 {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-serif);
  font-size: clamp(1.2rem, 2.6vw, 1.7rem);
  font-weight: 500;
  letter-spacing: 0;
}

.related-games nav {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.related-games a {
  display: grid;
  gap: 5px;
  min-width: 0;
  min-height: 72px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  color: var(--ink-muted);
  text-decoration: none;
}

.related-games a:hover { color: var(--ink); border-color: var(--line-strong); }
.related-games strong, .related-games span { min-width: 0; overflow-wrap: anywhere; }
.related-games strong { color: var(--ink); font-size: 0.9rem; line-height: 1.15; }
.related-games span { font-size: 0.78rem; line-height: 1.35; }

@media (max-width: 820px) {
  .related-games nav { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 420px) {
  .related-games { padding: 14px; }
  .related-games nav { grid-template-columns: 1fr; }
  .related-games a { min-height: 0; }
}

.shell > * { min-width: 0; max-width: 100%; }

@media (min-width: 721px) and (max-width: 900px) {
  .shell { width: min(100% - 24px, 760px); gap: 8px; padding-top: 10px; }
  .promo-banner { order: 8; margin: 8px 0 0; }
  .stage-wrap {
    width: min(100%, calc((100svh - 410px - env(safe-area-inset-bottom)) * 15 / 16));
    max-height: calc(100svh - 410px - env(safe-area-inset-bottom));
  }
  .controls { gap: 8px; }
}

@media (max-width: 720px) and (orientation: portrait) {
  .shell { width: min(100% - 12px, 540px); min-height: 100svh; gap: 6px; }
  .stage-wrap {
    width: min(100%, calc((100svh - 224px - env(safe-area-inset-bottom)) * 15 / 16));
    max-height: calc(100svh - 224px - env(safe-area-inset-bottom));
  }
  .controls { gap: 6px; }
  .action { min-height: 48px; font-size: 0.82rem; }
  .dpad { grid-template-columns: repeat(3, 48px); grid-template-rows: repeat(2, 48px); gap: 6px; }
  .pad { min-width: 48px; min-height: 48px; }
}

@media (max-width: 420px) and (max-height: 700px) and (orientation: portrait) {
  .stage-wrap {
    width: min(100%, calc((100svh - 290px - env(safe-area-inset-bottom)) * 15 / 16));
    max-height: calc(100svh - 290px - env(safe-area-inset-bottom));
  }
}

@media (max-height: 480px) and (orientation: landscape) {
  .shell {
    width: min(100vw - 10px, 980px);
    min-height: 100svh;
    grid-template-columns: minmax(0, 1fr) minmax(160px, 220px);
    align-items: start;
    padding: 4px 0 calc(4px + env(safe-area-inset-bottom));
    gap: 4px;
  }
  .brandbar,
  .hud,
  .promo-banner,
  .game-guide,
  .related-games,
  .game-footer { grid-column: 1 / -1; }
  .brandbar { min-height: 34px; padding: 5px 8px; border-radius: var(--radius-md); }
  .brandbar p { display: none; }
  .promo-banner { display: none; }
  .hud { grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 4px; }
  .hud div { padding: 5px 6px; }
  .hud .label { font-size: .62rem; }
  .hud strong { font-size: .9rem; margin-top: 2px; }
  .stage-wrap {
    grid-column: 1;
    width: min(100%, calc((100svh - 92px - env(safe-area-inset-bottom)) * 15 / 16));
    max-height: calc(100svh - 92px - env(safe-area-inset-bottom));
    justify-self: end;
  }
  /* The overlay's own content (icon/title/text/buttons) needs to actually fit this
     short box, not just be scrollable inside it — trim it hard for this breakpoint. */
  .overlay { padding: 8px; gap: 5px; }
  .overlay-tile { display: none; }
  .overlay h1 { font-size: clamp(1.3rem, 6vw, 2rem); }
  .overlay p { font-size: .78rem; line-height: 1.2; max-width: 90%; }
  .overlay-buttons button { min-height: 48px; min-width: 90px; font-size: .82rem; }
  .keys { display: none; }
  .controls {
    grid-column: 2;
    align-self: center;
    grid-template-columns: repeat(2, minmax(72px, 1fr));
    width: 100%;
    gap: 5px;
  }
  .controls .dpad { grid-column: 1 / -1; justify-self: center; }
  .dpad { grid-template-columns: repeat(3, 48px); grid-template-rows: repeat(2, 48px); gap: 4px; }
  .pad { min-width: 48px; min-height: 48px; }
  .action { min-height: 48px; font-size: 0.78rem; }
}
