:root {
  color-scheme: dark;
  --ink: #171711;
  --ink-soft: #24231a;
  --paper: #f6edcf;
  --paper-dim: #d9caa1;
  --ochre: #d99b35;
  --terracotta: #a84e31;
  --field: #68713a;
  --field-dark: #454b26;
  --magenta: #d80b72;
  --magenta-light: #ff4ca0;
  --danger: #f06445;
  --pedestrian: #75cdd7;
  --safe: #8bad55;
  --stone: #aaa083;
  --white: #fffaf0;
  --shadow: rgba(0, 0, 0, 0.42);
}

* {
  box-sizing: border-box;
}

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

body {
  min-width: 320px;
  background:
    radial-gradient(circle at 18% 12%, rgba(216, 11, 114, 0.12), transparent 25rem),
    radial-gradient(circle at 82% 82%, rgba(217, 155, 53, 0.12), transparent 30rem),
    #171711;
}

button {
  min-height: 44px;
  border: 0;
  font: inherit;
  font-weight: 900;
  letter-spacing: 0.055em;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible,
canvas:focus-visible {
  outline: 3px solid var(--white);
  outline-offset: 3px;
}

button:active {
  transform: translateY(2px);
}

.game-shell {
  width: min(1180px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  padding: 16px clamp(10px, 2.4vw, 28px) max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.game-header,
.game-footer {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
}

.game-header {
  margin-bottom: 10px;
}

.game-header h1 {
  margin: 0;
  color: var(--white);
  font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
  font-size: clamp(2rem, 4vw, 3.9rem);
  font-weight: 900;
  letter-spacing: 0.025em;
  line-height: 0.92;
  text-transform: uppercase;
}

.eyebrow,
.screen-kicker {
  margin: 0 0 5px;
  color: var(--magenta-light);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.prototype-badge {
  flex: 0 0 auto;
  padding: 7px 9px;
  border: 1px solid var(--paper-dim);
  color: var(--paper-dim);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.12em;
}

.game-panel {
  overflow: hidden;
  border: 3px solid var(--paper);
  border-radius: 6px;
  background: var(--ink-soft);
  box-shadow: 0 20px 60px var(--shadow), 0 0 0 1px rgba(216, 11, 114, 0.45);
}

.hud {
  min-height: 66px;
  display: grid;
  grid-template-columns: 1.25fr repeat(3, 1fr) auto;
  align-items: stretch;
  background: linear-gradient(180deg, var(--white), var(--paper));
  color: var(--ink);
  box-shadow: inset 0 -4px 0 rgba(112, 95, 54, 0.2);
}

.hud-stat {
  min-width: 0;
  padding: 10px 13px 8px;
  border-right: 2px solid var(--ink);
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: inset 0 -3px 0 rgba(23, 23, 17, 0.08);
}

.hud-stat:nth-child(2) strong {
  color: #3d692f;
}

.hud-stat:nth-child(3) strong {
  color: #7a3f12;
}

.hud-stat span {
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.12em;
}

.hud-stat strong {
  overflow: hidden;
  font-family: "Courier New", ui-monospace, monospace;
  font-size: clamp(1rem, 2.2vw, 1.35rem);
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.75);
}

.hud-energy strong {
  color: var(--magenta);
  letter-spacing: 0.08em;
}

.hud-actions {
  display: flex;
  align-items: stretch;
}

.hud-button {
  min-height: 100%;
  padding: 8px 12px;
  border-left: 2px solid var(--ink);
  background: linear-gradient(180deg, #e9b957, var(--ochre));
  color: var(--ink);
  font-size: 0.875rem;
}

.hud-button:hover {
  background: var(--white);
}

.stage {
  position: relative;
  width: 100%;
  overflow: hidden;
  background: #7f7a47;
  aspect-ratio: 19 / 13;
  box-shadow: inset 0 0 0 2px rgba(23, 23, 17, 0.6);
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  background: #7f7a47;
}

.toast {
  position: absolute;
  z-index: 4;
  top: 16px;
  left: 50%;
  max-width: calc(100% - 24px);
  padding: 8px 14px;
  border: 2px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  font-size: clamp(0.82rem, 1.8vw, 1rem);
  font-weight: 900;
  letter-spacing: 0.04em;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -10px);
  transition: opacity 120ms ease, transform 120ms ease;
  white-space: nowrap;
  box-shadow: 5px 5px 0 rgba(23, 23, 17, 0.45);
}

.toast-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.screen {
  position: absolute;
  z-index: 8;
  inset: 0;
  padding: clamp(12px, 3vw, 30px);
  display: none;
  place-items: center;
  overflow-y: auto;
  background:
    linear-gradient(rgba(23, 23, 17, 0.78), rgba(23, 23, 17, 0.88)),
    repeating-linear-gradient(90deg, transparent 0 47px, rgba(246, 237, 207, 0.045) 47px 48px),
    repeating-linear-gradient(0deg, transparent 0 47px, rgba(246, 237, 207, 0.045) 47px 48px);
  backdrop-filter: blur(2px);
}

.screen-active {
  display: grid;
}

.screen-card {
  position: relative;
  width: min(600px, 100%);
  padding: clamp(20px, 4vw, 38px);
  border: 3px solid var(--paper);
  background:
    linear-gradient(135deg, rgba(216, 11, 114, 0.09), transparent 32%),
    rgba(23, 23, 17, 0.97);
  color: var(--paper);
  box-shadow: 10px 10px 0 rgba(0, 0, 0, 0.45);
  text-align: center;
  animation: screen-enter 150ms ease-out;
}

.screen-card::before {
  content: "";
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 7px;
  background: linear-gradient(90deg, var(--ochre) 0 33%, var(--magenta) 33% 66%, var(--paper) 66%);
}

.screen-card h2 {
  margin: 0 0 12px;
  color: var(--white);
  font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
  font-size: clamp(2rem, 6vw, 4.4rem);
  letter-spacing: 0.035em;
  line-height: 0.98;
  text-transform: uppercase;
}

.screen-card > p:not(.screen-kicker):not(.final-line):not(.record-message) {
  max-width: 42rem;
  margin: 0 auto 20px;
  font-size: clamp(1rem, 2.1vw, 1.18rem);
  line-height: 1.48;
}

.menu-card {
  position: relative;
  padding-top: clamp(54px, 8vw, 74px);
}

.pixel-dog {
  position: absolute;
  top: -37px;
  left: 50%;
  width: 86px;
  height: 74px;
  transform: translateX(-50%);
}

.dog-face {
  position: absolute;
  inset: 10px 13px 7px;
  border: 4px solid var(--paper);
  background: #111;
  box-shadow: inset 0 -10px #27271f;
}

.dog-face::before,
.dog-face::after {
  content: "";
  position: absolute;
  top: 16px;
  width: 7px;
  height: 7px;
  background: var(--paper);
}

.dog-face::before { left: 12px; }
.dog-face::after { right: 12px; }

.dog-face i {
  position: absolute;
  left: 50%;
  top: 29px;
  width: 16px;
  height: 11px;
  background: #716958;
  transform: translateX(-50%);
}

.dog-ear {
  position: absolute;
  top: 8px;
  width: 24px;
  height: 48px;
  border: 4px solid var(--paper);
  background: #0d0d0b;
}

.dog-ear-left { left: 0; transform: rotate(8deg); }
.dog-ear-right { right: 0; transform: rotate(-8deg); }
.dog-bandana {
  position: absolute;
  right: 9px;
  bottom: 0;
  width: 28px;
  height: 12px;
  background: var(--magenta);
  clip-path: polygon(0 0, 100% 0, 78% 100%, 10% 72%);
}

.screen-actions {
  width: min(390px, 100%);
  margin: 20px auto 0;
  display: grid;
  gap: 9px;
}

.primary-button,
.secondary-button,
.text-button {
  width: 100%;
  padding: 11px 16px;
}

.primary-button {
  border: 2px solid var(--magenta-light);
  background: linear-gradient(180deg, var(--magenta-light), var(--magenta) 48%);
  color: var(--white);
  box-shadow: 4px 4px 0 #73053e;
}

.primary-button:hover {
  background: var(--magenta-light);
  color: var(--ink);
}

.secondary-button {
  border: 2px solid var(--paper);
  background: transparent;
  color: var(--paper);
}

.secondary-button:hover {
  background: var(--paper);
  color: var(--ink);
}

.text-button {
  background: transparent;
  color: var(--paper-dim);
  text-decoration: underline;
  text-underline-offset: 4px;
}

.records {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid rgba(246, 237, 207, 0.3);
  display: flex;
  justify-content: center;
  gap: 24px;
  color: var(--paper-dim);
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.records strong {
  display: block;
  margin-top: 3px;
  color: var(--white);
  font-family: "Courier New", ui-monospace, monospace;
  font-size: 1rem;
}

.instruction-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  text-align: left;
}

.instruction-grid div {
  padding: 14px;
  border: 2px solid rgba(246, 237, 207, 0.35);
  background: rgba(246, 237, 207, 0.06);
}

.instruction-grid strong {
  color: var(--ochre);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
}

.instruction-grid p {
  margin: 6px 0 0;
  font-size: 0.9rem;
  line-height: 1.35;
}

.legend-list {
  width: min(410px, 100%);
  margin: 18px auto;
  padding: 0;
  display: grid;
  gap: 6px;
  list-style: none;
  text-align: left;
}

.legend-list li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.legend-symbol {
  width: 28px;
  height: 28px;
  display: inline-grid;
  place-items: center;
  border: 2px solid currentColor;
  font-weight: 900;
}

.animal-symbol { color: var(--ochre); }
.base-symbol { color: var(--safe); }
.danger-symbol { color: var(--danger); }
.pedestrian-symbol { color: var(--pedestrian); }

.compact-card {
  width: min(420px, 100%);
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border: 2px solid var(--paper-dim);
}

.result-grid span {
  min-width: 0;
  padding: 12px 7px;
  border-right: 1px solid var(--paper-dim);
  color: var(--paper-dim);
  font-size: 0.66rem;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.result-grid span:last-child { border-right: 0; }
.result-grid strong {
  display: block;
  margin-top: 5px;
  overflow: hidden;
  color: var(--white);
  font-family: "Courier New", ui-monospace, monospace;
  font-size: clamp(0.95rem, 2.4vw, 1.25rem);
  text-overflow: ellipsis;
}

.record-message {
  margin: 12px 0 0;
  color: var(--ochre);
  font-weight: 900;
}

.record-message[hidden] { display: none; }

.final-line {
  margin: 16px 0 0;
  color: var(--white);
  font-size: clamp(1.05rem, 2.4vw, 1.35rem);
  font-weight: 900;
}

.horizontal-actions {
  grid-template-columns: 2fr 1fr;
}

.touch-controls {
  padding: 12px;
  display: none;
  grid-template-columns: repeat(3, 60px);
  grid-template-rows: repeat(3, 54px);
  justify-content: center;
  gap: 5px;
  border-top: 3px solid var(--paper);
  background: #201f18;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.direction-button {
  min-height: 54px;
  border: 2px solid var(--paper-dim);
  background: linear-gradient(180deg, #454233, #2b2a20);
  color: var(--white);
  font-size: 1.35rem;
  box-shadow: 3px 3px 0 #070706;
}

.direction-button.is-pressed,
.direction-button:active {
  background: var(--magenta);
  border-color: var(--magenta-light);
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 #070706;
}

.direction-up { grid-column: 2; grid-row: 1; }
.direction-left { grid-column: 1; grid-row: 2; }
.control-center { grid-column: 2; grid-row: 2; border: 2px solid #3c3a2b; }
.direction-right { grid-column: 3; grid-row: 2; }
.direction-down { grid-column: 2; grid-row: 3; }

.game-footer {
  padding-top: 9px;
  color: var(--paper-dim);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.08em;
}

@media (pointer: coarse), (max-width: 760px) {
  body.game-active {
    overflow: hidden;
    overscroll-behavior: none;
  }

  .game-shell {
    min-height: 100dvh;
    padding: 7px 7px max(7px, env(safe-area-inset-bottom));
    justify-content: flex-start;
  }

  .game-header {
    min-height: 38px;
    margin-bottom: 6px;
    align-items: center;
  }

  .game-header h1 {
    font-size: clamp(1.55rem, 7.5vw, 2.2rem);
  }

  .eyebrow,
  .prototype-badge,
  .game-footer {
    display: none;
  }

  .game-panel {
    border-width: 2px;
  }

  .hud {
    min-height: 82px;
    grid-template-columns: repeat(4, 1fr);
  }

  .hud-stat {
    padding: 7px 6px 5px;
    border-right-width: 1px;
    text-align: center;
  }

  .hud-stat:nth-child(4) {
    border-right: 0;
  }

  .hud-stat span {
    font-size: 0.75rem;
  }

  .hud-stat strong {
    font-size: 1rem;
  }

  .hud-actions {
    grid-column: 1 / -1;
    min-height: 38px;
    border-top: 1px solid var(--ink);
  }

  .hud-button {
    width: 50%;
    min-height: 38px;
    border-left: 0;
    font-size: 0.875rem;
  }

  .hud-button + .hud-button {
    border-left: 1px solid var(--ink);
  }

  .stage {
    aspect-ratio: 19 / 13;
  }

  body:not(.game-active) .stage {
    min-height: min(68dvh, 520px);
    aspect-ratio: auto;
  }

  .touch-controls {
    display: grid;
  }

  body:not(.game-active) .touch-controls {
    display: none;
  }

  .screen {
    padding: 8px;
  }

  .screen-card {
    padding: 16px;
    border-width: 2px;
    box-shadow: 5px 5px 0 rgba(0, 0, 0, 0.45);
  }

  .menu-card {
    padding-top: 36px;
  }

  .pixel-dog {
    top: -27px;
    transform: translateX(-50%) scale(0.72);
  }

  .screen-card h2 {
    margin-bottom: 7px;
    font-size: clamp(1.6rem, 9vw, 2.7rem);
  }

  .screen-card > p:not(.screen-kicker):not(.final-line):not(.record-message) {
    margin-bottom: 9px;
    font-size: 0.88rem;
    line-height: 1.32;
  }

  .screen-kicker {
    margin-bottom: 2px;
    font-size: 0.62rem;
  }

  .screen-actions {
    margin-top: 10px;
    gap: 6px;
  }

  .screen-actions button {
    min-height: 40px;
    padding: 8px 12px;
    font-size: 0.875rem;
  }

  .records {
    margin-top: 9px;
    padding-top: 8px;
    font-size: 0.62rem;
  }

  .instruction-grid {
    gap: 5px;
  }

  .instruction-grid div {
    padding: 8px;
  }

  .instruction-grid p {
    font-size: 0.875rem;
  }

  .legend-list {
    margin: 8px auto;
    gap: 3px;
    font-size: 0.875rem;
  }

  .legend-symbol {
    width: 22px;
    height: 22px;
  }

  .result-grid span {
    padding: 7px 3px;
    font-size: 0.75rem;
  }

  .result-grid strong {
    font-size: 0.92rem;
  }

  .record-message,
  .final-line {
    margin-top: 7px;
    font-size: 0.88rem;
  }
}

@media (max-width: 380px) {
  .game-header h1 { font-size: 1.5rem; }
  .hud { min-height: 74px; }
  .touch-controls {
    padding: 6px;
    grid-template-columns: repeat(3, 54px);
    grid-template-rows: repeat(3, 47px);
  }
  .direction-button { min-height: 47px; }
}

@media (max-height: 700px) and (pointer: coarse) {
  .touch-controls {
    grid-template-columns: repeat(3, 50px);
    grid-template-rows: repeat(3, 42px);
    padding: 5px;
  }
  .direction-button { min-height: 42px; }
  .game-header { min-height: 30px; }
}

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

@keyframes screen-enter {
  from {
    opacity: 0;
    transform: translateY(7px) scale(0.99);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
