:root {
  color-scheme: dark;
  --page: #0c0f13;
  --machine-light: #d9dfe3;
  --machine-mid: #9ca7af;
  --machine-dark: #606a72;
  --screen: #15191e;
  --screen-deep: #0c1014;
  --screen-line: rgba(190, 214, 226, 0.09);
  --text: #eef4f6;
  --muted: #95a2aa;
  --display: #b8f1dd;
  --cyan: #22d3ee;
  --yellow: #ffc928;
  --orange: #ff9f1c;
  --red: #f05252;
  --shadow: 0 18px 46px rgba(0, 0, 0, 0.48);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  display: flow-root;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background:
    radial-gradient(circle at 50% -15%, #30383f 0, #171c21 34%, transparent 60%),
    linear-gradient(145deg, #090b0e, var(--page));
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.game-active {
  overflow: hidden;
  touch-action: none;
}

button {
  color: inherit;
  font: inherit;
}

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

h2,
p {
  margin: 0;
}

.app-shell {
  position: relative;
  width: min(calc(100% - 20px), 620px);
  margin: 10px auto;
  padding: 12px;
  overflow: hidden;
  border: 1px solid #e7ecef;
  border-radius: 26px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.36), transparent 5%, transparent 94%, rgba(255, 255, 255, 0.2)),
    linear-gradient(180deg, var(--machine-light) 0%, var(--machine-mid) 48%, #b8c1c7 100%);
  box-shadow:
    0 1px 0 #ffffff inset,
    0 -2px 0 rgba(50, 58, 64, 0.55) inset,
    var(--shadow);
}

.app-shell::before {
  position: absolute;
  inset: 5px;
  z-index: 0;
  border: 1px solid rgba(61, 70, 77, 0.48);
  border-radius: 21px;
  pointer-events: none;
  content: "";
}

.app-shell > * {
  position: relative;
  z-index: 1;
}

.eyebrow,
.panel-label {
  color: var(--display);
  font-family: "Courier New", ui-monospace, monospace;
  font-size: 0.61rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.primary-button,
.controls button {
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.screen-bezel {
  padding: 8px;
  border: 2px solid #46515a;
  border-radius: 17px;
  background: linear-gradient(145deg, #252c32, #0d1115);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.32),
    0 0 0 3px rgba(51, 59, 66, 0.32),
    0 4px 12px rgba(0, 0, 0, 0.44) inset;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
  margin-bottom: 7px;
}

.stat-card {
  min-width: 0;
  padding: 4px 5px 3px;
  border: 1px solid #2c353c;
  border-radius: 5px;
  background: linear-gradient(180deg, #151b20, #0d1115);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.54) inset;
  text-align: center;
}

.stat-card span {
  display: block;
  overflow: hidden;
  color: #7e8a92;
  font-family: "Courier New", ui-monospace, monospace;
  font-size: 0.54rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}

.stat-card strong {
  display: block;
  overflow: hidden;
  color: var(--display);
  font-family: "Courier New", ui-monospace, monospace;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
  line-height: 1.1;
  text-overflow: ellipsis;
  text-shadow: 0 0 7px rgba(184, 241, 221, 0.18);
  white-space: nowrap;
}

.game-layout {
  display: grid;
  grid-template-columns: auto 106px;
  align-items: start;
  gap: 8px;
  justify-content: center;
}

.game-column {
  min-width: 0;
  width: 360px;
}

.board-wrap {
  position: relative;
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
  border: 2px solid #4a555d;
  border-radius: 7px;
  background: var(--screen);
  box-shadow:
    0 0 0 2px #080b0e,
    0 0 0 3px rgba(203, 218, 226, 0.15),
    0 8px 16px rgba(0, 0, 0, 0.5) inset;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 2;
  image-rendering: auto;
}

.side-panel {
  display: grid;
  align-content: start;
  gap: 7px;
}

.panel-card {
  min-width: 0;
  padding: 7px;
  border: 1px solid #39434a;
  border-radius: 6px;
  background: linear-gradient(180deg, #171c21, #0c1014);
  box-shadow: 0 2px 7px rgba(0, 0, 0, 0.5) inset;
}

.next-card {
  text-align: left;
}

.next-card .panel-label {
  display: block;
  padding-left: 2px;
}

#nextCanvas {
  display: block;
  width: 100%;
  height: auto;
  margin-top: 3px;
  border: 1px solid rgba(159, 175, 184, 0.12);
  border-radius: 3px;
}

.desktop-help p {
  margin-top: 5px;
  overflow: hidden;
  color: #859098;
  font-family: "Courier New", ui-monospace, monospace;
  font-size: 0.62rem;
  line-height: 1.05;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.overlay {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  padding: 12px;
  background: rgba(7, 10, 13, 0.72);
  backdrop-filter: blur(3px);
}

.overlay.visible {
  display: grid;
}

.overlay-card {
  width: min(100%, 250px);
  padding: 16px;
  border: 1px solid #59666e;
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(38, 45, 51, 0.97), rgba(15, 19, 23, 0.98));
  box-shadow: 0 0 0 2px #0b0e11, 0 10px 24px rgba(0, 0, 0, 0.48);
  text-align: center;
}

.overlay-card h2 {
  margin: 4px 0 7px;
  font-family: "Courier New", ui-monospace, monospace;
  font-size: clamp(1.18rem, 5vw, 1.6rem);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.overlay-card p:not(.eyebrow) {
  color: #aeb8be;
  font-size: 0.78rem;
  line-height: 1.35;
}

.primary-button {
  width: 100%;
  min-height: 48px;
  margin-top: 12px;
  border: 1px solid #ba6b00;
  border-radius: 7px;
  background: linear-gradient(180deg, #ffc43d, var(--orange));
  box-shadow: 0 2px 0 rgba(255, 255, 255, 0.45) inset, 0 3px 0 #743c00;
  color: #241400;
  font-family: "Courier New", ui-monospace, monospace;
  font-weight: 900;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.controls {
  display: grid;
  grid-template-columns: minmax(0, 1.08fr) minmax(0, 0.92fr);
  gap: 9px;
  margin-top: 10px;
  padding: 9px;
  border: 1px solid #707a82;
  border-radius: 16px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.18), transparent 8%, transparent 92%, rgba(56, 65, 71, 0.12)),
    linear-gradient(180deg, #b9c2c8, #8d989f);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset, 0 -2px 0 rgba(64, 72, 78, 0.4) inset;
}

.control-group {
  display: grid;
  align-items: center;
  gap: 7px;
}

.movement-controls {
  grid-template-columns: repeat(4, minmax(48px, 1fr));
}

.utility-controls {
  grid-template-columns: minmax(88px, 1.5fr) repeat(2, minmax(58px, 1fr));
}

.controls button {
  position: relative;
  min-width: 48px;
  min-height: 52px;
  padding: 0 7px;
  border: 1px solid #1a2025;
  background: linear-gradient(160deg, #596771 0%, #3b464f 58%, #293239 100%);
  box-shadow:
    0 2px 1px rgba(255, 255, 255, 0.22) inset,
    0 -3px 3px rgba(5, 8, 10, 0.4) inset,
    0 4px 0 #20272c,
    0 6px 8px rgba(25, 30, 34, 0.32);
  color: #eff5f7;
  font-family: "Courier New", ui-monospace, monospace;
  font-size: 0.7rem;
  font-weight: 900;
  letter-spacing: 0.02em;
  text-shadow: 0 1px 1px #11171b;
  text-transform: uppercase;
}

.movement-controls button {
  width: 54px;
  height: 54px;
  justify-self: center;
  padding: 0;
  border-radius: 50%;
  font-size: 1.28rem;
}

.movement-controls button span {
  display: block;
  transform: translateY(-1px);
}

.movement-controls .rotate-control {
  background: linear-gradient(160deg, #657681, #40505a 60%, #2a353c);
  color: #d9f7ff;
  font-size: 1.52rem;
}

.drop-control {
  border-color: #8b4e00 !important;
  border-radius: 12px;
  background: linear-gradient(160deg, #ffc74d, #f39714 60%, #c76a00) !important;
  box-shadow:
    0 2px 1px rgba(255, 255, 255, 0.48) inset,
    0 -3px 3px rgba(91, 42, 0, 0.3) inset,
    0 4px 0 #7a4000,
    0 6px 8px rgba(25, 30, 34, 0.32) !important;
  color: #2b1800 !important;
  text-shadow: 0 1px rgba(255, 255, 255, 0.32) !important;
}

.utility-control {
  border-radius: 11px;
  font-size: 0.64rem !important;
}

.pause-control {
  background: linear-gradient(160deg, #607781, #3c4d56 62%, #29343a) !important;
}

.restart-control {
  background: linear-gradient(160deg, #6c6265, #463e42 62%, #302a2d) !important;
  color: #ffe8ea !important;
}

.controls button:hover:not(:disabled) {
  filter: brightness(1.08);
}

.controls button:active:not(:disabled),
.controls button[aria-pressed="true"]:not(:disabled),
.primary-button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.38) inset, 0 1px 0 #20272c;
}

.controls button:disabled {
  cursor: default;
  filter: grayscale(0.45);
  opacity: 0.44;
}

@media (max-width: 600px) {
  .app-shell {
    width: calc(100% - 12px);
    margin: 6px auto;
    padding: 8px;
    border-radius: 20px;
  }

  .app-shell::before {
    inset: 3px;
    border-radius: 17px;
  }

  .screen-bezel {
    padding: 6px;
    border-radius: 13px;
  }

  .stats-row {
    margin-bottom: 5px;
  }

  .stat-card {
    padding: 3px 2px 2px;
  }

  .stat-card strong {
    font-size: 0.78rem;
  }

  .game-layout {
    grid-template-columns: minmax(0, 1fr) 76px;
    gap: 6px;
    justify-content: stretch;
  }

  .game-column {
    width: 100%;
  }

  .panel-card {
    padding: 5px;
  }

  .desktop-help {
    display: none;
  }

  .controls {
    grid-template-columns: 1fr;
    gap: 8px;
    margin-top: 7px;
    padding: 8px 6px;
    border-radius: 13px;
  }

  .movement-controls {
    gap: 5px;
  }

  .movement-controls button {
    width: 50px;
    height: 50px;
    min-height: 50px;
  }

  .utility-controls {
    grid-template-columns: minmax(94px, 1.6fr) repeat(2, minmax(60px, 1fr));
    gap: 6px;
  }

  .utility-controls button {
    min-height: 48px;
  }

}

@media (max-width: 360px) {
  .eyebrow {
    font-size: 0.53rem;
  }

  .game-layout {
    grid-template-columns: minmax(0, 1fr) 68px;
    gap: 5px;
  }

  .movement-controls button {
    width: 48px;
    height: 48px;
    min-height: 48px;
  }

  .utility-controls {
    grid-template-columns: minmax(90px, 1.55fr) repeat(2, minmax(56px, 1fr));
    gap: 5px;
  }

  .utility-control {
    padding: 0 4px !important;
    font-size: 0.57rem !important;
  }
}

@media (max-height: 900px) {
  .board-wrap {
    width: min(100%, 360px, max(145px, calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 220px) / 2)));
  }
}

@media (min-width: 601px) and (max-height: 900px) {
  .game-column {
    width: max(145px, calc((100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 220px) / 2));
  }
}

@media (max-height: 650px) and (orientation: portrait) {
  .controls {
    gap: 6px;
    padding-top: 6px;
    padding-bottom: 6px;
  }
}

@media (min-width: 601px) and (max-height: 760px) {
  .desktop-help {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .controls button,
  .primary-button {
    transition: none;
  }
}
