:root {
  --bg: #0b0e14;
  --panel: #141924;
  --panel-2: #1b2230;
  --ink: #eef2fa;
  --muted: #a3aec4;
  --accent: #4ade80;
  --accent-ink: #04240f;
  --danger: #fb7185;
  --danger-ink: #3d0a14;
  --warn: #fbbf24;
  --line: #2c3546;
  --radius: 12px;
  --tap: 44px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  overflow-x: hidden;
}

h1, h2, h3 { line-height: 1.15; letter-spacing: -0.02em; }

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 12px 18px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  border-radius: 0 0 10px 0;
}
.skip-link:focus { left: 0; }

/* --- top bar -------------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(11, 14, 20, 0.92);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
  /* The report pill floats at the top right; leave it room so it never sits
     on top of the navigation. */
  padding-right: 190px;
}

.topbar__brand {
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-right: 4px;
}

.topbar__nav { display: flex; gap: 4px; flex: 1; min-width: 0; }

.navbtn {
  font: inherit;
  font-weight: 550;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 14px;
  min-height: var(--tap);
  cursor: pointer;
  transition: color 140ms ease, background 140ms ease;
}
.navbtn:hover { color: var(--ink); background: var(--panel-2); }
.navbtn.is-current { color: var(--accent); background: rgba(74, 222, 128, 0.12); }

.badge {
  font-size: 0.8125rem;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 12px;
  white-space: nowrap;
}
.badge[data-state="saving"] { color: var(--warn); border-color: rgba(251, 191, 36, 0.4); }
.badge[data-state="saved"] { color: var(--accent); border-color: rgba(74, 222, 128, 0.4); }
.badge[data-state="offline"] { color: var(--danger); border-color: rgba(251, 113, 133, 0.4); }
.badge[data-state="signed-out"] { color: var(--warn); border-color: rgba(251, 191, 36, 0.4); }

/* --- screens -------------------------------------------------------------- */
.screen[hidden] { display: none; }

.screen--arena {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 14px;
  padding: 14px;
  align-items: stretch;
}

.stage {
  position: relative;
  min-height: 62vh;
  height: calc(100dvh - 120px);
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid var(--line);
}

#scene { display: block; width: 100%; height: 100%; cursor: crosshair; }

/* --- crosshair ------------------------------------------------------------ */
.crosshair {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 18px;
  height: 18px;
  pointer-events: none;
}
.crosshair::before, .crosshair::after {
  content: "";
  position: absolute;
  background: rgba(238, 242, 250, 0.9);
}
.crosshair::before { left: 8px; top: 0; width: 2px; height: 18px; }
.crosshair::after { top: 8px; left: 0; height: 2px; width: 18px; }

/* --- hud ------------------------------------------------------------------ */
.hud {
  position: absolute;
  left: 12px;
  bottom: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  max-width: calc(100% - 24px);
  pointer-events: none;
}

.hud__stat {
  background: rgba(20, 25, 36, 0.85);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 12px;
  min-width: 78px;
  backdrop-filter: blur(6px);
}

.hud__label {
  display: block;
  font-size: 0.8125rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.hud__value {
  display: block;
  font-size: 1.25rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

/* --- overlay -------------------------------------------------------------- */
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(11, 14, 20, 0.74);
  backdrop-filter: blur(3px);
}
.overlay[hidden] { display: none; }

.overlay__card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 26px;
  max-width: 30rem;
  animation: rise 220ms ease-out;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

.overlay__eyebrow {
  margin: 0 0 6px;
  font-size: 0.8125rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
}
.overlay__title { margin: 0 0 10px; font-size: clamp(1.5rem, 5vw, 2.25rem); }
.overlay__body { margin: 0 0 20px; color: var(--muted); }

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 1px 6px;
  margin: 0 2px;
  text-align: center;
  font: inherit;
  font-size: 0.8125rem;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 6px;
}

/* --- side panel ----------------------------------------------------------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  align-self: start;
}

.panel__title { margin: 0 0 4px; font-size: 1.25rem; }
.panel__lead { margin: 0 0 16px; color: var(--muted); font-size: 0.9375rem; }

.statgrid { display: grid; gap: 8px; margin: 0 0 18px; }
.statgrid__item {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  padding: 9px 12px;
  background: var(--panel-2);
  border-radius: 10px;
}
.statgrid__item dt { color: var(--muted); font-size: 0.875rem; }
.statgrid__item dd { margin: 0; font-weight: 650; font-variant-numeric: tabular-nums; }

.panel__actions, .card__actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* --- pages ---------------------------------------------------------------- */
.screen--page { padding: 22px 16px 60px; }
.page { max-width: 62rem; margin: 0 auto; }
.page__title { margin: 0 0 6px; font-size: clamp(1.6rem, 4.5vw, 2.25rem); }
.page__lead { margin: 0 0 22px; color: var(--muted); }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 16px;
}
.card--danger { border-color: rgba(251, 113, 133, 0.35); }
.card__title { margin: 0 0 12px; font-size: 1.125rem; }
.card__subtitle { margin: 22px 0 10px; font-size: 0.9375rem; color: var(--muted); }
.card__note { margin: 0 0 14px; color: var(--muted); font-size: 0.9375rem; }

.empty {
  margin: 0;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  color: var(--muted);
  font-size: 0.9375rem;
}
.empty[hidden] { display: none; }

/* --- chart ---------------------------------------------------------------- */
.chart {
  width: 100%;
  height: 180px;
  display: block;
  background: var(--panel-2);
  border-radius: 10px;
  padding: 8px;
}
.chart path { fill: none; vector-effect: non-scaling-stroke; stroke-linejoin: round; stroke-linecap: round; }
.chart__best { stroke: var(--accent); stroke-width: 2; }
.chart__avg { stroke: #7dd3fc; stroke-width: 1.5; stroke-dasharray: 3 3; }

.legend { margin: 10px 0 0; color: var(--muted); font-size: 0.8125rem; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.legend__key { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.legend__key--best { background: var(--accent); margin-right: 2px; }
.legend__key--avg { background: #7dd3fc; margin-left: 12px; margin-right: 2px; }

/* --- agent lists ---------------------------------------------------------- */
.agents { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.agents[hidden] { display: none; }

.agent-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--panel-2);
  border-radius: 10px;
}
.agent-row__rank { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; min-width: 2.2em; }
.agent-row__main { display: grid; flex: 1; min-width: 0; }
.agent-row__name { font-weight: 600; }
.agent-row__meta { color: var(--muted); font-size: 0.8125rem; }
.agent-row__score { font-weight: 700; font-variant-numeric: tabular-nums; font-size: 1.125rem; }

.behaviours { display: grid; gap: 10px; margin-bottom: 12px; }
.behaviour { display: grid; grid-template-columns: minmax(0, 9rem) 1fr auto; gap: 10px; align-items: center; }
.behaviour__name { color: var(--muted); font-size: 0.875rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.behaviour__bar { background: var(--panel-2); border-radius: 999px; height: 10px; overflow: hidden; }
.behaviour__fill { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width 400ms ease; }
.behaviour__score { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 3ch; text-align: right; }

.kvlist { display: grid; gap: 6px; margin: 0 0 8px; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 8px 12px; background: var(--panel-2); border-radius: 8px; }
.kv__key { color: var(--muted); font-size: 0.875rem; }
.kv__value { margin: 0; font-weight: 650; font-variant-numeric: tabular-nums; }

.sessions { list-style: none; margin: 0 0 14px; padding: 0; display: grid; gap: 8px; }
.sessions[hidden] { display: none; }
.session { display: grid; padding: 10px 14px; background: var(--panel-2); border-radius: 10px; }
.session__name { font-weight: 600; }
.session__meta { color: var(--muted); font-size: 0.8125rem; }

/* --- forms ---------------------------------------------------------------- */
.form { display: grid; gap: 8px; max-width: 28rem; margin-bottom: 8px; }
.form__label { font-size: 0.875rem; color: var(--muted); }
.form__input {
  font: inherit;
  color: var(--ink);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 13px;
  min-height: var(--tap);
  width: 100%;
}
.form__input::placeholder { color: #6b788f; }
.form .btn { justify-self: start; }

/* --- buttons -------------------------------------------------------------- */
.btn {
  font: inherit;
  font-weight: 600;
  padding: 11px 18px;
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 120ms ease, filter 120ms ease, background 120ms ease;
}
.btn:hover { background: #232c3d; }
.btn:active { transform: translateY(1px); }
.btn[hidden] { display: none; }

.btn--primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn--primary:hover { filter: brightness(1.08); background: var(--accent); }

.btn--danger { background: var(--danger); color: var(--danger-ink); border-color: transparent; }
.btn--danger:hover { filter: brightness(1.06); background: var(--danger); }

.btn--quiet { background: transparent; color: var(--muted); }
.btn--quiet:hover { color: var(--ink); background: var(--panel-2); }

:focus-visible {
  outline: 3px solid #9ae6ff;
  outline-offset: 2px;
  border-radius: 8px;
}

/* --- toast ---------------------------------------------------------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translate(-50%, 20px);
  margin: 0;
  padding: 12px 18px;
  max-width: min(30rem, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  color: var(--ink);
  font-size: 0.9375rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease, transform 200ms ease;
  z-index: 50;
}
.toast.is-shown { opacity: 1; transform: translate(-50%, 0); }
.toast--warn { border-left-color: var(--warn); }

/* --- touch controls ------------------------------------------------------- */
.touch { display: none; }

.touch__stick {
  position: absolute;
  left: 18px;
  bottom: 92px;
  width: 116px;
  height: 116px;
  border-radius: 50%;
  background: rgba(20, 25, 36, 0.6);
  border: 1px solid var(--line);
  touch-action: none;
}
.touch__knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46px;
  height: 46px;
  margin: -23px 0 0 -23px;
  border-radius: 50%;
  background: rgba(74, 222, 128, 0.75);
}

.touch__btn {
  position: absolute;
  right: 18px;
  width: 84px;
  height: 84px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(20, 25, 36, 0.75);
  color: var(--ink);
  font: inherit;
  font-weight: 650;
  cursor: pointer;
  touch-action: none;
}
.touch__btn--fire { bottom: 96px; background: rgba(251, 113, 133, 0.28); }
.touch__btn--jump { bottom: 16px; right: 116px; width: 72px; height: 72px; }

/* --- responsive ----------------------------------------------------------- */
@media (max-width: 900px) {
  .screen--arena { grid-template-columns: 1fr; }
  .stage { height: 58vh; min-height: 340px; }
  .panel--arena { order: 2; }
}

@media (max-width: 620px) {
  /* On a narrow screen the pill spans the top, so the whole bar starts below it. */
  .topbar { gap: 8px; padding: 56px 12px 8px; }
  /* flex-basis, not width: the base rule sets flex:1, whose basis would
     otherwise win and keep the nav on the same row, overflowing off-screen. */
  .topbar__nav { order: 3; flex: 0 0 100%; width: 100%; }
  .navbtn { flex: 1; padding: 8px 6px; }
  .badge { margin-left: auto; }
  .screen--arena { padding: 10px; gap: 10px; }
  .stage { height: 56vh; min-height: 320px; }
  .hud__stat { min-width: 66px; padding: 5px 9px; }
  .hud__value { font-size: 1.0625rem; }
  .behaviour { grid-template-columns: 1fr; gap: 4px; }
  .behaviour__score { text-align: left; }
  .card { padding: 16px; }
  .btn { width: 100%; }
  .card__actions .btn, .panel__actions .btn { flex: 1 1 100%; }
  .form .btn { justify-self: stretch; width: 100%; }
}

/* Touch controls appear only where there is no mouse. */
@media (hover: none) and (pointer: coarse) {
  .touch { display: block; }
  #scene { cursor: default; }

  /* The stick and buttons own the lower half, so the readouts move up out of
     their way rather than sitting underneath them. */
  .hud {
    top: 10px;
    bottom: auto;
    left: 10px;
    right: 10px;
    max-width: none;
    gap: 6px;
  }
  .hud__stat { min-width: 0; flex: 1 1 auto; padding: 4px 8px; }
  .hud__label { font-size: 0.8125rem; letter-spacing: 0.06em; }
  .hud__value { font-size: 0.9375rem; }
  .stage { height: 62vh; min-height: 380px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
