:root {
  --bg: #0a0d12;
  --bg-elevated: #11151c;
  --bg-card: #141922;
  --border: #1f2630;
  --border-strong: #2a3340;

  --fg: #e6ecf3;
  --fg-muted: #8b96a5;
  --fg-dim: #566271;

  --accent: #00e5a0;
  --accent-dim: #00b37d;
  --accent-glow: rgba(0, 229, 160, 0.18);

  --red: #ff5a5f;
  --red-glow: rgba(255, 90, 95, 0.15);
  --amber: #ffb84d;
  --blue: #5ca2ff;

  --font-ui: 'Geist', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, Menlo, monospace;

  /* realm boundary line */
  --realm-edge: rgba(154, 170, 192, 0.5);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

body {
  display: grid;
  grid-template-rows: auto auto 1fr;
  height: 100dvh;
  position: relative;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 60% 40% at 50% 0%, var(--accent-glow), transparent 70%),
    linear-gradient(to bottom, rgba(17, 21, 28, 0.5), transparent 40%);
  pointer-events: none;
  z-index: 0;
}

body::after {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(to right, var(--border) 1px, transparent 1px),
    linear-gradient(to bottom, var(--border) 1px, transparent 1px);
  background-size: 80px 80px;
  opacity: 0.15;
  pointer-events: none;
  z-index: 0;
}

/* ------------------------------------------------------------
   Topbar
   ------------------------------------------------------------ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(10, 13, 18, 0.6);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  position: relative;
  z-index: 10;
}

.back {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  padding: 0.5rem 0.85rem;
  color: var(--fg-muted);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  transition: all 0.2s ease;
}
.back:hover { color: var(--accent); border-color: var(--accent); }

.title {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  color: var(--fg);
}
.title .sub { color: var(--fg-dim); font-weight: 400; }

/* ------------------------------------------------------------
   HUD
   ------------------------------------------------------------ */
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.65rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  position: relative;
  z-index: 5;
}

.hud-left {
  display: flex;
  gap: 2rem;
  align-items: flex-start;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.stat-label {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  color: var(--fg-dim);
}
.stat-value {
  font-family: var(--font-mono);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--fg);
  line-height: 1;
  letter-spacing: -0.02em;
}
.stat-value .unit {
  font-size: 0.7em;
  color: var(--fg-dim);
  font-weight: 400;
}
.stat-sub {
  font-family: var(--font-mono);
  font-size: 0.6rem;
  color: var(--fg-dim);
  letter-spacing: 0.08em;
}
.stat-sub #best-sub { color: var(--fg-muted); }

.hud-right {
  display: flex;
  gap: 0.5rem;
}

.hud-btn {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  padding: 0.5rem 0.75rem;
  color: var(--fg-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}
.hud-btn:hover { color: var(--accent); border-color: var(--accent); }
.hud-btn:active { transform: translateY(1px); }
.hud-btn .short { display: none; }

/* ------------------------------------------------------------
   Stage + board
   ------------------------------------------------------------ */
.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  padding: 1rem 1.25rem 1.25rem;
  position: relative;
  z-index: 2;
  min-height: 0;
}

.board {
  --n: 8;
  width: min(92vw, calc(100dvh - 235px), 540px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  background: var(--bg-card);
  border: 2px solid var(--border-strong);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  touch-action: manipulation;
}

.board-row { display: contents; }

.cell {
  position: relative;
  border: none;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: transparent;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.cell.last-c { border-right: none; }
.cell.last-r { border-bottom: none; }
.cell:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  z-index: 3;
}

/* Realm tints — 8 dusky hues, muted saturation so the accent pops */
.realm-0 { background-color: rgba(92, 138, 210, 0.30); }   /* steel blue  */
.realm-1 { background-color: rgba(148, 116, 220, 0.28); }  /* violet      */
.realm-2 { background-color: rgba(222, 104, 146, 0.26); }  /* rose        */
.realm-3 { background-color: rgba(230, 176, 72, 0.26); }   /* amber       */
.realm-4 { background-color: rgba(214, 92, 78, 0.26); }    /* rust        */
.realm-5 { background-color: rgba(158, 176, 86, 0.24); }   /* olive       */
.realm-6 { background-color: rgba(80, 184, 184, 0.22); }   /* sea teal    */
.realm-7 { background-color: rgba(196, 168, 124, 0.22); }  /* sand        */

/* Realm boundaries — slightly stronger than grid lines */
.cell.rb-r { border-right-color: var(--realm-edge); box-shadow: inset -1px 0 0 var(--realm-edge); }
.cell.rb-b { border-bottom-color: var(--realm-edge); box-shadow: inset 0 -1px 0 var(--realm-edge); }
.cell.rb-r.rb-b { box-shadow: inset -1px 0 0 var(--realm-edge), inset 0 -1px 0 var(--realm-edge); }

/* Cell contents */
.cell .bolt {
  width: 60%;
  height: 60%;
  display: none;
  pointer-events: none;
  filter: drop-shadow(0 0 4px var(--accent-glow)) drop-shadow(0 0 10px var(--accent-glow));
}
.cell .bolt path { fill: var(--accent); }
.cell.s-bolt .bolt { display: block; animation: boltIn 0.16s ease-out; }

.cell .dot {
  width: 15%;
  height: 15%;
  min-width: 5px;
  min-height: 5px;
  border-radius: 50%;
  background: var(--fg-dim);
  display: none;
  pointer-events: none;
}
.cell.s-mark .dot { display: block; }

@keyframes boltIn {
  from { transform: scale(0.5); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* Conflicts */
.cell.conflict::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255, 90, 95, 0.24);
  pointer-events: none;
}
.cell.conflict .bolt path { fill: var(--red); }
.cell.conflict .bolt {
  filter: drop-shadow(0 0 5px var(--red-glow)) drop-shadow(0 0 10px var(--red-glow));
}

/* Solved flash */
.board.solved .cell { cursor: default; }
.board.solved .cell.s-bolt .bolt { animation: boltWin 0.6s ease-out; }
@keyframes boltWin {
  0% { transform: scale(1); }
  40% { transform: scale(1.35); }
  100% { transform: scale(1); }
}

.status {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  color: var(--fg-dim);
  height: 1.1rem;
  line-height: 1.1rem;
}
.status.bad { color: var(--red); }
.status.good { color: var(--accent); }

/* ------------------------------------------------------------
   Overlays (start / end)
   ------------------------------------------------------------ */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: rgba(10, 13, 18, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.3s ease-out;
  overflow-y: auto;
}
.overlay[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; } }

.overlay-inner {
  max-width: 560px;
  width: 100%;
  text-align: center;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 2.5rem 2rem;
  margin: auto;
}

.overlay .eyebrow {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  color: var(--accent);
  margin-bottom: 1rem;
}
.overlay h1 {
  font-family: var(--font-mono);
  font-size: clamp(1.4rem, 4vw, 2.1rem);
  font-weight: 600;
  color: var(--fg);
  line-height: 1.15;
  margin-bottom: 1rem;
  letter-spacing: -0.02em;
}
.overlay h1 .accent { color: var(--accent); }
.overlay .lede {
  color: var(--fg-muted);
  font-size: 0.95rem;
  line-height: 1.55;
  margin-bottom: 1.5rem;
}

.keys {
  display: flex;
  justify-content: center;
  gap: 1.1rem;
  margin-bottom: 1.75rem;
  flex-wrap: wrap;
}
.key-group {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--fg-muted);
  letter-spacing: 0.08em;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.kbd {
  display: inline-block;
  min-width: 1.8em;
  padding: 2px 6px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 3px;
  color: var(--accent);
  font-weight: 600;
  text-align: center;
}

.primary {
  background: var(--accent);
  color: var(--bg);
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.9rem;
  letter-spacing: 0.1em;
  padding: 0.9rem 2.2rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.primary:hover {
  background: var(--fg);
  transform: translateY(-1px);
}

.overlay-cta {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 1.5rem;
}
.ghost {
  background: transparent;
  color: var(--fg-muted);
  font-family: var(--font-mono);
  font-weight: 500;
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  padding: 0.9rem 1.4rem;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.2s ease;
  display: inline-block;
}
.ghost:hover {
  color: var(--fg);
  border-color: var(--fg-muted);
}

.pb-tag {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  color: var(--bg);
  background: var(--accent);
  border-radius: 3px;
  padding: 0.25rem 0.6rem;
  margin-bottom: 0.75rem;
}
.pb-tag[hidden] { display: none; }

/* ------------------------------------------------------------
   Responsive
   ------------------------------------------------------------ */
@media (max-width: 640px) {
  .topbar { padding: 0.75rem 1rem; }
  .title { font-size: 0.75rem; }
  .title .sub { display: none; }

  .hud { padding: 0.55rem 0.75rem; gap: 0.5rem; }
  .hud-left { gap: 1rem; }
  .stat-value { font-size: 1.05rem; }
  .stat-sub { font-size: 0.55rem; }
  .hud-btn { padding: 0.45rem 0.55rem; font-size: 0.62rem; }
  .hud-btn .long { display: none; }
  .hud-btn .short { display: inline; }

  .stage { padding: 0.75rem 0.75rem 1rem; }
  .board { width: min(94vw, calc(100dvh - 205px), 540px); }

  .overlay-inner { padding: 1.75rem 1.25rem; }
  .keys { gap: 0.6rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
