:root {
  --bg: #0f1220;
  --bg-elevated: #1a1f35;
  --bg-panel: #151a2e;
  --border: #2a3355;
  --text: #e8ecf8;
  --text-muted: #8b95b5;
  --accent: #5b8cff;
  --accent-hover: #7aa3ff;
  --danger: #ff6b7a;
  --glow: rgba(91, 140, 255, 0.35);
  --radius: 14px;
  --font: "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei",
    system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: radial-gradient(ellipse at top, #1a2240 0%, var(--bg) 55%);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  padding: 16px 12px 24px;
}

.app {
  width: min(920px, 100%);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.header {
  text-align: center;
}

.title {
  margin: 0;
  font-size: clamp(1.5rem, 4vw, 2rem);
  font-weight: 700;
  letter-spacing: 0.08em;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
}

.layout {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 16px;
  align-items: start;
  justify-items: center;
}

.panel {
  width: 100%;
  max-width: 160px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-left {
  align-items: stretch;
}

.panel-right {
  align-items: stretch;
}

.stat,
.next-box {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.stat-label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.next-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#next {
  display: block;
  image-rendering: pixelated;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 14px;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover:not(:disabled) {
  background: #222a45;
  border-color: var(--accent);
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #7c5cff);
  border-color: transparent;
  color: #fff;
  font-weight: 600;
  box-shadow: 0 6px 20px var(--glow);
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, var(--accent-hover), #9178ff);
}

.board-wrap {
  position: relative;
  border-radius: calc(var(--radius) + 2px);
  padding: 6px;
  background: linear-gradient(145deg, #2a3558, #151a2e);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4), 0 0 0 1px var(--border);
}

#board {
  display: block;
  width: min(300px, 72vw);
  height: auto;
  aspect-ratio: 1 / 2;
  background: #0a0d18;
  border-radius: 10px;
  image-rendering: pixelated;
}

.overlay {
  position: absolute;
  inset: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(8, 10, 20, 0.78);
  backdrop-filter: blur(4px);
  border-radius: 10px;
  transition: opacity 0.2s;
}

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

.overlay-card {
  text-align: center;
  padding: 20px 24px;
  max-width: 240px;
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: 1.35rem;
}

.overlay-card p {
  margin: 0 0 16px;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

/* Touch controls: hidden on desktop
   Mobile: left = move cluster, far right = both rotates stacked */
.touch-controls {
  display: none;
  flex-direction: row;
  align-items: stretch;
  justify-content: space-between;
  gap: 12px;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
  flex-shrink: 0;
  padding: 0 2px;
}

.touch-move {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 280px;
}

.touch-rotate {
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 6px;
  flex: 0 0 auto;
  width: 56px;
}

.touch-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 12px;
  min-height: 44px;
  min-width: 0;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.1s, transform 0.08s;
}

.touch-btn:active,
.touch-btn.is-pressed {
  background: #2a3558;
  transform: scale(0.96);
  border-color: var(--accent);
}

.touch-btn-hard {
  font-size: 1.15rem;
  font-weight: 600;
  background: #1e2744;
}

.help {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.5;
}

.help p {
  margin: 0;
}

/* Tablet / phone landscape-ish */
@media (max-width: 768px) {
  html,
  body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }

  body {
    padding: 6px 8px 8px;
    align-items: stretch;
  }

  .app {
    height: 100%;
    max-height: 100dvh;
    gap: 6px;
    overflow: hidden;
  }

  .header {
    flex-shrink: 0;
  }

  .title {
    font-size: 1.15rem;
    letter-spacing: 0.06em;
  }

  .subtitle {
    display: none;
  }

  .layout {
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    grid-template-areas: "left board right";
    gap: 6px;
    flex: 1 1 auto;
    min-height: 0;
    align-items: start;
    align-content: start;
    justify-items: center;
  }

  .board-wrap {
    grid-area: board;
    padding: 4px;
    max-height: 100%;
  }

  #board {
    /* Wider playfield on portrait; still fits 100dvh with compact chrome */
    width: min(340px, 86vw, calc((100dvh - 175px) / 2));
    height: auto;
    max-height: calc(100dvh - 175px);
    aspect-ratio: 1 / 2;
  }

  .panel-left {
    grid-area: left;
    max-width: 88px;
    width: 100%;
    gap: 4px;
    justify-self: end;
  }

  .panel-right {
    grid-area: right;
    max-width: 88px;
    width: 100%;
    gap: 4px;
    justify-self: start;
  }

  .stat,
  .next-box {
    padding: 4px 6px;
    border-radius: 8px;
  }

  .stat-label {
    font-size: 0.58rem;
    margin-bottom: 1px;
  }

  .stat-value {
    font-size: 0.95rem;
  }

  #next {
    width: 48px;
    height: 48px;
  }

  .btn {
    padding: 5px 6px;
    font-size: 0.7rem;
    border-radius: 8px;
  }

  .actions {
    gap: 4px;
  }

  .touch-controls {
    max-width: 100%;
  }

  .touch-move {
    max-width: none;
  }

  .touch-rotate {
    width: 52px;
  }

  .touch-btn {
    min-height: 46px;
  }

  .overlay-card {
    padding: 12px 16px;
  }

  .overlay-card h2 {
    font-size: 1.1rem;
  }

  .overlay-card p {
    font-size: 0.8rem;
    margin-bottom: 10px;
  }

  .touch-controls {
    display: flex;
  }

  .help {
    display: none;
  }
}

/* Narrow phones */
@media (max-width: 480px) {
  body {
    padding: 4px 6px 6px;
  }

  .app {
    gap: 4px;
  }

  .title {
    font-size: 1rem;
  }

  .layout {
    gap: 4px;
  }

  #board {
    width: min(300px, 78vw, calc((100dvh - 160px) / 2));
    max-height: calc(100dvh - 160px);
  }

  .panel-left,
  .panel-right {
    max-width: 72px;
  }

  .stat-value {
    font-size: 0.85rem;
  }

  #next {
    width: 40px;
    height: 40px;
  }

  .touch-controls {
    gap: 8px;
    max-width: 100%;
  }

  .touch-move,
  .touch-rotate {
    gap: 4px;
  }

  .touch-rotate {
    width: 48px;
  }

  .touch-btn {
    min-height: 42px;
    border-radius: 10px;
    font-size: 0.95rem;
  }

  .touch-btn-hard {
    font-size: 1rem;
  }
}

/* Very short viewports (landscape phones / small height) */
@media (max-width: 768px) and (max-height: 560px) {
  .title {
    font-size: 0.9rem;
  }

  #board {
    width: min(220px, 55vw, calc((100dvh - 130px) / 2));
    max-height: calc(100dvh - 130px);
  }

  .panel-left,
  .panel-right {
    max-width: 64px;
  }

  .touch-rotate {
    width: 42px;
  }

  .touch-btn {
    min-height: 32px;
    font-size: 0.85rem;
  }

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

  #next {
    width: 40px;
    height: 40px;
  }
}
