:root {
  color-scheme: light;
  --ink: #172027;
  --muted: #62707c;
  --paper: #f7f3e8;
  --surface: #ffffff;
  --line: #d7d1c2;
  --accent: #1f7a8c;
  --accent-strong: #145569;
  --good: #2f8a57;
  --shadow: 0 18px 45px rgba(32, 38, 44, 0.14);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  min-height: 100vh;
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    linear-gradient(135deg, rgba(31, 122, 140, 0.08), rgba(240, 186, 65, 0.12)),
    var(--paper);
}

button,
label,
select,
input {
  font: inherit;
}

button,
.upload-button,
#sampleImageSelect {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 14px;
  color: var(--ink);
  background: var(--surface);
  cursor: pointer;
  transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}

button:hover,
.upload-button:hover,
#sampleImageSelect:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.46;
  transform: none;
}

button[aria-pressed="true"] {
  border-color: var(--accent);
  color: #ffffff;
  background: var(--accent);
}

.app-shell {
  width: min(1440px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

h1,
p {
  margin: 0;
}

h1 {
  font-size: clamp(28px, 4vw, 48px);
  line-height: 1;
  letter-spacing: 0;
}

.topbar p {
  margin-top: 8px;
  color: var(--muted);
  font-weight: 700;
}

.reading-mission {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin: -2px 0 18px;
  border: 1px solid #d2aa45;
  border-radius: 8px;
  padding: 14px 16px;
  background: #fff8df;
}

.reading-mission h2 {
  margin: 2px 0 5px;
  font-size: clamp(20px, 3vw, 28px);
  line-height: 1.15;
}

.mission-eyebrow {
  color: #795600;
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.mission-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 7px 14px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.mission-actions {
  display: grid;
  justify-items: end;
  gap: 8px;
  text-align: right;
}

.mission-actions strong {
  max-width: 30rem;
  color: #795600;
  font-size: 14px;
}

.mission-actions a,
.dialog-actions a {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #ffffff;
  padding: 0 14px;
  text-decoration: none;
  font-weight: 800;
}

.upload-button[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.46;
  transform: none;
}

.stats {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stats span {
  min-width: 94px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  text-align: center;
  background: rgba(255, 255, 255, 0.78);
}

.game-layout {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr) 250px;
  gap: 18px;
  align-items: start;
}

.toolbar,
.control-stack,
.side-panel {
  display: grid;
  gap: 10px;
}

.toolbar {
  position: sticky;
  top: 18px;
  align-self: start;
}

.mode-picker {
  display: grid;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px;
  background: rgba(255, 255, 255, 0.78);
}

.mode-picker button {
  min-height: 34px;
  padding: 0 8px;
  font-size: 14px;
  font-weight: 700;
}

.upload-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#sampleImageSelect {
  width: 100%;
  min-width: 0;
  appearance: none;
  padding-right: 34px;
  overflow: hidden;
  text-overflow: ellipsis;
  background:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%) calc(100% - 18px) 50% / 6px 6px no-repeat,
    linear-gradient(135deg, var(--muted) 50%, transparent 50%) calc(100% - 13px) 50% / 6px 6px no-repeat,
    var(--surface);
}

#imageUpload {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.board-zone {
  display: grid;
  justify-items: center;
  gap: 12px;
  min-width: 0;
}

.board-frame {
  position: relative;
  width: min(100%, calc(100vh - 128px));
  min-width: 320px;
  aspect-ratio: 1;
  margin: 0 auto;
  border: 1px solid rgba(23, 32, 39, 0.12);
  border-radius: 8px;
  overflow: hidden;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.puzzle-board {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(10, 1fr);
  touch-action: none;
  background:
    linear-gradient(135deg, rgba(31, 122, 140, 0.08), rgba(240, 186, 65, 0.1)),
    #e8edf0;
}

.slot {
  position: relative;
  border: 0;
  border-radius: 0;
  padding: 0;
  background:
    radial-gradient(circle at center, rgba(255, 255, 255, 0.54), rgba(255, 255, 255, 0) 56%),
    rgba(255, 255, 255, 0.18);
}

.slot:hover,
.slot:focus-visible {
  z-index: 2;
  outline: 2px solid #ffffff;
  outline-offset: -2px;
  box-shadow: inset 0 0 0 3px rgba(31, 122, 140, 0.62);
}

.piece {
  position: relative;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 0;
  padding: 0;
  background-repeat: no-repeat;
  background-size: 1000% 1000%;
  cursor: grab;
  transform: rotate(var(--turn, 0deg)) translateZ(0);
  transition: opacity 120ms ease, transform 120ms ease, box-shadow 120ms ease;
}

.jigsaw-piece {
  z-index: 1;
  overflow: visible;
  background: transparent;
}

.piece-art {
  position: absolute;
  top: -22%;
  left: -22%;
  display: block;
  width: 144%;
  height: 144%;
  overflow: visible;
  filter: drop-shadow(0 1px 2px rgba(23, 32, 39, 0.22));
  pointer-events: none;
}

.piece:hover,
.piece:focus-visible {
  z-index: 2;
  outline: 2px solid #ffffff;
  outline-offset: -2px;
  box-shadow: inset 0 0 0 3px var(--accent);
}

.piece.selected {
  z-index: 3;
  box-shadow: inset 0 0 0 4px #f0ba41;
}

.piece.dragging {
  opacity: 0.48;
  cursor: grabbing;
}

.piece.correct::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(47, 138, 87, 0.13);
  pointer-events: none;
}

.piece.hint-target {
  z-index: 4;
  animation: hintPulse 800ms ease-in-out infinite alternate;
}

.slot.hint-target {
  z-index: 3;
  box-shadow: inset 0 0 0 4px #f0ba41, 0 0 18px rgba(240, 186, 65, 0.56);
}

.slot-hint-marker {
  position: absolute;
  inset: 12%;
  z-index: 8;
  border: 3px solid #f0ba41;
  border-radius: 8px;
  background:
    linear-gradient(#f0ba41, #f0ba41) 50% 0 / 3px 26% no-repeat,
    linear-gradient(#f0ba41, #f0ba41) 50% 100% / 3px 26% no-repeat,
    linear-gradient(#f0ba41, #f0ba41) 0 50% / 26% 3px no-repeat,
    linear-gradient(#f0ba41, #f0ba41) 100% 50% / 26% 3px no-repeat,
    rgba(240, 186, 65, 0.16);
  box-shadow: 0 0 0 2px rgba(23, 32, 39, 0.22), 0 0 18px rgba(240, 186, 65, 0.8);
  pointer-events: none;
  animation: targetPulse 800ms ease-in-out infinite alternate;
}

.piece.rotated::before {
  content: "";
  position: absolute;
  top: 4px;
  right: 4px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f0ba41;
  box-shadow: 0 0 0 2px rgba(23, 32, 39, 0.24);
  pointer-events: none;
}

.show-grid .slot {
  box-shadow: inset 0 0 0 0.5px rgba(255, 255, 255, 0.75), inset 0 0 0 1px rgba(23, 32, 39, 0.13);
}

.show-grid .slot:empty::after {
  content: "";
  position: absolute;
  inset: 38%;
  border-radius: 50%;
  background: rgba(23, 32, 39, 0.12);
}

.tray-panel {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  width: min(100%, calc(100vh - 128px));
  min-width: 320px;
  margin: 0 auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: rgba(255, 255, 255, 0.82);
}

.tray-heading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 118px;
}

.tray-heading span {
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.tray-heading strong {
  font-size: 14px;
  white-space: nowrap;
}

.piece-tray {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.piece-tray .piece {
  aspect-ratio: 1;
  min-height: 0;
  border-radius: 6px;
  box-shadow: inset 0 0 0 1px rgba(23, 32, 39, 0.16);
}

.piece-tray .jigsaw-piece {
  overflow: visible;
}

.jigsaw-mode .slot {
  background:
    radial-gradient(circle at center, rgba(255, 255, 255, 0.38), rgba(255, 255, 255, 0) 56%),
    rgba(255, 255, 255, 0.14);
}

.preview-image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

.previewing .preview-image {
  opacity: 0.88;
}

.mini-preview,
.progress-block,
.challenge-block,
.record-block,
.leaderboard-block {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
  overflow: hidden;
}

.mini-preview {
  aspect-ratio: 1;
}

.mini-preview img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.progress-block,
.challenge-block,
.record-block,
.leaderboard-block {
  padding: 14px;
}

.progress-block span,
.challenge-block span,
.panel-heading span {
  display: block;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.progress-block strong,
.challenge-block strong {
  display: block;
  margin: 4px 0 10px;
  font-size: 30px;
  line-height: 1;
}

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

.panel-heading strong {
  font-size: 12px;
  color: var(--accent-strong);
}

.panel-heading button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 13px;
  font-weight: 700;
}

.progress-track {
  height: 10px;
  border-radius: 999px;
  overflow: hidden;
  background: #e4ded0;
}

.progress-track div {
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--good));
  transition: width 180ms ease;
}

.challenge-block dl,
.record-block dl {
  display: grid;
  gap: 8px;
  margin: 0;
}

.challenge-block dl div,
.record-block dl div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-top: 1px solid rgba(23, 32, 39, 0.1);
  padding-top: 8px;
}

.challenge-block dt,
.challenge-block dd,
.record-block dt,
.record-block dd {
  margin: 0;
  font-size: 14px;
}

.challenge-block dt,
.record-block dt {
  color: var(--muted);
  font-weight: 700;
}

.challenge-block dd,
.record-block dd {
  color: var(--ink);
  font-weight: 800;
}

.leaderboard-block > p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.leaderboard-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  border-top: 1px solid rgba(23, 32, 39, 0.1);
  padding-top: 8px;
  font-size: 13px;
}

.leaderboard-rank {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  color: #ffffff;
  background: var(--accent);
  font-weight: 800;
}

.leaderboard-player {
  min-width: 0;
}

.leaderboard-player strong,
.leaderboard-player span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-player span {
  color: var(--muted);
  font-size: 12px;
}

.leaderboard-score {
  font-weight: 900;
  white-space: nowrap;
}

.celebration {
  position: fixed;
  inset: 0;
  z-index: 30;
  overflow: hidden;
  pointer-events: none;
}

.celebration i {
  position: absolute;
  top: -18px;
  left: var(--x);
  width: 10px;
  height: 18px;
  border-radius: 2px;
  background: var(--color);
  transform: rotate(var(--rotation));
  animation: confettiDrop 1500ms cubic-bezier(0.14, 0.73, 0.36, 1) var(--delay) forwards;
}

.dialog {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(16, 24, 31, 0.42);
}

.dialog[hidden] {
  display: none;
}

.dialog-panel {
  width: min(440px, 100%);
  border-radius: 8px;
  padding: 24px;
  text-align: center;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.dialog-panel h2 {
  margin: 0 0 8px;
  font-size: 32px;
}

.dialog-panel p {
  margin-bottom: 18px;
  color: var(--muted);
}

.dialog-panel .mission-complete-status {
  border: 1px solid #d2aa45;
  border-radius: 8px;
  background: #fff8df;
  padding: 10px 12px;
  color: #795600;
  font-weight: 800;
}

.dialog-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.dialog-actions > * {
  width: 100%;
}

.score-submit {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
  text-align: left;
}

.score-submit label {
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.score-submit-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.score-submit input {
  min-width: 0;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--ink);
  background: var(--surface);
}

.score-submit input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.score-submit p {
  min-height: 18px;
  margin: 0;
  font-size: 13px;
}

@media (max-width: 1040px) {
  .game-layout {
    grid-template-columns: 1fr;
  }

  .toolbar {
    position: static;
    min-height: 0;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: auto;
  }

  .control-stack {
    grid-column: 1 / -1;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .mode-picker {
    grid-column: 1 / -1;
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .side-panel {
    grid-template-columns: minmax(120px, 180px) 1fr;
  }

  .board-frame,
  .tray-panel {
    width: min(100%, calc(100vh - 220px));
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100vw - 20px, 560px);
    padding: 14px 0;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
  }

  .reading-mission {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
  }

  .mission-actions {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    justify-items: start;
    text-align: left;
  }

  .mission-actions a {
    white-space: nowrap;
  }

  .stats {
    justify-content: stretch;
  }

  .stats span {
    flex: 1;
  }

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

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

  .mode-picker {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .mode-picker button {
    font-size: 13px;
  }

  .side-panel {
    grid-template-columns: 1fr;
  }

  .mini-preview {
    display: none;
  }

  .board-frame,
  .tray-panel {
    min-width: 0;
    width: 100%;
  }

  .tray-panel {
    gap: 8px;
    padding: 8px;
  }

  .tray-heading {
    min-width: 104px;
  }

  .tray-heading span,
  .tray-heading strong {
    font-size: 12px;
  }

  .piece-tray {
    gap: 6px;
  }

  .dialog-actions {
    grid-template-columns: 1fr;
  }
}

@keyframes hintPulse {
  from {
    box-shadow: inset 0 0 0 4px #f0ba41, 0 0 0 rgba(240, 186, 65, 0);
  }

  to {
    box-shadow: inset 0 0 0 4px #f0ba41, 0 0 22px rgba(240, 186, 65, 0.72);
  }
}

@keyframes targetPulse {
  from {
    opacity: 0.72;
    transform: scale(0.92);
  }

  to {
    opacity: 1;
    transform: scale(1.04);
  }
}

@keyframes confettiDrop {
  to {
    top: 110vh;
    transform: translateX(28px) rotate(720deg);
  }
}
