:root {
  color-scheme: light;
  --bg-0: #c9edff;
  --bg-1: #89c0e0;
  --panel: #f8fbff;
  --panel-border: #7da7be;
  --ink: #163243;
  --accent: #4b90b8;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 15%, #ffffff55 0 120px, transparent 120px),
    radial-gradient(circle at 85% 20%, #ffffff44 0 100px, transparent 100px),
    linear-gradient(160deg, var(--bg-0), var(--bg-1));
  display: grid;
  place-items: center;
  padding: 16px;
}

.app-shell {
  width: min(840px, 100%);
  background: color-mix(in srgb, var(--panel) 90%, #ffffff);
  border: 2px solid var(--panel-border);
  border-radius: 16px;
  box-shadow: 0 12px 30px #1f3f5229;
  padding: 14px;
}

.hud-bar h1 {
  margin: 0;
  font-size: clamp(1.3rem, 2.4vw, 1.7rem);
}

.hud-bar p {
  margin: 0.35rem 0 0;
  font-size: 0.96rem;
  opacity: 0.9;
}

.stage-wrap {
  position: relative;
  margin-top: 12px;
  border-radius: 10px;
  overflow: hidden;
  border: 2px solid #3e5f72;
  background: #8dc0dd;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
}

#game {
  position: relative;
  z-index: 1;
}

.bridge-graffiti {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 2;
}

.stage-fg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  background: transparent;
  z-index: 3;
}

.start-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  border: 0;
  padding: 0;
  font: inherit;
  color: #f4fbff;
  background: linear-gradient(180deg, #2d5973ee, #18374aee);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.start-overlay[hidden] {
  display: none;
}

.score-panel {
  width: min(94%, 540px);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.4rem;
  padding: 0.5rem 0.8rem;
}

.score-panel-title {
  margin: 0;
  text-align: center;
  font-family: "Sedgwick Ave", "Trebuchet MS", cursive;
  font-weight: 400;
  font-size: clamp(1.7rem, 4.4vw, 2.4rem);
  letter-spacing: 0.01em;
  line-height: 1;
  color: #ffd866;
  text-shadow: 0 2px 0 #0007;
  transform: rotate(-1.5deg);
}

.score-panel-result {
  margin: 0;
  text-align: center;
  font-size: clamp(0.8rem, 2vw, 1rem);
  font-weight: 600;
  color: #f4fbff;
  opacity: 0.95;
}

.score-panel-result:empty {
  display: none;
}

.score-panel-result .threshold {
  opacity: 0.7;
  font-weight: 500;
}

.score-stats {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  font-size: clamp(0.7rem, 1.8vw, 0.9rem);
  font-weight: 600;
  opacity: 0.95;
}

.score-stats .score-stat strong {
  color: #ffd866;
  margin-right: 0.25em;
}

.score-tables {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
  align-items: start;
}

.leaderboard {
  width: 100%;
  border-collapse: collapse;
  font-size: clamp(0.6rem, 1.6vw, 0.8rem);
  background: #ffffff14;
  border-radius: 10px;
  overflow: hidden;
}

.leaderboard.today {
  background: #ffd86618;
}
.leaderboard.today th {
  color: #ffe9a3;
}

.leaderboard th,
.leaderboard td {
  padding: 0.18em 0.45em;
  text-align: center;
  border-bottom: 1px solid #ffffff14;
}

.leaderboard thead tr:first-child th {
  font-family: "Sedgwick Ave", "Trebuchet MS", cursive;
  font-weight: 400;
  font-size: 1.15em;
  letter-spacing: 0.01em;
  background: transparent;
  border-bottom: 1px dashed #ffffff33;
  padding-top: 0.25em;
  padding-bottom: 0.1em;
}

.leaderboard thead tr:nth-child(2) th {
  background: #ffffff1a;
  color: #ffd866;
  font-weight: 700;
  font-size: 0.95em;
  letter-spacing: 0.04em;
}

.leaderboard tbody tr:last-child td {
  border-bottom: 0;
}

.leaderboard td.empty {
  opacity: 0.45;
}

.leaderboard td.name {
  font-family: "Courier New", monospace;
  letter-spacing: 0.08em;
}

.install-hint {
  margin: 0.1rem 0 0;
  text-align: center;
  font-size: clamp(0.65rem, 1.6vw, 0.8rem);
  opacity: 0.85;
  color: #cfeaff;
  line-height: 1.3;
}

.install-hint .share-icon {
  display: inline-block;
  padding: 0 0.25rem;
  border: 1px solid currentColor;
  border-radius: 4px;
  font-weight: 700;
}

.score-panel-cta {
  margin-top: 0.25rem;
  text-align: center;
  font-family: "Sedgwick Ave", "Trebuchet MS", cursive;
  font-weight: 400;
  font-size: clamp(1.1rem, 3vw, 1.5rem);
  color: #ffd866;
  text-shadow: 0 1px 0 #0008;
  transform: rotate(-1deg);
}

.score-panel-cta-button {
  align-self: center;
  margin-top: 0.3rem;
  border: 0;
  border-radius: 999px;
  padding: 0.45em 1.6em;
  font-family: "Sedgwick Ave", "Trebuchet MS", cursive;
  font-weight: 400;
  font-size: clamp(1.1rem, 3vw, 1.5rem);
  letter-spacing: 0.01em;
  color: #12324a;
  background: #f7d068;
  box-shadow: 0 3px 0 #0b1f2e44;
  cursor: pointer;
  transform: rotate(-1deg);
  transition: transform 0.12s ease, background 0.12s ease;
}

.score-panel-cta-button:hover {
  background: #ffd866;
  transform: rotate(-1deg) translateY(-1px);
}

.name-entry-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0d1f2af0;
}

.name-entry-overlay[hidden] {
  display: none;
}

.name-entry-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  padding: 1rem 1.2rem;
  background: #18374a;
  border: 2px solid #ffd866;
  border-radius: 12px;
  box-shadow: 0 8px 24px #0008;
  color: #f4fbff;
}

.name-entry-title {
  margin: 0;
  font-family: "Sedgwick Ave", "Trebuchet MS", cursive;
  font-weight: 400;
  font-size: clamp(1.2rem, 3.2vw, 1.6rem);
  color: #ffd866;
  transform: rotate(-1deg);
}

.name-entry-result {
  margin: 0;
  text-align: center;
  font-size: clamp(0.8rem, 2vw, 1rem);
  font-weight: 600;
  color: #f4fbff;
  opacity: 0.95;
}

.name-entry-result:empty {
  display: none;
}

.name-entry-result .threshold {
  opacity: 0.7;
  font-weight: 500;
}

.name-entry-sub {
  margin: 0;
  font-size: clamp(0.7rem, 1.8vw, 0.9rem);
  opacity: 0.85;
}

.name-entry-inputs {
  display: flex;
  gap: 0.3rem;
}

.name-entry-inputs input {
  width: clamp(1.5rem, 4.5vw, 2.1rem);
  height: clamp(1.9rem, 5.5vw, 2.6rem);
  text-align: center;
  font-size: clamp(0.95rem, 3vw, 1.3rem);
  font-family: "Courier New", monospace;
  font-weight: 700;
  text-transform: uppercase;
  border: 1px solid #ffd86688;
  border-radius: 4px;
  background: #0d1f2a;
  color: #ffd866;
  outline: none;
  caret-color: #ffd866;
}

.name-entry-inputs input:focus {
  border-color: #ffd866;
  box-shadow: 0 0 0 2px #ffd86644;
}

.name-entry-submit {
  margin-top: 0.3rem;
  border: 0;
  border-radius: 999px;
  padding: 0.5em 1.4em;
  font: inherit;
  font-weight: 700;
  color: #12324a;
  background: #ffd866;
  cursor: pointer;
}

.name-entry-submit:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.restart-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #2d5973ee, #18374aee);
  color: #f4fbff;
}

.restart-overlay[hidden] {
  display: none;
}

.fullscreen-resume {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 30;
  padding: 8px 12px;
  border: 0;
  border-radius: 999px;
  background: rgba(13, 31, 42, 0.85);
  color: #fff;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  -webkit-tap-highlight-color: transparent;
}
.fullscreen-resume[hidden] {
  display: none;
}

.rotate-hint {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 20;
  align-items: center;
  justify-content: center;
  background: #0d1f2ae8;
  color: #f4fbff;
  text-align: center;
  padding: 16px;
}

.rotate-hint-inner p {
  margin: 8px 0 0;
  font-size: clamp(0.9rem, 3vw, 1.1rem);
  font-weight: 600;
}

.rotate-icon {
  font-size: 2.6rem;
  animation: rotateIconSpin 1.8s ease-in-out infinite;
}

@keyframes rotateIconSpin {
  0% { transform: rotate(0deg); }
  50% { transform: rotate(-90deg); }
  100% { transform: rotate(-90deg); }
}

/* NOTE: keep the unconditional `.legend` rules ABOVE all media queries.
   Earlier we hit a regression where the landscape media query set
   `.legend { display: none }` but the rules below (declared later) had equal
   specificity and won the cascade, leaving the legend visible as a flex
   sibling of `.stage-wrap` and squishing the game to the left of the
   viewport. */
.legend {
  margin-top: 10px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 0.88rem;
}

.legend strong {
  color: #224c66;
}

/* Show rotate hint on touch devices in portrait orientation. */
@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
  .rotate-hint {
    display: flex;
  }
  .app-shell > *:not(.stage-wrap),
  .stage-wrap > *:not(.rotate-hint) {
    visibility: hidden;
  }
  /* When the name-entry panel is active we *want* portrait: the on-screen
     keyboard is much friendlier vertically. Hide the rotate hint, reveal
     the overlay, and let the stage wrap fill the viewport so the panel
     sits centred on screen. */
  body.name-entry-active .rotate-hint {
    display: none;
  }
  body.name-entry-active .stage-wrap > .name-entry-overlay {
    visibility: visible;
  }
  body.name-entry-active .stage-wrap {
    width: 100dvw;
    height: 100dvh;
  }
}

/* Portrait-friendly name-entry layout on touch devices: stack the input
   cells in two rows so each cell stays large enough to tap and the panel
   fits within a narrow viewport above the on-screen keyboard. */
@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
  body.name-entry-active .name-entry-inputs {
    display: grid;
    grid-template-columns: repeat(4, auto);
    gap: 0.4rem;
  }
  body.name-entry-active .name-entry-inputs input {
    width: clamp(2rem, 9vw, 2.6rem);
    height: clamp(2.4rem, 11vw, 3.2rem);
    font-size: clamp(1.1rem, 5vw, 1.5rem);
  }
  body.name-entry-active .name-entry-panel {
    max-width: 90vw;
  }
}

/* On mobile landscape, the stage takes over the entire viewport as a
   best-fit 2:1 box. The min(vw, vh) trick guarantees the stage fits within
   the viewport while preserving the canvas aspect ratio (300x150 = 2:1) -
   no letterboxing on either axis when the viewport happens to match, and
   only along the constrained axis otherwise. We use dvw/dvh so that iOS's
   shrinking address-bar / home-indicator areas don't push content off
   screen. */
@media (hover: none) and (pointer: coarse) and (orientation: landscape) {
  html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background: #0d1f2a;
  }
  body {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .app-shell {
    width: 100dvw;
    height: 100dvh;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .hud-bar,
  .legend {
    display: none;
  }
  .stage-wrap {
    /* Best-fit 2:1 box: width = min(available-width, 2 * available-height),
       height = min(available-width / 2, available-height). Both expressions
       collapse to a true 2:1 rectangle scaled to whichever axis is the
       binding constraint. We subtract the iOS safe-area insets so the canvas
       stays clear of the notch / status bar / home indicator both in Safari
       (with viewport-fit=cover) and when launched from the home screen as a
       fullscreen PWA. */
    --avail-w: calc(100dvw - env(safe-area-inset-left) - env(safe-area-inset-right));
    --avail-h: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    width: min(var(--avail-w), calc(var(--avail-h) * 2));
    height: min(calc(var(--avail-w) / 2), var(--avail-h));
    margin: 0;
    border-radius: 0;
    border: 0;
  }
  canvas {
    width: 100%;
    height: 100%;
  }
}
