:root {
  --bg: #171717;
  --panel: #202020;
  --grid: #111111;
  --cell: #2a2a2a;
  --text: #f7f7f7;
  --muted: #b8b8b8;
  --gold: #ffb700;
  --gold-soft: rgba(255, 183, 0, 0.14);
  --button: #ffb700;
  --button-text: #171717;
  --cyan: #41d6ff;
  --blue: #5a7dff;
  --orange: #ff9a3d;
  --yellow: #ffe45c;
  --green: #60e36e;
  --purple: #c77dff;
  --red: #ff5c5c;
}

* {
  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:
    radial-gradient(circle at 15% 12%, rgba(255, 183, 0, 0.08), transparent 28%),
    radial-gradient(circle at 86% 85%, rgba(65, 214, 255, 0.07), transparent 30%),
    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%;
  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;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.eyebrow,
.help,
.stat__label,
.mini-stats span {
  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;
}

.title-row h1 {
  margin: 0;
  font-size: 20px;
}

.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,
.mini-stats strong {
  color: var(--gold);
}

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: 0.86rem;
  line-height: 1;
  font-weight: 800;
}

.game-shell {
  width: 100%;
  min-height: 0;
  margin-top: 18px;
  display: grid;
  grid-template-columns: minmax(0, auto) minmax(116px, 170px);
  justify-content: center;
  align-items: start;
  gap: 14px;
}

.board {
  --cols: 10;
  --rows: 20;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  gap: 3px;
  aspect-ratio: var(--cols) / var(--rows);
  height: min(calc(100dvh - 190px), 78dvh);
  max-width: calc(100vw - 190px);
  align-self: center;
  background: var(--grid);
  padding: 3px;
  border-radius: 16px;
  position: relative;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  box-shadow:
    inset 0 0 0 1px rgba(255, 183, 0, 0.16),
    0 20px 45px rgba(0, 0, 0, 0.35);
}

.cell {
  background: var(--cell);
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.025);
}

.cell--ghost {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.24);
}

.cell--filled,
.next-cell--filled {
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.22),
    inset 0 -7px 0 rgba(0, 0, 0, 0.16);
}

.cell--I,
.next-cell--I { background: var(--cyan); }
.cell--J,
.next-cell--J { background: var(--blue); }
.cell--L,
.next-cell--L { background: var(--orange); }
.cell--O,
.next-cell--O { background: var(--yellow); }
.cell--S,
.next-cell--S { background: var(--green); }
.cell--T,
.next-cell--T { background: var(--purple); }
.cell--Z,
.next-cell--Z { background: var(--red); }
.cell--garbage { background: #626262; }

.side-card {
  min-width: 0;
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid rgba(255, 183, 0, 0.18);
  border-radius: 16px;
  background: rgba(17, 17, 17, 0.45);
}

.next-piece {
  width: 112px;
  height: 112px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 3px;
  padding: 3px;
  border-radius: 12px;
  background: var(--grid);
}

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

.mini-stats {
  display: grid;
  gap: 8px;
}

.mini-stats div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

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

.controls {
  margin-top: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.controls__row {
  gap: 10px;
}

.control-button {
  min-width: 54px;
  min-height: 54px;
  display: inline-grid;
  place-items: center;
  padding: 0 14px;
  border-radius: 16px;
  font-weight: 800;
  line-height: 1;
}

.control-button--arrow {
  min-width: 62px;
  min-height: 62px;
  font-size: 1.45rem;
}

.control-button--utility {
  border: 1px solid rgba(255, 183, 0, 0.22);
  background: rgba(255, 183, 0, 0.08);
  color: var(--text);
  font-size: 1.3rem;
}

.control-button--drop {
  min-width: 82px;
  letter-spacing: 0.06em;
}

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

.overlay.hidden,
.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(20% - 2px) calc(20% - 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--classic::before {
  width: 26px;
  height: 26px;
  left: 10px;
  top: 10px;
  background: var(--yellow);
  box-shadow: 0 13px 0 var(--yellow), 13px 13px 0 var(--yellow), 26px 13px 0 var(--yellow);
}

.mode-preview--classic::after {
  width: 13px;
  height: 39px;
  right: 14px;
  bottom: 12px;
  background: var(--cyan);
}

.mode-preview--sprint::before {
  width: 46px;
  height: 10px;
  left: 10px;
  top: 18px;
  background: var(--cyan);
  box-shadow: -7px 0 0 rgba(65, 214, 255, 0.28), -14px 0 0 rgba(65, 214, 255, 0.12);
}

.mode-preview--sprint::after {
  width: 44px;
  height: 10px;
  left: 14px;
  bottom: 16px;
  background: var(--gold);
}

.mode-preview--zen::before {
  width: 28px;
  height: 14px;
  left: 22px;
  top: 28px;
  background: var(--green);
  box-shadow: -14px 14px 0 var(--green), 14px -14px 0 rgba(255, 255, 255, 0.2);
}

.mode-preview--zen::after {
  width: 58px;
  height: 58px;
  left: 7px;
  top: 7px;
  border: 1px solid rgba(255, 183, 0, 0.38);
  background: transparent;
}

.mode-preview--garbage::before {
  width: 50px;
  height: 28px;
  left: 11px;
  bottom: 10px;
  background: #626262;
  box-shadow: inset 18px 0 0 #111111;
}

.mode-preview--garbage::after {
  width: 26px;
  height: 13px;
  right: 14px;
  top: 12px;
  background: var(--orange);
  box-shadow: 13px 13px 0 var(--orange);
}

.mode-preview--cascade::before {
  width: 13px;
  height: 39px;
  right: 14px;
  top: 9px;
  background: var(--purple);
  box-shadow: -17px 12px 0 rgba(199, 125, 255, 0.42), -34px 24px 0 rgba(199, 125, 255, 0.18);
}

.mode-preview--cascade::after {
  width: 42px;
  height: 9px;
  left: 15px;
  bottom: 10px;
  background: var(--red);
}

.mode-preview--tiny {
  box-shadow:
    inset 0 0 0 11px #171717,
    inset 0 0 0 12px rgba(255, 183, 0, 0.2);
}

.mode-preview--tiny::before {
  width: 13px;
  height: 39px;
  left: 29px;
  top: 15px;
  background: var(--cyan);
}

.mode-preview--tiny::after {
  width: 28px;
  height: 14px;
  left: 22px;
  bottom: 16px;
  background: var(--green);
}

@media (max-width: 760px) {
  .panel {
    padding: 16px;
  }

  .panel__header {
    align-items: flex-start;
  }

  .stat--level,
  .side-card {
    display: none;
  }

  .game-shell {
    display: flex;
    justify-content: center;
    margin-top: 12px;
  }

  .board {
    height: min(calc(100dvh - 205px), 76dvh);
    max-width: calc(100vw - 32px);
  }

  .help {
    display: none;
  }

  .controls {
    width: min(100%, 430px);
    display: grid;
    grid-template-columns: 64px minmax(0, 1fr) 86px;
    align-items: center;
    justify-content: center;
    column-gap: 18px;
    align-self: center;
  }

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

  .control-button {
    width: 100%;
    min-width: 0;
    min-height: 58px;
    border-radius: 14px;
  }

  .control-button--arrow {
    min-width: 0;
    min-height: 62px;
    font-size: 1.55rem;
  }

  .control-button--utility {
    min-height: 62px;
    font-size: 1.45rem;
  }

  .control-button--drop {
    min-width: 0;
    min-height: 62px;
    font-size: 0.86rem;
    box-shadow: 0 0 0 1px rgba(255, 183, 0, 0.24), 0 10px 22px rgba(0, 0, 0, 0.22);
  }

  .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: 900px) and (orientation: landscape) {
  .panel {
    display: grid;
    grid-template-columns: minmax(0, calc(100dvh * 0.5)) clamp(245px, 26vw, 340px);
    grid-template-rows: auto auto auto minmax(0, 1fr);
    grid-template-areas:
      "board header"
      "board side"
      "board help"
      "board controls";
    align-items: start;
    justify-content: center;
    gap: 18px 24px;
  }

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

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

  .game-shell {
    grid-area: board;
    margin-top: 0;
    display: contents;
  }

  .board {
    grid-area: board;
    height: calc(100dvh - 52px);
    max-width: 100%;
  }

  .side-card {
    grid-area: side;
    width: 100%;
    align-self: start;
  }

  .next-piece {
    width: 96px;
    height: 96px;
  }

  .help {
    grid-area: help;
    margin: 0;
  }

  .controls {
    grid-area: controls;
    justify-content: flex-start;
    align-self: start;
    margin-top: 0;
  }
}

@media (max-width: 430px) {
  .controls {
    grid-template-columns: 52px minmax(0, 1fr) 70px;
    column-gap: 12px;
  }

  .controls__row {
    gap: 6px;
  }

  .control-button {
    min-width: 0;
    min-height: 50px;
    padding: 0 10px;
  }

  .control-button--arrow {
    min-width: 0;
    min-height: 56px;
    font-size: 1.38rem;
  }

  .control-button--drop {
    min-width: 0;
    min-height: 56px;
    font-size: 0.78rem;
  }
}
