:root {
  --bg: #171717;
  --panel: #202020;
  --grid: #111111;
  --text: #f7f7f7;
  --muted: #b8b8b8;
  --snake: #ffb700;
  --snake-head: #ffd15c;
  --food: #ffffff;
  --button: #ffb700;
  --button-text: #171717;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  height: 100dvh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 0;
  overflow: hidden;
}

.panel {
  width: 100%;
  height: 100%;
  max-height: none;
  background: var(--panel);
  border: 1px solid rgba(255, 183, 0, 0.2);
  border-radius: 0;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
  padding: 24px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.panel__header,
.header-actions,
.controls__row,
.title-row {
  display: flex;
  align-items: center;
}

.panel__header {
  justify-content: space-between;
  gap: 16px;
}

.panel__header > div {
  min-width: 0;
  flex: 1;
}

.header-actions {
  flex: 0 0 auto;
  gap: 8px;
}

.eyebrow,
.help,
.stat__label {
  color: var(--muted);
}

.eyebrow {
  margin: 0 0 6px;
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.title-row {
  gap: 16px;
  justify-content: space-between;
  align-items: center;
}

.title-row h1 {
  margin: 0;
  font-size: clamp(1.8rem, 4vw, 2.8rem);
  font-size: 20px;
  margin:0;
}

.stat {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  padding: 5px 10px;
  border: 1px solid rgba(255, 183, 0, 0.18);
  border-radius: 999px;
  background: rgba(255, 183, 0, 0.08);
}

.stat__label {
  display: inline;
  font-size: 0.8rem;
  margin-bottom: 0;
}

.stat strong {
  color: var(--snake);
}

button {
  border: 0;
  border-radius: 999px;
  background: var(--button);
  color: var(--button-text);
  padding: 10px 16px;
  font: inherit;
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.55;
}

button:focus-visible {
  outline: 3px solid rgba(255, 183, 0, 0.35);
  outline-offset: 2px;
}

.secondary-button {
  border: 1px solid rgba(255, 183, 0, 0.22);
  background: rgba(255, 183, 0, 0.08);
  color: var(--text);
}

.icon-button {
  width: 38px;
  height: 38px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  font-size: 1.45rem;
  line-height: 1;
}

.board {
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 1fr));
  gap: 3px;
  aspect-ratio: 1;
  width: min(100%, calc(100dvh - 250px));
  max-height: 100%;
  align-self: center;
  background: var(--grid);
  padding: 3px;
  border-radius: 16px;
  position: relative;
  overflow: hidden;
}

.snake-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.cell {
  background: #2a2a2a;
  border-radius: 4px;
}

.cell--snake {
  background: var(--snake);
}

.cell--head {
  background: var(--snake-head);
}

.cell--food {
  background: var(--food);
}

.cell--obstacle {
  background: #5c5c5c;
  box-shadow: inset 0 0 0 1px rgba(255, 183, 0, 0.2);
}

.cell--poison {
  background: #ff4d4d;
  border-radius: 50%;
  transform: scale(0.78);
  box-shadow: 0 0 0 4px rgba(255, 77, 77, 0.1);
}

.cell--trail {
  background: rgba(255, 183, 0, 0.36);
  box-shadow: inset 0 0 0 1px rgba(255, 183, 0, 0.18);
}

.help {
  margin: 16px 0 0;
  line-height: 1.5;
}

.controls {
  margin-top: 20px;
  display: grid;
  justify-items: center;
  gap: 10px;
}

.controls__row {
  gap: 10px;
}

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

.overlay.hidden {
  display: none;
}

.hidden {
  display: none;
}

.modal {
  width: min(100%, 720px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 20px;
  border: 1px solid rgba(255, 183, 0, 0.24);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
  text-align: left;
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.modal h2 {
  margin: 2px 0 0;
}

.final-score {
  margin: 0 0 16px;
  color: var(--muted);
}

.final-score strong {
  color: var(--text);
}

.overlay-options {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0;
}

.language-menu {
  position: relative;
}

.flag-icon {
  width: 24px;
  height: 16px;
  display: block;
}

.language-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 174px;
  max-height: min(360px, calc(100dvh - 120px));
  overflow: auto;
  padding: 6px;
  border: 1px solid rgba(255, 183, 0, 0.18);
  border-radius: 12px;
  background: #202020;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.42);
  z-index: 20;
}

.language-option {
  width: 100%;
  min-height: 36px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  text-align: left;
  box-shadow: none;
}

.language-option:hover,
.language-option.active {
  background: rgba(255, 183, 0, 0.1);
}

.sound-toggle {
  border: 1px solid rgba(255, 183, 0, 0.22);
  background: rgba(255, 183, 0, 0.08);
  color: var(--text);
  box-shadow: none;
}

.sound-toggle[aria-pressed="true"] {
  background: var(--button);
  color: var(--button-text);
}

.mode-heading {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.mode-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.mode-button {
  width: 100%;
  border-radius: 12px;
  padding: 8px;
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  background: #272727;
  border: 1px solid rgba(255, 183, 0, 0.16);
  color: var(--text);
  text-align: left;
  transition: border-color 120ms ease, background 120ms ease, transform 120ms ease;
}

.mode-button:hover,
.mode-button:active {
  background: #303030;
  border-color: rgba(255, 183, 0, 0.46);
  transform: translateY(-1px);
}

.mode-copy,
.mode-copy span,
.mode-button small {
  display: block;
}

.mode-copy span {
  font-weight: 700;
}

.mode-button small {
  margin-top: 3px;
  color: var(--muted);
  line-height: 1.35;
}

.mode-preview {
  position: relative;
  width: 72px;
  aspect-ratio: 1;
  border-radius: 8px;
  background:
    linear-gradient(#2a2a2a 0 0) 0 0 / calc(25% - 2px) calc(25% - 2px),
    #111111;
  background-repeat: round;
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(255, 183, 0, 0.14);
}

.mode-preview::before,
.mode-preview::after {
  content: "";
  position: absolute;
  border-radius: 3px;
}

.mode-preview::before {
  background: var(--snake);
}

.mode-preview::after {
  background: var(--food);
}

.mode-preview--classic::before {
  width: 42px;
  height: 12px;
  left: 12px;
  top: 30px;
}

.mode-preview--classic::after {
  width: 10px;
  height: 10px;
  right: 12px;
  top: 12px;
  border-radius: 50%;
}

.mode-preview--portal {
  box-shadow:
    inset 0 0 0 2px rgba(255, 183, 0, 0.5),
    inset 0 0 0 6px rgba(255, 183, 0, 0.08);
}

.mode-preview--portal::before {
  width: 12px;
  height: 44px;
  left: 30px;
  top: 14px;
}

.mode-preview--portal::after {
  width: 8px;
  height: 8px;
  left: 8px;
  bottom: 8px;
  border-radius: 50%;
}

.mode-preview--rush::before {
  width: 46px;
  height: 10px;
  left: 10px;
  top: 31px;
  box-shadow: -7px 0 0 rgba(255, 183, 0, 0.35), -14px 0 0 rgba(255, 183, 0, 0.16);
}

.mode-preview--rush::after {
  width: 10px;
  height: 10px;
  right: 9px;
  bottom: 12px;
  border-radius: 50%;
}

.mode-preview--poison::before {
  width: 34px;
  height: 12px;
  left: 12px;
  top: 30px;
}

.mode-preview--poison::after {
  width: 9px;
  height: 9px;
  right: 12px;
  top: 12px;
  border-radius: 50%;
  box-shadow: -28px 34px 0 #ff4d4d, 8px 34px 0 #ff4d4d, -8px 18px 0 #ff4d4d;
}

.mode-preview--trail::before {
  width: 36px;
  height: 12px;
  left: 22px;
  top: 30px;
  box-shadow: -12px 0 0 rgba(255, 183, 0, 0.42), -24px 0 0 rgba(255, 183, 0, 0.2);
}

.mode-preview--trail::after {
  width: 10px;
  height: 10px;
  right: 12px;
  top: 12px;
  border-radius: 50%;
}

.mode-preview--tiny {
  background:
    linear-gradient(#2a2a2a 0 0) center / calc(16.66% - 2px) calc(16.66% - 2px),
    #111111;
  background-repeat: round;
  box-shadow:
    inset 0 0 0 12px #171717,
    inset 0 0 0 13px rgba(255, 183, 0, 0.2);
}

.mode-preview--tiny::before {
  width: 26px;
  height: 10px;
  left: 23px;
  top: 31px;
}

.mode-preview--tiny::after {
  width: 8px;
  height: 8px;
  right: 22px;
  top: 22px;
  border-radius: 50%;
}

.mode-preview--hunt::before {
  width: 38px;
  height: 12px;
  left: 10px;
  bottom: 16px;
}

.mode-preview--hunt::after {
  width: 10px;
  height: 10px;
  right: 10px;
  top: 10px;
  border-radius: 50%;
  box-shadow: -7px 7px 0 rgba(255, 255, 255, 0.28), -14px 14px 0 rgba(255, 255, 255, 0.12);
}

@media (max-width: 640px) {
  .modal {
    padding: 16px;
  }

  .modal-header {
    flex-direction: column;
    gap: 12px;
    margin-bottom: 12px;
  }

  .overlay-options {
    justify-content: flex-start;
  }

  .language-dropdown {
    left: 0;
    right: auto;
  }

  .mode-options {
    grid-template-columns: 1fr;
  }

  .mode-button {
    grid-template-columns: 54px minmax(0, 1fr);
    padding: 8px;
    gap: 9px;
  }

  .mode-preview {
    width: 54px;
  }

  .mode-button small {
    line-height: 1.2;
  }
}

@media (min-width: 721px) and (orientation: landscape) {
  .panel {
    display: grid;
    grid-template-columns: minmax(0, calc(100dvh - 52px)) clamp(220px, 24vw, 320px);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "board header"
      "board help";
    align-items: start;
    justify-content: center;
    gap: 20px 24px;
  }

  .panel__header {
    grid-area: header;
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
  }

  .panel__header > div {
    flex: 0 0 auto;
  }

  .title-row {
    justify-content: space-between;
  }

  .header-actions {
    justify-content: flex-start;
  }

  .board {
    grid-area: board;
    align-self: center;
    justify-self: center;
    width: 100%;
    max-width: calc(100dvh - 52px);
    aspect-ratio: 1;
  }

  .help {
    grid-area: help;
    margin: 0;
    align-self: end;
  }

  .controls {
    display: none;
  }
}

@media (max-width: 640px) {
  .app {
    height: 100dvh;
    padding: 0;
  }

  .panel {
    width: 100vw;
    height: 100dvh;
    min-height: 100dvh;
    max-height: 100dvh;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .panel__header {
    gap: 10px;
  }

  .title-row {
    justify-content: space-between;
    gap: 14px;
  }

  .stat {
    min-width: 58px;
  }

  h1 {
    font-size: 1.9rem;
  }

  button {
    padding: 8px 32px;
  }

  #pause-button {
    min-height: 34px;
    padding: 7px 12px;
  }

  .icon-button {
    width: 34px;
    height: 34px;
    font-size: 1.25rem;
  }

  .board {
    width: min(calc(100vw - 16px), max(180px, calc(100dvh - 190px)));
    max-width: none;
    flex: 0 1 auto;
    align-self: center;
    border-radius: 14px;
  }

  .help {
    display: none;
  }

  .controls {
    margin-top: 12px;
    flex-shrink: 0;
    gap: 8px;
  }

  .controls button {
    min-height: 65px;
  }
}

@media (max-width: 640px) and (max-height: 680px) {
  h1 {
    font-size: 1.7rem;
  }

  .eyebrow {
    display: none;
  }

  .controls {
    margin-top: 8px;
    gap: 8px;
  }

  .controls button {
    min-height: 34px;
    padding-block: 8px;
  }

  .board {
    width: min(calc(100vw - 16px), max(160px, calc(100dvh - 160px)));
  }
}
