/* ===== Tetris — System 96 / Neon Arcade ===== */
:root {
  --bg: #131313;
  --hdr-bg: #353534;
  --surface: #2a2a2a;
  --surface-low: #1c1b1b;
  --surface-high: #353534;
  --titlebar: linear-gradient(90deg, #1a237e, #311b92);
  --text: #e5e2e1;
  --text-muted: #bcc9c6;
  --accent: #66d9cc;
  --accent-dim: rgba(102, 217, 204, 0.18);
  --danger: #ef5350;
  --gold: #ffd36a;
  --silver: #c0c8d0;
  --bronze: #d4956a;
  --bev-out: inset 1px 1px 0 rgba(255, 255, 255, 0.15), inset -1px -1px 0 #000;
  --bev-in: inset 1px 1px 0 #000, inset -1px -1px 0 rgba(255, 255, 255, 0.1);
  --panel-glow: 0 0 24px rgba(102, 217, 204, 0.08);
  --font-ui: "IBM Plex Sans", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", "Courier Prime", monospace;
  --font-display: "IBM Plex Sans", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  font-family: var(--font-mono);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

.mat-icon {
  font-family: "Material Symbols Outlined";
  font-style: normal;
  font-size: 20px;
  line-height: 1;
  display: inline-block;
  font-variation-settings: "FILL" 0, "wght" 400, "GRAD" 0, "opsz" 24;
  user-select: none;
  vertical-align: middle;
}

/* ===== Header ===== */
header {
  height: 28px;
  background: var(--hdr-bg);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  flex-shrink: 0;
  box-shadow: inset 1px 1px 0 rgba(255, 255, 255, 0.08),
    inset -1px -1px 0 rgba(0, 0, 0, 0.5);
  z-index: 50;
}

.hdr-logo {
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 6px;
  color: #fff;
  text-decoration: none;
}

.hdr-logo:hover {
  color: var(--accent);
}

.hdr-right {
  display: flex;
  align-items: center;
  gap: 2px;
}

.hdr-icon-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 22px;
  border-radius: 2px;
}
.hdr-icon-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}
.hdr-icon-btn[aria-pressed="false"] {
  opacity: 0.55;
}

/* ===== Main layout ===== */
.main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(140px, 200px) minmax(0, 1fr) minmax(180px, 240px);
  gap: 12px;
  padding: 12px;
  background:
    radial-gradient(ellipse 80% 60% at 50% 40%, rgba(26, 35, 126, 0.18), transparent 70%),
    var(--bg);
}

/* ===== Panels ===== */
.panel {
  background: var(--surface);
  box-shadow: var(--bev-out), var(--panel-glow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.panel-titlebar {
  background: var(--titlebar);
  color: #fff;
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  height: 22px;
}

.panel-body {
  padding: 10px;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.side-block {
  background: var(--surface-low);
  box-shadow: var(--bev-in);
  padding: 8px;
}

.side-label {
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 6px;
}

.stat-grid {
  display: grid;
  gap: 8px;
}

.stat {
  background: var(--surface-low);
  box-shadow: var(--bev-in);
  padding: 8px 10px;
}

.stat-label {
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.stat-value {
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 700;
  color: var(--accent);
  text-shadow: 0 0 12px rgba(102, 217, 204, 0.35);
  line-height: 1.2;
  margin-top: 2px;
}

#hold-canvas,
#next-canvas {
  display: block;
  width: 100%;
  background: #0a0a12;
  box-shadow: var(--bev-in);
}

#hold-canvas {
  height: 88px;
}
#next-canvas {
  height: 260px;
  flex: 1;
  min-height: 160px;
}

.controls-hint {
  font-size: 10px;
  color: var(--text-muted);
  line-height: 1.45;
  font-family: var(--font-ui);
}
.controls-hint kbd {
  display: inline-block;
  background: var(--surface-high);
  box-shadow: var(--bev-out);
  padding: 0 4px;
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--text);
  margin: 0 1px;
}

/* ===== Stage (center) ===== */
.stage-wrap {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.stage-panel {
  flex: 1;
  min-height: 0;
  position: relative;
  background: var(--surface);
  box-shadow: var(--bev-out), var(--panel-glow);
  display: flex;
  flex-direction: column;
}

#stage {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: #08080f;
}

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

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(8, 8, 15, 0.78);
  backdrop-filter: blur(4px);
  z-index: 10;
  padding: 16px;
}
.overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--surface);
  box-shadow: var(--bev-out), 0 12px 40px rgba(0, 0, 0, 0.55);
  width: min(360px, 100%);
  max-height: 100%;
  overflow: auto;
}

.overlay-card .panel-titlebar {
  justify-content: center;
  letter-spacing: 0.12em;
}

.overlay-body {
  padding: 20px 18px 18px;
  text-align: center;
}

.overlay-title {
  font-family: var(--font-display);
  font-size: 36px;
  font-weight: 700;
  letter-spacing: 0.18em;
  color: #fff;
  text-shadow: 0 0 24px rgba(102, 217, 204, 0.55), 0 2px 0 #000;
  margin-bottom: 6px;
}

.overlay-sub {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.final-score {
  font-family: var(--font-mono);
  font-size: 32px;
  font-weight: 700;
  color: var(--gold);
  text-shadow: 0 0 16px rgba(255, 211, 106, 0.4);
  margin-bottom: 14px;
}
.final-score.hidden {
  display: none;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}

.btn {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 700;
  padding: 8px 16px;
  border: none;
  cursor: pointer;
  background: var(--surface-high);
  color: var(--text);
  box-shadow: var(--bev-out);
  min-width: 100px;
}
.btn:active {
  box-shadow: var(--bev-in);
  transform: translate(0.5px, 0.5px);
}
.btn.primary {
  background: linear-gradient(180deg, #3d8f88, #1f5c56);
  color: #fff;
  outline: 1px solid var(--accent);
  box-shadow: var(--bev-out), 0 0 16px rgba(102, 217, 204, 0.25);
}
.btn.primary:hover {
  filter: brightness(1.08);
}
.btn.ghost {
  background: transparent;
  box-shadow: none;
  color: var(--text-muted);
  text-decoration: underline;
  min-width: auto;
}
.btn.hidden {
  display: none;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Name entry */
.name-panel {
  text-align: left;
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.name-panel.hidden {
  display: none;
}
.name-panel label {
  display: block;
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.name-panel input {
  width: 100%;
  font-family: var(--font-mono);
  font-size: 16px;
  padding: 8px 10px;
  background: var(--surface-low);
  color: var(--text);
  border: none;
  box-shadow: var(--bev-in);
  outline: none;
}
.name-panel input:focus {
  outline: 1px solid var(--accent);
}
.name-error {
  min-height: 18px;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--danger);
  margin-top: 6px;
}
.submit-result {
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
  text-align: center;
}
.submit-result.ok {
  color: var(--accent);
}

/* ===== Leaderboard ===== */
.lb-status {
  font-family: var(--font-ui);
  font-size: 11px;
  color: var(--text-muted);
  min-height: 16px;
}

#leaderboard-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-right: 2px;
}

.lb-row {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 6px;
  align-items: center;
  padding: 6px 8px;
  background: var(--surface-low);
  box-shadow: var(--bev-in);
  font-size: 12px;
}

.lb-rank {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--text-muted);
  text-align: center;
}
.lb-name {
  font-family: var(--font-ui);
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lb-score {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--accent);
  font-size: 11px;
}

.lb-row.top-1 {
  background: linear-gradient(90deg, rgba(255, 211, 106, 0.15), transparent);
}
.lb-row.top-1 .lb-rank {
  color: var(--gold);
}
.lb-row.top-2 {
  background: linear-gradient(90deg, rgba(192, 200, 208, 0.12), transparent);
}
.lb-row.top-2 .lb-rank {
  color: var(--silver);
}
.lb-row.top-3 {
  background: linear-gradient(90deg, rgba(212, 149, 106, 0.12), transparent);
}
.lb-row.top-3 .lb-rank {
  color: var(--bronze);
}

.lb-meta {
  font-family: var(--font-ui);
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 8px;
  line-height: 1.4;
}

/* ===== Touch controls ===== */
#touch-controls {
  display: none;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  padding: 8px 0 0;
  flex-shrink: 0;
}

.tbtn {
  height: 48px;
  background: var(--surface);
  color: var(--text);
  border: none;
  box-shadow: var(--bev-out);
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  touch-action: manipulation;
}
.tbtn .mat-icon {
  font-size: 22px;
}
.tbtn.active,
.tbtn:active {
  box-shadow: var(--bev-in);
  background: var(--accent-dim);
  color: var(--accent);
}

/* ===== Help modal ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal.hidden {
  display: none;
}
.modal-card {
  background: var(--surface);
  box-shadow: var(--bev-out), 0 16px 48px rgba(0, 0, 0, 0.5);
  width: min(420px, 100%);
  max-height: min(80dvh, 560px);
  display: flex;
  flex-direction: column;
}
.modal-body {
  padding: 14px 16px 16px;
  overflow-y: auto;
  font-family: var(--font-ui);
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
}
.modal-body h3 {
  font-size: 13px;
  color: var(--accent);
  margin: 12px 0 6px;
  letter-spacing: 0.04em;
}
.modal-body h3:first-child {
  margin-top: 0;
}
.modal-body ul {
  padding-left: 18px;
  color: var(--text-muted);
}
.modal-body li {
  margin-bottom: 4px;
}
.modal-body kbd {
  background: var(--surface-high);
  box-shadow: var(--bev-out);
  padding: 0 5px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text);
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
  .main {
    grid-template-columns: 110px minmax(0, 1fr) 150px;
    gap: 8px;
    padding: 8px;
  }
  .stat-value {
    font-size: 16px;
  }
  #next-canvas {
    height: 200px;
  }
  .controls-hint {
    display: none;
  }
}

@media (max-width: 700px) {
  body {
    overflow: auto;
  }
  .main {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(48dvh, 1fr) auto;
    overflow-y: auto;
  }
  .left-col {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
  }
  .left-col .panel-body {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .left-col .stat-grid {
    grid-template-columns: 1fr;
    width: 100%;
    grid-column: 1 / -1;
  }
  .left-col .side-block.hold-block,
  .left-col .side-block.next-block {
    flex: 1;
  }
  #next-canvas {
    height: 100px;
  }
  #hold-canvas {
    height: 72px;
  }
  .right-col {
    max-height: 220px;
  }
  #touch-controls {
    display: grid;
  }
  .overlay-title {
    font-size: 28px;
  }
}

@media (max-width: 420px) {
  .left-col {
    grid-template-columns: 1fr 1fr;
  }
}
