:root {
  color-scheme: dark;
  --ink: #ecf5ee;
  --muted: #9eb0aa;
  --panel: #101613;
  --panel-2: #15211d;
  --line: #284239;
  --floor: #1f6f5c;
  --floor-lit: #36a576;
  --wall-top: #d9b15c;
  --wall-left: #9d6f3d;
  --wall-right: #6b4d37;
  --portal: #4bc8d6;
  --crystal: #f06c72;
  --focus: #f3d96b;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  background: #0a0e0c;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100dvh;
  min-height: 100dvh;
  background:
    radial-gradient(circle at 12% 14%, rgba(75, 200, 214, 0.16), transparent 30%),
    linear-gradient(135deg, #08100e 0%, #101714 45%, #18120f 100%);
}

.topbar {
  display: grid;
  grid-template-columns: minmax(170px, 1fr) auto minmax(320px, 1fr);
  align-items: center;
  gap: 14px;
  padding: 12px clamp(14px, 2.2vw, 28px);
  border-bottom: 1px solid var(--line);
  background: rgba(9, 15, 13, 0.92);
  backdrop-filter: blur(16px);
}

.brand-block,
.metrics,
.seed-form,
.controls {
  display: flex;
  align-items: center;
}

.brand-block {
  gap: 10px;
  min-width: 0;
}

.mark {
  display: block;
  width: 28px;
  height: 28px;
  flex: 0 0 auto;
  transform: rotate(45deg);
  border: 2px solid #54c992;
  background: linear-gradient(135deg, #133a32 0%, #e7ba57 100%);
  box-shadow: 0 0 0 4px rgba(84, 201, 146, 0.11);
}

h1 {
  margin: 0;
  overflow: hidden;
  font-size: clamp(1rem, 2vw, 1.35rem);
  font-weight: 760;
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.metrics {
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.metric {
  min-width: 78px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(21, 33, 29, 0.86);
}

.score-metric {
  min-width: 94px;
}

.streak-metric {
  min-width: 86px;
}

.best-metric {
  min-width: 112px;
}

.metric-label {
  display: block;
  color: var(--muted);
  font-size: 0.68rem;
  line-height: 1.1;
}

.metric strong {
  display: block;
  margin-top: 2px;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.seed-form {
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.seed-form label,
.difficulty-picker legend {
  color: var(--muted);
  font-size: 0.82rem;
}

.difficulty-picker {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.difficulty-picker legend {
  padding: 0;
}

.difficulty-options {
  display: flex;
  min-width: 0;
  overflow: hidden;
  border: 1px solid #3a5f52;
  border-radius: 8px;
  background: #0d1512;
}

.segmented-button {
  min-height: 36px;
  min-width: 64px;
  border: 0;
  border-right: 1px solid #284239;
  padding: 0 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 140ms ease, color 140ms ease;
}

.segmented-button:last-child {
  border-right: 0;
}

.segmented-button:hover {
  color: var(--ink);
  background: #173126;
}

.segmented-button[aria-pressed="true"] {
  color: #f3d96b;
  background: #324020;
}

.segmented-button:focus-visible {
  position: relative;
  z-index: 1;
  outline: 3px solid rgba(243, 217, 107, 0.5);
  outline-offset: -3px;
}

.seed-form input {
  width: min(170px, 34vw);
  min-width: 82px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: 0;
  padding: 0 10px;
  background: #0d1512;
  color: var(--ink);
}

.seed-form input:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px rgba(243, 217, 107, 0.18);
}

.text-button,
.icon-button {
  border: 1px solid #3a5f52;
  border-radius: 8px;
  background: #173126;
  color: var(--ink);
  cursor: pointer;
  transition: transform 140ms ease, border-color 140ms ease, background-color 140ms ease;
}

.text-button {
  min-height: 38px;
  padding: 0 13px;
  white-space: nowrap;
}

.icon-button {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  font-size: 1.15rem;
  line-height: 1;
}

.text-button:hover,
.icon-button:hover {
  border-color: #60c796;
  background: #1d4837;
}

.text-button:active,
.icon-button:active {
  transform: translateY(1px);
}

.text-button:focus-visible,
.icon-button:focus-visible {
  outline: 3px solid rgba(243, 217, 107, 0.5);
  outline-offset: 2px;
}

.primary-button {
  border-color: #d5b655;
  background: #5a4219;
}

.primary-button:hover {
  border-color: #f3d96b;
  background: #71531f;
}

.sound-button {
  font-size: 1rem;
}

.sound-button[aria-pressed="true"] {
  border-color: #d5b655;
  color: #f3d96b;
  background: #2f351d;
}

.sound-button[aria-pressed="false"] {
  color: #81928c;
  background: #111a17;
}

.stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(7, 11, 10, 0) 0%, rgba(8, 10, 9, 0.52) 100%),
    radial-gradient(circle at 50% 42%, rgba(65, 129, 112, 0.18), transparent 46%);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.status-line {
  position: absolute;
  left: 50%;
  bottom: clamp(12px, 2.5vh, 24px);
  max-width: min(560px, calc(100% - 28px));
  transform: translateX(-50%);
  border: 1px solid rgba(236, 245, 238, 0.13);
  border-radius: 8px;
  padding: 9px 13px;
  background: rgba(10, 14, 12, 0.78);
  color: var(--ink);
  text-align: center;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.24);
}

.win-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(4, 7, 6, 0.62);
  backdrop-filter: blur(12px);
}

.win-overlay[hidden] {
  display: none;
}

.win-panel {
  width: min(520px, 100%);
  border: 1px solid rgba(243, 217, 107, 0.38);
  border-radius: 8px;
  padding: clamp(20px, 4vw, 30px);
  background:
    linear-gradient(180deg, rgba(25, 36, 30, 0.97), rgba(13, 19, 16, 0.98)),
    #101613;
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.46);
}

.danger-overlay .win-panel {
  border-color: rgba(240, 68, 68, 0.48);
}

.win-kicker {
  margin: 0 0 7px;
  color: #f3d96b;
  font-size: 0.78rem;
  font-weight: 760;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.danger-overlay .win-kicker {
  color: #f06c72;
}

.win-panel h2 {
  margin: 0;
  font-size: clamp(1.55rem, 6vw, 2.25rem);
  line-height: 1.05;
}

.win-copy {
  margin: 10px 0 0;
  color: var(--muted);
  line-height: 1.5;
}

.win-best {
  margin: 14px 0 0;
  border: 1px solid rgba(243, 217, 107, 0.5);
  border-radius: 8px;
  padding: 9px 11px;
  background: rgba(90, 66, 25, 0.42);
  color: #f3d96b;
  font-weight: 760;
  text-align: center;
}

.win-best[hidden] {
  display: none;
}

.win-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 20px 0;
}

.win-stats div {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: rgba(10, 18, 15, 0.72);
}

.win-stats dt {
  color: var(--muted);
  font-size: 0.68rem;
}

.win-stats dd {
  margin: 3px 0 0;
  overflow: hidden;
  font-weight: 760;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.win-actions {
  display: flex;
  gap: 10px;
}

.win-actions .text-button {
  flex: 1 1 0;
}

.controls {
  justify-content: center;
  min-height: 78px;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--line);
  background: rgba(9, 15, 13, 0.9);
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 42px);
  grid-template-rows: repeat(2, 42px);
  gap: 6px;
}

.dpad-up {
  grid-column: 2;
}

.dpad-left {
  grid-column: 1;
  grid-row: 2;
}

.dpad-right {
  grid-column: 3;
  grid-row: 2;
}

.dpad-down {
  grid-column: 2;
  grid-row: 2;
}

@media (max-width: 1320px) and (min-width: 841px) {
  .topbar {
    grid-template-columns: minmax(170px, 1fr) auto;
  }

  .metrics {
    justify-content: flex-end;
  }

  .seed-form {
    grid-column: 1 / -1;
    justify-content: center;
    flex-wrap: wrap;
  }

  .difficulty-options {
    flex: 0 1 auto;
  }
}

@media (max-width: 840px) {
  body {
    overflow: auto;
  }

  .app-shell {
    height: 100svh;
    min-height: 100svh;
  }

  .topbar {
    grid-template-columns: 1fr;
    align-items: stretch;
    gap: 10px;
  }

  .metrics {
    justify-content: stretch;
  }

  .metric {
    flex: 1 1 0;
    min-width: 0;
  }

  .seed-form {
    justify-content: stretch;
    flex-wrap: wrap;
  }

  .difficulty-picker {
    flex: 1 1 100%;
  }

  .difficulty-options {
    flex: 1 1 auto;
  }

  .segmented-button {
    flex: 1 1 0;
  }

  .seed-form input {
    flex: 1 1 120px;
    width: auto;
  }

  .text-button {
    flex: 1 1 auto;
  }

  .win-overlay {
    align-items: end;
    padding: 14px;
  }
}

@media (max-width: 430px) {
  .topbar {
    padding: 10px;
  }

  .metric {
    padding: 6px 7px;
  }

  .metric-label {
    font-size: 0.62rem;
  }

  .metric strong {
    font-size: 0.9rem;
  }

  .status-line {
    bottom: 8px;
    font-size: 0.86rem;
  }

  .win-stats {
    grid-template-columns: 1fr;
  }

  .win-actions {
    flex-direction: column;
  }
}

@media (prefers-reduced-motion: reduce) {
  .text-button,
  .icon-button,
  .segmented-button {
    transition: none;
  }
}
