:root {
  --bg-deep: #090d16;
  --bg-panel: #111827;
  --bg-panel-border: rgba(148, 163, 184, 0.16);
  --bg-board: #060911;
  --text-primary: #f8fafc;
  --text-muted: #94a3b8;
  --accent: #38bdf8;
  --accent-gold: #fbbf24;
  --accent-glow: rgba(56, 189, 248, 0.35);
  --btn-bg: #1e293b;
  --btn-bg-active: #334155;
  --btn-border: rgba(148, 163, 184, 0.22);
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

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

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background:
    radial-gradient(circle at 20% 0%, rgba(56, 189, 248, 0.12), transparent 42%),
    radial-gradient(circle at 80% 100%, rgba(168, 85, 247, 0.12), transparent 45%),
    var(--bg-deep);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.app-shell {
  width: 100%;
  height: 100dvh;
  max-width: 980px;
  margin: 0 auto;
  padding: calc(10px + var(--safe-top)) 14px calc(12px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Top Bar */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-badge {
  display: grid;
  grid-template-columns: repeat(2, 8px);
  grid-template-rows: repeat(2, 8px);
  gap: 2px;
}

.brand-badge i {
  display: block;
  border-radius: 2px;
}
.brand-badge i:nth-child(1) { background: #00e5ff; }
.brand-badge i:nth-child(2) { background: #b44cff; }
.brand-badge i:nth-child(3) { background: #ffd600; }
.brand-badge i:nth-child(4) { background: #ff3b5c; }

.brand-title {
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: 0.14em;
  color: var(--text-primary);
}

.top-stats-mobile {
  display: none;
  align-items: center;
  gap: 6px;
  flex: 1;
  justify-content: center;
}

.mini-stat {
  background: rgba(17, 24, 39, 0.85);
  border: 1px solid var(--bg-panel-border);
  border-radius: 8px;
  padding: 3px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 52px;
}

.mini-label {
  font-size: 0.62rem;
  color: var(--text-muted);
  line-height: 1.1;
}

.mini-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1.25;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.08s ease, background-color 0.15s ease, border-color 0.15s ease;
}

.icon-btn:hover {
  background: var(--btn-bg-active);
  border-color: rgba(148, 163, 184, 0.4);
}

.icon-btn:active {
  transform: scale(0.93);
}

.icon-btn.muted {
  opacity: 0.48;
}

.icon-glyph {
  font-size: 1rem;
  line-height: 1;
}

/* Main Arena */
.arena {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
}

.side-panel {
  width: 210px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-self: stretch;
  justify-content: center;
}

.card {
  background: rgba(17, 24, 39, 0.86);
  border: 1px solid var(--bg-panel-border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(8px);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.canvas-box {
  background: var(--bg-board);
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.small-box {
  height: 96px;
}

.next-box {
  height: 292px;
}

#hold-canvas {
  width: 120px;
  height: 96px;
}

#next-canvas {
  width: 120px;
  height: 292px;
}

#m-hold-canvas {
  display: block;
  width: 68px;
  height: 54px;
}

#m-next-canvas {
  display: block;
  width: 68px;
  height: 164px;
}

.stat-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 5px 0;
  border-bottom: 1px solid rgba(148, 163, 184, 0.08);
}

.stat-row:last-child {
  border-bottom: none;
}

.stat-label {
  font-size: 0.82rem;
  color: var(--text-muted);
}

.stat-val {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--text-primary);
}

.accent {
  color: var(--accent-gold) !important;
}

.key-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.key-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

kbd {
  display: inline-block;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 5px;
  border-radius: 5px;
  background: #1e293b;
  border: 1px solid rgba(148, 163, 184, 0.28);
  color: #e2e8f0;
  margin-left: 2px;
}

/* Center Board Stage */
.board-stage {
  height: 100%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.mobile-side-rail {
  display: none;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  gap: 8px;
  width: 78px;
  flex-shrink: 0;
}

.mobile-preview-box {
  background: rgba(15, 23, 42, 0.88);
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 11px;
  padding: 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);
}

.mobile-hold-box {
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.mobile-hold-box:active {
  transform: scale(0.95);
  border-color: rgba(56, 189, 248, 0.5);
  background: rgba(30, 41, 59, 0.92);
}

.mobile-box-label {
  font-size: 0.6rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  line-height: 1;
}

.mobile-restart-btn {
  width: 100%;
  height: 34px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  background: rgba(30, 41, 59, 0.85);
  color: #cbd5e1;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.08s ease, background-color 0.15s ease;
}

.mobile-restart-btn:active {
  transform: scale(0.95);
  background: #334155;
}

.board-frame {
  position: relative;
  height: 100%;
  max-height: 760px;
  aspect-ratio: 1 / 2;
  flex-shrink: 0;
  background: var(--bg-board);
  border-radius: 14px;
  border: 2px solid rgba(148, 163, 184, 0.26);
  box-shadow:
    0 0 0 1px rgba(15, 23, 42, 0.9),
    0 18px 48px rgba(0, 0, 0, 0.55),
    inset 0 0 24px rgba(56, 189, 248, 0.06);
  overflow: hidden;
  transition: transform 0.08s ease;
}

.board-frame.shake {
  animation: boardShake 0.16s ease;
}

@keyframes boardShake {
  0% { transform: translateY(0); }
  35% { transform: translateY(3px); }
  70% { transform: translateY(-1px); }
  100% { transform: translateY(0); }
}

#board-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Callout Layer */
.callout-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  z-index: 4;
}

.callout-text {
  font-weight: 900;
  font-size: 1.35rem;
  letter-spacing: 0.08em;
  color: #ffffff;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.85), 0 0 18px var(--accent-glow);
  animation: calloutPop 0.95s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.callout-text.gold {
  color: #fde047;
  text-shadow: 0 2px 14px rgba(0, 0, 0, 0.9), 0 0 20px rgba(250, 204, 21, 0.55);
}

@keyframes calloutPop {
  0% {
    opacity: 0;
    transform: translateY(16px) scale(0.75);
  }
  20% {
    opacity: 1;
    transform: translateY(0) scale(1.08);
  }
  75% {
    opacity: 1;
    transform: translateY(-12px) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(-28px) scale(0.95);
  }
}

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(6, 9, 17, 0.82);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}

.overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.overlay-card {
  width: 100%;
  max-width: 280px;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid rgba(148, 163, 184, 0.24);
  border-radius: 16px;
  padding: 22px 18px;
  text-align: center;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.5);
}

.overlay-badge {
  display: inline-block;
  font-size: 0.66rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  color: var(--accent);
  background: rgba(56, 189, 248, 0.12);
  border: 1px solid rgba(56, 189, 248, 0.3);
  border-radius: 999px;
  padding: 3px 10px;
  margin-bottom: 10px;
}

.overlay-title {
  font-size: 1.55rem;
  font-weight: 800;
  margin-bottom: 6px;
}

.overlay-sub {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 16px;
  line-height: 1.45;
}

.overlay-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  background: rgba(6, 9, 17, 0.65);
  border: 1px solid rgba(148, 163, 184, 0.14);
  border-radius: 12px;
  padding: 10px 8px;
  margin-bottom: 16px;
}

.summary-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.summary-item span {
  font-size: 0.66rem;
  color: var(--text-muted);
}

.summary-item strong {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 1rem;
  font-weight: 800;
}

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

.primary-btn,
.secondary-btn {
  border: none;
  border-radius: 11px;
  padding: 11px 16px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.15s ease, background-color 0.15s ease;
}

.primary-btn {
  background: linear-gradient(135deg, #0ea5e9, #3b82f6);
  color: #ffffff;
  box-shadow: 0 8px 20px rgba(14, 165, 233, 0.32);
}

.primary-btn:hover {
  filter: brightness(1.08);
}

.primary-btn:active,
.secondary-btn:active {
  transform: scale(0.97);
}

.secondary-btn {
  background: var(--btn-bg);
  color: var(--text-primary);
  border: 1px solid var(--btn-border);
}

.secondary-btn:hover {
  background: var(--btn-bg-active);
}

.full-width {
  width: 100%;
}

.overlay-tip {
  margin-top: 12px;
  font-size: 0.72rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.hidden {
  display: none !important;
}

/* Mobile Touch Controls (Hidden on large desktop with fine pointer unless narrow screen) */
.touch-controls {
  display: none;
  flex-direction: column;
  flex-shrink: 0;
  gap: 10px;
  padding: 10px 10px 11px;
  border-radius: 18px;
  background: rgba(15, 23, 42, 0.76);
  border: 1px solid rgba(148, 163, 184, 0.15);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.38), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
}

.touch-secondary-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.touch-primary-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.touch-move-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  min-width: 0;
}

.touch-action-group {
  display: grid;
  grid-template-columns: 1fr 1.08fr;
  gap: 8px;
  min-width: 0;
}

.ctrl-btn {
  border-radius: 14px;
  border: 1px solid rgba(148, 163, 184, 0.24);
  background: linear-gradient(180deg, #1f2937, #111827);
  color: var(--text-primary);
  font-weight: 700;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.08s ease, border-color 0.08s ease;
  touch-action: none;
  min-width: 0;
}

/* Visually weakened secondary utility pills (HOLD / Soft Drop / CCW) */
.ctrl-secondary {
  height: 32px;
  border-radius: 9px;
  background: rgba(15, 23, 42, 0.52);
  border: 1px solid rgba(148, 163, 184, 0.14);
  color: #94a3b8;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  box-shadow: none;
  white-space: nowrap;
}

.ctrl-secondary:active,
.ctrl-secondary.pressed {
  transform: scale(0.96);
  background: rgba(51, 65, 85, 0.72);
  color: #e2e8f0;
  border-color: rgba(148, 163, 184, 0.3);
}

/* Large primary buttons (Left / Right / Hard Drop / Rotate) */
.ctrl-primary {
  height: 86px;
  border-radius: 16px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.48), inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

.ctrl-primary:active,
.ctrl-primary.pressed {
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.48);
}

.ctrl-move {
  background: linear-gradient(180deg, #273449, #151e2e);
  border-color: rgba(148, 163, 184, 0.34);
}

.ctrl-move:active,
.ctrl-move.pressed {
  background: #334155;
  border-color: rgba(148, 163, 184, 0.55);
}

.ctrl-rotate {
  background: linear-gradient(180deg, #0ea5e9, #0369a1);
  border-color: rgba(56, 189, 248, 0.65);
  box-shadow: 0 4px 0 rgba(3, 105, 161, 0.72), 0 6px 16px rgba(14, 165, 233, 0.24), inset 0 1px 0 rgba(255, 255, 255, 0.24);
}

.ctrl-rotate:active,
.ctrl-rotate.pressed {
  background: #38bdf8;
}

.ctrl-drop {
  background: linear-gradient(180deg, #8b5cf6, #5b21b6);
  border-color: rgba(167, 139, 250, 0.65);
  box-shadow: 0 4px 0 rgba(91, 33, 182, 0.72), 0 6px 16px rgba(139, 92, 246, 0.24), inset 0 1px 0 rgba(255, 255, 255, 0.24);
}

.ctrl-drop:active,
.ctrl-drop.pressed {
  background: #a78bfa;
}

.btn-icon {
  font-size: 1.75rem;
  line-height: 1.05;
}

.btn-text {
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  opacity: 0.92;
}

.ctrl-move .btn-text {
  font-size: 0.68rem;
  color: #cbd5e1;
  opacity: 0.78;
}

/* Responsive Breakpoint: Mobile & Tablet Portrait */
@media (max-width: 820px), (pointer: coarse) and (max-width: 1024px) {
  /* Distribute top HUD, center board, and bottom controls evenly across the viewport */
  .app-shell {
    padding: calc(10px + var(--safe-top)) 10px calc(18px + var(--safe-bottom));
    gap: 10px;
    justify-content: space-between;
  }

  .top-bar {
    gap: 6px;
  }

  .brand {
    display: none;
  }

  .top-stats-mobile {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    flex: 1;
    min-width: 0;
  }

  .mini-stat {
    min-width: 0;
    padding: 3px 4px;
    border-radius: 8px;
    background: rgba(15, 23, 42, 0.88);
  }

  .mini-label {
    font-size: 0.56rem;
    letter-spacing: 0.04em;
  }

  .mini-value {
    font-size: 0.82rem;
    font-variant-numeric: tabular-nums;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .top-actions {
    gap: 4px;
    flex-shrink: 0;
  }

  .icon-btn {
    width: 30px;
    height: 30px;
    border-radius: 8px;
  }

  .icon-glyph {
    font-size: 0.86rem;
  }

  /* Hug board height tightly so controls pull right up underneath the board */
  .arena {
    flex: 0 0 auto;
    width: 100%;
    align-items: flex-start;
    gap: 0;
  }

  .side-panel {
    display: none;
  }

  .board-stage {
    width: 100%;
    height: auto;
    justify-content: center;
    align-items: flex-start;
    padding-top: 0;
    gap: 8px;
  }

  .mobile-side-rail {
    display: flex;
  }

  .board-frame {
    border-radius: 12px;
    max-height: none;
  }

  /* Full-screen overlay on mobile so modal card is never cramped inside narrow board */
  .overlay {
    position: fixed;
    inset: 0;
    z-index: 50;
    padding: 20px;
    background: rgba(6, 9, 17, 0.78);
  }

  .overlay-card {
    max-width: 310px;
    padding: 22px 20px;
  }

  .touch-controls {
    display: flex;
    margin-top: 0;
  }
}

/* Compact height mobile screens (e.g. iPhone SE / Android in-app browsers) */
@media (max-width: 820px) and (max-height: 700px), (pointer: coarse) and (max-height: 700px) {
  .app-shell {
    padding: calc(6px + var(--safe-top)) 8px calc(10px + var(--safe-bottom));
    gap: 6px;
  }

  .touch-controls {
    padding: 7px 8px 8px;
    gap: 6px;
    border-radius: 15px;
  }

  .touch-primary-row {
    gap: 9px;
  }

  .touch-secondary-row,
  .touch-move-group,
  .touch-action-group {
    gap: 6px;
  }

  .ctrl-secondary {
    height: 28px;
    font-size: 0.66rem;
  }

  .ctrl-primary {
    height: 72px;
    border-radius: 14px;
  }

  .btn-icon {
    font-size: 1.55rem;
  }

  .btn-text {
    font-size: 0.7rem;
  }

  .mobile-side-rail {
    gap: 6px;
  }

  .mobile-restart-btn {
    height: 28px;
  }
}

/* Extra-narrow phones (<= 360px) */
@media (max-width: 360px) {
  .app-shell {
    padding: calc(3px + var(--safe-top)) 6px calc(8px + var(--safe-bottom));
  }

  .top-bar {
    gap: 4px;
  }

  .top-stats-mobile {
    gap: 3px;
  }

  .mini-stat {
    padding: 2px 2px;
  }

  .mini-value {
    font-size: 0.74rem;
  }

  .top-actions {
    gap: 3px;
  }

  .icon-btn {
    width: 27px;
    height: 27px;
    border-radius: 7px;
  }

  .icon-glyph {
    font-size: 0.8rem;
  }

  .mobile-side-rail {
    width: 66px;
    gap: 6px;
  }

  #m-hold-canvas {
    width: 56px;
    height: 45px;
  }

  #m-next-canvas {
    width: 56px;
    height: 136px;
  }

  .touch-controls {
    padding: 7px;
  }

  .touch-primary-row {
    gap: 8px;
  }

  .ctrl-primary {
    height: 76px;
  }

  .ctrl-secondary {
    font-size: 0.64rem;
  }
}

/* Short landscape phones */
@media (max-height: 520px) and (pointer: coarse) {
  .app-shell {
    gap: 4px;
    padding: 3px 8px calc(4px + var(--safe-bottom));
  }

  .touch-controls {
    padding: 5px 6px;
    gap: 5px;
  }

  .ctrl-secondary {
    height: 24px;
    font-size: 0.62rem;
  }

  .ctrl-primary {
    height: 56px;
  }

  .btn-icon {
    font-size: 1.3rem;
  }
}
