:root {
  --ink: #20231f;
  --muted: #676b61;
  --paper: #f2efe7;
  --card: #fffdf7;
  --line: #20231f;
  --accent: #c92c2c;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(circle at 12% 0%, rgba(255,255,255,.9), transparent 28rem),
    var(--paper);
  color: var(--ink);
  font-family: "Courier New", Courier, monospace;
  overscroll-behavior: none;
}

button, a, input { font: inherit; }

.page-shell {
  width: min(1120px, calc(100% - 28px));
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 28px 0 18px;
}

.topbar,
.leaderboard-head {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 24px;
}

.topbar { margin-bottom: 12px; }

.top-actions {
  display: flex;
  align-items: end;
  gap: 18px;
}

.eyebrow, .kicker {
  margin: 0 0 4px;
  font-size: 12px;
  letter-spacing: .16em;
  font-weight: 700;
}

h1 {
  margin: 0;
  font-size: clamp(30px, 5vw, 58px);
  letter-spacing: -.08em;
  line-height: .9;
}

.live-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 9px;
  border: 2px solid var(--line);
  background: var(--card);
  padding: 5px 8px;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .06em;
}

.live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #2d8a47;
  box-shadow: 0 0 0 3px rgba(45,138,71,.14);
}

.score-panel {
  display: flex;
  gap: 20px;
  font-size: clamp(10px, 1.8vw, 13px);
  white-space: nowrap;
}

.score-panel span { color: var(--muted); }
.score-panel strong { color: var(--ink); }

.game-card {
  position: relative;
  border: 3px solid var(--line);
  background: var(--card);
  box-shadow: 8px 8px 0 var(--line);
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  background: #fbfaf4;
  image-rendering: pixelated;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(251, 250, 244, .72);
  backdrop-filter: blur(2px);
  overflow: auto;
}

.hidden { display: none !important; }

.overlay-box {
  text-align: center;
  width: min(540px, 100%);
  padding: 24px 28px;
  border: 3px solid var(--line);
  background: var(--card);
  box-shadow: 7px 7px 0 var(--line);
}

.overlay-box h2 {
  margin: 3px 0 8px;
  font-size: clamp(24px, 5vw, 42px);
  line-height: 1;
  letter-spacing: -.06em;
}

.overlay-box p { margin: 8px 0 14px; }
.game-intro { max-width: 470px; color: var(--muted); font-size: 11px; line-height: 1.45; }
.controls-line { font-weight: 800; letter-spacing: .04em; }

.username-label {
  display: block;
  margin: 16px 0 6px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
}

.username-input {
  width: min(320px, 100%);
  border: 3px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 11px 12px;
  outline: none;
  text-align: center;
  font-weight: 800;
  box-shadow: 3px 3px 0 #878b7e;
}

.username-input:focus { box-shadow: 5px 5px 0 #878b7e; }
.input-help, .share-hint { color: var(--muted); font-size: 10px; }
.form-error { color: var(--accent); font-size: 11px; font-weight: 800; }
.submit-status { min-height: 16px; color: var(--muted); font-size: 11px; }

button {
  border: 3px solid var(--line);
  background: var(--ink);
  color: white;
  padding: 11px 20px;
  cursor: pointer;
  font-weight: 800;
  box-shadow: 4px 4px 0 #878b7e;
}

button:hover { transform: translate(-1px, -1px); box-shadow: 5px 5px 0 #878b7e; }
button:active { transform: translate(3px, 3px); box-shadow: 1px 1px 0 #878b7e; }
button.secondary { background: var(--card); color: var(--ink); }
button:disabled { opacity: .55; cursor: not-allowed; transform: none; }

.actions { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }
.small-button { padding: 7px 11px; font-size: 10px; box-shadow: 3px 3px 0 #878b7e; }

.mobile-hint {
  display: none;
  margin-top: 12px;
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .09em;
}

.feature-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 18px;
}

.feature-strip span {
  border: 2px solid var(--line);
  background: rgba(255,253,247,.65);
  padding: 8px 10px;
  text-align: center;
  font-size: 10px;
  font-weight: 800;
}

.leaderboards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  margin-top: 18px;
}

.leaderboard-card {
  border: 3px solid var(--line);
  background: var(--card);
  padding: 18px 20px 16px;
  box-shadow: 6px 6px 0 var(--line);
}

.leaderboard-head h2 { margin: 0; font-size: 26px; letter-spacing: -.06em; }

.leaderboard-day {
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
}

.leaderboard-list {
  list-style: none;
  counter-reset: rank;
  margin: 16px 0 0;
  padding: 0;
  border-top: 2px solid var(--line);
}

.leaderboard-list li {
  counter-increment: rank;
  display: grid;
  grid-template-columns: 42px 1fr auto;
  gap: 10px;
  align-items: center;
  min-height: 38px;
  border-bottom: 1px dashed #9b9e94;
  font-size: 13px;
}

.leaderboard-list li::before { content: counter(rank) "."; color: var(--muted); font-weight: 800; }
.leaderboard-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 800; }
.leaderboard-score { font-weight: 800; }
.leaderboard-list .leaderboard-empty { grid-template-columns: 1fr; color: var(--muted); }
.leaderboard-list .leaderboard-empty::before { display: none; }
.leaderboard-status { margin: 10px 0 0; min-height: 14px; font-size: 10px; color: var(--muted); }

footer {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  margin-top: 20px;
  color: var(--muted);
  font-size: 10px;
}

footer p { margin: 0; max-width: 760px; }
footer a { color: inherit; font-weight: 700; }

@media (max-width: 780px) {
  body { background: var(--paper); }

  .page-shell {
    width: 100%;
    padding: max(10px, env(safe-area-inset-top)) 8px max(14px, env(safe-area-inset-bottom));
    justify-content: flex-start;
  }

  .topbar {
    align-items: start;
    gap: 8px;
    padding: 0 3px;
  }

  .top-actions {
    align-items: end;
    gap: 7px;
  }

  .score-panel {
    flex-direction: column;
    gap: 1px;
    text-align: right;
    font-size: 9px;
  }

  .live-pill {
    margin-top: 7px;
    padding: 4px 6px;
    font-size: 9px;
  }

  .game-card {
    box-shadow: 4px 4px 0 var(--line);
  }

  canvas {
    width: 100%;
    height: auto;
    min-height: 0;
  }

  .overlay {
    padding: 7px;
    align-items: center;
  }

  .overlay-box {
    width: min(96%, 430px);
    max-height: 96%;
    overflow: auto;
    padding: 12px 10px;
    box-shadow: 4px 4px 0 var(--line);
  }

  .overlay-box h2 {
    font-size: clamp(22px, 8vw, 34px);
    margin-bottom: 5px;
  }

  .overlay-box p { margin: 4px 0 7px; }
  .game-intro { display: none; }
  .username-label { margin-top: 7px; }
  .username-input { padding: 8px 9px; }
  .input-help { display: none; }
  .share-hint { display: none; }
  .controls-line { font-size: 10px; }

  .mobile-hint { display: block; }

  .feature-strip {
    grid-template-columns: repeat(2, 1fr);
    margin-top: 14px;
  }

  .leaderboards {
    grid-template-columns: 1fr;
    gap: 12px;
    margin-top: 14px;
  }

  .leaderboard-card {
    padding: 13px 11px;
    box-shadow: 4px 4px 0 var(--line);
  }

  .leaderboard-list { margin-top: 10px; }
  .leaderboard-list li { grid-template-columns: 32px 1fr auto; min-height: 34px; font-size: 12px; }

  footer {
    flex-direction: column;
    padding: 0 3px;
  }
}

@media (max-width: 520px) {
  h1 { font-size: 34px; }
  .top-actions { flex-direction: column; }
  .small-button { padding: 5px 7px; font-size: 9px; }
  .feature-strip span { font-size: 9px; padding: 7px 5px; }
}

@media (orientation: landscape) and (max-height: 520px) {
  .page-shell { padding-top: 6px; }
  .topbar { margin-bottom: 6px; }
  .feature-strip, .leaderboards, footer, .mobile-hint { display: none; }
  .game-card { max-height: calc(100vh - 92px); }
  canvas { max-height: calc(100vh - 92px); width: auto; max-width: 100%; margin: 0 auto; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
