:root {
  --bg0: #070a12;
  --bg1: #0b1224;
  --bg2: #0f1b39;
  --ink: rgba(255, 255, 255, 0.92);
  --muted: rgba(255, 255, 255, 0.65);
  --card: rgba(255, 255, 255, 0.06);
  --card2: rgba(255, 255, 255, 0.1);
  --good: #5ef0b8;
  --bad: #ff6b84;
  --line: rgba(255, 255, 255, 0.12);
  --shadow: 0 20px 70px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    Segoe UI,
    Roboto,
    Arial,
    "Apple Color Emoji",
    "Segoe UI Emoji";
  background: var(--bg0);
}

.wrap {
  max-width: 440px;
  padding: max(4px, env(safe-area-inset-top, 0px)) max(10px, env(safe-area-inset-right, 0px)) 18px
    max(10px, env(safe-area-inset-left, 0px));
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.game-credit {
  margin-top: 16px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.5);
  line-height: 1.4;
  padding-bottom: max(4px, env(safe-area-inset-bottom, 0px));
}

.game-credit__text {
  text-align: left;
}

.game-credit__linkedin {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
  color: #0a66c2;
  text-decoration: none;
  flex-shrink: 0;
}

.game-credit__linkedin:hover {
  opacity: 0.88;
}

.game-credit__linkedin:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.55);
  outline-offset: 3px;
  border-radius: 4px;
}

.game-credit__linkedin svg {
  display: block;
  width: 20px;
  height: 20px;
}

.layout-mid {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.center-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
  width: 100%;
  max-width: 420px;
}

/* HUD + messages: stacked in same grid cell as canvas (reliable overlay on mobile). */
.frame-top-ui {
  grid-column: 1;
  grid-row: 1;
  align-self: start;
  justify-self: stretch;
  width: 100%;
  z-index: 3;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 10px 0;
  pointer-events: none;
}

/* Single panel: title row, then Stage · Goal · ♥ on one line + meter */
.play-hud {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 10px 10px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.14);
}

.play-hud__title-row {
  min-width: 0;
}

.play-hud__title {
  font-weight: 800;
  font-size: 14px;
  letter-spacing: 0.04em;
  color: rgba(255, 255, 255, 0.96);
  line-height: 1.2;
  min-width: 0;
}

.play-hud__stage-goal-line {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 6px 8px;
  font-size: 12px;
  line-height: 1.25;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.play-hud__stage-goal-line::-webkit-scrollbar {
  display: none;
}

.play-hud__stage-part {
  font-weight: 900;
  letter-spacing: 0.04em;
  color: rgba(255, 255, 255, 0.95);
}

.play-hud__mid-dot {
  color: rgba(255, 255, 255, 0.35);
  font-weight: 700;
  user-select: none;
}

.play-hud__goal-part {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

.play-hud__goal-label {
  font-size: 10px;
  letter-spacing: 0.18px;
  color: var(--muted);
  text-transform: uppercase;
  font-weight: 800;
}

.play-hud__goal-text {
  font-size: 12px;
  font-weight: 900;
  color: rgba(255, 255, 255, 0.92);
  font-variant-numeric: tabular-nums;
}

.play-hud__meter {
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.play-hud__meter-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #2f9e3d, #5ef0b8);
  transition: width 0.12s ease-out;
}

.play-hud__hearts {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.play-hud__hearts-icon {
  font-size: 19px;
  line-height: 1;
  color: #ff6b84;
}

.play-hud__hearts-value {
  font-size: 16px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: rgba(255, 255, 255, 0.95);
}

.play-banner {
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(6, 10, 20, 0.78);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  font-weight: 700;
  font-size: 12px;
  line-height: 1.35;
  color: rgba(255, 255, 255, 0.95);
  text-align: center;
}

.hidden {
  display: none;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px))
    max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(6px);
}

/* Must win over `display: flex` above (same element also has `.hidden`). */
.modal-overlay.hidden {
  display: none;
}

/* Welcome / name entry only — no backdrop dim */
.modal-overlay--welcome {
  background: transparent;
  backdrop-filter: none;
}

/* Game over: 30% dim so the playfield stays visible behind */
.modal-overlay--gameover {
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}

/* Game over: scroll if viewport is short; padding from .modal-overlay */
#gameOverModal.modal-overlay {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  align-items: center;
}

.modal-card--welcome {
  width: min(340px, 100%);
  max-width: 100%;
  margin: auto;
  flex-shrink: 0;
  padding: 0;
  overflow: hidden;
  border-radius: 24px;
  border: none;
  outline: none;
  background: #ffffff;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.1);
  transform: translateY(-100px);
}

.welcome-header {
  padding: 20px 22px 14px;
  background: #ffffff;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.welcome-title {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  color: #000000;
}

.welcome-frost {
  padding: 18px 20px 20px;
  background: #ffffff;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  border-top: none;
}

.welcome-hint {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  color: #000000;
  line-height: 1.35;
}

.welcome-input {
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.18);
  background: rgba(255, 255, 255, 0.85);
  color: #000000;
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
}

.welcome-input::placeholder {
  color: rgba(0, 0, 0, 0.4);
}

.welcome-play-btn {
  width: 100%;
  padding: 11px 16px;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
  color: #052e16;
  background: linear-gradient(180deg, #d1fae5, #a7f3d0);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8) inset;
}

.welcome-play-btn:hover {
  filter: brightness(1.04);
}

/* Game over: art panel (leaderboard.png) + overlay table + invisible hit buttons */
.modal-card--gameover-sheet {
  max-width: 100%;
  max-height: none;
  margin: auto;
  flex-shrink: 0;
}

.gameover-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.gameover-panel {
  position: relative;
  width: min(380px, 92vw);
  padding: 0;
  border: none;
  background: none;
  box-shadow: none;
  filter: none;
}

.gameover-panel__aspect {
  position: relative;
  display: block;
  width: 100%;
  line-height: 0;
  border-radius: 22px;
  overflow: hidden;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.35);
}

.gameover-panel__frame {
  display: block;
  width: 100%;
  height: auto;
  vertical-align: top;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  border-radius: inherit;
}

.gameover-panel__layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* Tunable: align scroll area with parchment; buttons with art (percent of panel) */
.gameover-panel {
  --go-inset-x: 10%;
  --go-table-top: 16.5%;
  /* Keep list well above the illustrated buttons + hamster */
  --go-table-bottom: 40%;
  --go-btn-bottom: 25.5%;
  /* Nudge invisible hit targets up (px) vs art */
  --go-btn-nudge-up: 10px;
  --go-btn-h: 9.5%;
  --go-save-left: 5.5%;
  --go-save-w: 40%;
  --go-play-right: 5.5%;
  --go-play-w: 40%;
  /* ~4 data rows visible (tbody); thead is sr-only */
  --go-visible-rows: 4;
  /* Push score list down (px) — subtract from max-height so buttons stay clear */
  --go-records-shift-y: 70px;
}

.gameover-panel__scroll {
  position: absolute;
  z-index: 1;
  left: var(--go-inset-x);
  right: var(--go-inset-x);
  top: calc(var(--go-table-top) + var(--go-records-shift-y));
  /* At most 4 rows; never extend into the button + hamster art */
  max-height: min(
    calc(var(--go-visible-rows) * 2.45em),
    calc(100% - var(--go-table-top) - var(--go-table-bottom) - var(--go-records-shift-y))
  );
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
  pointer-events: auto;
  border-radius: 6px;
  transform: translateZ(0);
}

.gameover-hitbtn {
  position: absolute;
  z-index: 10;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
  opacity: 0;
  min-width: 44px;
  min-height: 44px;
  outline: none;
  box-shadow: none;
}

.gameover-hitbtn:focus,
.gameover-hitbtn:focus-visible {
  outline: none;
  box-shadow: none;
  opacity: 0;
}

.gameover-hitbtn--save {
  left: var(--go-save-left);
  bottom: calc(var(--go-btn-bottom) + var(--go-btn-nudge-up));
  width: var(--go-save-w);
  height: var(--go-btn-h);
}

.gameover-hitbtn--play {
  right: var(--go-play-right);
  bottom: calc(var(--go-btn-bottom) + var(--go-btn-nudge-up));
  width: var(--go-play-w);
  height: var(--go-btn-h);
}

#gameOverModal .modal-card--gameover-sheet.gameover-panel {
  overflow: visible;
  transform: translateY(-40px);
}

.modal-card {
  width: min(360px, 100%);
  padding: 22px 20px 20px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: linear-gradient(165deg, rgba(20, 28, 52, 0.98), rgba(10, 14, 28, 0.99));
  box-shadow: var(--shadow);
}

.modal-title {
  margin: 0 0 8px;
  font-size: 20px;
  font-weight: 800;
}

.modal-hint {
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}

.modal-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.35);
  color: var(--ink);
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 14px;
}

.modal-input::placeholder {
  color: rgba(255, 255, 255, 0.35);
}

.modal-btn {
  width: 100%;
  padding: 11px 16px;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
  color: #0a0f1a;
  background: linear-gradient(90deg, #5ef0b8, #86f7cd);
}

.modal-btn:hover {
  filter: brightness(1.06);
}

.modal-hint--tight {
  margin-bottom: 10px;
}

.leaderboard-scroll--modal {
  overflow: visible;
  max-height: none;
  margin-bottom: 12px;
  flex: 0 0 auto;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: rgba(0, 0, 0, 0.03);
}

.gameover-panel .leaderboard-scroll--modal {
  margin-bottom: 0;
  border: none;
  border-radius: 0;
  background: transparent;
  min-height: 0;
}

/* Headers are drawn on leaderboard.png; keep markup for screen readers only */
.gameover-panel__thead {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Game-over table only (no .leaderboard-table) so rail styles don’t override headers */
.leaderboard-table--modal {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 13px;
  color: #000000;
}

.leaderboard-table--modal thead th {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.12px;
  padding: 8px 8px 7px;
  background: rgba(245, 245, 245, 0.98);
  color: #000000;
  -webkit-text-fill-color: #000000;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  text-align: left;
  vertical-align: bottom;
}

.leaderboard-table--modal thead th:nth-child(1),
.leaderboard-table--modal td:nth-child(1) {
  width: 22%;
}

.leaderboard-table--modal thead th:nth-child(2),
.leaderboard-table--modal td:nth-child(2) {
  width: 30%;
}

.leaderboard-table--modal thead th:nth-child(3),
.leaderboard-table--modal td:nth-child(3) {
  width: 18%;
  text-align: right;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.leaderboard-table--modal thead th:nth-child(4),
.leaderboard-table--modal td:nth-child(4) {
  width: 18%;
  text-align: right;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.leaderboard-table--modal td {
  padding: 6px 8px;
  white-space: normal;
  word-break: break-word;
  color: #000000;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.leaderboard-table--modal .leaderboard-empty {
  color: #000000;
}

/* Parchment-style rows over art; highlight.png only for this session’s run if it made top 4 */
.gameover-panel__table {
  font-size: 12px;
  line-height: 1.3;
  color: #3d2914;
}

.gameover-panel__table td {
  padding: 5px 6px;
  color: #3d2914;
  border-bottom: 1px dashed rgba(91, 62, 32, 0.22);
  background: transparent;
  outline: none;
}

.gameover-panel__table tr:last-child td {
  border-bottom: none;
}

.gameover-panel__table tr.leaderboard-row--qualified {
  background: url("./art/highlight.png") center / 100% 100% no-repeat;
}

.gameover-panel__table tr.leaderboard-row--qualified td {
  background: transparent;
  color: #2a1a0a;
  font-weight: 900;
  border-bottom: 1px dashed rgba(91, 62, 32, 0.15);
  box-shadow: none;
  outline: none;
}

.gameover-panel__table .leaderboard-empty {
  color: rgba(61, 41, 20, 0.65);
  font-weight: 600;
}

/* Hearts HUD: red + pulse when fewer than 2 full hearts shown */
@keyframes hearts-value-low-pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

#heartsValue.hearts-value--low {
  color: #ff4d6a;
  animation: hearts-value-low-pulse 0.85s ease-in-out infinite;
}

.frame {
  position: relative;
  width: min(420px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto;
  align-items: start;
}

.frame > canvas {
  grid-column: 1;
  grid-row: 1;
  z-index: 0;
  align-self: start;
  justify-self: stretch;
}

/* Mobile: 50% tap zones — white ~50% opacity chevrons near the hamster (pointer-events none). */
.lane-guides {
  display: none;
  grid-column: 1;
  grid-row: 1;
  align-self: stretch;
  justify-self: stretch;
  width: 100%;
  min-height: 100%;
  z-index: 2;
  pointer-events: none;
  border-radius: 18px;
  overflow: hidden;
}

.lane-guides__half {
  flex: 1 1 50%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 22%;
  min-width: 0;
}

.lane-guides__half--left {
  justify-content: flex-start;
  padding-left: max(8px, 3vw);
}

.lane-guides__half--right {
  justify-content: flex-end;
  padding-right: max(8px, 3vw);
}

.lane-guides__chevron {
  width: clamp(34px, 10.5vw, 58px);
  height: auto;
  color: rgba(255, 255, 255, 0.5);
  filter: drop-shadow(0 2px 10px rgba(0, 0, 0, 0.45));
}

canvas {
  width: 100%;
  height: auto;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: transparent;
  box-shadow: var(--shadow);
  display: block;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

/* Touch / coarse pointer: stop double-tap zoom (phones + iPad; not tied to width) */
@media (max-width: 768px), (hover: none) and (pointer: coarse) {
  html {
    touch-action: manipulation;
  }

  body {
    overflow-x: hidden;
    touch-action: manipulation;
    overscroll-behavior-y: contain;
  }

  .modal-overlay {
    touch-action: manipulation;
  }

  .wrap,
  .wrap *,
  #nameModal,
  #nameModal *,
  #gameOverModal,
  #gameOverModal * {
    touch-action: manipulation;
  }

  #nameModal .welcome-input {
    touch-action: auto;
  }

  #gameOverModal .gameover-panel__scroll {
    touch-action: pan-y !important;
  }

  .frame {
    touch-action: manipulation;
  }

  .play-hud,
  .play-banner,
  .game-credit {
    touch-action: manipulation;
  }
}

/* Narrow phones only (layout + game-over nudge); 768px-wide iPad portrait uses tablet rule below */
@media (max-width: 767px) {
  .wrap {
    padding-left: max(8px, env(safe-area-inset-left, 0px));
    padding-right: max(8px, env(safe-area-inset-right, 0px));
  }

  .center-col {
    max-width: 100%;
  }

  .frame-top-ui {
    padding: 8px 8px 0;
  }

  .play-hud {
    padding: 7px 8px 8px;
    gap: 6px;
  }

  .play-hud__title {
    font-size: 13px;
  }

  .play-hud__stage-goal-line {
    font-size: 11px;
  }

  .play-hud__goal-text {
    font-size: 11px;
  }

  .play-hud__hearts-icon {
    font-size: 17px;
  }

  .play-hud__hearts-value {
    font-size: 15px;
  }

  canvas {
    border-radius: 14px;
  }

  .lane-guides {
    display: flex;
    border-radius: 14px;
  }

  .gameover-panel {
    width: min(340px, 94vw);
    --go-records-shift-y: 60px;
  }

  #gameOverModal .modal-card--gameover-sheet.gameover-panel {
    transform: translateY(0);
  }

  .game-credit {
    font-size: 12px;
    margin-top: 10px;
  }
}

/* Tablet-sized touch (incl. iPad mini): optical vertical center; phones stay on translateY(0) above */
@media (hover: none) and (pointer: coarse) and (min-width: 600px) {
  #gameOverModal .modal-card--gameover-sheet.gameover-panel {
    transform: translateY(-5vh);
  }

  /* White tap-hint chevrons (same as narrow phones; iPad is wider than 767px so needs this rule) */
  .lane-guides {
    display: flex;
    border-radius: 18px;
  }
}
