:root {
  --bg1: #e9eff6;
  --bg2: #cfd9e6;
  --card: #eef2f8;
  --well: #e2e8f1;
  --edge: #b7c3d4;
  --ink: #1f2a3a;
  --muted: #6b7a90;
  --blue: #2f7df6;
  --blue-d: #1b4fb8;
  --font:
    ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, sans-serif;
  --board-size: min(680px, calc(100dvh - 250px));
}
* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  min-height: 100%;
}
body {
  min-height: 100dvh;
  background: linear-gradient(160deg, var(--bg1), var(--bg2)) fixed;
  color: var(--ink);
  font-family: var(--font);
  padding: max(14px, env(safe-area-inset-top)) 14px
    max(20px, env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Layout: mobile first (single column) ---------- */
.app {
  display: grid;
  gap: 14px;
  text-align: center;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "head" "stats" "settings" "stage" "pad" "board";
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 18px 16px 22px;
  background: linear-gradient(180deg, #f7f9fc, var(--card));
  border-radius: 28px;
  box-shadow:
    0 1px 0 #fff inset,
    0 -8px 0 rgba(31, 42, 58, 0.07) inset,
    0 2px 0 var(--edge),
    0 34px 50px -24px rgba(31, 42, 58, 0.45);
}
.app > header {
  grid-area: head;
}
.stats {
  grid-area: stats;
}
.settings {
  grid-area: settings;
}
.stage {
  grid-area: stage;
}
.dpad {
  grid-area: pad;
}
.board {
  grid-area: board;
}

h1 {
  margin: 0;
  font-size: clamp(1.6rem, 6vw, 2rem);
  font-weight: 800;
  letter-spacing: -0.01em;
}
.by {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.stats {
  display: flex;
  gap: 10px;
}
.stats div {
  flex: 1;
  padding: 7px 4px 8px;
  border-radius: 16px;
  background: var(--well);
  box-shadow:
    inset 0 3px 7px rgba(31, 42, 58, 0.16),
    0 2px 0 #fff;
}
.stats span {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
}
.stats strong {
  font-size: 1.45rem;
  font-variant-numeric: tabular-nums;
}

.settings {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}
.settings label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  text-align: left;
}
.settings button {
  align-self: flex-end;
}

/* ---------- Controls ---------- */
select,
input,
button {
  font: inherit;
  color: var(--ink);
  border: 0;
  border-radius: 14px;
  padding: 9px 12px;
}
select,
input {
  background: var(--well);
  box-shadow:
    inset 0 2px 5px rgba(31, 42, 58, 0.18),
    0 2px 0 #fff;
}
button {
  cursor: pointer;
  font-weight: 700;
  background: linear-gradient(180deg, #fff, #e4eaf3);
  box-shadow:
    0 5px 0 var(--edge),
    0 9px 12px -2px rgba(31, 42, 58, 0.25),
    0 1px 0 #fff inset;
  transition:
    transform 0.08s,
    box-shadow 0.08s;
}
button:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow:
    0 1px 0 var(--edge),
    0 2px 4px rgba(31, 42, 58, 0.2),
    0 1px 0 #fff inset;
}
button:disabled {
  opacity: 0.5;
  cursor: default;
}
:focus-visible {
  outline: 3px solid rgba(47, 125, 246, 0.55);
  outline-offset: 2px;
}

/* ---------- Game board ---------- */
.stage {
  position: relative;
  padding: 9px;
  border-radius: 26px;
  background: linear-gradient(180deg, #c3cedd, #d6dfeb);
  box-shadow:
    inset 0 4px 10px rgba(31, 42, 58, 0.3),
    0 2px 0 #fff;
}
canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  height: auto;
  touch-action: none;
  border-radius: 18px;
  box-shadow:
    0 0 0 2px rgba(31, 42, 58, 0.12),
    0 10px 18px -6px rgba(31, 42, 58, 0.5);
}
.overlay {
  position: absolute;
  inset: 9px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 18px;
  border-radius: 18px;
  background: rgba(238, 242, 248, 0.82);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}
.overlay[hidden],
#saveForm[hidden] {
  display: none;
}
.overlay h2 {
  margin: 0;
  font-size: clamp(1.4rem, 5.5vw, 2rem);
  font-weight: 800;
}
.overlay p {
  margin: 0;
  max-width: 28ch;
}
.hint {
  color: var(--muted) !important;
  font-size: 0.78rem;
}
#playBtn {
  color: #fff;
  padding: 12px 34px;
  font-size: 1.05rem;
  background: linear-gradient(180deg, #5b9bff, var(--blue));
  box-shadow:
    0 6px 0 var(--blue-d),
    0 12px 16px -2px rgba(27, 79, 184, 0.5),
    0 1px 0 rgba(255, 255, 255, 0.5) inset;
}
#playBtn:active {
  box-shadow:
    0 1px 0 var(--blue-d),
    0 2px 5px rgba(27, 79, 184, 0.4),
    0 1px 0 rgba(255, 255, 255, 0.5) inset !important;
  transform: translateY(5px) !important;
}
#saveForm {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}
#saveForm input {
  width: 9em;
}

/* D-pad: only on touch devices, never on mouse-driven desktops */
.dpad {
  display: none;
  justify-self: center;
  width: clamp(160px, 48vw, 200px);
  aspect-ratio: 1;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  gap: 8px;
  margin-top: 4px;
}
@media (pointer: coarse) {
  .dpad {
    display: grid;
  }
}
.dpad button {
  padding: 0;
  font-size: 1.2rem;
  color: var(--blue-d);
  touch-action: manipulation;
  user-select: none;
  border-radius: 18px;
}
.dpad [data-dir="up"] {
  grid-area: 1 / 2;
}
.dpad [data-dir="left"] {
  grid-area: 2 / 1;
}
.dpad [data-dir="right"] {
  grid-area: 2 / 3;
}
.dpad [data-dir="down"] {
  grid-area: 3 / 2;
}

/* ---------- Leaderboard ---------- */
.board {
  padding: 12px 16px 8px;
  border-radius: 20px;
  background: var(--well);
  box-shadow:
    inset 0 3px 8px rgba(31, 42, 58, 0.14),
    0 2px 0 #fff;
  align-self: start;
}
.board h2 {
  margin: 2px 0 8px;
  font-size: 1.05rem;
  font-weight: 800;
}
.board ol {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: rank;
}
.board li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 0;
  border-bottom: 1px solid rgba(31, 42, 58, 0.08);
  counter-increment: rank;
  font-weight: 600;
}
.board li::before {
  content: counter(rank);
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  font-size: 0.8rem;
  background: linear-gradient(180deg, #fff, #dde4ee);
  box-shadow: 0 2px 0 var(--edge);
}
.board li:nth-child(1)::before {
  background: radial-gradient(circle at 35% 30%, #fff1a8, #f2b705);
  box-shadow: 0 2px 0 #a97800;
}
.board li span:first-of-type {
  flex: 1;
  text-align: left;
}
.board li:last-child {
  border-bottom: 0;
}
.board .empty {
  justify-content: center;
  color: var(--muted);
  counter-increment: none;
  font-weight: 500;
}
.board .empty::before {
  content: none;
}

/* ---------- Tablet portrait: roomier single column ---------- */
@media (min-width: 600px) {
  body {
    padding-top: 24px;
  }
  .app {
    max-width: 600px;
    padding: 24px 24px 28px;
    gap: 16px;
  }
}

/* ---------- Desktop / large tablet landscape: board left, info right ---------- */
@media (min-width: 900px) and (min-height: 560px) {
  body {
    padding: 28px;
  }
  .app {
    max-width: 1100px;
    padding: 28px 32px 34px;
    column-gap: 32px;
    row-gap: 18px;
    grid-template-columns: max(380px, var(--board-size)) minmax(300px, 380px);
    justify-content: center;
    align-content: start;
    grid-template-areas:
      "head  head"
      "stage stats"
      "stage settings"
      "stage board"
      "pad   board";
  }
  h1 {
    font-size: 2.3rem;
  }
  .settings {
    justify-content: flex-start;
  }
  .overlay h2 {
    font-size: 2.1rem;
  }
}

/* ---------- Phones in landscape: board fits the short screen ---------- */
@media (orientation: landscape) and (max-height: 559px) {
  body {
    padding: 10px;
  }
  .app {
    max-width: 960px;
    padding: 14px 18px;
    gap: 10px 20px;
    grid-template-columns: max(240px, calc(100dvh - 44px)) minmax(240px, 1fr);
    grid-template-areas: "stage stats" "stage settings" "stage board" "pad board";
    align-content: start;
  }
  .app > header {
    display: none;
  }
  .stage {
    padding: 7px;
    border-radius: 20px;
  }
  .overlay {
    inset: 7px;
    gap: 6px;
    padding: 8px;
  }
  .overlay .hint {
    display: none;
  }
  .overlay h2 {
    font-size: 1.3rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  button {
    transition: none;
  }
}

/* ---------- Typography ---------- */
:root {
  --font-display: "Fredoka", ui-rounded, "Segoe UI", system-ui, sans-serif;
  --font-body: "DM Sans", "Segoe UI", system-ui, sans-serif;
}
body {
  font-family: var(--font-body);
  font-weight: 500;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}
h1,
.overlay h2 {
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: -0.005em;
  color: #1a2538;
  text-shadow:
    0 2px 0 #fff,
    0 5px 8px rgba(31, 42, 58, 0.2);
}
.board h2 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.15rem;
}
.by {
  font-weight: 500;
  letter-spacing: 0.01em;
}
.stats span,
.settings label {
  font-weight: 600;
  letter-spacing: 0.02em;
}
.stats strong {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.65rem;
  line-height: 1.15;
}
button {
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: 0.01em;
}
select,
input {
  font-family: var(--font-body);
  font-weight: 600;
}
.overlay p {
  font-weight: 500;
  line-height: 1.45;
}
.hint {
  font-weight: 500;
}
.board li {
  font-weight: 600;
}
.board li span:last-of-type {
  font-family: var(--font-display);
  font-weight: 600;
}

/* ---------- Leaderboard tabs ---------- */
.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 0 0 8px;
  border-radius: 14px;
  background: #d3dbe7;
  box-shadow: inset 0 2px 5px rgba(31, 42, 58, 0.2);
}
.tabs button {
  flex: 1;
  padding: 6px 4px;
  font-size: 0.85rem;
  color: var(--muted);
  background: transparent;
  box-shadow: none;
  border-radius: 11px;
}
.tabs button:active:not(:disabled) {
  transform: none;
  box-shadow: none;
}
.tabs button.active {
  color: var(--ink);
  background: linear-gradient(180deg, #fff, #e8edf5);
  box-shadow:
    0 3px 0 var(--edge),
    0 5px 8px -2px rgba(31, 42, 58, 0.25);
}
