:root {
  --bg: #070b18;
  --field: #0b1226;
  --ink: #e8ecff;
  --muted: #8a93b8;
  --accent: #39e6ff;
  --accent-2: #ff4fa3;
  --gold: #ffd166;
  --panel: rgba(10, 16, 36, 0.86);
  --line: rgba(140, 160, 230, 0.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(ellipse at center, #111a3a 0%, var(--bg) 70%);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

body {
  display: grid;
  place-items: center;
}

#stage {
  position: relative;
  width: min(96vw, calc(94vh * 1.6));
  aspect-ratio: 8 / 5;
  container-type: size;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 0 60px rgba(57, 230, 255, 0.08), 0 20px 60px rgba(0, 0, 0, 0.5);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: none;
}

/* ---------- overlay screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(5, 8, 20, 0.62);
  backdrop-filter: blur(3px);
  font-size: clamp(11px, 2.1cqw, 18px);
}
.hidden { display: none !important; }

.menu-grid {
  display: grid;
  grid-template-columns: 1.15fr 1fr;
  gap: 4cqw;
  width: 86%;
  align-items: center;
}

.title {
  margin: 0 0 0.6em;
  font-size: 4.2em;
  font-weight: 900;
  letter-spacing: 0.08em;
  line-height: 1;
  color: var(--ink);
  text-shadow: 0 0 18px rgba(57, 230, 255, 0.55);
}
.title span {
  display: block;
  font-size: 0.32em;
  letter-spacing: 0.5em;
  color: var(--accent);
  text-transform: uppercase;
  margin-top: 0.35em;
  text-shadow: none;
}

h2 {
  margin: 0 0 0.8em;
  font-size: 1.3em;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
}

.field { display: grid; gap: 0.35em; margin-bottom: 1.1em; }
.field > span, .setting > span {
  font-size: 0.8em;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

input[type="text"] {
  font: inherit;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.55em 0.75em;
  outline: none;
}
input[type="text"]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(57, 230, 255, 0.18); }

.buttons { display: grid; gap: 0.6em; }
.buttons.row { grid-auto-flow: column; justify-content: end; margin-top: 1.2em; }

.btn {
  font: inherit;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.7em 1.4em;
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.15s ease, background 0.15s ease;
}
.btn:hover:not(:disabled), .btn:focus-visible {
  border-color: var(--accent);
  background: rgba(57, 230, 255, 0.1);
  outline: none;
}
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn.primary {
  background: linear-gradient(135deg, rgba(57, 230, 255, 0.25), rgba(255, 79, 163, 0.2));
  border-color: rgba(57, 230, 255, 0.6);
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn small.tag { background: rgba(57, 230, 255, 0.2); color: var(--accent); }

.board-sub {
  display: block;
  margin-top: 0.35em;
  font-size: 0.55em;
  letter-spacing: 0.1em;
  color: var(--muted);
}
.note {
  margin: -0.3em 0 1.1em;
  font-size: 0.8em;
  color: var(--gold);
}
.btn small {
  font-size: 0.6em;
  padding: 0.15em 0.5em;
  margin-left: 0.5em;
  border-radius: 99px;
  background: var(--accent-2);
  color: #fff;
  vertical-align: middle;
}

.hint { margin: 1.2em 0 0; font-size: 0.75em; color: var(--muted); line-height: 1.6; }
kbd {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.9em;
  padding: 0.05em 0.4em;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--ink);
}

/* ---------- leaderboard ---------- */
.board {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1.3em 1.4em;
}
#leaderboard { list-style: none; margin: 0; padding: 0; counter-reset: rank; }
#leaderboard li {
  counter-increment: rank;
  display: grid;
  grid-template-columns: 1.8em 1fr auto;
  align-items: baseline;
  gap: 0.5em;
  padding: 0.5em 0;
  border-bottom: 1px solid rgba(140, 160, 230, 0.12);
}
#leaderboard li:last-child { border-bottom: none; }
#leaderboard li::before {
  content: counter(rank);
  font-weight: 800;
  color: var(--muted);
}
#leaderboard li:nth-child(1)::before { color: var(--gold); }
#leaderboard li .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
#leaderboard li .meta { display: block; font-size: 0.65em; color: var(--muted); font-weight: 400; }
#leaderboard li .time { font-family: ui-monospace, Consolas, monospace; color: var(--accent); }
#leaderboard li.highlight { color: var(--gold); }
#leaderboard li.highlight .time { color: var(--gold); }
.muted { color: var(--muted); margin: 0; }

/* ---------- panels ---------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1.6em 2em;
  width: min(32em, 80%);
}
.panel.narrow { width: min(20em, 70%); text-align: center; }

.setting { display: grid; gap: 0.5em; margin-bottom: 1.1em; }
.setting > span b { color: var(--ink); float: right; letter-spacing: 0; text-transform: none; }
.setting.check { grid-template-columns: 1fr auto; align-items: center; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
input[type="checkbox"] { width: 1.2em; height: 1.2em; accent-color: var(--accent); }

.segmented { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.segmented button {
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: none;
  padding: 0.55em;
  cursor: pointer;
}
.segmented button[aria-checked="true"] { color: #06101f; background: var(--accent); }

.big-time {
  margin: 0.1em 0 0.3em;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 2.8em;
  font-weight: 800;
  color: var(--accent);
  text-shadow: 0 0 16px rgba(57, 230, 255, 0.5);
}
#result-msg { margin: 0 0 1.2em; min-height: 1.4em; line-height: 1.4; }
#result-msg.record { color: var(--gold); font-weight: 700; }

/* ---------- online lobby ---------- */
.panel.lobby { width: min(34em, 84%); }
#lobby-list {
  list-style: none;
  margin: 0.4em 0 0.8em;
  padding: 0;
  max-height: 14em;
  overflow-y: auto;
}
#lobby-list li {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.8em;
  padding: 0.5em 0;
  border-bottom: 1px solid rgba(140, 160, 230, 0.12);
}
#lobby-list li:last-child { border-bottom: none; }
#lobby-list .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
#lobby-list .who small { display: block; font-weight: 400; font-size: 0.75em; color: var(--muted); }
#lobby-list .who small.incoming { color: var(--gold); }
#lobby-list .actions { display: flex; gap: 0.4em; }
#lobby-list .btn { padding: 0.4em 0.9em; font-size: 0.8em; }
#lobby-list li.me .who { color: var(--accent); }
.lobby-foot { display: flex; align-items: center; justify-content: space-between; gap: 1em; margin-top: 0.6em; }
.small { font-size: 0.75em; }
