:root {
  color: #17201c;
  background: #f6f8f5;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  font-size: 16px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(231, 240, 230, 0.85), rgba(249, 250, 247, 0.92)),
    #f6f8f5;
}

button,
input,
select,
textarea {
  font: inherit;
  min-width: 0;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.75rem;
  border: 0;
  border-radius: 6px;
  padding: 0 1rem;
  background: #23553e;
  color: #ffffff;
  font-weight: 700;
  line-height: 1.15;
  overflow-wrap: anywhere;
  text-align: center;
  transition:
    background 120ms ease,
    transform 120ms ease;
}

button:not(:disabled):hover {
  background: #1a4330;
}

button:not(:disabled):active {
  transform: translateY(1px);
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.game-board:focus-visible {
  outline: 3px solid #d94a2b;
  outline-offset: 3px;
}

.game-setting:focus-within,
.reset-confirm:focus-within {
  border-radius: 6px;
  box-shadow: 0 0 0 3px rgba(217, 74, 43, 0.18);
}

button:disabled {
  cursor: not-allowed;
  background: #d6dfda;
  color: #3f5148;
}

[hidden] {
  display: none !important;
}

.app-shell {
  display: grid;
  min-height: 100vh;
  padding: clamp(1rem, 3vw, 2rem);
  place-items: center;
}

.game-panel {
  width: min(100%, 44rem);
  border: 1px solid #d8e1dc;
  border-radius: 8px;
  padding: clamp(1rem, 2.5vw, 1.5rem);
  background: #ffffff;
  box-shadow: 0 20px 60px rgba(34, 51, 42, 0.12);
}

.game-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 1rem;
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 7vw, 3.5rem);
  line-height: 1;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.score-panel {
  display: grid;
  grid-template-columns: repeat(2, minmax(4.5rem, 1fr));
  gap: 0.75rem;
  margin: 0;
}

.score-panel div {
  min-width: 4.5rem;
  border: 1px solid #d8e1dc;
  border-radius: 6px;
  padding: 0.6rem 0.75rem;
  background: #f8faf7;
  text-align: right;
}

.score-panel dt {
  color: #64766f;
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.score-panel dd {
  margin: 0.15rem 0 0;
  color: #17201c;
  font-size: 1.35rem;
  font-weight: 800;
}

.status-row {
  display: grid;
  gap: 0.35rem;
  justify-items: center;
  width: 100%;
  align-items: center;
  min-height: 2.5rem;
  margin: 1rem 0;
  border-block: 1px solid #e3e9e5;
  padding: 0.35rem 0;
}

.status-row p {
  margin: 0;
  color: #43544d;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.mode-status {
  color: #34496a;
  font-size: 0.92rem;
  max-width: 100%;
  text-align: center;
  text-wrap: balance;
}

.game-panel[data-status="game-over"] .status-row {
  border-color: #edc0ae;
  background: #fff5f1;
}

.game-panel[data-status="game-over"] #game-status {
  color: #8c2f1f;
}

.game-panel[data-status="paused"] .status-row {
  border-color: #cbd7e6;
  background: #f4f7fb;
}

.game-panel[data-status="paused"] #game-status {
  color: #34496a;
}

.board-frame {
  width: min(100%, 560px);
  margin: 0 auto;
}

.game-board {
  display: grid;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border: 2px solid #17201c;
  border-radius: 8px;
  background: #e9efe5;
  grid-template-columns: repeat(var(--board-width, var(--board-size, 20)), 1fr);
  grid-template-rows: repeat(var(--board-height, var(--board-size, 20)), 1fr);
}

.board-cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid rgba(94, 118, 106, 0.14);
  border-bottom: 1px solid rgba(94, 118, 106, 0.14);
  background: #f4f7f0;
}

.board-cell--snake {
  border-color: rgba(255, 255, 255, 0.2);
  background: #2d7654;
}

.board-cell--snake-head {
  background: #163728;
}

.game-panel[data-status="game-over"] .board-cell--snake-head {
  background: #8c2f1f;
}

.board-cell--food::after {
  position: absolute;
  inset: 18%;
  border-radius: 999px;
  background: #d94a2b;
  box-shadow: inset 0 -3px 0 rgba(87, 23, 14, 0.28);
  content: "";
}

.board-cell--obstacle {
  background: #3f4652;
}

.board-cell--obstacle::after {
  position: absolute;
  inset: 22%;
  border-radius: 3px;
  background: #1f252d;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.16);
  content: "";
}

.game-actions {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 0.75rem;
  width: 100%;
  margin: 1rem auto 0;
  max-width: 38rem;
}

.game-actions > button {
  grid-column: span 2;
}

.setup-status {
  grid-column: 1 / -1;
  margin: 0;
  color: #43544d;
  font-weight: 700;
  overflow-wrap: anywhere;
  text-align: center;
}

.game-setting {
  display: grid;
  grid-column: span 3;
  grid-template-columns: auto minmax(8rem, 1fr);
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
  color: #43544d;
  font-weight: 800;
}

.game-setting span {
  font-size: 0.78rem;
  text-transform: uppercase;
  white-space: nowrap;
}

.game-setting select {
  width: 100%;
  min-width: 0;
  min-height: 2.75rem;
  border: 1px solid #b8c8bf;
  border-radius: 6px;
  padding: 0 0.75rem;
  background: #ffffff;
  color: #17201c;
  font-weight: 700;
}

.game-setting--toggle {
  grid-template-columns: auto minmax(2.75rem, auto);
  justify-content: center;
}

.game-setting--toggle input {
  width: 1.25rem;
  height: 1.25rem;
  margin: 0;
  accent-color: #23553e;
}

.game-summary {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
  gap: 1rem;
  margin-top: 1rem;
  border-top: 1px solid #e3e9e5;
  padding-top: 1rem;
}

.game-summary h2 {
  margin: 0 0 0.65rem;
  color: #64766f;
  font-size: 0.78rem;
  line-height: 1.2;
  text-transform: uppercase;
}

.session-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.65rem;
  margin: 0;
}

.session-stats div {
  min-width: 0;
  border-bottom: 1px solid #e3e9e5;
  padding-bottom: 0.45rem;
}

.session-stats dt {
  color: #64766f;
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
}

.session-stats dd {
  margin: 0.15rem 0 0;
  color: #17201c;
  font-size: 1.15rem;
  font-weight: 800;
}

.recent-scores__empty {
  margin: 0;
  color: #64766f;
  font-weight: 700;
}

.recent-scores__list {
  display: grid;
  gap: 0.35rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.recent-score {
  border-bottom: 1px solid #e3e9e5;
  padding-bottom: 0.35rem;
  color: #43544d;
  font-weight: 700;
}

.local-data {
  display: grid;
  grid-column: 1 / -1;
  gap: 0.65rem;
  border-top: 1px solid #e3e9e5;
  padding-top: 1rem;
}

.local-data__header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 1rem;
}

.local-data__status {
  margin: 0;
  color: #64766f;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.local-data__export {
  width: 100%;
  min-height: 8rem;
  max-height: 14rem;
  border: 1px solid #b8c8bf;
  border-radius: 6px;
  padding: 0.75rem;
  resize: vertical;
  background: #fbfcfa;
  color: #17201c;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.85rem;
  line-height: 1.45;
}

.local-data__actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(8rem, auto) minmax(0, 1fr);
  gap: 0.75rem;
  align-items: center;
}

.reset-confirm {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-width: 0;
  min-height: 2.75rem;
  color: #43544d;
  font-weight: 800;
  overflow-wrap: anywhere;
  text-align: center;
}

.reset-confirm input {
  width: 1.1rem;
  height: 1.1rem;
  margin: 0;
  accent-color: #8c2f1f;
}

#reset-data-button:not(:disabled) {
  background: #8c2f1f;
}

#reset-data-button:not(:disabled):hover {
  background: #702317;
}

.touch-controls {
  display: none;
  grid-template-columns: repeat(3, minmax(3.25rem, 3.5rem));
  grid-template-rows: repeat(2, minmax(3.25rem, 3.5rem));
  gap: 0.5rem;
  justify-content: center;
  margin: 1rem auto 0;
  touch-action: manipulation;
}

.touch-control {
  width: 100%;
  min-height: 3.25rem;
  padding: 0;
  font-size: 1.5rem;
  line-height: 1;
}

.touch-controls__spacer {
  min-width: 0;
  min-height: 0;
}

@media (max-width: 560px) {
  .app-shell {
    align-items: start;
    padding: 0.75rem;
  }

  .game-panel {
    padding: 0.85rem;
  }

  .game-header {
    flex-direction: column;
  }

  .score-panel,
  .game-actions {
    max-width: none;
    width: 100%;
  }

  .game-summary {
    grid-template-columns: 1fr;
  }

  .game-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .game-actions > button {
    grid-column: span 1;
  }

  .game-setting,
  .setup-status {
    grid-column: 1 / -1;
  }

  .local-data__header,
  .local-data__actions {
    grid-template-columns: 1fr;
  }

  .local-data__header {
    display: grid;
  }

  .score-panel div {
    width: 100%;
    padding: 0.5rem 0.6rem;
  }

  .score-panel dd {
    font-size: 1.15rem;
  }

  .status-row {
    margin: 0.75rem 0;
  }

  .board-cell {
    border-width: 0;
  }

  .touch-controls {
    display: grid;
  }
}

@media (max-width: 380px) {
  button {
    padding-inline: 0.7rem;
  }

  .game-setting {
    grid-template-columns: 1fr;
    gap: 0.35rem;
  }

  .game-setting--toggle {
    grid-template-columns: minmax(0, 1fr) auto;
    justify-content: stretch;
  }

  .game-setting--toggle span {
    white-space: normal;
  }

  .reset-confirm {
    justify-content: start;
  }
}

@media (pointer: coarse) {
  .touch-controls {
    display: grid;
  }
}
