* {
  box-sizing: border-box;
}

:root {
  --bg: #06090f;
  --surface: #0b1019;
  --surface-2: #101725;
  --surface-3: #151d2c;
  --line: rgba(255, 255, 255, 0.10);
  --line-bright: rgba(128, 233, 255, 0.28);
  --text: #f5f8fc;
  --muted: #7d8aa0;
  --accent: #80e9ff;
  --accent-2: #b5ff6d;
  --danger: #ff6174;

  --game-width: 760px;
  --game-height: 602px;
  --game-scale: 1;
}

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

body {
  min-height: 100dvh;
  background:
    radial-gradient(circle at 18% 12%, rgba(75, 107, 255, 0.12), transparent 28rem),
    radial-gradient(circle at 84% 82%, rgba(0, 231, 255, 0.08), transparent 25rem),
    linear-gradient(180deg, #070a11, #05070c 75%);
  color: var(--text);
  font-family: Consolas, "Courier New", monospace;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

.app-shell {
  width: 100%;
  height: 100dvh;
  margin: 0 auto;
  padding: clamp(5px, 1vh, 12px) clamp(7px, 1vw, 16px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: clamp(4px, 0.65vh, 9px);
  overflow: hidden;
}

.topbar {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 0;
}

.brand {
  text-align: center;
  margin: 0;
}

.eyebrow,
.stage {
  margin: 0;
  letter-spacing: 0.18em;
  color: var(--muted);
  font-size: clamp(0.50rem, 1.05vh, 0.69rem);
  line-height: 1.1;
}

.brand h1 {
  margin: clamp(1px, 0.2vh, 3px) 0;
  letter-spacing: 0.18em;
  font-size: clamp(1.6rem, 4.6vh, 3rem);
  line-height: 0.92;
  text-shadow:
    0 0 28px rgba(128, 233, 255, 0.10),
    0 0 60px rgba(128, 233, 255, 0.04);
}

.icon-button {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.035);
  cursor: pointer;
  transition: 120ms ease;
}

.top-actions {
  position: absolute;
  right: 0;
  top: 50%;
  display: flex;
  gap: 6px;
  transform: translateY(-50%);
}

.text-icon {
  width: auto;
  min-width: 42px;
  padding: 0 8px;
  font-size: 0.58rem;
  letter-spacing: 0.07em;
}

.icon-button:hover {
  border-color: var(--line-bright);
  background: rgba(128, 233, 255, 0.08);
}

.game-fit {
  position: relative;
  width: 100%;
  min-height: 0;
  overflow: hidden;
}

.game-layout {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--game-width);
  height: var(--game-height);
  display: grid;
  grid-template-columns: 210px 302px 210px;
  gap: 19px;
  align-items: start;
  justify-content: center;
  transform:
    translate(-50%, -50%)
    scale(var(--game-scale));
  transform-origin: center center;
  will-change: transform;
}

.panel,
.board-wrap {
  background:
    linear-gradient(180deg, rgba(16, 23, 37, 0.98), rgba(8, 12, 20, 0.99));
  border: 1px solid var(--line);
  box-shadow:
    0 20px 80px rgba(0, 0, 0, 0.32),
    inset 0 1px 0 rgba(255, 255, 255, 0.025);
}

.panel {
  width: 210px;
  height: 602px;
  padding: 17px;
  overflow: hidden;
}

.stat {
  padding: 8px 0 11px;
  border-bottom: 1px solid var(--line);
}

.stat-primary strong {
  color: var(--accent);
}

.label {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 0.68rem;
  letter-spacing: 0.15em;
}

.stat strong {
  font-size: 1.37rem;
  letter-spacing: 0.07em;
}

.active-high-score {
  color: var(--accent-2);
  text-shadow: 0 0 16px rgba(181, 255, 109, 0.22);
}

.stat-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  border-bottom: 1px solid var(--line);
}

.mini-stat {
  padding: 10px 0;
}

.mini-stat + .mini-stat {
  padding-left: 12px;
  border-left: 1px solid var(--line);
}

.mini-stat strong {
  font-size: 1.05rem;
}

.controls-help {
  margin-top: 13px;
  color: #bbc5d5;
  font-size: 0.68rem;
  line-height: 1.38;
}

.controls-help p {
  margin: 4px 0;
  display: flex;
  align-items: center;
  gap: 4px;
}

kbd {
  display: inline-grid;
  min-width: 20px;
  height: 18px;
  padding: 0 4px;
  place-items: center;
  border: 1px solid rgba(255,255,255,0.13);
  background: rgba(255,255,255,0.04);
  color: #eaf1fb;
  font-size: 0.62rem;
}

.board-wrap {
  position: relative;
  width: 302px;
  height: 602px;
  overflow: hidden;
}

#gameCanvas {
  display: block;
  width: 300px;
  height: 600px;
  background: #080c14;
}

.preview-box {
  display: grid;
  place-items: center;
  min-height: 160px;
  background:
    radial-gradient(circle at center, rgba(128, 233, 255, 0.025), transparent 65%),
    rgba(0, 0, 0, 0.17);
  border: 1px solid var(--line);
  margin-bottom: 13px;
  overflow: hidden;
}

.compact-preview {
  min-height: 118px;
}

.next-preview {
  min-height: 278px;
}

#holdCanvas {
  width: 160px;
  height: 120px;
}

#nextCanvas {
  width: 160px;
  height: 280px;
}

.status-box {
  border-top: 1px solid var(--line);
  padding: 12px 0 4px;
}

#status {
  color: var(--accent-2);
  letter-spacing: 0.11em;
  font-size: 0.92rem;
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
  margin-top: 5px;
}

.primary-button,
.secondary-button,
.danger-button {
  border: 1px solid rgba(128, 233, 255, 0.26);
  background: rgba(128, 233, 255, 0.07);
  color: var(--text);
  padding: 10px 10px;
  cursor: pointer;
  transition:
    transform 120ms ease,
    background 120ms ease,
    border-color 120ms ease;
}

.primary-button {
  width: 100%;
}

.secondary-button {
  font-size: 0.70rem;
}

.primary-button:hover,
.secondary-button:hover {
  transform: translateY(-1px);
  background: rgba(128, 233, 255, 0.13);
  border-color: rgba(128, 233, 255, 0.55);
}

.danger-button {
  width: 100%;
  margin-top: 16px;
  border-color: rgba(255, 97, 116, 0.26);
  background: rgba(255, 97, 116, 0.07);
  color: #ff95a2;
}

.danger-button:hover {
  background: rgba(255, 97, 116, 0.13);
  border-color: rgba(255, 97, 116, 0.48);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  background: rgba(2, 5, 10, 0.72);
  backdrop-filter: blur(3px);
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  width: calc(100% - 42px);
  text-align: center;
  padding: 22px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  background: rgba(10, 15, 25, 0.95);
  box-shadow: 0 20px 70px rgba(0,0,0,0.35);
}

.overlay-kicker {
  margin: 0 0 8px;
  color: var(--accent);
  letter-spacing: 0.2em;
  font-size: 0.68rem;
}

.overlay h2 {
  margin: 0 0 8px;
  letter-spacing: 0.1em;
}

.overlay p {
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.5;
}

.event-banner {
  position: absolute;
  z-index: 6;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%) scale(0.94);
  min-width: 190px;
  max-width: 270px;
  padding: 9px 12px;
  text-align: center;
  color: white;
  background: rgba(6, 10, 17, 0.90);
  border: 1px solid rgba(128, 233, 255, 0.30);
  letter-spacing: 0.08em;
  font-size: 0.72rem;
  opacity: 0;
  pointer-events: none;
}

.event-banner.show {
  animation: event-pop 700ms ease both;
}

@keyframes event-pop {
  0% {
    opacity: 0;
    transform: translate(-50%, -45%) scale(0.90);
  }
  15%, 72% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -58%) scale(1.03);
  }
}

.tetris-impact {
  animation: tetris-impact 340ms ease;
}

@keyframes tetris-impact {
  0%, 100% { transform: translateX(0); filter: brightness(1); }
  18% { transform: translateX(-3px); filter: brightness(1.45); }
  36% { transform: translateX(4px); }
  54% { transform: translateX(-2px); }
  72% { transform: translateX(2px); }
}

footer {
  display: flex;
  justify-content: center;
  gap: 28px;
  margin: 0;
  color: #5f6c81;
  font-size: clamp(0.50rem, 0.95vh, 0.62rem);
  letter-spacing: 0.10em;
  line-height: 1;
  white-space: nowrap;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(2, 4, 8, 0.74);
  backdrop-filter: blur(6px);
}

.modal.hidden {
  display: none;
}

.modal-card {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(11, 16, 25, 0.98);
  box-shadow: 0 30px 100px rgba(0,0,0,0.48);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding-bottom: 15px;
  border-bottom: 1px solid var(--line);
}

.modal-header h2 {
  margin: 1px 0 0;
  letter-spacing: 0.10em;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}

.setting-row strong,
.setting-row small {
  display: block;
}

.setting-row strong {
  font-size: 0.86rem;
}

.setting-row small {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.68rem;
}

.setting-row input {
  width: 18px;
  height: 18px;
  accent-color: #80e9ff;
}

.mobile-controls {
  display: none;
}

/* Touch mode keeps the complete interface inside one viewport. */
body.touch-ui .game-layout {
  height: 720px;
  grid-template-rows: 602px 102px;
}

body.touch-ui .mobile-controls {
  grid-column: 1 / 4;
  grid-row: 2;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 46px 46px;
  gap: 6px;
  width: 100%;
  height: 102px;
  padding-top: 4px;
}

.mobile-controls button {
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.045);
  color: #e9eff8;
  font-size: 0.86rem;
  cursor: pointer;
  touch-action: manipulation;
}

.mobile-controls button.pressed {
  border-color: rgba(128, 233, 255, 0.56);
  background: rgba(128, 233, 255, 0.16);
  transform: translateY(1px);
}

.mobile-controls .wide-control {
  grid-column: span 3;
}

@media (max-height: 680px) {
  .eyebrow {
    display: none;
  }

  .brand h1 {
    font-size: clamp(1.45rem, 4.1vh, 2.25rem);
  }

  footer {
    font-size: 0.48rem;
  }
}

@media (max-width: 520px) {
  .app-shell {
    padding-left: 4px;
    padding-right: 4px;
  }

  footer {
    gap: 12px;
    letter-spacing: 0.05em;
  }

  .topbar > .icon-button {
    width: 30px;
    height: 30px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
}


/* Stage 4 — Advanced Gameplay */
.lock-meter {
  position: absolute;
  z-index: 7;
  left: 8px;
  right: 8px;
  bottom: 7px;
  height: 3px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.07);
  opacity: 0;
  transition: opacity 90ms ease;
  pointer-events: none;
}

.lock-meter.active {
  opacity: 1;
}

.lock-meter span {
  display: block;
  width: 100%;
  height: 100%;
  transform: scaleX(0);
  transform-origin: left center;
  background: linear-gradient(90deg, var(--accent-2), var(--danger));
  will-change: transform;
}

.tspin-impact {
  animation: tspin-impact 410ms ease;
}

@keyframes tspin-impact {
  0%, 100% {
    filter: brightness(1);
    box-shadow:
      0 20px 80px rgba(0, 0, 0, 0.32),
      inset 0 1px 0 rgba(255, 255, 255, 0.025);
  }
  35% {
    filter: brightness(1.45);
    box-shadow:
      0 0 34px rgba(178, 108, 255, 0.34),
      0 20px 80px rgba(0, 0, 0, 0.32);
  }
  65% {
    filter: brightness(1.18);
  }
}


/* Stage 5 — Game Modes */
.mode-row .mini-stat strong {
  font-size: 0.74rem;
  letter-spacing: 0.04em;
}

#modeTime {
  font-variant-numeric: tabular-nums;
}

.mode-card {
  width: min(540px, calc(100vw - 32px));
}

.mode-list {
  display: grid;
  gap: 9px;
  margin-top: 15px;
}

.mode-option {
  display: grid;
  grid-template-columns: 1fr;
  gap: 5px;
  width: 100%;
  padding: 13px 14px;
  text-align: left;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.025);
  cursor: pointer;
  transition:
    border-color 120ms ease,
    background 120ms ease,
    transform 120ms ease;
}

.mode-option:hover,
.mode-option.active {
  border-color: rgba(128, 233, 255, 0.46);
  background: rgba(128, 233, 255, 0.08);
}

.mode-option.active {
  transform: translateX(2px);
}

.mode-option strong {
  color: var(--text);
  font-size: 0.9rem;
  letter-spacing: 0.08em;
}

.mode-option span {
  color: #b9c3d2;
  font-size: 0.72rem;
  line-height: 1.45;
}

.mode-option small {
  color: var(--accent);
  font-size: 0.62rem;
  letter-spacing: 0.07em;
}

.mode-play-button {
  margin-top: 14px;
}

@media (max-width: 520px) {
  .top-actions {
    gap: 4px;
  }

  .top-actions .icon-button {
    height: 28px;
    min-width: 36px;
    font-size: 0.52rem;
  }

  .top-actions .icon-button:last-child {
    width: 30px;
    min-width: 30px;
  }
}


/* Stage 6 — PWA / Final Application */
.hidden {
  display: none !important;
}

.install-button {
  border-color: rgba(181, 255, 109, 0.34);
  color: var(--accent-2);
  background: rgba(181, 255, 109, 0.06);
}

.install-button:hover {
  border-color: rgba(181, 255, 109, 0.60);
  background: rgba(181, 255, 109, 0.11);
}

.connection-status {
  color: var(--accent-2);
}

.connection-status.offline {
  color: #ffb347;
}

@media (display-mode: standalone) {
  #installButton {
    display: none !important;
  }
}
