:root {
  --ink: #173f45;
  --muted: #5d6d75;
  --paper: #effdf8;
  --line: #b9ebe0;
  --green: #16b7aa;
  --blue: #2f9fd6;
  --yellow: #ffd166;
  --orange: #ff8b5e;
  --red: #ef476f;
  --mint: #d9fff3;
  --cream: #fff8e8;
  --white: #fff;
  --shadow: 0 24px 70px rgba(23, 63, 69, 0.18);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
  background: #fff;
}

button {
  font: inherit;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: clamp(10px, 2vw, 24px);
  background: #fff;
}

.screen {
  display: none;
  width: min(1180px, 100%);
  min-width: 0;
}

.screen--active {
  display: block;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.menu {
  width: min(980px, 100%);
  margin: 0 auto;
  padding: clamp(22px, 4vw, 38px);
  border: 3px solid rgba(22, 183, 170, 0.55);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: var(--shadow);
  text-align: center;
}

.kicker {
  margin: 0 0 10px;
  color: var(--green);
  font-size: 0.82rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  color: #0f5f63;
  line-height: 1;
}

h1 {
  font-size: clamp(2.1rem, 7vw, 4.2rem);
}

.menu > h2 {
  font-size: clamp(2rem, 6vw, 3.45rem);
}

.intro {
  max-width: 62ch;
  margin: 18px auto 0;
  font-size: clamp(1rem, 2.4vw, 1.14rem);
  line-height: 1.52;
}

.chooser,
.leaderboard-panel {
  margin-top: 18px;
  text-align: left;
}

.section-heading {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.section-heading h2 {
  font-size: clamp(1rem, 2.6vw, 1.25rem);
}

.section-heading span,
.score-status {
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 800;
}

.selected-puzzle {
  position: relative;
  width: min(440px, 100%);
  margin: 0 auto;
}

.selected-puzzle img {
  display: block;
  width: 100%;
  height: 200px;
  aspect-ratio: 4 / 3;
  border: 3px solid rgba(22, 183, 170, 0.42);
  border-radius: 8px;
  object-fit: cover;
  box-shadow: 0 10px 26px rgba(23, 63, 69, 0.12);
}

.selected-image-title {
  display: block;
  margin-top: 8px;
  color: #0f5f63;
  font-size: 1rem;
  font-weight: 900;
  text-align: center;
}

.difficulty-picker,
.control-hints,
.start-actions,
.end-actions,
.game-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  align-items: center;
  justify-content: center;
}

.difficulty-picker {
  max-width: 100%;
}

.difficulty-button,
.image-nav-button,
.control-hints span {
  border: 2px solid rgba(22, 183, 170, 0.42);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
  font-weight: 900;
}

.difficulty-button {
  min-height: 48px;
  padding: 0 14px;
  cursor: pointer;
  max-width: 100%;
}

.image-nav-button {
  position: absolute;
  top: 50%;
  z-index: 2;
  display: grid;
  place-items: center;
  min-width: 46px;
  min-height: 46px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 10px 24px rgba(23, 63, 69, 0.2);
  cursor: pointer;
  font-size: 2rem;
  line-height: 1;
  transform: translateY(-50%);
}

.image-nav-button--previous {
  left: -18px;
}

.image-nav-button--next {
  right: -18px;
}

.difficulty-button[aria-pressed="true"] {
  border-color: #0f5f63;
  background: linear-gradient(180deg, #e8fff9, #c8fff0);
}

.control-hints {
  margin: 14px 0 0;
}

.control-hints span {
  padding: 8px 12px;
  font-size: 0.94rem;
}

.start-actions,
.end-actions {
  margin-top: 18px;
}

.start-actions {
  align-items: flex-start;
  display: grid;
  grid-template-columns: repeat(2, max-content);
  justify-content: center;
  gap: 10px;
}

.primary-button,
.secondary-button,
.ghost-button,
.image-nav-button {
  min-height: 48px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 900;
  touch-action: manipulation;
  transition: transform 120ms ease, box-shadow 120ms ease, filter 120ms ease;
}

.primary-button,
.secondary-button {
  border: 0;
  color: #fff;
  box-shadow: 0 8px 0 rgba(36, 49, 63, 0.2);
}

.primary-button {
  padding: 0 24px;
  background: linear-gradient(180deg, #1dd3c6, var(--green));
}

.secondary-button {
  padding: 0 16px;
  background: linear-gradient(180deg, #54c4ec, var(--blue));
}

.icon-button {
  min-width: 56px;
  padding: 0 14px;
  font-size: 1.35rem;
}

.help-button[aria-pressed="true"] {
  background: linear-gradient(180deg, #ffd979, #f1b93f);
  color: #173f45;
}

.ghost-button {
  padding: 0 14px;
  border: 2px solid rgba(23, 63, 69, 0.24);
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
}

.primary-button:hover,
.secondary-button:hover,
.ghost-button:hover,
.difficulty-button:hover,
.image-nav-button:hover {
  filter: brightness(1.04);
}

.primary-button:active,
.secondary-button:active {
  transform: translateY(5px);
  box-shadow: 0 3px 0 rgba(36, 49, 63, 0.24);
}

.game-layout {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.hud {
  display: grid;
  grid-template-columns: minmax(160px, 1.35fr) repeat(4, minmax(92px, 0.75fr)) minmax(92px, 0.55fr);
  gap: 10px;
  align-items: stretch;
}

.hud-stat {
  min-width: 0;
  padding: 10px 12px;
  border: 2px solid rgba(22, 183, 170, 0.42);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 10px 28px rgba(23, 63, 69, 0.12);
}

.hud-stat--action {
  display: grid;
  place-items: center;
  padding: 8px;
}

.hud-stat--action .secondary-button {
  width: 100%;
  min-height: 44px;
  box-shadow: none;
}

.hud-label {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.hud strong {
  display: block;
  margin-top: 5px;
  overflow: hidden;
  font-size: clamp(1.1rem, 2.5vw, 1.55rem);
  line-height: 1.05;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.play-area {
  position: relative;
  overflow: hidden;
  border: 3px solid rgba(23, 63, 69, 0.82);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
  max-width: 100%;
}

#puzzleCanvas {
  display: block;
  width: 100%;
  aspect-ratio: 128 / 78;
  max-height: calc(100dvh - 190px);
  min-height: 330px;
  background: #fff;
  touch-action: none;
  max-width: 100%;
}

.game-controls {
  justify-content: flex-end;
}

.theme-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 16px;
  border: 2px solid rgba(22, 183, 170, 0.45);
  border-radius: 8px;
  background: #fff;
  color: #0f5f63;
  font-weight: 900;
  text-decoration: none;
}

.score-status .theme-action {
  min-height: 34px;
  margin: 0 3px;
  padding: 0 11px;
  vertical-align: middle;
}

.theme-action--primary {
  border-color: transparent;
  background: #16b7aa;
  color: #fff;
}

.score-status a {
  color: #0f5f63;
  font-weight: 900;
}

.leaderboard-panel {
  width: min(620px, 100%);
  margin-inline: auto;
  border: 0;
}

.leaderboard-panel summary {
  display: flex;
  width: fit-content;
  min-height: 42px;
  margin: 0 auto;
  padding: 0 8px 0 16px;
  gap: 8px;
  align-items: center;
  justify-content: center;
  border: 2px solid rgba(22, 183, 170, 0.3);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: #0f5f63;
  cursor: pointer;
  font-weight: 900;
  list-style: none;
}

.leaderboard-panel summary::-webkit-details-marker {
  display: none;
}

.leaderboard-panel[open] summary {
  margin-bottom: 10px;
}

.leaderboard-panel--start {
  width: auto;
  margin: 0;
}

.leaderboard-panel--start summary,
.leaderboard-panel--start[open] summary {
  width: auto;
  min-height: 48px;
  margin: 0;
  padding: 0 10px 0 18px;
  border: 0;
  border-radius: 8px;
  background: linear-gradient(180deg, #54c4ec, var(--blue));
  color: #fff;
  box-shadow: 0 8px 0 rgba(36, 49, 63, 0.2);
}

.leaderboard-panel--start:not([open]) + .start-leaderboard {
  display: none;
}

.start-leaderboard {
  grid-column: 1 / -1;
  width: min(620px, calc(100vw - 48px));
  margin: 2px auto 0;
}

.refresh-button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 999px;
  background: rgba(22, 183, 170, 0.12);
  color: #0f5f63;
  cursor: pointer;
  font-size: 1.05rem;
  font-weight: 900;
}

.leaderboard-list {
  display: grid;
  gap: 7px;
  min-height: 42px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) minmax(72px, auto) minmax(54px, auto);
  gap: 9px;
  align-items: center;
  padding: 9px 11px;
  border: 2px solid rgba(22, 183, 170, 0.22);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.8);
  font-weight: 800;
}

.leaderboard-list .leaderboard-empty {
  display: block;
  color: var(--muted);
  text-align: center;
}

.leaderboard-rank {
  display: inline-grid;
  place-items: center;
  min-width: 32px;
  color: var(--green);
  font-weight: 900;
}

.leaderboard-cup {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 999px;
  background: rgba(22, 183, 170, 0.13);
  font-size: 1.05rem;
  line-height: 1;
}

.leaderboard-cup--1 {
  background: rgba(255, 209, 102, 0.34);
}

.leaderboard-cup--2 {
  background: rgba(93, 109, 117, 0.16);
}

.leaderboard-cup--3 {
  background: rgba(255, 139, 94, 0.18);
}

.leaderboard-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-player {
  color: inherit;
  text-decoration: none;
}

.leaderboard-player:hover,
.leaderboard-player:focus-visible {
  color: #0f5f63;
  text-decoration: underline;
}

.leaderboard-score,
.leaderboard-time {
  color: #0f5f63;
  white-space: nowrap;
}

.final-score {
  display: inline-grid;
  gap: 3px;
  min-width: 150px;
  margin: 22px 0 12px;
  padding: 12px 20px;
  border: 2px solid rgba(22, 183, 170, 0.38);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
}

.final-score span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 900;
  text-transform: uppercase;
}

.final-score strong {
  font-size: 2.1rem;
  line-height: 1;
}

@media (max-width: 820px) {
  .app {
    align-items: start;
    padding: 10px;
  }

  .hud {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .hud-stat--wide {
    grid-column: 1 / -1;
  }

  .hud-stat--action {
    grid-column: 1 / -1;
  }

  .game-controls {
    justify-content: center;
  }

  #puzzleCanvas {
    max-height: none;
    min-height: 360px;
  }
}

@media (max-width: 520px) {
  .app {
    padding: 8px 6px;
  }

  .menu {
    padding: 20px 14px;
  }

  .difficulty-picker {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .difficulty-button {
    min-width: 0;
    padding: 0 8px;
    font-size: 0.9rem;
  }

  .difficulty-button:last-child {
    grid-column: 1 / -1;
  }

  .start-actions {
    grid-template-columns: 1fr;
    width: 100%;
  }

  .start-actions > .primary-button,
  .leaderboard-panel--start,
  .leaderboard-panel--start summary {
    width: 100%;
  }

  .start-leaderboard {
    width: 100%;
  }

  .hud {
    gap: 7px;
  }

  .hud-stat {
    padding: 8px;
  }

  .hud strong {
    font-size: 1rem;
  }

  .play-area {
    border-width: 2px;
  }

  .selected-puzzle {
    width: min(360px, calc(100% - 22px));
  }

  .selected-puzzle img {
    height: 168px;
  }

  .image-nav-button {
    min-width: 40px;
    min-height: 40px;
    font-size: 1.7rem;
  }

  .image-nav-button--previous {
    left: -12px;
  }

  .image-nav-button--next {
    right: -12px;
  }

  .leaderboard-list li {
    grid-template-columns: 30px minmax(0, 1fr) auto;
    gap: 6px;
    padding: 8px;
    font-size: 0.86rem;
  }

  .leaderboard-score,
  .leaderboard-time {
    justify-self: end;
  }

  .leaderboard-time {
    grid-column: 3;
  }

  .leaderboard-score {
    max-width: 72px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
