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

html { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;           /* fallback */
  height: 100dvh;          /* dynamic viewport — accounts for iOS browser chrome */
  overflow: hidden;        /* body never scrolls; .page-content scrolls instead */
  background: #0f172a;
  align-items: center;
}

/* ── Page content (scrollable area above bottom chrome) ──────────────────── */
.page-content {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start; /* flex-start so long content is reachable from top */
  width: 100%;
  padding: 20px 0 8px;
}

/* ── Bottom chrome wrapper (music bar + tab bar) ─────────────────────────── */
#bottom-chrome {
  flex-shrink: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom, 0px); /* iOS home indicator */
}

/* ── Game view ────────────────────────────────────────────────────────────── */
#game-view {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  max-width: 1024px;
  padding: 0 16px;
  margin: auto; /* vertically centres the game within the flex-start page-content */
}

/* ── Status bar (above canvas) ────────────────────────────────────────────── */
#game-status {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 14px;
  background: #0c111d;
  border: 1px solid #1f2937;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
}

#game-status.mode-hint { align-items: center; }

#status-row1 {
  display: flex;
  align-items: center;
  gap: 10px;
}

#status-row2 {
  display: flex;
  align-items: flex-start;
}

#status-label {
  font: bold 22px monospace;
  flex-shrink: 0;
}

#status-label.status-hit  { color: #f59e0b; }
#status-label.status-miss { color: #ef4444; }
#status-label.status-hint { color: #9ca3af; font: 14px monospace; font-weight: normal; }

#status-sub {
  color: #6b7280;
  font: 14px monospace;
  flex-shrink: 0;
}

#status-msg {
  color: #f9fafb;
  font: 15px / 1.4 sans-serif;
  white-space: normal;
}

/* Hide empty row2 when there's no message (hint state) */
#game-status.mode-hint #status-row2 { display: none; }

@media (max-width: 520px) {
  #game-status { padding: 6px 10px; gap: 3px; }
  #status-label { font-size: 18px; }
  #status-label.status-hint { font-size: 12px; }
  #status-sub { font-size: 12px; }
  #status-msg { font-size: 13px; }
}

/* ── Canvas frame with gold corner brackets ───────────────────────────────── */
.game-frame {
  position: relative;
  display: block;
  margin-top: 16px;
  margin-bottom: 16px;
}

/* Top-left + bottom-right corner brackets */
.game-frame::before {
  content: '';
  position: absolute;
  inset: -8px;
  pointer-events: none;
  background:
    linear-gradient(#f59e0b, #f59e0b) top left    / 28px 2px no-repeat,
    linear-gradient(#f59e0b, #f59e0b) top left    / 2px 28px no-repeat,
    linear-gradient(#f59e0b, #f59e0b) bottom right / 28px 2px no-repeat,
    linear-gradient(#f59e0b, #f59e0b) bottom right / 2px 28px no-repeat;
}

/* Top-right + bottom-left corner brackets */
.game-frame::after {
  content: '';
  position: absolute;
  inset: -8px;
  pointer-events: none;
  background:
    linear-gradient(#f59e0b, #f59e0b) top right   / 28px 2px no-repeat,
    linear-gradient(#f59e0b, #f59e0b) top right   / 2px 28px no-repeat,
    linear-gradient(#f59e0b, #f59e0b) bottom left  / 28px 2px no-repeat,
    linear-gradient(#f59e0b, #f59e0b) bottom left  / 2px 28px no-repeat;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
}

/* ── Result action buttons (below canvas) ─────────────────────────────────── */
#result-actions {
  display: flex;
  gap: 8px;
  padding: 4px 2px;
  flex-wrap: wrap;
  justify-content: center;
}

#result-actions button {
  background: none;
  border: 1px solid #374151;
  color: #9ca3af;
  font: 13px monospace;
  padding: 6px 14px;
  border-radius: 4px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

#result-actions button:hover { color: #f59e0b; border-color: #f59e0b; }

/* ── Idle mode buttons (below canvas in IDLE state) ──────────────────────── */
#idle-actions {
  display: flex;
  gap: 12px;
  padding: 12px 2px 4px;
  justify-content: center;
  flex-wrap: wrap;
}

#idle-actions button {
  background: none;
  border: 2px solid #374151;
  color: #9ca3af;
  font: bold 15px monospace;
  padding: 12px 28px;
  border-radius: 8px;
  cursor: pointer;
  min-height: 48px;
  min-width: 130px;
  letter-spacing: 0.04em;
  transition: color 0.15s, border-color 0.15s;
}

#idle-actions button:hover,
#idle-actions button.active {
  color: #f59e0b;
  border-color: #f59e0b;
}

#idle-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

#idle-actions button.loading {
  color: #f59e0b;
  border-color: #f59e0b;
  opacity: 1;
  pointer-events: none;
  position: relative;
  padding-left: 40px;
}

#idle-actions button.loading::before {
  content: '';
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  border: 2px solid #f59e0b;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: translateY(-50%) rotate(360deg); }
}

#camera-error-msg {
  color: #f87171;
  font-size: 13px;
  text-align: center;
  margin: 4px 0 0;
  padding: 0 8px;
}

/* ── Tap confirm button (mobile HIDDEN state) ─────────────────────────────── */
#tap-confirm-wrap {
  display: flex;
  justify-content: center;
  padding: 10px 2px 4px;
}

#tap-confirm {
  background: #f59e0b;
  border: none;
  border-radius: 8px;
  color: #0f172a;
  font: bold 16px monospace;
  padding: 12px 40px;
  cursor: pointer;
  letter-spacing: 0.04em;
  box-shadow: 0 2px 10px rgba(245, 158, 11, 0.4);
  transition: background 0.15s;
  /* Large tap target */
  min-height: 48px;
}

#tap-confirm:active { background: #d97706; }

/* ── Leaderboard HTML view ────────────────────────────────────────────────── */
#leaderboard-view {
  flex: 1;               /* fill remaining height of .page-content */
  min-height: 0;
  width: 100%;
  max-width: 680px;
  padding: 16px 16px 0;
  display: flex;
  flex-direction: column;
}

#leaderboard-panel {
  flex: 1;
  min-height: 0;
  background: #111827;
  border: 2px solid #f59e0b;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  overflow: hidden;      /* clip; rows scroll inside */
}

/* Sticky header inside panel */
.lb-panel-head {
  flex-shrink: 0;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

/* My record card — pinned below title */
#lb-my-record {
  flex-shrink: 0;
  margin: 12px 16px;
  padding: 10px 14px;
  background: #0d1f10;
  border: 1px solid #22c55e;
  border-radius: 6px;
  display: flex;
  align-items: center;
  gap: 10px;
}
#lb-my-record.hidden { display: none; }
.lb-my-label {
  font: 11px monospace;
  color: #4ade80;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  flex-shrink: 0;
}
.lb-my-name {
  flex: 1;
  font: bold 14px monospace;
  color: #4ade80;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lb-my-rank  { font: 12px monospace; color: #6b7280; flex-shrink: 0; }
.lb-my-score { font: bold 15px monospace; color: #22c55e; flex-shrink: 0; }

/* Divider between my-record and the list */
.lb-divider {
  flex-shrink: 0;
  height: 1px;
  background: #1f2937;
  margin: 0 16px;
}

/* Scrollable rows list */
#lb-rows {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0 8px;
  scrollbar-width: thin;
  scrollbar-color: #991b1b transparent;
}

#lb-rows::-webkit-scrollbar       { width: 4px; }
#lb-rows::-webkit-scrollbar-track { background: transparent; }
#lb-rows::-webkit-scrollbar-thumb { background: #991b1b; border-radius: 2px; }

.lb-title {
  margin: 0;
  color: #f59e0b;
  font: bold 20px monospace;
  text-align: center;
  letter-spacing: 0.06em;
}

.lb-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid #1a2332;
  color: #f9fafb;
}
.lb-row:nth-child(even) { background: #0d1420; }
.lb-row:last-child { border-bottom: none; }
.lb-row.gold   { color: #f59e0b; }
.lb-row.silver { color: #d1d5db; }
.lb-row.bronze { color: #cd7c4a; }
.lb-row.me {
  background: #0d1f10 !important;
  border-left: 3px solid #22c55e;
  padding-left: 9px;
}
.lb-row.me .lb-name { color: #4ade80; }

.lb-rank {
  font: bold 13px monospace;
  width: 32px;
  text-align: center;
  flex-shrink: 0;
  color: #6b7280;
}
.lb-row.gold   .lb-rank,
.lb-row.silver .lb-rank,
.lb-row.bronze .lb-rank { font-size: 20px; color: inherit; }

.lb-meta  { flex: 1; display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.lb-name  { font: 14px monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-dist  { font: 11px monospace; color: #4b5563; }
.lb-row.gold   .lb-dist,
.lb-row.silver .lb-dist,
.lb-row.bronze .lb-dist { color: inherit; opacity: 0.6; }
.lb-row.me .lb-dist { color: #4ade80; opacity: 0.7; }

.lb-score { color: #22c55e; font: bold 15px monospace; flex-shrink: 0; letter-spacing: 0.03em; }

.lb-name s {
  text-decoration-line:  line-through;
  text-decoration-style: double;
  text-decoration-color: #fef3c7;
}

.lb-badword { color: #ef4444; font: bold 11px monospace; letter-spacing: 0.05em; }

.lb-empty {
  text-align: center;
  color: #4b5563;
  font: 14px monospace;
  padding: 60px 0;
}

/* ── Bottom tab bar — full width ──────────────────────────────────────────── */
#tab-bar {
  width: 100%;
  display: flex;
  background: #0c111d;
  border-top: 1px solid #1f2937;
}

.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 10px 0;
  background: none;
  border: none;
  cursor: pointer;
  color: #4b5563;
  transition: color 0.15s;
  position: relative;
}

.tab::before {
  content: '';
  position: absolute;
  top: 0;
  left: 20%;
  right: 20%;
  height: 2px;
  background: #f59e0b;
  border-radius: 0 0 2px 2px;
  opacity: 0;
  transition: opacity 0.15s;
}

.tab.active         { color: #f59e0b; }
.tab.active::before { opacity: 1; }
.tab:not(.active):hover { color: #9ca3af; }

.tab-icon  { font-size: 18px; line-height: 1; }
.tab-label { font: bold 10px monospace; letter-spacing: 0.08em; }

#btn-help {
  position: fixed;
  top: 12px;
  right: 14px;
  z-index: 90;
  padding: 7px 14px;
  background: #f59e0b;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  color: #0f172a;
  font: bold 11px monospace;
  letter-spacing: 0.06em;
  box-shadow: 0 2px 8px rgba(245, 158, 11, 0.45);
  transition: background 0.15s, box-shadow 0.15s;
}

#btn-help:hover {
  background: #fbbf24;
  box-shadow: 0 3px 12px rgba(245, 158, 11, 0.65);
}

/* ── Music bar (inline, full-width, above tab bar) ───────────────────────── */
#music-float {
  width: 100%;
  background: #0c111d;
  border-top: 1px solid #1f2937;
  padding: 6px 16px;
  display: flex;
  align-items: center;
}

#music-controls-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

#music-float button {
  background: none;
  border: 1px solid #374151;
  color: #6b7280;
  padding: 3px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  flex-shrink: 0;
  transition: color 0.15s, border-color 0.15s;
}

#music-float button:hover { color: #f59e0b; border-color: #f59e0b; }

#vol-slider { width: 58px; accent-color: #f59e0b; flex-shrink: 0; }

#vol-ios-note { color: #4b5563; font: 10px monospace; flex-shrink: 0; }

#track-name {
  flex: 1;
  color: #4b5563;
  font: 10px monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* ── Utility ──────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── How to Play overlay ──────────────────────────────────────────────────── */
#intro-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-start;     /* flex-start so the box can scroll out of view */
  justify-content: center;
  background: rgba(0, 0, 0, 0.82);
  z-index: 200;
  padding: 15vh 16px;          /* top/bottom padding centres it on normal viewports */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#intro-overlay.hidden { display: none; }

#intro-box {
  background: #111827;
  border: 2px solid #f59e0b;
  border-radius: 10px;
  padding: 28px;
  max-width: 680px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  margin: auto;                /* keeps it vertically centred when content fits */
}

#intro-title {
  margin: 0;
  color: #f59e0b;
  font: bold 22px monospace;
  letter-spacing: 0.04em;
  text-align: center;
}

#intro-subtitle {
  margin: 0;
  color: #9ca3af;
  font: 13px monospace;
  text-align: center;
}

#intro-modes {
  display: flex;
  flex-direction: row;
  gap: 0;
  width: 100%;
  border: 1px solid #1f2937;
  border-radius: 8px;
  overflow: hidden;
}

.intro-mode {
  flex: 1;
  padding: 18px 16px 22px;
  background: #0f172a;
  min-width: 0;
}

.intro-divider {
  width: 1px;
  background: #1f2937;
  flex-shrink: 0;
}

/* Show desktop-only / mobile-only intro sections based on body class */
.mobile-only  { display: none; }
.desktop-only { display: block; }
body.is-mobile .mobile-only  { display: block; }
body.is-mobile .desktop-only { display: none; }

.intro-mode-header {
  color: #f59e0b;
  font: bold 13px monospace;
  letter-spacing: 0.06em;
  margin-bottom: 12px;
}

.intro-steps {
  margin: 0;
  padding-left: 16px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.intro-steps li {
  color: #d1d5db;
  font: 13px / 1.5 monospace;
}

.intro-steps strong { color: #f9fafb; }

kbd {
  display: inline-block;
  background: #1f2937;
  border: 1px solid #4b5563;
  border-radius: 3px;
  padding: 1px 6px;
  font: 12px monospace;
  color: #f59e0b;
  line-height: 1.6;
}

/* ── Mobile: stack modes vertically ──────────────────────────────────────── */
@media (max-width: 520px) {
  #intro-overlay { padding: 12px 12px; }

  #intro-box { padding: 20px 16px; gap: 14px; border-radius: 8px; }

  #intro-title { font-size: 18px; }

  #intro-modes { flex-direction: column; }

  .intro-divider { width: 100%; height: 1px; }

  .intro-mode { padding: 16px 14px; }

  .intro-mode-header { font-size: 13px; margin-bottom: 10px; }

  .intro-steps li { font-size: 13px; }
}

#intro-confirm {
  background: #f59e0b;
  border: none;
  border-radius: 6px;
  color: #111827;
  cursor: pointer;
  font: bold 15px monospace;
  padding: 10px 32px;
  margin-top: 4px;
  transition: background 0.15s;
}

#intro-confirm:hover { background: #fbbf24; }

/* ── Scoring info button (inside leaderboard header) ─────────────────────── */
#btn-scoring {
  background: none;
  border: 1px solid #374151;
  color: #6b7280;
  font: bold 11px monospace;
  letter-spacing: 0.06em;
  padding: 5px 12px;
  border-radius: 20px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

#btn-scoring:hover {
  color: #f59e0b;
  border-color: #f59e0b;
}

/* ── Scoring modal overlay ────────────────────────────────────────────────── */
#scoring-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background: rgba(0, 0, 0, 0.82);
  z-index: 200;
  padding: 10vh 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#scoring-overlay.hidden { display: none; }

#scoring-box {
  background: #111827;
  border: 2px solid #f59e0b;
  border-radius: 10px;
  padding: 28px 24px 24px;
  max-width: 580px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  margin: auto;
}

#scoring-title {
  margin: 0;
  color: #f59e0b;
  font: bold 20px monospace;
  letter-spacing: 0.04em;
  text-align: center;
}

/* Top-level formula bar */
.scoring-section {
  width: 100%;
  background: #0f172a;
  border: 1px solid #1f2937;
  border-radius: 8px;
  padding: 12px 16px;
}

.scoring-formula-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.scoring-formula-label {
  font: bold 13px monospace;
  color: #9ca3af;
  letter-spacing: 0.06em;
}

.scoring-formula-eq {
  font: bold 16px monospace;
  color: #4b5563;
}

.scoring-formula-parts {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sf-accuracy {
  font: bold 14px monospace;
  color: #f59e0b;
  background: rgba(245, 158, 11, 0.1);
  border: 1px solid rgba(245, 158, 11, 0.3);
  border-radius: 4px;
  padding: 3px 10px;
}

.sf-plus {
  font: bold 15px monospace;
  color: #4b5563;
}

.sf-speed {
  font: bold 14px monospace;
  color: #38bdf8;
  background: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.3);
  border-radius: 4px;
  padding: 3px 10px;
}

/* Two cards side-by-side */
.scoring-breakdown {
  width: 100%;
  display: flex;
  gap: 12px;
}

.scoring-card {
  flex: 1;
  background: #0f172a;
  border-radius: 8px;
  padding: 14px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.accuracy-card { border: 1px solid rgba(245, 158, 11, 0.35); }
.speed-card    { border: 1px solid rgba(56, 189, 248, 0.35); }

.sc-header {
  display: flex;
  align-items: center;
  gap: 6px;
}

.sc-icon  { font-size: 16px; line-height: 1; }
.sc-title { font: bold 13px monospace; color: #f9fafb; flex: 1; letter-spacing: 0.04em; }

.accuracy-card .sc-title { color: #f59e0b; }
.speed-card    .sc-title { color: #38bdf8; }

.sc-max {
  font: 10px monospace;
  color: #4b5563;
  letter-spacing: 0.04em;
}

.sc-formula {
  font: bold 13px monospace;
  color: #d1d5db;
  background: #1f2937;
  border-radius: 4px;
  padding: 6px 10px;
  text-align: center;
  letter-spacing: 0.02em;
}

.sc-desc {
  margin: 0;
  font: 12px / 1.5 monospace;
  color: #6b7280;
}

.sc-desc strong { color: #22c55e; }

.sc-examples {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 2px;
}

.sc-ex {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3px 0;
  border-bottom: 1px solid #1a2332;
}

.sc-ex:last-child { border-bottom: none; }

.sc-ex-cond {
  font: 12px monospace;
  color: #6b7280;
}

.sc-ex-score {
  font: bold 12px monospace;
}

.sc-ex-score.acc { color: #22c55e; }
.sc-ex-score.mid { color: #f59e0b; }
.sc-ex-score.bad { color: #ef4444; }

/* Perfect score callout */
.scoring-max-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(245, 158, 11, 0.07);
  border: 1px solid rgba(245, 158, 11, 0.25);
  border-radius: 6px;
  padding: 10px 16px;
  width: 100%;
  flex-wrap: wrap;
  justify-content: center;
}

.scoring-max-label {
  font: bold 12px monospace;
  color: #9ca3af;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.scoring-max-value {
  font: bold 22px monospace;
  color: #f59e0b;
  letter-spacing: 0.04em;
}

.scoring-max-hint {
  font: 11px monospace;
  color: #4b5563;
}

#scoring-confirm {
  background: #f59e0b;
  border: none;
  border-radius: 6px;
  color: #111827;
  cursor: pointer;
  font: bold 15px monospace;
  padding: 10px 36px;
  transition: background 0.15s;
}

#scoring-confirm:hover { background: #fbbf24; }

@media (max-width: 520px) {
  #scoring-overlay { padding: 8px 12px; }
  #scoring-box { padding: 20px 14px 18px; gap: 14px; }
  #scoring-title { font-size: 17px; }
  .scoring-breakdown { flex-direction: column; }
  .scoring-card { padding: 12px; }
  .sc-formula { font-size: 12px; }
}

/* ── Name input overlay ───────────────────────────────────────────────────── */
#name-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.7);
  z-index: 100;
}

#name-overlay.hidden { display: none !important; }

#name-box {
  background: #1f2937;
  border: 2px solid #f59e0b;
  border-radius: 8px;
  padding: 24px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#name-box p {
  margin: 0;
  color: #f59e0b;
  font: 20px sans-serif;
}

#name-error {
  font: 12px monospace;
  color: #f87171;
  min-height: 16px;
  text-align: center;
}

#name-input {
  background: #111827;
  border: 1px solid #4b5563;
  border-radius: 4px;
  color: #f9fafb;
  font: 16px monospace;
  padding: 6px 12px;
  width: 200px;
  text-align: center;
  outline: none;
}

#name-input:focus { border-color: #f59e0b; }

#name-confirm {
  background: #f59e0b;
  border: none;
  border-radius: 4px;
  color: #111827;
  cursor: pointer;
  font: bold 15px monospace;
  padding: 6px 24px;
}

#name-confirm:hover { background: #fbbf24; }
