:root {
  --ink: #0c1b33;
  --muted: #5d708a;
  --paper: #eef3f9;
  --white: #ffffff;
  --line: #d5e2f2;
  --blue-950: #071422;
  --blue-900: #0c1c33;
  --blue-800: #123a73;
  --blue-600: #0b63f6;
  --sky: #5eb7ff;
  --shadow: 0 12px 32px rgba(12, 28, 51, 0.08);
  --radius: 14px;
  --touch-min: 44px;
  color-scheme: light;
}

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

html {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  padding: 10px;
  overflow-x: hidden;
  min-height: 100vh;
  color: var(--ink);
  font: 500 0.9375rem/1.4 Manrope, "Segoe UI", sans-serif;
  background:
    radial-gradient(ellipse at 50% -20%, rgba(11, 99, 246, 0.14), transparent 52%),
    linear-gradient(180deg, #f7faff 0%, var(--paper) 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.app {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.topbar {
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--blue-600);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  flex: 1 1 auto;
  text-decoration: none;
}

.brand-logo {
  display: block;
  height: 36px;
  width: auto;
  max-width: min(55vw, 180px);
  object-fit: contain;
}

.button-compact {
  flex: 0 0 auto;
  padding-left: 10px;
  padding-right: 10px;
  font-size: 0.75rem;
}

.game {
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.stats-panel,
.board-wrap,
.controls-panel {
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.stats-panel {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  justify-content: center;
  gap: 8px;
  padding: 8px;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

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

.stat {
  min-width: 0;
  padding: 6px 8px;
  background: #f4f8fd;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.label {
  display: block;
  color: var(--blue-800);
  font-size: 0.625rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  overflow-wrap: anywhere;
}

.stat strong {
  display: block;
  margin-top: 2px;
  font-size: clamp(1rem, 4.2vw, 1.25rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--blue-900);
  overflow-wrap: anywhere;
}

.next-block {
  flex: 0 1 108px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.nextbox {
  width: 100%;
  max-width: 108px;
  min-height: 72px;
  display: grid;
  place-items: center;
  background: var(--blue-950);
  border: 2px solid var(--blue-600);
  border-radius: 10px;
}

.nextbox canvas {
  display: block;
  max-width: 100%;
  width: 88px;
  height: auto;
}

.help {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
  word-wrap: break-word;
}

.help-keyboard,
.help-rules {
  display: none;
}

.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 8px;
  background: linear-gradient(180deg, #0d2244 0%, var(--blue-950) 28%);
  border: 2px solid var(--blue-600);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.board-wrap canvas {
  display: block;
  max-width: 85vw;
  width: auto;
  height: auto;
  max-height: 50vh;
  aspect-ratio: 1 / 2;
  object-fit: contain;
  background: var(--blue-950);
  border-radius: 8px;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  border-radius: 8px;
  background: rgba(7, 20, 34, 0.88);
  color: #f5f8ff;
  text-align: center;
}

.overlay.hidden {
  display: none;
}

.overlay-inner {
  width: 100%;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.overlay h2 {
  margin: 0 0 8px;
  font-size: clamp(1.25rem, 5vw, 1.5rem);
}

.overlay p {
  margin: 0 0 12px;
  color: #c5d4ea;
  font-size: 0.8125rem;
}

.controls-panel {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  padding: 10px;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.controls {
  width: 100%;
  max-width: 100%;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.controls button {
  min-height: var(--touch-min);
  min-width: 0;
  border: 1px solid #c5d6ee;
  border-radius: 12px;
  background: #f4f8fd;
  color: var(--blue-800);
  font: 700 1.25rem/1 Manrope, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  padding: 8px;
}

.controls #rotateBtn {
  grid-column: 2;
}

.controls #leftBtn {
  grid-column: 1;
  grid-row: 2;
}

.controls #downBtn {
  grid-column: 2;
  grid-row: 2;
}

.controls #rightBtn {
  grid-column: 3;
  grid-row: 2;
}

.controls .drop {
  grid-column: 1 / -1;
  grid-row: 3;
  min-height: calc(var(--touch-min) + 2px);
  font-size: 0.8125rem;
  font-weight: 800;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  background: var(--blue-600);
  color: #fff;
  border-color: var(--blue-600);
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.button {
  border: 0;
  border-radius: 11px;
  background: var(--blue-600);
  color: #fff;
  min-height: var(--touch-min);
  padding: 10px 12px;
  font: 700 0.875rem/1 Manrope, "Segoe UI", sans-serif;
  cursor: pointer;
  touch-action: manipulation;
}

.button.secondary {
  background: var(--white);
  color: var(--blue-800);
  border: 1px solid #b9cceb;
}

.button:focus-visible,
.controls button:focus-visible {
  outline: 3px solid var(--sky);
  outline-offset: 2px;
}

.mobile-help {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.75rem;
}

.foot {
  width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 0 4px;
  text-align: center;
  color: var(--muted);
  font-size: 0.6875rem;
  overflow-wrap: anywhere;
}

/* Desktop: três colunas, placar e controlos laterais */
@media (min-width: 768px) {
  body {
    padding: 16px;
  }

  .app {
    width: min(1040px, 100%);
    gap: 14px;
  }

  .topbar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
  }

  .brand {
    grid-column: 2;
    justify-content: center;
  }

  .button-compact {
    grid-column: 3;
    justify-self: end;
  }

  .game {
    display: grid;
    grid-template-columns: minmax(200px, 1fr) minmax(260px, 320px) minmax(200px, 1fr);
    grid-template-areas: "stats board controls";
    align-items: start;
    gap: 14px;
  }

  .stats-panel {
    grid-area: stats;
    flex-direction: column;
    align-items: stretch;
    padding: 14px;
  }

  .stats {
    flex: none;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .stat strong {
    font-size: 1.375rem;
  }

  .next-block {
    flex: none;
    width: 100%;
    max-width: none;
    align-items: stretch;
  }

  .nextbox {
    max-width: none;
    min-height: 110px;
  }

  .nextbox canvas {
    width: 120px;
  }

  .help-keyboard {
    display: block;
  }

  .board-wrap {
    grid-area: board;
    padding: 12px;
  }

  .board-wrap canvas {
    max-width: min(100%, 290px);
    max-height: 70vh;
  }

  .controls-panel {
    grid-area: controls;
    padding: 14px;
  }

  .help-rules {
    display: block;
  }

  .mobile-help {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .button,
  .controls button {
    transition: none;
  }
}
