:root {
  --ink: #17212b;
  --muted: #667788;
  --paper: #f5f1e8;
  --panel: #fffaf0;
  --line: #d7ceb9;
  --blue: #147cc2;
  --sky: #dff2ff;
  --green: #20aa78;
  --yellow: #f0b82c;
  --red: #dc3654;
  --shadow: 0 18px 50px rgba(23, 33, 43, 0.15);
}

* {
  box-sizing: border-box;
}

body {
  height: 100vh;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(23, 33, 43, 0.05) 1px, transparent 1px),
    linear-gradient(rgba(23, 33, 43, 0.05) 1px, transparent 1px),
    var(--paper);
  background-size: 28px 28px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(520px, 1fr) 390px;
  gap: 22px;
  height: 100vh;
  overflow: hidden;
  padding: 22px;
}

.play-section,
.tool-panel {
  min-width: 0;
}

.play-section {
  display: grid;
  grid-template-rows: auto auto auto;
  gap: 14px;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.94);
  box-shadow: var(--shadow);
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--blue);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(2rem, 4vw, 3.35rem);
  line-height: 0.92;
}

h2 {
  font-size: 1.18rem;
  line-height: 1.1;
}

.score-strip {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  max-width: 620px;
}

.score-strip span,
.map-button {
  min-height: 36px;
  padding: 8px 10px;
  border: 1px solid #bfd0df;
  border-radius: 8px;
  background: white;
  color: #4d5e6f;
  font-size: 0.88rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.score-strip strong {
  display: inline-block;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

#coin-count {
  width: 2ch;
}

#timer {
  width: 5ch;
}

#status-pill {
  min-width: 58px;
  text-align: center;
}

.map-selector {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 4px;
  padding: 3px;
  border: 1px solid #bfd0df;
  border-radius: 8px;
  background: #eef7ff;
}

.map-button {
  display: grid;
  place-items: center;
  width: 30px;
  padding: 0;
}

.map-button.active {
  border-color: var(--blue);
  background: var(--blue);
  color: white;
}

#status-pill.win {
  border-color: #99d6bd;
  background: #e5fff3;
  color: #116d4d;
}

#status-pill.ouch {
  border-color: #f0a9b7;
  background: #fff0f3;
  color: #9f1834;
}

.game-wrap {
  position: relative;
  height: clamp(360px, 52vh, 520px);
  min-height: 360px;
  overflow: hidden;
  border: 1px solid #263544;
  border-radius: 8px;
  background: #162636;
  box-shadow: var(--shadow);
}

.state-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(23, 33, 43, 0.32);
}

.state-overlay.hidden {
  display: none;
}

.state-panel {
  display: grid;
  gap: 14px;
  min-width: min(330px, 100%);
  padding: 22px;
  border: 1px solid #b8c9d8;
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.97);
  box-shadow: 0 20px 60px rgba(23, 33, 43, 0.26);
  text-align: center;
}

.button-row.centered {
  justify-content: center;
}

.state-panel h2 {
  font-size: 2rem;
}

.result-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.result-stats span {
  padding: 10px;
  border: 1px solid #bfd0df;
  border-radius: 8px;
  background: white;
  color: #4d5e6f;
  font-weight: 900;
}

.result-stats strong {
  display: block;
  color: var(--ink);
  font-size: 1.35rem;
}

#game-root,
#game-root canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.control-help {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.control-help span {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.94);
  color: #4c5c6a;
  font-size: 0.88rem;
  font-weight: 800;
}

.tool-panel {
  display: grid;
  align-content: start;
  gap: 14px;
  max-height: calc(100vh - 44px);
  overflow: auto;
}

.panel-block {
  display: grid;
  gap: 14px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.94);
  box-shadow: var(--shadow);
}

.panel-heading {
  display: grid;
  gap: 2px;
}

.slider-row {
  display: grid;
  gap: 8px;
}

.slider-row span {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: #4c5c6a;
  font-size: 0.9rem;
  font-weight: 900;
}

.slider-row strong {
  color: var(--ink);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--blue);
}

.button-row,
.tile-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.primary-button,
.secondary-button,
.tile-tool {
  min-height: 42px;
  border: 1px solid #b7c7d4;
  border-radius: 8px;
  font-weight: 900;
}

.primary-button,
.secondary-button {
  padding: 0 14px;
}

.primary-button {
  background: var(--blue);
  color: white;
}

.secondary-button,
.tile-tool {
  background: white;
  color: var(--ink);
}

.secondary-button:hover,
.tile-tool:hover,
.tile-tool.active {
  border-color: var(--blue);
  background: #e4f4ff;
}

.tile-tool {
  display: grid;
  place-items: center;
  width: 42px;
}

.level-editor {
  display: grid;
  grid-template-columns: repeat(60, 22px);
  gap: 2px;
  padding: 9px;
  overflow-x: auto;
  border: 1px solid #c3d2de;
  border-radius: 8px;
  background: #bfe0f4;
}

.edit-tile {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-width: 0;
  border: 1px solid rgba(255, 255, 255, 0.32);
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.3);
  color: var(--ink);
  font-size: 0.72rem;
  font-weight: 900;
}

.edit-tile.block {
  background: #56626f;
  border-color: #394550;
}

.edit-tile.metal {
  background: #dce3e8;
  color: #46535e;
}

.edit-tile.brick {
  background: #cc6731;
  color: #ffe0c8;
}

.edit-tile.rail {
  background: #edf4f8;
  color: #263544;
}

.edit-tile.spike {
  background: #fff0f2;
  color: var(--red);
}

.edit-tile.coin {
  background: #fff4b8;
  color: #b97900;
}

.edit-tile.spring {
  background: #ddf9ee;
  color: #138e63;
}

.edit-tile.flag {
  background: #e9f3ff;
  color: #165fa8;
}

.sprite-reference img {
  width: 100%;
  max-height: 210px;
  object-fit: contain;
  border: 1px solid #c3d2de;
  border-radius: 8px;
  background:
    linear-gradient(45deg, #e9e9e9 25%, transparent 25%),
    linear-gradient(-45deg, #e9e9e9 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e9e9e9 75%),
    linear-gradient(-45deg, transparent 75%, #e9e9e9 75%),
    #f7f7f7;
  background-position:
    0 0,
    0 10px,
    10px -10px,
    -10px 0;
  background-size: 20px 20px;
  image-rendering: pixelated;
}

@media (max-width: 1080px) {
  .app-shell {
    grid-template-columns: 1fr;
    overflow: auto;
  }

  .play-section {
    grid-template-rows: auto auto auto;
  }

  .game-wrap {
    height: 52vh;
    min-height: 340px;
    max-height: 460px;
  }

  .tool-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    max-height: none;
    overflow: visible;
  }

  .sprite-reference {
    grid-column: 1 / -1;
  }
}

@media (max-width: 720px) {
  .app-shell {
    padding: 14px;
  }

  .topbar {
    align-items: start;
    flex-direction: column;
  }

  .score-strip {
    justify-content: flex-start;
  }

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