:root {
  color-scheme: dark;
  --bg: #080c18;
  --panel: #0e1628;
  --surface: #141e34;
  --border: rgba(255, 255, 255, .12);
  --text: #f6f8ff;
  --muted: #9aa9c1;
  --green: #5ee6a8;
  --green-dark: #0b8f68;
  --red: #ff6d83;
  --yellow: #ffd166;
  --blue: #6ecbff;
  font-family: Inter, system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-width: 300px; min-height: 100%; }
body {
  color: var(--text);
  background: radial-gradient(circle at 18% 0, rgba(94, 230, 168, .12), transparent 28rem), var(--bg);
}
button, input, select { font: inherit; }
button, input[type=text], select {
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: .65rem;
  background: rgba(255, 255, 255, .055);
}
button { padding: .58rem .72rem; cursor: pointer; font-weight: 750; }
button:hover { background: rgba(255, 255, 255, .11); }
button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 2px;
}
h1, h2, p { margin: 0; }
header {
  display: flex;
  height: 4.5rem;
  padding: .7rem 1.15rem;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: rgba(8, 12, 24, .9);
}
.eyebrow {
  color: var(--green);
  font-size: .64rem;
  font-weight: 850;
  letter-spacing: .12em;
  text-transform: uppercase;
}
#headerStatus { color: var(--muted); font-size: .75rem; }
main {
  display: grid;
  height: calc(100vh - 4.5rem);
  min-height: 0;
  grid-template-columns: minmax(0, 1fr) 22rem;
}
.workspace {
  display: grid;
  min-width: 0;
  min-height: 0;
  padding: .75rem;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: .7rem;
}
.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: .6rem;
}
.metrics article {
  padding: .55rem .75rem;
  border: 1px solid var(--border);
  border-radius: .75rem;
  background: var(--surface);
}
.metrics span { display: block; color: var(--muted); font-size: .65rem; }
.metrics strong { display: block; margin-top: .1rem; font-size: 1rem; }
.board-stage {
  position: relative;
  display: grid;
  min-height: 0;
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 1rem;
  background: #090f1d;
  box-shadow: 0 1rem 3rem rgba(0, 0, 0, .22);
}
canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  border-radius: .6rem;
  touch-action: none;
}
.game-message {
  position: absolute;
  display: grid;
  width: min(22rem, calc(100% - 2rem));
  padding: 1.15rem;
  gap: .55rem;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, .2);
  border-radius: 1rem;
  background: rgba(9, 15, 29, .82);
  backdrop-filter: blur(8px);
  box-shadow: 0 1rem 3rem rgba(0, 0, 0, .35);
}
.game-message[hidden] { display: none; }
.game-message strong { font-size: 1.35rem; }
.game-message span { color: var(--muted); font-size: .8rem; }
.game-message button { margin-top: .3rem; color: #062016; background: var(--green); }
.board-footer, .legend {
  display: flex;
  align-items: center;
  gap: .7rem;
}
.board-footer {
  min-width: 0;
  justify-content: space-between;
  color: var(--muted);
  font-size: .68rem;
}
.legend { flex-wrap: wrap; }
.legend span { white-space: nowrap; }
.legend span::before {
  display: inline-block;
  width: .62rem;
  height: .62rem;
  margin-right: .28rem;
  border-radius: .2rem;
  content: "";
}
.snake-key::before { background: var(--green); }
.food-key::before { border-radius: 50% !important; background: var(--red); }
.obstacle-key::before { background: #68758d; }
.path-key::before { background: var(--blue); }
#aiState {
  max-width: 48%;
  overflow: hidden;
  color: var(--blue);
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#controls {
  height: 100%;
  padding: .8rem;
  overflow: auto;
  border-left: 1px solid var(--border);
  background: var(--panel);
}
.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .5rem;
}
.primary { color: #062016; background: linear-gradient(135deg, var(--green), #8ef0c4); }
#controls section {
  margin-top: .7rem;
  padding: .7rem;
  border: 1px solid var(--border);
  border-radius: .8rem;
  background: rgba(255, 255, 255, .025);
}
#controls h2 { margin-bottom: .55rem; font-size: .82rem; }
#controls label {
  display: grid;
  margin-top: .55rem;
  gap: .3rem;
  color: var(--muted);
  font-size: .68rem;
}
#controls label:first-of-type { margin-top: 0; }
#controls output { float: right; color: var(--green); font-weight: 800; }
select, input[type=text] { width: 100%; min-height: 2.25rem; padding: .4rem .5rem; }
input[type=range] { width: 100%; accent-color: var(--green); }
.check { display: flex !important; align-items: center; gap: .45rem !important; }
.seed-row { display: grid; grid-template-columns: 1fr auto; gap: .4rem; }
.instructions p { color: var(--muted); font-size: .68rem; line-height: 1.5; }
kbd {
  padding: .08rem .28rem;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: .3rem;
  background: rgba(255, 255, 255, .08);
}
.dpad { display: none; }

@media (max-width: 48rem) {
  header { height: 4rem; }
  main { display: flex; height: auto; min-height: calc(100vh - 4rem); flex-direction: column; }
  .workspace { min-height: calc(100vh - 4rem); order: 1; grid-template-rows: auto minmax(18rem, 1fr) auto auto; }
  .metrics { gap: .35rem; }
  .metrics article { padding: .45rem .5rem; }
  .board-stage { min-height: min(78vw, 30rem); }
  .board-footer { align-items: flex-start; flex-direction: column; }
  #aiState { max-width: 100%; text-align: left; }
  #controls { height: auto; order: 2; border-top: 1px solid var(--border); border-left: 0; }
  .dpad {
    display: grid;
    width: 11rem;
    margin: 0 auto;
    grid-template-columns: repeat(3, 1fr);
    gap: .35rem;
  }
  .dpad button { min-height: 2.7rem; font-size: 1.15rem; }
  .dpad button:nth-child(1) { grid-column: 2; }
  .dpad button:nth-child(2) { grid-column: 1; grid-row: 2; }
  .dpad button:nth-child(3) { grid-column: 2; grid-row: 2; }
  .dpad button:nth-child(4) { grid-column: 3; grid-row: 2; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
