:root {
  --gray: #6b7280;
  --red: #dc2626;
  --green: #16a34a;
  --amber: #d97706;
  --teal: #0d9488;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

/* ---------- Reaction test ---------- */

.test-page {
  background: var(--gray);
}

.stage {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  color: #fff;
  text-align: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background-color 140ms ease;
}

.stage[data-state="idle"] {
  background: var(--gray);
}

.stage[data-state="waiting"] {
  background: var(--red);
}

.stage[data-state="go"],
.stage[data-state="result"] {
  background: var(--green);
}

.stage[data-state="early"] {
  background: var(--amber);
}

.stage[data-state="saved"] {
  background: var(--teal);
}

.stage-content h1 {
  margin: 0 0 0.6rem;
  font-size: clamp(2.1rem, 7vw, 5rem);
  letter-spacing: -0.035em;
  line-height: 1.05;
}

.stage-content p {
  margin: 0;
  font-size: clamp(1rem, 2vw, 1.35rem);
  opacity: 0.92;
}

.stats-link {
  position: fixed;
  right: 1.1rem;
  bottom: 1.1rem;
  z-index: 20;
  padding: 0.6rem 1rem;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.35);
  color: #fff;
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 600;
  backdrop-filter: blur(8px);
  transition: background-color 140ms ease;
}

.stats-link:hover {
  background: rgba(15, 23, 42, 0.55);
}

/* ---------- Save modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(6px);
}

.modal[hidden] {
  display: none;
}

.modal form {
  width: min(440px, 100%);
  max-height: calc(100dvh - 2rem);
  overflow-y: auto;
  padding: 1.75rem;
  border-radius: 20px;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 30px 70px -25px rgba(15, 23, 42, 0.6);
}

.modal h2 {
  margin: 0;
  font-size: 1.3rem;
  letter-spacing: -0.02em;
}

.rt-display {
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
  margin: 0.5rem 0 0.25rem;
  font-size: 2.6rem;
  font-weight: 700;
  letter-spacing: -0.04em;
  color: var(--green);
}

.rt-display small {
  font-size: 1rem;
  font-weight: 600;
  color: var(--muted);
}

.form-hint {
  margin: 0 0 1.2rem;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.5;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 0.9rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: #334155;
}

.field em {
  font-style: normal;
  font-weight: 400;
  color: var(--muted);
}

.field input {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border: 1px solid #cbd5e1;
  border-radius: 0.6rem;
  font: inherit;
  font-weight: 400;
  color: var(--ink);
  background: #fff;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

.field input:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18);
}

.actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 1.2rem;
}

.actions button {
  flex: 1;
  padding: 0.7rem 1rem;
  border: 0;
  border-radius: 0.65rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 120ms ease;
}

.actions .primary {
  background: #2563eb;
  color: #fff;
}

.actions .primary:hover {
  background: #1d4ed8;
}

.actions .primary:disabled {
  background: #93c5fd;
  cursor: progress;
}

.actions .secondary {
  flex: 0 0 auto;
  background: #f1f5f9;
  color: #475569;
}

.actions .secondary:hover {
  background: #e2e8f0;
}

.error {
  margin: 0.9rem 0 0;
  padding: 0.6rem 0.75rem;
  border-radius: 0.5rem;
  background: #fef2f2;
  color: #b91c1c;
  font-size: 0.84rem;
}

.error[hidden] {
  display: none;
}

/* ---------- Stats page ---------- */

.stats-page {
  min-height: 100dvh;
  background: #f1f5f9;
  color: var(--ink);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.1rem clamp(1rem, 4vw, 3rem);
  background: rgba(255, 255, 255, 0.9);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.topbar h1 {
  margin: 0;
  font-size: 1.3rem;
  letter-spacing: -0.02em;
}

.topbar p {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.84rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.5);
  animation: pulse 2s infinite;
}

.live-dot[data-offline="true"] {
  background: #94a3b8;
  animation: none;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.45);
  }
  70% {
    box-shadow: 0 0 0 7px rgba(22, 163, 74, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(22, 163, 74, 0);
  }
}

.topbar nav {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.topbar a {
  padding: 0.55rem 0.9rem;
  border-radius: 0.6rem;
  color: #334155;
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 500;
}

.topbar a:hover {
  background: #f1f5f9;
}

.topbar a.button {
  background: #2563eb;
  color: #fff;
  font-weight: 600;
}

.topbar a.button:hover {
  background: #1d4ed8;
}

main.stats {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  max-width: 1400px;
  margin: 0 auto;
  padding: clamp(1rem, 3vw, 2rem) clamp(1rem, 4vw, 3rem) 4rem;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
  gap: 1rem;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 1rem 1.2rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.card-label {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.card strong {
  font-size: 1.7rem;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1.25rem;
}

.panel {
  margin: 0;
  padding: 1rem 1.1rem 1.1rem;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.panel figcaption,
.panel h2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin: 0 0 0.6rem;
  font-size: 0.95rem;
  font-weight: 600;
}

.panel canvas {
  display: block;
  width: 100%;
  height: 270px;
}

.r {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.r[data-strength="strong"] {
  color: var(--green);
}

.r[data-strength="moderate"] {
  color: var(--amber);
}

.r[data-strength="weak"] {
  color: var(--muted);
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.panel-head h2 {
  margin: 0;
}

.tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.25rem;
  border-radius: 0.65rem;
  background: #f1f5f9;
}

.tabs button {
  padding: 0.4rem 0.75rem;
  border: 0;
  border-radius: 0.5rem;
  background: transparent;
  color: #475569;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 120ms ease;
}

.tabs button:hover {
  color: var(--ink);
}

.tabs button.active {
  background: #fff;
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.12);
}

.note {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.table-panel {
  overflow-x: auto;
}

.table-panel h2 {
  margin-bottom: 0.4rem;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  font-variant-numeric: tabular-nums;
}

th,
td {
  padding: 0.6rem 0.7rem;
  border-bottom: 1px solid #eef2f7;
  text-align: left;
  white-space: nowrap;
}

th {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

tbody tr {
  cursor: pointer;
}

tbody tr:hover {
  background: #f8fafc;
}

tbody tr.selected {
  background: #eff6ff;
}

.empty-row td {
  padding: 1.4rem 0.7rem;
  color: var(--muted);
  text-align: center;
}

.actions-col {
  text-align: right;
}

.actions-cell {
  display: flex;
  justify-content: flex-end;
  gap: 0.4rem;
}

.text-button {
  padding: 0.35rem 0.6rem;
  border: 1px solid #dbe2ea;
  border-radius: 0.5rem;
  background: #fff;
  color: #334155;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

.text-button:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
}

.text-button.danger {
  color: #b91c1c;
  border-color: #fecaca;
}

.text-button.danger:hover {
  background: #fef2f2;
  border-color: #fca5a5;
}

@media (max-width: 640px) {
  .stage {
    padding: 1.25rem;
  }

  .panel canvas {
    height: 230px;
  }

  .topbar {
    align-items: flex-start;
  }
}
