@font-face {
  font-family: "Fredoka";
  src: url("fonts/Fredoka-VariableFont_wdth,wght.ttf") format("truetype");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

:root {
  --ink: #1b2a41;
  --sky: #cdeffc;
  --water: #0e4f8a;
  --gold: #ffd84d;
  --ice: #ffffff;
  --panel: #0f3460;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--water);
  color: var(--ice);
  font-family: "Fredoka", "Trebuchet MS", sans-serif;
}

body {
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.top {
  position: relative;
  z-index: 6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 12px 0;
  background: var(--water);
}

.top a {
  color: var(--ice);
  font-weight: 700;
  font-size: 16px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.top h1 {
  margin: 0;
  flex: 1 1 auto;
  min-width: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.01em;
  text-align: center;
}

html.embed .top a {
  display: none;
}

.how-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  margin: 4px 12px 0;
}

.how-to,
.how-close {
  min-height: 36px;
  padding: 4px 14px;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: var(--gold);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}

.how-to:focus-visible,
.how-close:focus-visible {
  outline: 3px solid var(--ice);
  outline-offset: 2px;
}

.how-panel {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(1, 17, 43, 0.72);
}

.how-panel[hidden] {
  display: none !important;
}

.how-card {
  width: min(40rem, 100%);
  max-height: min(78dvh, 640px);
  overflow: auto;
  padding: 14px 14px 16px;
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: #12365f;
  color: var(--ice);
  box-shadow: 0 12px 40px rgba(1, 17, 43, 0.45);
}

.how-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.how-card h2 {
  margin: 0;
  font-size: 22px;
  line-height: 1.1;
}

.how-copy {
  margin: 10px 0 0;
  color: var(--sky);
  font-size: 16px;
  line-height: 1.4;
  font-weight: 600;
}

.hud {
  display: none;
  flex: 0 0 auto;
  width: min(100%, 960px);
  margin: 0 0 6px;
  padding: 6px 8px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  background: #01112b;
  color: var(--ice);
}

.hud p {
  margin: 0;
}

.hud-stat {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
}

.hud-k {
  color: #d7f4ff;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.hud-stat strong,
.hud-combo {
  color: var(--gold);
  font-size: clamp(20px, 6.2vw, 28px);
  font-weight: 700;
  line-height: 1;
  text-shadow: 0 1px 0 #000;
}

.hud-combo {
  color: #fff;
}

.hud-hearts {
  color: #ff5fa2;
  font-size: clamp(22px, 6.4vw, 30px);
  line-height: 1;
  letter-spacing: 1px;
  text-shadow: 0 1px 0 #000;
}

.hud-hearts .heart.off {
  color: #8fb4d9;
}

.hud-timer {
  justify-self: end;
  padding: 3px 8px;
  border: 2px solid var(--gold);
  border-radius: 999px;
  background: var(--water);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  line-height: 1.1;
}

.hud-timer.is-warn {
  color: #1b2a41;
  background: var(--gold);
}

.hud-bonus {
  justify-self: start;
  padding: 3px 8px;
  border-radius: 999px;
  background: #ffe7a8;
  color: #1b2a41;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.1;
  box-shadow: 0 0 0 2px #1b2a41;
}

.hud-timer[hidden],
.hud-bonus[hidden] {
  display: none !important;
}

html.html-hud .hud {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) auto auto;
  grid-template-areas:
    "score combo hearts"
    "best timer bonus";
  align-items: center;
  column-gap: 8px;
  row-gap: 4px;
}

html.html-hud .hud-stat:first-child {
  grid-area: score;
}

html.html-hud .hud-stat:nth-child(2) {
  grid-area: best;
}

html.html-hud .hud-combo {
  grid-area: combo;
  justify-self: end;
}

html.html-hud .hud-hearts {
  grid-area: hearts;
  justify-self: end;
}

html.html-hud .hud-timer {
  grid-area: timer;
}

html.html-hud .hud-bonus {
  grid-area: bonus;
  justify-self: end;
}

.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 6px 8px;
}

.playfield {
  position: relative;
  flex: 1 1 auto;
  align-self: stretch;
  min-width: 0;
  min-height: 0;
  width: 100%;
  max-width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

html.embed .how-bar,
html.embed .hud {
  display: none;
}

html.embed .pads button {
  min-height: 44px;
}

html.embed .pads button svg {
  width: 22px;
  height: 22px;
}

html.embed .pad-label {
  font-size: 11px;
}

.sound {
  position: relative;
  z-index: 2;
  display: flex;
  flex: none;
  gap: 6px;
  touch-action: none;
}

.mute {
  position: relative;
  box-sizing: border-box;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 6px;
  margin: -6px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  touch-action: none;
}

.mute::before {
  content: "";
  position: absolute;
  inset: 6px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: var(--ice);
  pointer-events: none;
}

.mute svg {
  position: relative;
  z-index: 1;
  width: 16px;
  height: 16px;
  display: block;
  pointer-events: none;
}

.mute .off {
  display: none;
}

.mute[aria-pressed="true"] {
  background: transparent;
  color: var(--ice);
}

.mute[aria-pressed="true"]::before {
  background: var(--panel);
}

.mute[aria-pressed="true"] .on {
  display: none;
}

.mute[aria-pressed="true"] .off {
  display: block;
}

.mute:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

.stage,
canvas,
.sound,
.mute,
.pads,
.pad,
.pads button,
.pad-label {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.board {
  position: absolute;
  z-index: 4;
  left: 8px;
  right: 8px;
  bottom: 46px;
  max-height: 46%;
  overflow: auto;
  padding: 8px 10px 10px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: rgba(15, 52, 96, 0.94);
  color: var(--ice);
  pointer-events: auto;
  touch-action: pan-y;
  user-select: text;
  -webkit-user-select: text;
}

.board[hidden] {
  display: none !important;
}

/* Playing keeps the list on a phone. Everywhere else it stays out of the picture. */
#board.is-play {
  display: none !important;
}

.board-note,
.board h2,
.board p,
.board ol {
  margin: 0;
}

.board-note {
  font-size: 12px;
  line-height: 1.3;
  color: var(--sky);
}

.board-note + .board-note {
  margin-top: 2px;
}

.board h2 {
  margin-top: 8px;
  font-size: 16px;
  line-height: 1.2;
}

.board label {
  display: block;
  margin-top: 6px;
  font-size: 13px;
  font-weight: 700;
}

.board input {
  width: 100%;
  min-height: 44px;
  margin-top: 3px;
  padding: 6px 8px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: var(--ice);
  color: var(--ink);
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  user-select: text;
  -webkit-user-select: text;
  touch-action: manipulation;
}

.handle-field {
  display: flex;
  align-items: center;
  gap: 4px;
}

.handle-field span {
  flex: 0 0 auto;
  font-size: 16px;
  font-weight: 700;
}

.handle-field input {
  flex: 1 1 auto;
  min-width: 0;
}

.board-error {
  min-height: 1.1em;
  margin-top: 4px;
  color: #ffd0d0;
  font-size: 13px;
  font-weight: 700;
}

.board-actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.board-actions button {
  flex: 1 1 0;
  min-height: 44px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  background: var(--gold);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.board-actions button[type="button"] {
  background: var(--sky);
}

.board-actions button:disabled {
  opacity: 0.6;
}

.board-actions button:focus-visible,
.board input:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

.board ol {
  margin-top: 4px;
  padding-left: 1.2rem;
  max-height: 9rem;
  overflow: auto;
}

.board li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 2px 0;
  font-size: 14px;
  line-height: 1.35;
}

.board .who {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  min-width: 0;
}

.board .x-handle {
  color: var(--sky);
}

.board li.mine .nick {
  color: var(--gold);
}

.board.is-save {
  max-height: 72%;
}

@media (max-height: 560px) {
  .board.is-save {
    position: fixed;
    inset: 4px;
    max-height: none;
    bottom: 4px;
  }
}

canvas {
  display: block;
  background: var(--water);
  touch-action: none;
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}

.pads {
  position: relative;
  z-index: 6;
  display: grid;
  grid-template-columns: 1fr 1fr 1.3fr;
  gap: 8px;
  width: min(640px, 100%);
  margin: 0 auto;
  padding: 0 12px 12px;
  touch-action: none;
}

.pad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  min-width: 0;
  touch-action: none;
}

.pads button {
  width: 100%;
  min-height: 64px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--sky);
  color: var(--ink);
  cursor: pointer;
  touch-action: none;
}

.pads button svg {
  width: 34px;
  height: 34px;
  display: block;
  pointer-events: none;
}

.pads .pad:last-child button {
  background: var(--gold);
}

.pad-label {
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
  color: var(--sky);
  pointer-events: none;
}

.pads button:active {
  transform: translateY(2px);
}

.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;
}

@media (prefers-reduced-motion: reduce) {
  .pads button:active {
    transform: none;
  }
}

@media (max-height: 560px) {
  .top {
    padding-top: max(4px, env(safe-area-inset-top));
  }

  .pads {
    gap: 6px;
    padding-bottom: 4px;
  }

  .pads button {
    min-height: 40px;
  }

  .pads button svg {
    width: 22px;
    height: 22px;
  }

  .pad-label {
    font-size: 11px;
  }

  .sound {
    gap: 4px;
  }
}

@media (max-width: 420px) {
  .top a {
    font-size: 14px;
  }

  .sound {
    gap: 4px;
  }
}

/* Nickname and handle sit on one row when the sheet is wide enough. */
#board.is-save {
  container-type: inline-size;
}

@container (min-width: 260px) {
  #save-form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: 8px;
    row-gap: 4px;
    align-items: start;
  }

  #save-form > h2,
  #save-form > .board-error,
  #save-form > .board-actions {
    grid-column: 1 / -1;
  }

  #save-form > label[for="nick"],
  #save-form > #nick {
    grid-column: 1;
  }

  #save-form > label[for="x-handle"],
  #save-form > .handle-field {
    grid-column: 2;
  }

  #save-form > label[for="nick"],
  #save-form > label[for="x-handle"] {
    grid-row: 2;
    margin-top: 0;
  }

  #save-form > #nick,
  #save-form > .handle-field {
    grid-row: 3;
    margin-top: 0;
  }

  #save-form > h2,
  #save-form > .board-error,
  #save-form > .board-actions {
    margin-top: 0;
  }
}

#save-form[hidden] {
  display: none;
}

/* Wide game over: the sheet sits beside the picture so the score and
   Play Again stay uncovered. The canvas keeps 960:540. */
@media (min-width: 701px) {
  html.embed .stage:has(#board.is-over:not([hidden])),
  html:not(.embed) .stage:has(#board.is-over:not([hidden])) {
    container-type: size;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 10px;
  }

  html.embed .stage:has(#board.is-over:not([hidden])) .playfield,
  html:not(.embed) .stage:has(#board.is-over:not([hidden])) .playfield {
    flex: 0 0 auto;
    width: min(66cqw, calc(100cqh * 960 / 540));
    max-width: 66%;
  }

  html.embed .stage:has(#board.is-over:not([hidden])) canvas,
  html:not(.embed) .stage:has(#board.is-over:not([hidden])) canvas {
    width: auto !important;
    height: auto !important;
    max-width: 100% !important;
    max-height: none !important;
    aspect-ratio: 960 / 540;
  }

  html.embed #board.is-over,
  html:not(.embed) #board.is-over {
    position: static;
    inset: auto;
    flex: 1 1 16rem;
    width: auto;
    min-width: 0;
    max-height: 100%;
    margin: 0;
    overflow: auto;
  }

  html.embed #board.is-over ol,
  html:not(.embed) #board.is-over ol {
    max-height: 6.5rem;
  }
}

@media (max-height: 560px) {
  html.embed #board.is-over,
  html:not(.embed) #board.is-over {
    position: static;
    inset: auto;
    max-height: 100%;
  }
}

@media (max-width: 700px) {
  .top h1 {
    font-size: 16px;
    line-height: 1.05;
  }

  .top a {
    font-size: 13px;
    line-height: 1.05;
  }

  html.embed body {
    justify-content: flex-start;
  }

  html.embed .stage {
    flex: 0 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    padding: 4px 8px;
    overflow: hidden;
  }

  html.embed .playfield {
    flex: 0 0 auto;
    width: 100%;
    max-width: 100%;
  }

  html.embed canvas {
    width: auto !important;
    height: auto !important;
    max-width: 100% !important;
    max-height: none !important;
    aspect-ratio: 960 / 540;
  }

  /* The home frame is short, so the title-screen board would cover the
     logo. The same notes and list already sit under the frame. */
  html.embed #board:not(.is-over) {
    display: none !important;
  }

  html.embed .stage:has(#board.is-over:not([hidden])) {
    flex: 1 1 auto;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
  }

  html.embed #board.is-over {
    position: static;
    inset: auto;
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    max-height: none;
    margin: 6px 0 0;
    overflow: auto;
    padding-bottom: 12px;
  }

  html.embed #board.is-over ol {
    max-height: 7.5rem;
  }

  /* Phone /play: the canvas and the controls stay put. The board fills
     the space above the canvas in every state. */
  html:not(.embed) body {
    justify-content: flex-start;
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
  }

  html:not(.embed) .top,
  html:not(.embed) .how-bar,
  html:not(.embed) .hud {
    flex: 0 0 auto;
  }

  html:not(.embed) .stage {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-end;
    padding: 4px 8px 4px;
    overflow: hidden;
  }

  html:not(.embed) .playfield {
    flex: 0 1 auto;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    min-height: 0;
    width: 100%;
    max-width: 100%;
    max-height: 100%;
    order: 1;
  }

  html:not(.embed) canvas {
    max-width: 100%;
    max-height: 100%;
  }

  html:not(.embed) .pads {
    flex: 0 0 auto;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }

  html:not(.embed) #board {
    position: static;
    inset: auto;
    order: 0;
    flex: 0 1 auto;
    min-height: 0;
    width: 100%;
    max-height: 28%;
    margin: 0 0 4px;
    overflow: auto;
    padding-bottom: 12px;
  }

  html:not(.embed) #board.is-play {
    display: block !important;
    background: #12365f;
  }

  html:not(.embed) .board ol {
    max-height: none;
  }
}
