/* Component styles for both pages. Layout is mobile-first; the content
   column is capped at 960px. All numeric readouts use tabular figures so
   in-place refreshes never shift layout. */

* { box-sizing: border-box; }

/* Author display rules (flex/grid) must never override the hidden
   attribute — the UA's [hidden] rule loses to any author declaration. */
[hidden] { display: none !important; }

html { font-family: var(--font-sans); }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: var(--fs-md);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

.wrap {
  max-width: 960px;
  margin-inline: auto;
  padding-inline: var(--sp-4);
}

:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--r-sm);
}

/* Announced by screen readers, invisible on screen — for status that's
   otherwise carried by color alone. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* --- Top bar -------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  min-height: 52px;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-weight: 650;
  font-size: var(--fs-lg);
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.01em;
}

.brand-mark { flex: none; }

.topbar-right {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
}

.btn-small { padding: 4px 12px; font-size: var(--fs-xs); }
.btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: var(--fs-sm);
  font-weight: 500;
}
.back-link:hover { color: var(--text); }

/* Connection indicator — the single source of freshness truth. */
.conn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: var(--fs-sm);
  color: var(--text-muted);
  white-space: nowrap;
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-faint);
  flex: none;
}

.conn.live .conn-dot {
  background: var(--ok);
  animation: pulse 2.4s ease-in-out infinite;
}
.conn.live .conn-word { color: var(--ok-text); font-weight: 600; }
.conn.reconnecting .conn-dot { background: var(--warn); }
.conn.reconnecting .conn-word { color: var(--warn-text); font-weight: 600; }
.conn.stale .conn-dot { background: var(--text-faint); }
.conn.stale .conn-word { font-weight: 600; }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ok) 45%, transparent); }
  50%      { box-shadow: 0 0 0 5px transparent; }
}

/* --- Banner (degraded connectivity / staleness) ---------------------- */

.banner {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-top: var(--sp-4);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-sm);
  font-size: var(--fs-sm);
  border: 1px solid var(--border);
}
.banner[hidden] { display: none; }
.banner.warn {
  background: var(--warn-tint);
  color: var(--warn-text);
  border-color: color-mix(in srgb, var(--warn) 35%, transparent);
}
.banner.stale {
  background: var(--nodata-tint);
  color: var(--nodata-text);
}

/* --- Summary strip --------------------------------------------------- */

.summary {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-4);
  list-style: none;
  margin: var(--sp-5) 0 0;
  padding: 0;
  font-size: var(--fs-md);
  color: var(--text-muted);
}

.summary b {
  font-weight: 650;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.summary .zero { opacity: 0.55; }

/* --- Status dots (shape + color, redundant channels) ------------------ */

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
  display: inline-block;
}
.dot.running { background: var(--ok); }
.dot.forming {
  /* half-filled: forming = "half way there" */
  border: 2px solid var(--warn);
  background: linear-gradient(to top, var(--warn) 50%, transparent 50%);
  width: 10px; height: 10px;
}
.dot.idle { border: 2px solid var(--down); background: transparent; }
.dot.no_data { border: 2px dashed var(--text-faint); background: transparent; }

/* --- Casino sections -------------------------------------------------- */

.section { margin-top: var(--sp-6); }

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
}

.section-name {
  margin: 0;
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.section-fresh {
  font-size: var(--fs-xs);
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.section-error {
  margin: 0 0 var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-sm);
  background: var(--warn-tint);
  color: var(--warn-text);
  font-size: var(--fs-sm);
  overflow-wrap: anywhere;
}

/* --- Tiles ------------------------------------------------------------ */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: var(--sp-3);
}

.tile {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--text-faint);
  border-radius: var(--r-md);
  text-decoration: none;
  color: var(--text);
  transition: background 120ms ease, transform 120ms ease,
              box-shadow 120ms ease;
}

.tile:hover {
  background: var(--surface-2);
  transform: translateY(-1px);
  box-shadow: var(--shadow-1);
}

.tile.running { border-left-color: var(--ok); }
.tile.forming { border-left-color: var(--warn); }
.tile.idle    { border-left-color: var(--down); }
.tile.no_data { border-left-color: var(--text-faint); }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  align-self: flex-start;
  padding: 3px 10px;
  border-radius: var(--r-pill);
  font-size: var(--fs-sm);
  font-weight: 600;
}
.pill.running { background: var(--ok-tint); color: var(--ok-text); }
.pill.forming { background: var(--warn-tint); color: var(--warn-text); }
.pill.idle    { background: var(--down-tint); color: var(--down-text); }
.pill.no_data { background: var(--nodata-tint); color: var(--nodata-text); }

.tile-label {
  font-size: var(--fs-lg);
  font-weight: 600;
  letter-spacing: -0.01em;
}

.tile-buyin {
  font-size: var(--fs-sm);
  color: var(--text-muted);
}
.tile-buyin:empty { display: none; }

.tile-stats {
  font-size: var(--fs-md);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.tile-stats b { color: var(--text); font-weight: 650; }

/* A stale tile can't be trusted — desaturate rather than lie in green. */
.tile.is-stale, .degraded .tile {
  filter: saturate(0.35) opacity(0.75);
}

.unmatched {
  margin-top: var(--sp-3);
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}

.chip {
  padding: 3px 10px;
  border-radius: var(--r-pill);
  border: 1px dashed var(--border-strong);
  color: var(--text-faint);
  font-size: var(--fs-xs);
}

/* --- Edit mode ---------------------------------------------------------- */

.editpanel {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
  padding: var(--sp-3);
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--r-md);
}

.edit-games {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 2px var(--sp-4);
}

.edit-game {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-2);
  padding: 5px 6px;
  border-radius: var(--r-sm);
  font-size: var(--fs-sm);
  cursor: pointer;
}
.edit-game:hover { background: var(--surface-2); }
/* Checkbox and dot sit on the first line, not centered across both. */
.edit-game input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  margin: 1px 0 0;
  flex: none;
}
.edit-game .dot {
  width: 8px;
  height: 8px;
  margin-top: 5px;
}
.edit-game .dot.forming, .edit-game .dot.idle { border-width: 2px; }

.edit-game-body {
  flex: 1;
  min-width: 0;  /* lets long labels and ids shrink instead of overflowing */
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.edit-game-top {
  display: flex;
  align-items: baseline;
  gap: var(--sp-2);
}

.edit-game-name {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.edit-game-id {
  margin-left: auto;
  flex: none;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--fs-xs);
  font-variant-numeric: tabular-nums;
  color: var(--text-faint);
}

/* Plain inline flow, not flex: a long buy-in then wraps mid-sentence
   ("…(Taking / Requests) · 2 waiting") instead of exiling the counts to a
   line of their own that opens with a stray "·". The separator lives on
   the part that follows it, and the parts are appended with no whitespace
   between them, so it can never end up dangling at a line end. */
.edit-game-meta {
  font-size: var(--fs-xs);
  line-height: 1.45;
  color: var(--text-muted);
  overflow-wrap: anywhere;
}
.edit-game-meta > * + *::before {
  content: "·";
  margin: 0 4px 0 5px;
  color: var(--text-faint);
}
.edit-game-stats {
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.edit-game-via {
  font-size: var(--fs-xs);
  line-height: 1.45;
  color: var(--text-faint);
  overflow-wrap: anywhere;
}

.edit-rules {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-2);
}

.chip.rule {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border-style: solid;
  color: var(--text-muted);
}

.chip-x {
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  line-height: 1;
  padding: 0 0 0 2px;
}
.chip-x:hover { color: var(--down-text); }

.edit-add {
  display: flex;
  gap: var(--sp-2);
  align-items: center;
}
.edit-add input {
  flex: 1;
  max-width: 300px;
  font: inherit;
  font-size: var(--fs-sm);
  padding: 6px 10px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
}
.edit-add input::placeholder { color: var(--text-faint); }

.edit-hint {
  font-size: var(--fs-xs);
  color: var(--text-faint);
}

/* --- Empty / error states --------------------------------------------- */

.empty {
  margin-top: var(--sp-7);
  padding: var(--sp-6);
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  color: var(--text-muted);
}
.empty h2 { margin: 0 0 var(--sp-2); font-size: var(--fs-lg); color: var(--text); }
.empty p { margin: 0; font-size: var(--fs-sm); }
.empty code {
  font-family: var(--font-mono);
  background: var(--surface-2);
  padding: 1px 6px;
  border-radius: 4px;
}

.loading {
  margin-top: var(--sp-6);
  color: var(--text-faint);
  text-align: center;
}

/* --- History page: live header ---------------------------------------- */

.game-head { margin-top: var(--sp-5); }

.game-casino {
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.game-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-3);
  margin-top: var(--sp-1);
}

.game-title {
  margin: 0;
  font-size: var(--fs-xl);
  font-weight: 650;
  letter-spacing: -0.015em;
}

.game-live {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--fs-sm);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.game-buyin {
  margin: 2px 0 0;
  font-size: var(--fs-sm);
  color: var(--text-muted);
}
.game-buyin:empty { display: none; }

/* --- History summary card ---------------------------------------------- */

.range-card {
  margin-top: var(--sp-5);
  padding: var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-5);
}

.range-headline {
  font-size: var(--fs-xl);
  font-weight: 650;
  color: var(--ok-text);
  letter-spacing: -0.01em;
}

.range-label { font-size: var(--fs-sm); color: var(--text-muted); }

.range-counts {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-3);
  font-size: var(--fs-sm);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  margin-left: auto;
}
.range-counts .dot { width: 8px; height: 8px; }
.range-counts .dot.forming, .range-counts .dot.idle { border-width: 2px; }
.range-counts span { display: inline-flex; align-items: center; gap: 6px; }

/* --- History grid ------------------------------------------------------ */

.hist {
  margin-top: var(--sp-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: var(--sp-4);
  overflow-x: auto;
}

.hist-inner { min-width: 560px; }

.hist-row {
  display: grid;
  grid-template-columns: 64px repeat(24, minmax(12px, 1fr));
  gap: 3px;
  align-items: center;
  margin-top: 3px;
}

.hist-row.axis { margin: 0 0 var(--sp-1); }

.axis-tick {
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  color: var(--text-faint);
  text-align: left;
}

.day-label {
  font-size: var(--fs-xs);
  color: var(--text-faint);
  padding-right: var(--sp-2);
  white-space: nowrap;
}
.day-label.weekend { color: var(--text-muted); font-weight: 600; }

.cell {
  aspect-ratio: 1;
  width: 100%;
  border: none;
  border-radius: var(--r-cell);
  padding: 0;
  background: transparent;
  cursor: default;
}

.cell.running { background: var(--cell-running); cursor: pointer; }
.cell.forming { background: var(--cell-forming); cursor: pointer; }
.cell.idle    { background: var(--cell-idle); cursor: pointer; }
.cell.no_data { background: var(--cell-nodata); cursor: pointer; }
.cell.future  { background: transparent; pointer-events: none; }

.cell.now {
  outline: 1.5px solid var(--accent);
  outline-offset: 1px;
  animation: nowpulse 2.4s ease-in-out infinite;
}

@keyframes nowpulse {
  0%, 100% { outline-color: var(--accent); }
  50%      { outline-color: transparent; }
}

.cell:hover:not(.future) {
  filter: brightness(1.12);
  box-shadow: inset 0 0 0 1px var(--border-strong);
}

/* --- Legend + load-older ----------------------------------------------- */

.hist-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--sp-3);
  margin-top: var(--sp-4);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-4);
  font-size: var(--fs-xs);
  color: var(--text-muted);
  list-style: none;
  padding: 0;
  margin: 0;
}

.legend li { display: inline-flex; align-items: center; gap: 6px; }

.swatch {
  width: 12px;
  height: 12px;
  border-radius: var(--r-cell);
  flex: none;
}
.swatch.running { background: var(--cell-running); }
.swatch.forming { background: var(--cell-forming); }
.swatch.idle    { background: var(--cell-idle); }
.swatch.no_data { background: var(--cell-nodata); }

.btn {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 550;
  padding: 7px 16px;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: background 120ms ease;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }

/* --- Tooltip ----------------------------------------------------------- */

.tooltip {
  position: fixed;
  z-index: 30;
  max-width: 280px;
  padding: var(--sp-2) var(--sp-3);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-2);
  font-size: var(--fs-sm);
  pointer-events: none;
}
.tooltip[hidden] { display: none; }
.tooltip .tip-when { font-weight: 650; }
.tooltip .tip-status { display: flex; align-items: center; gap: 7px; margin-top: 2px; }
.tooltip .tip-extra { color: var(--text-muted); margin-top: 2px; font-size: var(--fs-xs); }

/* --- Small screens ------------------------------------------------------ */

@media (max-width: 480px) {
  .hist { padding: var(--sp-3); }
  .hist-row { grid-template-columns: 56px repeat(24, minmax(9px, 1fr)); gap: 2px; }
  .hist-inner { min-width: 470px; }
  .day-label { overflow: hidden; text-overflow: ellipsis; }
  .summary { gap: var(--sp-2) var(--sp-3); font-size: var(--fs-sm); }
  .range-counts { margin-left: 0; }
  .axis-tick.minor { visibility: hidden; }
  .brand { font-size: var(--fs-md); white-space: nowrap; }
  #conn-when { display: none; }
}

/* --- Reduced motion ------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  .conn.live .conn-dot { animation: none; }
  .cell.now { animation: none; }
  .tile { transition: none; }
  .tile:hover { transform: none; }
}
