:root {
  --bg: #07060f;
  --bg-2: #120d1f;
  --panel: rgba(18, 16, 32, 0.78);
  --panel-border: rgba(255, 255, 255, 0.08);
  --ink: #f6f3ff;
  --muted: #b7afd0;
  --cyan: #2ee8ff;
  --yellow: #ffd21a;
  --purple: #c86bff;
  --green: #b8e426;
  --red: #ff5a4e;
  --orange: #ff7f0a;
  --magenta: #ff1c85;
  --glow: 0 0 24px rgba(46, 232, 255, 0.18);
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Nunito", ui-rounded, system-ui, sans-serif;
  overflow: hidden;
  height: 100dvh;
}
button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}
button:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
}

.bg {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(900px 500px at 15% -10%, rgba(200, 107, 255, 0.22), transparent 55%),
    radial-gradient(800px 480px at 90% 0%, rgba(46, 232, 255, 0.16), transparent 50%),
    radial-gradient(700px 500px at 50% 110%, rgba(255, 28, 133, 0.14), transparent 50%),
    linear-gradient(180deg, var(--bg-2), var(--bg));
}
.bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: radial-gradient(rgba(255,255,255,0.07) 1px, transparent 1px);
  background-size: 22px 22px;
  opacity: 0.35;
}

.app {
  position: relative;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: calc(8px + var(--safe-t)) 12px calc(8px + var(--safe-b));
  gap: 8px;
}

.hero {
  text-align: center;
  flex: 0 0 auto;
}
.logo {
  margin: 0;
  font-family: "Fredoka", "Nunito", sans-serif;
  font-weight: 700;
  font-size: clamp(1.8rem, 5vw, 2.8rem);
  letter-spacing: 0.02em;
  line-height: 1;
  background: linear-gradient(90deg, var(--cyan), var(--purple) 40%, var(--magenta) 70%, var(--yellow));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 16px rgba(200, 107, 255, 0.35));
}
.logo span { font-weight: 600; }
.tag {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 700;
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  width: min(960px, 100%);
  display: grid;
  grid-template-columns: minmax(120px, 180px) minmax(0, 1fr) minmax(120px, 180px);
  gap: 12px;
  align-items: stretch;
}

.rail {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  min-height: 0;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 10px 10px 12px;
  backdrop-filter: blur(12px);
  box-shadow: var(--glow);
}
.panel h2 {
  margin: 0 0 8px;
  font-size: 0.72rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}
.panel canvas {
  display: block;
  width: 100%;
  height: auto;
}

.stats { display: grid; gap: 8px; }
.stat { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.stat-label { color: var(--muted); font-size: 0.78rem; letter-spacing: 0.08em; text-transform: uppercase; }
.stat-value {
  font-family: "Fredoka", sans-serif;
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
}
.actions { display: grid; gap: 8px; }

.legend {
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.legend h2 { margin-bottom: 6px; }
.legend-list {
  display: grid;
  gap: 3px;
  margin: 0;
  padding: 0;
  min-height: 0;
  overflow-y: auto;
  list-style: none;
  scrollbar-width: none;
}
.legend-list::-webkit-scrollbar { display: none; }
.legend-item {
  --c: var(--cyan);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 5px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
  background: linear-gradient(90deg, color-mix(in srgb, var(--c) 15%, transparent), transparent 72%);
  transition: transform 0.14s ease, border-color 0.14s ease, box-shadow 0.14s ease;
}
.legend-item img {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  object-fit: contain;
  filter: drop-shadow(0 0 5px color-mix(in srgb, var(--c) 55%, transparent));
}
.legend-name {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 0.72rem;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.legend-key {
  flex: 0 0 auto;
  padding: 0 5px;
  border: 1px solid color-mix(in srgb, var(--c) 40%, transparent);
  border-radius: 6px;
  color: var(--c);
  font-family: "Fredoka", sans-serif;
  font-size: 0.62rem;
  line-height: 1.5;
}
.legend-item:hover,
.legend-item.is-active {
  transform: translateX(2px);
  border-color: color-mix(in srgb, var(--c) 55%, transparent);
  box-shadow: 0 0 14px color-mix(in srgb, var(--c) 22%, transparent);
}
.legend-item.is-active {
  background: linear-gradient(90deg, color-mix(in srgb, var(--c) 30%, transparent), transparent 78%);
}

.board-col {
  min-width: 0;
  min-height: 0;
  display: flex;
  justify-content: center;
}
.board-frame {
  position: relative;
  height: 100%;
  aspect-ratio: 1 / 2;
  max-width: 100%;
  border-radius: 22px;
  padding: 8px;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.02)),
    rgba(8, 7, 16, 0.85);
  border: 1px solid rgba(255,255,255,0.1);
  box-shadow:
    0 0 0 1px rgba(46, 232, 255, 0.08),
    0 20px 50px rgba(0,0,0,0.45),
    inset 0 0 40px rgba(200, 107, 255, 0.05);
}
#board {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 14px;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.03), transparent 30%),
    #0b0a14;
}

.overlay {
  position: absolute;
  inset: 8px;
  display: grid;
  place-items: center;
  background: rgba(7, 6, 15, 0.62);
  backdrop-filter: blur(8px);
  border-radius: 14px;
}
.overlay[hidden] { display: none; }
.overlay-card {
  width: min(92%, 280px);
  text-align: center;
  padding: 18px 16px 16px;
}
.overlay-card h2 {
  margin: 8px 0 6px;
  font-family: "Fredoka", sans-serif;
  font-size: 1.7rem;
}
.overlay-card p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 0.92rem;
}
.overlay-score {
  color: var(--ink) !important;
  font-family: "Fredoka", sans-serif;
  font-size: 1.1rem !important;
}
.bot-row {
  display: flex;
  justify-content: center;
  gap: 2px;
  margin-bottom: 6px;
}
.bot-row img {
  width: 28px;
  height: 28px;
  object-fit: contain;
  filter: drop-shadow(0 0 6px rgba(255,255,255,0.2));
}

.btn {
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 10px 16px;
  font-weight: 800;
  transition: transform 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}
.btn.primary {
  background: linear-gradient(90deg, var(--cyan), var(--purple));
  color: #12081a;
  box-shadow: 0 8px 24px rgba(46, 232, 255, 0.25);
}
.btn.ghost {
  background: rgba(255,255,255,0.04);
  border-color: rgba(255,255,255,0.1);
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.touch {
  display: none;
  width: min(520px, 100%);
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  flex: 0 0 auto;
}
.pad {
  min-height: 48px;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 14px;
  background: rgba(18, 16, 32, 0.88);
  font-size: 1.05rem;
  font-weight: 800;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.pad.accent {
  background: linear-gradient(180deg, rgba(46,232,255,0.22), rgba(200,107,255,0.16));
  border-color: rgba(46,232,255,0.35);
}
.pad:active { transform: scale(0.96); background: rgba(46,232,255,0.16); }

.keys {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px 14px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
}
kbd {
  display: inline-block;
  min-width: 1.3em;
  padding: 1px 5px;
  margin-right: 2px;
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.06);
  color: var(--ink);
  font-family: inherit;
  font-size: 0.72rem;
}

@media (max-width: 760px) {
  body, html { overflow: hidden; }
  .app { gap: 6px; }
  .tag { display: none; }
  .stage {
    grid-template-columns: 72px minmax(0, 1fr) 72px;
    gap: 6px;
  }
  .panel { padding: 6px; border-radius: 12px; }
  .panel h2 { font-size: 0.6rem; margin-bottom: 4px; }
  .stat { flex-direction: column; gap: 0; align-items: flex-start; }
  .stat-value { font-size: 0.95rem; }
  .actions { display: none; }
  .keys { display: none; }
  .touch { display: grid; }
  .legend-list { gap: 1px; }
  .legend-item { gap: 4px; padding: 1px 2px; border-radius: 8px; }
  .legend-item img { width: 14px; height: 14px; }
  .legend-name { font-size: 0.5rem; letter-spacing: -0.01em; }
  .legend-key { display: none; }
  .board-frame { padding: 5px; border-radius: 16px; }
  .logo { font-size: 1.55rem; }
}

@media (max-height: 700px) {
  .hero { display: none; }
  .keys { display: none; }
}

/* Wide-but-short windows (landscape phones) have no room left in the rail. */
@media (max-height: 600px) and (min-width: 761px) {
  .legend { display: none; }
}
@media (max-height: 500px) {
  .legend { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .legend-item { transition: none; }
  .legend-item:hover,
  .legend-item.is-active { transform: none; }
}

/* ---------- mode switch + Jev vs Opus ---------- */
.mode-nav {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 10px;
}
.mode-link {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.1);
  background: rgba(255,255,255,0.04);
  color: var(--muted);
  text-decoration: none;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.mode-link.is-active,
.mode-link[aria-current="page"] {
  color: #12081a;
  background: linear-gradient(90deg, var(--cyan), var(--purple));
  border-color: transparent;
}

html.versus-page,
body.versus-page {
  overflow: auto;
  height: auto;
  min-height: 100dvh;
}
.versus-app {
  height: auto;
  min-height: 100dvh;
  max-width: 1180px;
  margin: 0 auto;
  gap: 12px;
  padding-bottom: calc(20px + var(--safe-b));
}
.versus-controls {
  width: min(1100px, 100%);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
}
.seed-field {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 800;
}
.seed-field input {
  width: 9rem;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 999px;
  background: rgba(8, 7, 16, 0.7);
  color: var(--ink);
  padding: 8px 12px;
  font: inherit;
  letter-spacing: 0;
  text-transform: none;
}
.versus-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.baseline-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}
.baseline-toggle input { accent-color: var(--yellow); }
.honesty {
  width: min(1100px, 100%);
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1.4;
}
.versus-stage {
  width: min(1100px, 100%);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  align-items: start;
}
.versus-stage:has(.ai-card:not([hidden]):nth-child(3)) {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
.ai-card {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.ai-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.ai-head h2 {
  margin: 0;
  font-family: "Fredoka", sans-serif;
  font-size: 1.25rem;
}
.ai-head p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
}
.ai-head code {
  font-family: inherit;
  color: var(--cyan);
}
.badge {
  flex: 0 0 auto;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.05);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
}
.badge[data-state="not_connected"],
.badge[data-state="error"] {
  color: #12081a;
  background: var(--red);
  border-color: transparent;
}
.badge[data-state="thinking"],
.badge[data-state="placing"],
.badge[data-state="play"] {
  color: #12081a;
  background: var(--cyan);
  border-color: transparent;
}
.badge[data-state="over"],
.badge[data-state="done"] {
  background: rgba(200, 107, 255, 0.25);
  border-color: rgba(200, 107, 255, 0.45);
}
.versus-frame {
  width: min(100%, 280px);
  margin: 0 auto;
  height: auto;
  aspect-ratio: 1 / 2;
}
.versus-board {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 14px;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.03), transparent 30%),
    #0b0a14;
}
.versus-overlay .overlay-card { width: min(92%, 240px); }
.versus-overlay .overlay-card h2 { font-size: 1.25rem; }
.versus-overlay .overlay-card p { margin-bottom: 0; font-size: 0.82rem; }
.versus-overlay .overlay-card:has(.overlay-retry:not([hidden])) p { margin-bottom: 12px; }
.overlay-retry {
  margin-top: 2px;
  font-size: 0.85rem;
  padding: 8px 16px;
}
.overlay-retry[hidden] { display: none !important; }
.ai-card .scoreboard {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}
.ai-card .scoreboard .stat { margin: 0; }
.ai-card .scoreboard .stat-value {
  margin: 0;
  font-size: 1rem;
}
.baseline-card .ai-head h2 { color: var(--yellow); }
.baseline-card.is-hidden { display: none; }

@media (max-width: 860px) {
  .versus-stage,
  .versus-stage:has(.ai-card:not([hidden]):nth-child(3)) {
    grid-template-columns: 1fr;
  }
  .versus-frame { width: min(100%, 260px); }
  .tag { display: block; }
}
@media (max-width: 760px) {
  html.versus-page .hero,
  body.versus-page .hero { display: block; }
  .versus-app { gap: 10px; }
  .versus-btns .btn { padding: 8px 12px; font-size: 0.85rem; }
}
@media (max-height: 700px) {
  html.versus-page .hero,
  body.versus-page .hero { display: block; }
}
