:root {
  --bg: #16181d;
  --panel: #20232a;
  --line: #333844;
  --text: #e6e8ee;
  --muted: #9aa1b1;
  --accent: #f5a524;
  --orange: #f97316;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  position: relative;
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  font-family: system-ui, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

#popup-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1000;
  background-color: rgba(0, 0, 0, 0.5);
}

#popup-container:not([hidden]) {
  display: grid;
  place-items: start center;
  animation: fade-in .2s ease-out;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

#popup {
  position: relative;
  display: flex;
  flex-direction: column;
  background-color: var(--panel);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .5);
  max-width: 560px;
  max-height: calc(100dvh - 10rem);
  padding: 1.5rem;
  margin-top: 5rem;
}

#popup-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin: 0 -0.5rem;
  padding: 0 .5rem;
}

#popup:focus {
  outline: none;
}

@keyframes popup-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(.97);
  }
}

#popup-container:not([hidden]) #popup {
  animation: popup-in .25s ease-out;
}

#popup-close,
#zoom-close,
#success-close {
  position: absolute;
  top: .35rem;
  right: .35rem;
  width: 44px;
  height: 44px;
  background: none;
  border: none;
  border-radius: 6px;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}

#popup-close:hover,
#zoom-close:hover,
#success-close:hover {
  color: var(--text);
}

#popup h2 {
  margin: 0 0 1rem;
  padding-right: 3rem;
  line-height: 1.3;
}

#popup img {
  max-width: 100%;
  border-radius: 8px;
  margin-bottom: 1rem;
}

@keyframes hint-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
}

#popup-hints .hint {
  margin: .75rem 0 0;
  padding: .6rem .8rem;
  background: rgba(255, 255, 255, .04);
  border-radius: 8px;
  color: var(--muted);
  animation: hint-in .25s ease-out;
}

#popup-hints .hint .hint-label {
  color: var(--text);
  font-weight: 600;
}

#popup-hints .hint.answer {
  background: rgba(245, 165, 36, .12);
  color: var(--text);
}

#popup-hints .hint.answer .hint-label {
  color: var(--accent);
}

#hint-btn {
  margin-top: 1rem;
  width: 100%;
  background: var(--accent);
  color: #0d1020;
  border-color: var(--accent);
  font-weight: 600;
}

#hint-btn:hover:not(:disabled) {
  filter: brightness(1.08);
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 1rem;
  padding: .8rem 1.2rem;
  border-bottom: 1px solid var(--line);
  min-height: 48px;
}

h1 {
  font-size: 1.5rem;
  margin: 0;
  color: var(--orange);
}

.help {
  margin: 0;
  color: var(--muted);
  font-size: 1rem;
}

.btn {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  min-height: 44px;
  padding: .45rem .8rem;
  cursor: pointer;
  font: inherit;
  transition: border-color .15s ease-out, transform .1s ease-out;
}

.btn:active:not(:disabled) {
  transform: scale(.97);
}

.btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.btn:disabled {
  opacity: .4;
  cursor: default;
}

main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto;
  gap: .75rem 1rem;
  padding: .75rem 1.2rem .75rem;
}

#controls-left,
#controls-right {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(2, minmax(0, 1fr));
  grid-auto-flow: column;
  gap: .6rem;
  min-height: 0;
  overflow-y: auto;
}

#controls-left {
  grid-column: 1;
  grid-row: 2;
}

#controls-right {
  grid-column: 2;
  grid-row: 2;
}

#view-toggle {
  display: none;
}

.tab.active {
  background: var(--accent);
  color: #0d1020;
  border-color: var(--accent);
  font-weight: 600;
}

.param {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: .5rem .5rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .5rem;
  min-width: 0;
  min-height: 0;
}

.param img {
  margin: auto;
  max-width: 100%;
  max-height: 35dvh;
}

.param-info {
  flex: 1;
  min-width: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

.param-head {
  width: 100%;
  min-width: 0;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: .5rem;
  padding-bottom: .25rem;
}

.param-head .btn {
  flex: none;
  min-height: 36px;
  padding: .25rem .6rem;
}

.param h2 {
  flex: 1;
  min-width: 0;
  font-size: 1rem;
  margin: 0;
  color: var(--orange);
  overflow-wrap: anywhere;
}

.param input[type=range] {
  width: 100%;
  height: 2rem;
  accent-color: var(--orange);
  display: none;
}

#views {
  grid-column: 1 / -1;
  grid-row: 1;
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  grid-template-rows: minmax(0, 1fr);
  gap: 1rem;
  min-width: 0;
  min-height: 0;
}

#map {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
}

#success-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1000;
  background-color: rgba(0, 0, 0, 0.5);
}

#success-container:not([hidden]) {
  display: grid;
  place-items: center;
  animation: fade-in .2s ease-out;
}

#success {
  position: relative;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: .5rem;
  text-align: center;
  background: var(--panel);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .5);
  max-width: min(92vw, 480px);
  max-height: calc(100dvh - 6rem);
  overflow-y: auto;
  padding: 1.5rem;
  margin: 0;
}

#success:focus {
  outline: none;
}

#success-container:not([hidden]) #success {
  animation: success-in .4s ease-out;
}

@keyframes success-in {
  from {
    opacity: 0;
    transform: scale(.96);
  }
}

#success h2 {
  margin: 0 0 1rem 0;
  font-size: 1.5rem;
  color: var(--orange);
}

#success p {
  margin: 0;
  color: var(--muted);
}

#success-img {
  max-width: 100%;
  max-height: 36dvh;
  object-fit: contain;
  border-radius: 6px;
}

#countdown {
  margin-top: 1rem;
  color: var(--accent);
  font-size: 1.5rem;
  font-variant-numeric: tabular-nums;
}

#views.won {
  grid-template-columns: 1fr;
}

#views.won #canvas-figure {
  display: none;
}

figure {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: .4rem;
  align-items: center;
  justify-content: center;
}

#canvas-figure {
  grid-column: 2;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
}

figcaption {
  color: var(--muted);
}

canvas {
  width: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  background: #0c0d10;
  border-radius: 6px;
}

#canvas-stack {
  position: relative;
  display: inline-block;
  max-width: 100%;
  min-width: min(240px, 100%);
  min-height: 160px;
  background: #0c0d10;
  border-radius: 6px;
}

#result {
  display: block;
}

#result-front {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: none;
  border-radius: 6px;
  pointer-events: none;
}

#success-img {
  transition: none;
}

@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

#zoom-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1000;
  background-color: rgba(0, 0, 0, 0.5);
}

#zoom-container:not([hidden]) {
  display: grid;
  place-items: start center;
  animation: fade-in .2s ease-out;
}

#zoom {
  position: relative;
  background-color: var(--panel);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  max-width: min(80vw, 900px);
  max-height: calc(100dvh - 6rem);
  overflow-y: auto;
  padding: 1.5rem;
  margin-top: 3rem;
}

#zoom:focus {
  outline: none;
}

#zoom img {
  display: block;
  max-width: 100%;
  max-height: 70dvh;
  width: auto;
  height: auto;
  border-radius: 8px;
  margin: 0 auto 1rem;
}

.param img {
  cursor: zoom-in;
  outline: 2px solid transparent;
  outline-offset: 2px;
  transition: outline-color .15s ease-out;
}

.param img:hover {
  outline-color: var(--orange);
}

.puzzle-badge {
  background: none;
  border: none;
  cursor: pointer;
}

.puzzle-badge span {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--orange);
  color: #0d1020;
  font-weight: 700;
  font-size: 1rem;
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .5);
}

.puzzle-badge.locked span {
  background: #3a3f4b;
  border-color: #5a6273;
  color: #aab1c2;
}

@media (max-height: 600px) {
  #popup {
    margin-top: 1rem;
    max-height: calc(100dvh - 2rem);
  }
}

@media (min-width: 900px) {
  main {
    grid-template-columns: 1fr minmax(0, 1fr) 1fr;
    grid-template-rows: minmax(0, 1fr);
  }

  #views {
    grid-column: 2;
    grid-row: 1;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1.15fr) minmax(0, 1fr);
  }

  #map,
  #success {
    grid-column: 1;
    grid-row: 1;
  }

  #canvas-figure {
    grid-column: 1;
    grid-row: 2;
  }

  #controls-left {
    grid-column: 1;
    grid-row: 1;
  }

  #controls-right {
    grid-column: 3;
    grid-row: 1;
  }

  #controls-left,
  #controls-right {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: repeat(2, minmax(0, 1fr));
    gap: .5rem;
  }
}

@media (max-width: 899.98px) {
  body {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  main {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    gap: .75rem;
    padding: .5rem;
  }

  #views {
    grid-column: 1;
    height: 60dvh;
    position: sticky;
    top: 0;
    z-index: 900;
    background-color: var(--bg);
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    gap: .5rem;
    padding: .5rem 0;
  }

  #view-toggle {
    display: flex;
    gap: .5rem;
    grid-column: 1;
    grid-row: 1;
  }

  #map,
  #success,
  #canvas-figure {
    grid-column: 1;
    grid-row: 2;
  }

  #views[data-tab="canvas"] #map {
    display: none;
  }

  #views[data-tab="map"] #canvas-figure {
    display: none;
  }

  #views.won {
    grid-template-rows: auto minmax(320px, 1fr);
    height: auto;
    min-height: 60dvh;
  }

  #controls-left,
  #controls-right {
    grid-column: 1;
    grid-row: auto;
  }

  .param img {
    max-width: 100%;
    max-height: 12dvh;
  }
}