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

:root {
  --bg: #0b1120;
  --surface: #151d2e;
  --surface-2: #1a2332;
  --border: #2a3548;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #22d3ee;
  --accent-dim: rgba(34, 211, 238, 0.25);
  --purple: #7c3aed;
  --purple-hover: #8b5cf6;
  --grid-line: rgba(148, 163, 184, 0.08);
  /* control panel anchor points (red-box layout) */
  --ctrl-dpad-x: 27%;
  --ctrl-rotate-x: 73%;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  user-select: none;
  -webkit-user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding:
    max(12px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(14px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
  gap: 10px;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-shrink: 0;
  min-height: 44px;
}

.header .title {
  margin: 0;
  font-size: clamp(1.05rem, 4.2vw, 1.45rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.15;
}

/* Play area */
.play-area {
  flex: 1;
  display: flex;
  gap: 10px;
  min-height: 0;
  align-items: stretch;
}

.board-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px;
  overflow: hidden;
}

#screen {
  display: block;
  width: 100%;
  height: 100%;
  max-height: 100%;
  border-radius: 10px;
}

.overlay {
  position: absolute;
  inset: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(11, 17, 32, 0.82);
  border-radius: 10px;
  pointer-events: none;
}

.overlay p {
  margin: 0;
  font-size: clamp(0.9rem, 3.5vw, 1rem);
  font-weight: 600;
  text-align: center;
  padding: 16px;
  color: var(--text);
}

.overlay.hidden {
  display: none;
}

/* HUD */
.hud {
  width: clamp(108px, 30vw, 132px);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px;
}

.label {
  display: block;
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 4px;
}

.score-card .value {
  display: block;
  font-size: clamp(1.5rem, 6vw, 1.85rem);
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.02em;
}

.score-card .meta {
  display: block;
  margin-top: 6px;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.3;
}

.score-card .dot {
  margin: 0 2px;
}

.next-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.next-preview-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 6px;
  min-height: 72px;
  aspect-ratio: 1;
  width: 100%;
  max-width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
  overflow: hidden;
}

#next-preview {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
}

.hint-card {
  flex-shrink: 0;
}

.hint-card p {
  margin: 0;
  font-size: 0.68rem;
  line-height: 1.45;
  color: var(--muted);
}

/* Controls — full width aligned with play-area above */
.controls {
  flex-shrink: 0;
  width: 100%;
  margin-top: 14px;
  padding: 0;
}

.handheld-plate {
  width: 100%;
  box-sizing: border-box;
  padding: 14px 12px 16px;
  background: linear-gradient(180deg, #1c2538 0%, #121a2a 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 4px 14px rgba(0, 0, 0, 0.35);
}

/*
 * Fixed positions (match red-box mockup):
 * - D-pad center ~34% from left (shifted right)
 * - Rotate center ~66% from left (shifted left)
 * - Aux row top-right
 */
.controls-layout {
  position: relative;
  width: 100%;
  height: 148px;
  min-height: 148px;
}

.aux-row {
  position: absolute;
  top: 4px;
  right: 6px;
  display: flex;
  gap: 10px;
}

.controls-layout > .dpad {
  position: absolute;
  left: var(--ctrl-dpad-x);
  bottom: 8px;
  transform: translateX(-50%);
}

.controls-layout > .action-zone {
  position: absolute;
  left: var(--ctrl-rotate-x);
  bottom: 8px;
  transform: translateX(-50%);
}

.aux-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.aux-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  color: #fff;
  font-size: 14px;
  line-height: 1;
  transition: transform 0.07s ease, box-shadow 0.07s ease, filter 0.07s ease;
}

.aux-red {
  background: radial-gradient(ellipse 75% 50% at 35% 22%, #f87171 0%, #dc2626 45%, #991b1b 100%);
  box-shadow:
    0 3px 0 #5c1515,
    0 5px 10px rgba(0, 0, 0, 0.35),
    inset 0 2px 3px rgba(255, 255, 255, 0.35),
    inset 0 -2px 4px rgba(0, 0, 0, 0.3);
}

.aux-green {
  background: radial-gradient(ellipse 75% 50% at 35% 22%, #4ade80 0%, #16a34a 45%, #14532d 100%);
  box-shadow:
    0 3px 0 #14532d,
    0 5px 10px rgba(0, 0, 0, 0.35),
    inset 0 2px 3px rgba(255, 255, 255, 0.35),
    inset 0 -2px 4px rgba(0, 0, 0, 0.3);
}

.aux-btn.muted .aux-icon {
  opacity: 0.45;
}

.aux-btn.muted::after {
  content: "";
  position: absolute;
  width: 22px;
  height: 2px;
  background: rgba(255, 255, 255, 0.9);
  transform: rotate(-35deg);
  border-radius: 1px;
}

.aux-btn {
  position: relative;
}

.aux-btn.active {
  box-shadow:
    0 1px 0 #14532d,
    inset 0 3px 6px rgba(0, 0, 0, 0.45),
    inset 0 1px 2px rgba(255, 255, 255, 0.15);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.aux-label {
  font-size: 0.58rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--muted);
  text-transform: uppercase;
}

.aux-btn:active,
.aux-btn.pressed {
  transform: translateY(2px) scale(0.96);
  filter: brightness(0.92);
}

/* D-pad — solid blue cross + capsule keys */
.dpad {
  --arm: 40px;
  --reach: 48px;
  position: relative;
  width: 128px;
  height: 128px;
  min-width: 128px;
  min-height: 128px;
  flex-shrink: 0;
}

.dpad-bar {
  position: absolute;
  pointer-events: none;
  border-radius: 14px;
  background: linear-gradient(180deg, #6ba8dc 0%, #4a85c4 42%, #3569a8 100%);
  box-shadow:
    0 3px 0 #1a3d66,
    inset 0 2px 4px rgba(255, 255, 255, 0.35),
    inset 0 -3px 5px rgba(0, 0, 0, 0.25);
}

.dpad-bar-v {
  top: 0;
  bottom: 0;
  left: 50%;
  width: var(--arm);
  transform: translateX(-50%);
  z-index: 0;
}

.dpad-bar-h {
  left: 0;
  right: 0;
  top: 50%;
  height: var(--arm);
  transform: translateY(-50%);
  z-index: 0;
}

.dpad-key {
  position: absolute;
  border: none;
  cursor: pointer;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: linear-gradient(180deg, #6ba8dc 0%, #4a85c4 42%, #3569a8 100%);
  box-shadow:
    0 3px 0 #1a3d66,
    inset 0 2px 4px rgba(255, 255, 255, 0.38),
    inset 0 -3px 5px rgba(0, 0, 0, 0.22);
  transition: transform 0.07s ease, box-shadow 0.07s ease;
}

.dpad-key.up {
  top: 0;
  left: 50%;
  width: var(--arm);
  height: var(--reach);
  transform: translateX(-50%);
  border-radius: 16px 16px 4px 4px;
}

.dpad-key.down {
  bottom: 0;
  left: 50%;
  width: var(--arm);
  height: var(--reach);
  transform: translateX(-50%);
  border-radius: 4px 4px 16px 16px;
}

.dpad-key.left {
  left: 0;
  top: 50%;
  width: var(--reach);
  height: var(--arm);
  transform: translateY(-50%);
  border-radius: 16px 4px 4px 16px;
}

.dpad-key.right {
  right: 0;
  top: 50%;
  width: var(--reach);
  height: var(--arm);
  transform: translateY(-50%);
  border-radius: 4px 16px 16px 4px;
}

/* Debossed arrow glyphs */
.dpad-glyph {
  display: block;
  width: 0;
  height: 0;
  pointer-events: none;
}

.dpad-glyph.up {
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 10px solid #1a3558;
  filter: drop-shadow(0 1px 0 rgba(255, 255, 255, 0.22));
  margin-top: -4px;
}

.dpad-glyph.down {
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 10px solid #1a3558;
  filter: drop-shadow(0 -1px 0 rgba(255, 255, 255, 0.15));
  margin-bottom: -4px;
}

.dpad-glyph.left {
  border-top: 7px solid transparent;
  border-bottom: 7px solid transparent;
  border-right: 10px solid #1a3558;
  filter: drop-shadow(1px 0 0 rgba(255, 255, 255, 0.18));
  margin-left: -4px;
}

.dpad-glyph.right {
  border-top: 7px solid transparent;
  border-bottom: 7px solid transparent;
  border-left: 10px solid #1a3558;
  filter: drop-shadow(-1px 0 0 rgba(255, 255, 255, 0.18));
  margin-right: -4px;
}

.dpad-key:active,
.dpad-key.pressed {
  box-shadow:
    0 1px 0 #1a3d66,
    inset 0 4px 10px rgba(0, 0, 0, 0.38);
}

.dpad-key.up:active,
.dpad-key.up.pressed,
.dpad-key.down:active,
.dpad-key.down.pressed {
  transform: translateX(-50%) translateY(2px);
}

.dpad-key.left:active,
.dpad-key.left.pressed {
  transform: translateY(-50%) translateX(2px);
}

.dpad-key.right:active,
.dpad-key.right.pressed {
  transform: translateY(-50%) translateX(-2px);
}

/* Rotate — same pill family, circular */
.action-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.action-btn {
  width: 64px;
  height: 64px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: linear-gradient(180deg, #6ba8dc 0%, #4a85c4 38%, #3569a8 100%);
  box-shadow:
    0 4px 0 #1a3d66,
    0 6px 12px rgba(0, 0, 0, 0.32),
    inset 0 2px 5px rgba(255, 255, 255, 0.38),
    inset 0 -4px 7px rgba(0, 0, 0, 0.25);
  transition: transform 0.07s ease, box-shadow 0.07s ease;
}

.action-icon {
  width: 30px;
  height: 30px;
  fill: #1a3558;
  filter: drop-shadow(0 1px 0 rgba(255, 255, 255, 0.25));
}

.action-label {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-transform: uppercase;
}

.action-btn:active,
.action-btn.pressed {
  transform: translateY(2px);
  box-shadow:
    0 1px 0 #1a3d66,
    inset 0 5px 12px rgba(0, 0, 0, 0.38);
}

/* Short screens */
@media (max-height: 640px) {
  #app {
    gap: 8px;
    padding-top: 8px;
  }

  .controls {
    margin-top: 10px;
  }

  .hint-card {
    display: none;
  }

  .dpad {
    --arm: 36px;
    --reach: 42px;
    width: 112px;
    height: 112px;
    min-width: 112px;
    min-height: 112px;
  }

  .action-btn {
    width: 58px;
    height: 58px;
  }

  .controls-layout {
    height: 132px;
    min-height: 132px;
  }

  .controls-layout > .dpad {
    left: 26%;
  }

  .controls-layout > .action-zone {
    left: 72%;
  }

  .aux-btn {
    width: 32px;
    height: 32px;
  }
}

@media (max-width: 340px) {
  .hud {
    width: 100px;
  }

  .hint-card p {
    font-size: 0.62rem;
  }
}
