:root {
  color-scheme: dark;
  font-family: "Courier New", Courier, monospace;
  background: #131313;
  color: #f4f0df;
  --panel: #20252a;
  --panel-strong: #2b3236;
  --line: #485057;
  --accent: #f0c541;
  --accent-dark: #8a5b11;
  --sky: #64b9dc;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: #131313;
  image-rendering: pixelated;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app {
  height: 100vh;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  padding: 12px;
  overflow: hidden;
}

.topbar {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
}

.topbar h1,
.topbar p,
.panel h2,
.play-help {
  margin: 0;
}

.topbar h1 {
  font-size: 20px;
  letter-spacing: 0;
}

.topbar p {
  margin-top: 4px;
  color: #c7d1d8;
  font-size: 12px;
  line-height: 1.45;
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.primary-button,
.ghost-button,
.tool-button,
.layer-button {
  min-height: 40px;
  padding: 0 14px;
  color: #fff6d7;
  background: var(--panel-strong);
  border: 1px solid var(--line);
}

.primary-button {
  color: #241800;
  background: var(--accent);
  border-color: #ffe175;
  box-shadow: inset 0 -3px 0 var(--accent-dark);
}

.ghost-button:hover,
.tool-button:hover,
.tool-button.is-active,
.layer-button:hover,
.layer-button.is-active,
.tab-button:hover,
.tab-button.is-active {
  border-color: var(--accent);
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(220px, 38vh) minmax(0, 1fr);
  gap: 12px;
  overflow: hidden;
}

.toolbox {
  min-height: 0;
  display: grid;
  gap: 12px;
  align-content: start;
  overflow: auto;
  padding-right: 2px;
}

.panel,
.editor-stage,
.play-panel {
  border: 1px solid var(--line);
  background: var(--panel);
}

.panel {
  padding: 12px;
}

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

.panel h2 {
  font-size: 14px;
}

#tileLabel,
.stage-toolbar,
.meta-panel,
.helper-text,
.tileset-meta {
  color: #c7d1d8;
  font-size: 12px;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  margin-top: 12px;
}

.tab-button {
  min-height: 36px;
  color: #fff6d7;
  background: #171b1f;
  border: 1px solid var(--line);
}

.tab-button.is-active {
  background: var(--panel-strong);
}

.tab-panel {
  margin-top: 12px;
}

.helper-text {
  margin: 8px 0 0;
  line-height: 1.45;
}

.full-button {
  width: 100%;
  margin-top: 10px;
}

.file-picker {
  display: grid;
  gap: 8px;
  margin-top: 8px;
  color: #fff6d7;
  font-size: 12px;
}

.file-picker input {
  width: 100%;
  min-height: 38px;
  padding: 8px;
  color: #c7d1d8;
  background: #171b1f;
  border: 1px solid var(--line);
}

.tileset-meta {
  margin-top: 12px;
  padding: 8px;
  overflow-wrap: anywhere;
  background: #171b1f;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.palette {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(34px, 1fr));
  gap: 8px;
  margin-top: 12px;
  max-height: 280px;
  overflow: auto;
}

.tile-swatch {
  aspect-ratio: 1;
  min-width: 34px;
  display: grid;
  place-items: center;
  padding: 0;
  background-color: #20252a;
  background-image:
    var(--tile-image),
    linear-gradient(45deg, #333b41 25%, transparent 25%),
    linear-gradient(-45deg, #333b41 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #333b41 75%),
    linear-gradient(-45deg, transparent 75%, #333b41 75%);
  background-position:
    var(--tile-position),
    0 0,
    0 4px,
    4px -4px,
    -4px 0;
  background-size:
    var(--tile-bg-size),
    8px 8px,
    8px 8px,
    8px 8px,
    8px 8px;
  border: 2px solid #111;
  outline: 1px solid var(--line);
  image-rendering: pixelated;
}

.tile-swatch.is-selected {
  outline: 3px solid var(--accent);
}

.tile-swatch span {
  align-self: end;
  justify-self: end;
  min-width: 14px;
  color: #fff;
  background: rgba(0, 0, 0, 0.65);
  font-size: 10px;
  line-height: 14px;
  text-align: center;
}

.tool-grid,
.layer-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}

.layer-grid {
  grid-template-columns: 1fr;
}

.check-row {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 12px;
  color: #e8edee;
  font-size: 12px;
  line-height: 1.35;
}

.check-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.meta-panel dl {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 12px 0 0;
}

.meta-panel dt {
  color: #8ea0aa;
}

.meta-panel dd {
  margin: 4px 0 0;
  color: #fff6d7;
}

.editor-stage {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

.stage-toolbar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.stage-toolbar > span:last-child {
  grid-column: 1 / -1;
}

.zoom-controls {
  display: inline-grid;
  grid-template-columns: 34px 64px 34px;
  gap: 4px;
}

.zoom-controls button {
  min-height: 32px;
  color: #fff6d7;
  background: var(--panel-strong);
  border: 1px solid var(--line);
}

.zoom-controls button:hover {
  border-color: var(--accent);
}

.canvas-shell {
  min-height: 0;
  overflow: auto;
  touch-action: none;
  background: #161a1d;
}

#editorCanvas {
  display: block;
  width: 1280px;
  height: 720px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: crosshair;
}

.play-panel {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  justify-items: center;
  padding: 12px;
  overflow: hidden;
}

.play-panel[hidden],
.workspace[hidden] {
  display: none;
}

#game {
  width: min(100%, calc((100vh - 170px) * 1.333333), 960px);
  aspect-ratio: 4 / 3;
  background: var(--sky);
  border: 4px solid #f4f0df;
  overflow: hidden;
}

#game canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.play-help {
  color: #c7d1d8;
  font-size: 12px;
  text-align: center;
}

@media (min-width: 760px) {
  .app {
    padding: 16px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
    align-items: center;
  }

  .workspace {
    grid-template-columns: 260px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
  }

  .toolbox {
    align-content: start;
  }

  .palette {
    grid-template-columns: repeat(3, 48px);
  }

  .tile-swatch {
    width: 48px;
    min-width: 48px;
  }

  .editor-stage {
    min-height: 0;
  }

  .stage-toolbar {
    grid-template-columns: auto auto 1fr;
  }

  .stage-toolbar > span:last-child {
    grid-column: auto;
    justify-self: end;
  }
}
