:root {
  color-scheme: light;
  --bg: #f4f6f3;
  --panel: #ffffff;
  --ink: #18211c;
  --muted: #5d675f;
  --line: #ccd5cd;
  --accent: #1d766f;
  --accent-dark: #12544f;
  --warn: #9a3412;
  --shadow: 0 16px 40px rgba(24, 33, 28, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  padding: 0 12px;
}

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

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app {
  width: min(1480px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  gap: 18px;
  align-items: end;
  margin-bottom: 16px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.2;
}

h2 {
  font-size: 15px;
  line-height: 1.3;
}

.status {
  margin-top: 6px;
  color: var(--muted);
  font-size: 14px;
}

.status.warn {
  color: var(--warn);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.controls label {
  display: inline-grid;
  grid-template-columns: auto 74px;
  align-items: center;
  gap: 7px;
  min-height: 38px;
  color: var(--muted);
}

.controls input[type="number"] {
  width: 74px;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 8px;
  color: var(--ink);
}

.controls .frameOption {
  grid-template-columns: 18px auto;
  width: max-content;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}

.controls .frameOption input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

#run {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

#run:hover:not(:disabled) {
  background: var(--accent-dark);
}

.workspace {
  min-height: 280px;
}

.canvasWrap {
  width: 100%;
  height: min(68vh, 900px);
  min-height: 280px;
  overflow: auto;
  display: grid;
  place-items: center;
  background: #dfe6df;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

#gridCanvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  background: #f8faf7;
  image-rendering: pixelated;
  cursor: crosshair;
}

.palettePanel {
  margin-top: 16px;
  padding: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.paletteHeader {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
  color: var(--muted);
}

.tilePalette {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  gap: 10px;
}

.tileButton {
  display: grid;
  grid-template-rows: 48px auto;
  gap: 6px;
  justify-items: center;
  align-items: center;
  min-height: 78px;
  padding: 8px 6px;
}

.tileButton[aria-pressed="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(29, 118, 111, 0.16);
}

.tileButton.isExcluded {
  background: #edf0ed;
  color: var(--muted);
}

.tileButton.isExcluded img {
  filter: grayscale(1);
  opacity: 0.38;
}

.tileButton.isExcluded span {
  opacity: 0.72;
}

.tileButton img {
  width: 45px;
  height: 45px;
  object-fit: contain;
  image-rendering: pixelated;
}

.tileButton span {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 820px) {
  .app {
    padding: 12px;
  }

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

  .controls {
    justify-content: stretch;
  }

  .controls button {
    flex: 1 1 150px;
  }

  .controls label {
    flex: 1 1 110px;
    grid-template-columns: auto 1fr;
  }

  .controls .frameOption {
    flex-basis: 240px;
    grid-template-columns: 18px auto;
  }

  .controls input {
    width: 100%;
  }

  .controls .frameOption input {
    width: 16px;
  }
}
