:root {
  --ink: #1d261f;
  --paper: #f7f1de;
  --panel: #fff9e8;
  --line: #314031;
  --grass: #7fb56c;
  --leaf: #557f55;
  --water: #508fc2;
  --clay: #c77f4b;
  --sun: #f3d36b;
  --shadow: rgba(29, 38, 31, 0.24);
  font-family: "Trebuchet MS", "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(255, 249, 232, 0.18) 1px, transparent 1px),
    linear-gradient(rgba(255, 249, 232, 0.18) 1px, transparent 1px),
    linear-gradient(135deg, #5f8d5a 0%, #8fbf70 42%, #d8b96d 100%);
  background-size: 24px 24px, 24px 24px, auto;
  overflow: hidden;
}

button,
a {
  font: inherit;
}

button {
  border: 2px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
  box-shadow: 0 3px 0 rgba(29, 38, 31, 0.2);
}

button:hover,
a:hover {
  transform: translateY(-1px);
}

button:active {
  transform: translateY(1px);
  box-shadow: none;
}

button:focus-visible,
a:focus-visible {
  outline: 4px solid var(--sun);
  outline-offset: 3px;
}

.app-shell {
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(300px, 370px);
  gap: 14px;
  padding: 14px;
}

.play-band,
.tool-panel {
  min-height: 0;
  border: 3px solid var(--line);
  background: rgba(255, 249, 232, 0.88);
  box-shadow: 0 16px 36px var(--shadow);
}

.play-band {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
  padding: 10px;
}

.top-bar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
}

.home-link,
.plain-button,
.action-grid button,
.level-tools button,
.file-tools button,
.tab,
.mobile-pad button {
  min-height: 42px;
  border-radius: 8px;
}

.home-link {
  display: inline-grid;
  place-items: center;
  min-width: 76px;
  padding: 0 12px;
  border: 2px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  text-decoration: none;
  font-weight: 900;
  box-shadow: 0 3px 0 rgba(29, 38, 31, 0.2);
}

.readouts {
  display: flex;
  justify-content: center;
  gap: 8px;
  min-width: 0;
  font-size: 0.85rem;
  font-weight: 900;
}

.readouts span {
  min-width: 0;
  padding: 8px 10px;
  border: 2px solid rgba(49, 64, 49, 0.25);
  background: rgba(255, 255, 255, 0.55);
  border-radius: 8px;
  white-space: nowrap;
}

#gameCanvas {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: block;
  border: 3px solid var(--line);
  background: #74aadd;
  image-rendering: pixelated;
  touch-action: none;
}

.mobile-pad {
  position: fixed;
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(3, 64px);
  gap: 8px;
  padding: 9px;
  border: 3px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 249, 232, 0.9);
  box-shadow: 0 16px 36px var(--shadow);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  user-select: none;
  touch-action: manipulation;
}

.mobile-pad button {
  width: 64px;
  height: 54px;
  padding: 0 4px;
  background: #e7ca75;
  font-size: 0.82rem;
}

.mobile-pad .pad-delete {
  background: #e9a064;
}

.mobile-pad .pad-add {
  background: #9dcf6f;
}

.tool-panel {
  display: grid;
  grid-template-rows: auto auto auto auto auto auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: 12px;
}

.selected-card {
  display: grid;
  grid-template-columns: 58px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  padding: 10px;
  border: 2px solid rgba(49, 64, 49, 0.3);
  background: rgba(255, 255, 255, 0.58);
  border-radius: 8px;
}

#selectedSprite {
  width: 48px;
  height: 48px;
  border: 2px solid var(--line);
  background: #8fbf70;
  image-rendering: pixelated;
}

.selected-card h1 {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.05;
  letter-spacing: 0;
}

.meta-label {
  margin: 0 0 4px;
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.72;
}

.action-grid,
.carpet-tool,
.level-tools,
.file-tools,
.tabs {
  display: grid;
  gap: 8px;
}

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

.carpet-tool {
  grid-template-columns: minmax(0, 1fr) 56px;
  align-items: center;
  min-height: 44px;
  padding: 6px 8px;
  border: 2px solid rgba(49, 64, 49, 0.28);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.46);
  font-size: 0.84rem;
  font-weight: 900;
}

.carpet-tool input {
  width: 48px;
  height: 32px;
  padding: 0;
  border: 2px solid var(--line);
  border-radius: 6px;
  background: transparent;
}

.level-tools {
  grid-template-columns: 1fr 1fr 1fr;
}

.file-tools {
  grid-template-columns: 1fr 1fr;
}

.tabs {
  grid-template-columns: repeat(3, 1fr);
}

.action-grid button:first-child {
  background: #9dcf6f;
}

.action-grid button:last-child {
  background: #e9a064;
}

.level-tools button {
  background: #b9d6df;
  font-size: 0.84rem;
}

.file-tools button {
  background: #d8c17a;
  font-size: 0.84rem;
}

.tab {
  background: #f1e0a5;
  font-size: 0.84rem;
}

.tab.is-active {
  background: #6ea85f;
  color: #fff9e8;
}

.inventory {
  min-height: 0;
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  align-content: start;
  gap: 8px;
  padding-right: 4px;
}

.inventory::after {
  content: "";
  grid-column: 1 / -1;
  min-height: 96px;
}

.item-button {
  display: grid;
  justify-items: center;
  gap: 5px;
  min-height: 88px;
  padding: 6px;
  border-radius: 8px;
  background: #fffdf2;
  font-size: 0.72rem;
  line-height: 1.05;
}

.item-button.is-selected {
  background: #f3d36b;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.68), 0 3px 0 rgba(29, 38, 31, 0.2);
}

.item-button canvas {
  width: 42px;
  height: 42px;
  image-rendering: pixelated;
}

.item-button span {
  max-width: 100%;
  overflow-wrap: anywhere;
}

.hint {
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.35;
  font-weight: 800;
  opacity: 0.72;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%) translateY(18px);
  max-width: min(560px, calc(100vw - 28px));
  padding: 10px 14px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 12px 30px var(--shadow);
  font-weight: 900;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

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

  .app-shell {
    min-height: 100%;
    height: auto;
    grid-template-columns: 1fr;
  }

  .play-band {
    min-height: 72vh;
  }

  .tool-panel {
    min-height: 560px;
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: 8px;
    gap: 8px;
  }

  .top-bar {
    grid-template-columns: 1fr auto;
  }

  .readouts {
    grid-column: 1 / -1;
    order: 3;
    flex-wrap: wrap;
    justify-content: start;
    font-size: 0.74rem;
  }

  .readouts span {
    padding: 6px 7px;
  }

  .play-band {
    min-height: 74vh;
  }

  .tool-panel {
    min-height: 600px;
  }

  .mobile-pad {
    grid-template-columns: repeat(3, 56px);
    gap: 6px;
    padding: 7px;
  }

  .mobile-pad button {
    width: 56px;
    height: 50px;
    font-size: 0.74rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
