:root {
  color-scheme: light;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  background: #f7e8c7;
  color: #17333a;
  --ink: #17333a;
  --paper: #f8f1d7;
  --blueprint: #4ec4bf;
  --blueprint-dark: #216b72;
  --coral: #ff765f;
  --orange: #ffb347;
  --yellow: #ffe36f;
  --green: #69c77f;
  --wood: #b36a32;
  --shadow: rgba(79, 50, 25, 0.22);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  display: flex;
  justify-content: center;
  padding: 16px;
  background:
    linear-gradient(90deg, rgba(23, 51, 58, 0.08) 0 2px, transparent 2px 48px),
    linear-gradient(180deg, #ffe594 0%, #f7c779 44%, #e98f67 100%);
}

button {
  min-height: 58px;
  border: 0;
  border-radius: 8px;
  color: var(--ink);
  font: 900 1rem/1.08 "Trebuchet MS", "Segoe UI", sans-serif;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible,
#gameCanvas:focus-visible {
  outline: 4px solid #ffffff;
  outline-offset: 3px;
  box-shadow: 0 0 0 7px rgba(23, 51, 58, 0.42);
}

.game-shell {
  position: relative;
  width: min(100%, 1000px);
  min-height: 100vh;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 10px;
}

.eyebrow {
  margin: 0 0 4px;
  color: #7a351f;
  font-size: 0.76rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

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

h1 {
  max-width: 690px;
  color: var(--ink);
  font-size: 2.55rem;
  line-height: 1;
}

h2 {
  font-size: 1.2rem;
}

.sound-toggle,
.action-row button,
.piece-button,
.result-card button {
  box-shadow: 0 7px 0 var(--shadow);
}

.sound-toggle {
  flex: 0 0 auto;
  min-width: 132px;
  padding: 0 18px;
  background: var(--ink);
  color: #fff8dc;
}

.hud-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}

.hud-tile {
  min-width: 0;
  min-height: 66px;
  padding: 10px;
  border: 3px solid rgba(23, 51, 58, 0.14);
  border-radius: 8px;
  background: rgba(255, 248, 220, 0.88);
  box-shadow: 0 6px 0 rgba(79, 50, 25, 0.12);
}

.hud-tile span {
  display: block;
  color: #6f4a2c;
  font-size: 0.74rem;
  font-weight: 900;
  text-transform: uppercase;
}

.hud-tile strong {
  display: block;
  margin-top: 3px;
  color: var(--ink);
  font-size: 1.8rem;
  line-height: 1;
  white-space: nowrap;
}

.canvas-wrap {
  width: 100%;
  border: 4px solid #fff8dc;
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.24) 0 2px, transparent 2px 40px),
    linear-gradient(180deg, var(--blueprint), #3fa9aa);
  box-shadow: 0 12px 0 rgba(79, 50, 25, 0.16);
  overflow: hidden;
  touch-action: none;
}

#gameCanvas {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  max-height: 62vh;
  background: #4ec4bf;
  touch-action: none;
}

.build-panel {
  display: grid;
  gap: 8px;
  margin-top: 10px;
  padding: 10px;
  border: 3px solid rgba(23, 51, 58, 0.14);
  border-radius: 8px;
  background: rgba(255, 248, 220, 0.9);
}

.palette-heading {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 10px;
}

.palette-heading p {
  margin: 0;
  color: #6f4a2c;
  font-weight: 900;
}

.piece-palette {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  touch-action: none;
}

.piece-button {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 6px;
  min-width: 0;
  min-height: 70px;
  padding: 8px 6px;
  background: #fff8dc;
  touch-action: none;
}

.piece-button[aria-pressed="true"] {
  background: var(--yellow);
  box-shadow: 0 4px 0 rgba(79, 50, 25, 0.26);
  transform: translateY(2px);
}

.piece-mark {
  display: block;
  width: 34px;
  height: 18px;
  border: 3px solid rgba(23, 51, 58, 0.28);
  border-radius: 5px;
  background: var(--coral);
  box-shadow: inset 0 3px 0 rgba(255, 255, 255, 0.42);
}

.pillar-piece .piece-mark {
  width: 18px;
  height: 34px;
  background: var(--orange);
}

.brace-piece .piece-mark {
  width: 34px;
  height: 34px;
  background:
    linear-gradient(135deg, transparent 0 38%, var(--green) 39% 62%, transparent 63%),
    #fff8dc;
}

.ramp-piece .piece-mark {
  width: 36px;
  height: 26px;
  clip-path: polygon(0 100%, 100% 100%, 100% 0);
  background: var(--yellow);
}

.erase-piece .piece-mark {
  background:
    linear-gradient(45deg, transparent 0 42%, #ffffff 43% 57%, transparent 58%),
    var(--ink);
}

.grid-status,
.status-text {
  min-height: 48px;
  margin: 10px 0 0;
  padding: 12px 14px;
  border-radius: 8px;
  background: rgba(255, 248, 220, 0.86);
  color: var(--ink);
  font-weight: 900;
}

.grid-status {
  border-left: 8px solid var(--green);
}

.touch-controls {
  touch-action: none;
}

.action-row {
  display: grid;
  grid-template-columns: 1.4fr repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.action-row button,
.result-card button {
  padding: 0 12px;
  background: #fff8dc;
}

.primary-action {
  background: var(--coral) !important;
  color: #24160f;
}

#undoButton {
  background: var(--yellow);
}

#clearButton {
  background: #93dfac;
}

#restartButton {
  background: #98dff0;
}

.status-text {
  text-align: center;
}

.result-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(23, 51, 58, 0.52);
  z-index: 10;
}

.result-card {
  width: min(100%, 430px);
  padding: 24px;
  border: 4px solid var(--ink);
  border-radius: 8px;
  background: #fff8dc;
  box-shadow: 0 14px 0 rgba(79, 50, 25, 0.26);
}

.result-card p:not(.eyebrow) {
  margin: 12px 0 18px;
  font-weight: 800;
}

.result-card button {
  min-width: 170px;
  background: var(--orange);
}

@media (max-width: 720px) {
  body {
    padding: 8px;
  }

  .game-shell {
    min-height: auto;
  }

  .game-header {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 8px;
  }

  h1 {
    font-size: 1.58rem;
    line-height: 1.04;
  }

  .sound-toggle {
    width: 100%;
  }

  .hud-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    margin-bottom: 8px;
  }

  .hud-tile {
    min-height: 54px;
    padding: 8px;
  }

  .hud-tile strong {
    font-size: 1.34rem;
  }

  #gameCanvas {
    max-height: 52vh;
  }

  .palette-heading {
    align-items: start;
    flex-direction: column;
    gap: 4px;
  }

  .piece-palette {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }

  .piece-button {
    min-height: 58px;
    grid-template-columns: auto minmax(0, 1fr);
    align-content: center;
    justify-items: start;
    padding: 7px 8px;
    font-size: 0.92rem;
  }

  .piece-mark {
    width: 30px;
    height: 16px;
  }

  .action-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }

  .action-row button,
  .result-card button {
    min-height: 58px;
    padding: 0 6px;
    font-size: 0.92rem;
  }

  .grid-status,
  .status-text {
    min-height: 44px;
    margin-top: 8px;
    padding: 9px 10px;
    font-size: 0.9rem;
  }
}

@media (max-width: 360px) {
  body {
    padding: 6px;
  }

  h1 {
    font-size: 1.36rem;
  }

  .eyebrow,
  .hud-tile span {
    font-size: 0.68rem;
  }

  .hud-tile strong {
    font-size: 1.18rem;
  }

  .piece-button,
  .action-row button,
  .sound-toggle,
  .result-card button {
    font-size: 0.84rem;
  }

  .piece-mark {
    width: 26px;
    height: 14px;
  }
}

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

  .piece-button[aria-pressed="true"] {
    transform: none;
  }
}
