:root {
  color-scheme: dark;
  --shell: #d7d2bd;
  --shell-dark: #aaa58f;
  --screen-frame: #56606a;
  --screen: #111a22;
  --ink: #f6f0dc;
  --muted: #bcc5c0;
  --accent: #dc5366;
  --blue: #517ec1;
  --button: #242935;
  --button-edge: #11141b;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at top left, rgba(81, 126, 193, 0.24), transparent 28rem),
    linear-gradient(135deg, #25313c 0%, #121920 100%);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  letter-spacing: 0;
  overscroll-behavior: none;
}

body {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button,
canvas {
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  touch-action: none;
  cursor: pointer;
}

.game-shell {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 28px;
}

.handheld {
  width: min(100%, 980px);
  padding: 18px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 24px 24px 64px 24px;
  background: linear-gradient(150deg, var(--shell), #eee8cf 54%, var(--shell-dark));
  box-shadow: 0 28px 70px rgba(0, 0, 0, 0.32), inset 0 -8px 0 rgba(0, 0, 0, 0.12);
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px 12px;
  color: #33414c;
  font-size: 13px;
  font-weight: 800;
}

.brand-light {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #d6454f;
  box-shadow: 0 0 8px rgba(214, 69, 79, 0.7);
}

.screen-frame {
  padding: 18px;
  border-radius: 16px 16px 42px 16px;
  background: linear-gradient(145deg, #6d747d, var(--screen-frame));
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.13), inset 0 -6px 0 rgba(0, 0, 0, 0.24);
}

.screen {
  display: grid;
  grid-template-columns: minmax(288px, 360px) 220px;
  gap: 18px;
  align-items: start;
  justify-content: center;
  min-height: 0;
  padding: 16px;
  border: 2px solid #202932;
  border-radius: 8px;
  background: var(--screen);
}

.board-panel {
  position: relative;
  width: min(100%, 360px);
  aspect-ratio: 3 / 5;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: crisp-edges;
  border: 2px solid rgba(222, 241, 226, 0.24);
  border-radius: 4px;
  background: #18222d;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 16px;
  padding: 18px;
  text-align: center;
  background: rgba(12, 17, 23, 0.72);
  backdrop-filter: blur(3px);
}

.overlay.hidden {
  pointer-events: none;
  opacity: 0;
}

.overlay-title {
  font-size: 28px;
  font-weight: 850;
}

.side-panel {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.stat,
.next-box {
  display: grid;
  gap: 6px;
  padding: 10px;
  border: 1px solid rgba(213, 232, 221, 0.18);
  border-radius: 6px;
  background: #1a2530;
}

.stat span,
.next-box span {
  color: var(--muted);
  font-size: 13px;
}

.stat strong {
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: 24px;
  line-height: 1.1;
}

#next {
  width: 112px;
  height: 112px;
  justify-self: center;
  border: 1px solid rgba(213, 232, 221, 0.16);
  border-radius: 4px;
}

.desktop-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.primary-button,
.secondary-button {
  min-height: 42px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: white;
  font-weight: 800;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.2);
}

.secondary-button {
  background: var(--blue);
}

.primary-button:active,
.secondary-button:active,
.control:active,
.round-control:active,
.pressed {
  transform: translateY(2px);
  filter: brightness(0.94);
}

.help-text {
  margin: 0;
  color: #d8dfd9;
  font-size: 13px;
  line-height: 1.55;
}

.mobile-controls {
  display: none;
}

@media (max-width: 760px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .game-shell {
    min-height: 100dvh;
    padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    align-items: stretch;
  }

  .handheld {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) clamp(190px, 30dvh, 250px);
    width: min(calc(100vw - 20px), 430px);
    min-width: 0;
    min-height: calc(100dvh - 20px);
    max-height: calc(100dvh - 20px);
    margin: 0 auto;
    padding: 14px;
    border-radius: 24px 24px 58px 24px;
  }

  .brand-row {
    padding-bottom: 8px;
  }

  .screen-frame {
    min-height: 0;
    padding: 12px;
    border-radius: 14px 14px 34px 14px;
  }

  .screen {
    height: 100%;
    grid-template-columns: minmax(0, 1fr) clamp(86px, 25vw, 112px);
    gap: 10px;
    padding: 10px;
    overflow: hidden;
  }

  .board-panel {
    width: 100%;
    height: auto;
    max-height: 100%;
    justify-self: center;
    align-self: center;
  }

  .side-panel {
    align-content: start;
    gap: 8px;
  }

  .stat,
  .next-box {
    padding: 7px;
    gap: 4px;
  }

  .stat span,
  .next-box span,
  .help-text {
    font-size: 11px;
  }

  .stat strong {
    font-size: 18px;
  }

  #next {
    width: min(84px, 100%);
    height: auto;
    aspect-ratio: 1;
  }

  .desktop-actions {
    grid-template-columns: 1fr;
    gap: 7px;
  }

  .secondary-button {
    min-height: 34px;
    font-size: 13px;
  }

  .help-text {
    display: none;
    line-height: 1.35;
  }

  .overlay-title {
    font-size: 22px;
  }

  .mobile-controls {
    display: grid;
    grid-template-columns: auto auto;
    align-items: center;
    justify-content: space-between;
    column-gap: clamp(22px, 7vw, 42px);
    width: min(100%, 390px);
    margin: 0 auto;
    padding: clamp(12px, 2.8dvh, 20px) clamp(10px, 4vw, 18px) 8px;
    min-height: 0;
    touch-action: none;
  }

  .dpad {
    position: relative;
    width: clamp(120px, 36vw, 158px);
    aspect-ratio: 1;
    justify-self: center;
  }

  .control {
    position: absolute;
    width: 34%;
    height: 34%;
    border: 0;
    border-radius: 8px;
    background: linear-gradient(145deg, #343b45, var(--button));
    color: #f7f3e3;
    font-weight: 900;
    box-shadow: 0 6px 0 var(--button-edge), 0 9px 16px rgba(0, 0, 0, 0.25);
  }

  .key-w {
    left: 33%;
    top: 0;
  }

  .key-a {
    left: 0;
    top: 33%;
  }

  .key-s {
    left: 33%;
    bottom: 0;
  }

  .key-d {
    right: 0;
    top: 33%;
  }

  .action-pad {
    position: relative;
    width: clamp(156px, 43vw, 188px);
    aspect-ratio: 1.72;
    justify-self: center;
  }

  .action-pad::before {
    content: "";
    position: absolute;
    inset: 10% 3%;
    border-radius: 999px;
    background: rgba(96, 85, 90, 0.2);
    box-shadow: inset 0 2px 3px rgba(255, 255, 255, 0.16), inset 0 -4px 8px rgba(0, 0, 0, 0.14);
    transform: rotate(-18deg);
    pointer-events: none;
  }

  .round-control {
    position: absolute;
    width: clamp(56px, 16vw, 70px);
    height: clamp(56px, 16vw, 70px);
    border: 0;
    border-radius: 50%;
    background: linear-gradient(145deg, #d95a6b, #a82745);
    color: #fff7ec;
    font-size: clamp(13px, 3.7vw, 15px);
    font-weight: 900;
    z-index: 1;
    box-shadow: 0 7px 0 #641426, 0 11px 18px rgba(0, 0, 0, 0.26);
  }

  .rotate-control {
    left: 0;
    bottom: 10px;
  }

  .hard-control {
    right: 0;
    top: 8px;
  }
}

@media (max-width: 370px) {
  .handheld {
    padding: 10px;
  }

  .screen {
    grid-template-columns: minmax(0, 1fr) 84px;
    gap: 8px;
    padding: 8px;
  }

  .help-text {
    display: none;
  }

  .mobile-controls {
    column-gap: 18px;
    width: min(100%, 340px);
    padding-top: 14px;
  }

  .dpad {
    width: clamp(108px, 34vw, 124px);
  }

  .action-pad {
    width: clamp(148px, 42vw, 158px);
  }

  .round-control {
    width: clamp(50px, 15.5vw, 58px);
    height: clamp(50px, 15.5vw, 58px);
    font-size: 13px;
  }
}

@media (min-width: 761px) {
  .handheld {
    border-radius: 18px;
  }
}

@media (orientation: landscape) and (max-width: 920px) {
  html,
  body {
    overflow: hidden;
  }

  .game-shell {
    align-items: stretch;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .handheld {
    display: grid;
    grid-template-columns: minmax(96px, 0.72fr) minmax(320px, 400px) minmax(96px, 0.72fr);
    grid-template-rows: auto minmax(0, 1fr);
    gap: 10px 14px;
    width: min(100%, 900px);
    min-height: calc(100dvh - 16px);
    max-height: calc(100dvh - 16px);
    padding: 10px 14px 12px;
    border-radius: 18px 18px 44px 18px;
  }

  .brand-row {
    grid-column: 1 / -1;
    padding: 0 6px;
  }

  .screen-frame {
    grid-column: 2;
    grid-row: 2;
    min-height: 0;
    height: 100%;
    padding: 10px;
  }

  .screen {
    height: 100%;
    grid-template-columns: minmax(0, min(28vw, calc((100dvh - 88px) * 0.6))) clamp(78px, 13vw, 104px);
    align-items: center;
    justify-content: center;
  }

  .board-panel {
    width: 100%;
    height: auto;
    max-width: 100%;
  }

  .side-panel {
    gap: 4px;
  }

  .stat,
  .next-box {
    padding: 4px;
    gap: 3px;
  }

  .stat span,
  .next-box span {
    font-size: 10px;
  }

  .stat strong {
    font-size: 15px;
  }

  #next {
    width: 58px;
  }

  .desktop-actions {
    gap: 5px;
  }

  .secondary-button {
    min-height: 26px;
    font-size: 12px;
  }

  .help-text {
    display: none;
  }

  .mobile-controls {
    display: contents;
  }

  .dpad {
    grid-column: 1;
    grid-row: 2;
    width: clamp(104px, 16vw, 126px);
    align-self: center;
    justify-self: center;
  }

  .action-pad {
    grid-column: 3;
    grid-row: 2;
    width: clamp(112px, 16vw, 130px);
    align-self: center;
    justify-self: center;
  }

  .round-control {
    width: clamp(50px, 7.8vw, 58px);
    height: clamp(50px, 7.8vw, 58px);
  }
}
