:root {
  --color-bg: #f4f6fb;
  --color-panel: #ffffff;
  --color-panel-border: #e2e8f0;
  --color-text: #1f2937;
  --color-text-dim: #6b7280;
  --color-accent: #059669;
  --color-accent-dim: #d1fae5;
  --color-accent-text: #047857;
  --color-danger: #dc2626;
  --color-danger-dim: #fee2e2;
  --color-warning-dim: #fef3c7;
  --color-warning-text: #92400e;
  --color-1: #ef4444;
  --color-2: #3b82f6;
  --color-3: #22c55e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 6px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.app {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
}

/* ---------- Header ---------- */

.app-header {
  margin-bottom: 24px;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.app-header h1 {
  margin: 0 0 4px;
  font-size: 2rem;
}

.subtitle {
  margin: 0 0 16px;
  color: var(--color-text-dim);
}

.help-btn {
  background: #fff;
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  padding: 10px 16px;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--color-text);
  cursor: pointer;
  box-shadow: var(--shadow);
}

.help-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-accent-text);
}

.header-stats {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.stat {
  background: var(--color-panel);
  border: 2px solid var(--color-panel-border);
  border-radius: var(--radius);
  padding: 7px 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: var(--shadow);
}

.stat[data-size="12"] .stat-value { color: #059669; }
.stat[data-size="21"] .stat-value { color: #2563eb; }
.stat[data-size="42"] .stat-value { color: #9333ea; }

.stat.active[data-size="12"] { border-color: #059669; background: #ecfdf5; }
.stat.active[data-size="21"] { border-color: #2563eb; background: #eff6ff; }
.stat.active[data-size="42"] { border-color: #9333ea; background: #faf5ff; }

.info-btn {
  margin-left: 6px;
  background: #f1f5f9;
  border: 1px solid var(--color-panel-border);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--color-text-dim);
  cursor: pointer;
}

.info-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-accent-text);
}

.info-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.info-btn:disabled:hover {
  border-color: var(--color-panel-border);
  color: var(--color-text-dim);
}

.stat-label {
  color: var(--color-text-dim);
  font-size: 0.85rem;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--color-accent-text);
}

.stat-max {
  color: var(--color-text-dim);
}

/* ---------- Action box: every control lives here ---------- */

.action-box {
  background: var(--color-panel);
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 22px;
  margin-bottom: 20px;
  border-left: 6px solid var(--color-text-dim);
}

.size-selector {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding-bottom: 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--color-panel-border);
}

.size-selector-left,
.size-selector-right {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.size-selector-label {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--color-text);
}

.size-buttons {
  display: flex;
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.size-btn {
  background: var(--color-panel);
  color: var(--color-text-dim);
  border: none;
  border-right: 1px solid var(--color-panel-border);
  padding: 10px 18px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 700;
}

.size-btn:last-child {
  border-right: none;
}

.size-btn.active {
  background: var(--color-accent-dim);
  color: var(--color-accent-text);
}

.size-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.brain-source-selector {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  padding-bottom: 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--color-panel-border);
}

.brain-source-hint {
  font-size: 0.82rem;
  color: var(--color-text-dim);
}

#brain-mine-btn.active {
  background: var(--color-warning-dim);
  color: var(--color-warning-text);
}

.brain-source-badge {
  margin-left: 6px;
  background: #f1f5f9;
  border: 1px solid var(--color-panel-border);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--color-text-dim);
}

.brain-source-badge.mine {
  background: var(--color-warning-dim);
  border-color: #f59e0b;
  color: var(--color-warning-text);
}

.action-status {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  border-radius: var(--radius);
  padding: 6px;
}

.status-icon {
  font-size: 1.8rem;
  line-height: 1;
}

.status-text {
  flex: 1;
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--color-text);
  white-space: pre-line;
}

.action-primary {
  display: flex;
}

.take-buttons {
  display: flex;
  gap: 10px;
  width: 100%;
  margin-top: 14px;
}

.take-buttons[hidden] {
  display: none;
}

.take-btn {
  flex: 1;
  padding: 14px;
  border-radius: var(--radius);
  border: 1px solid var(--color-panel-border);
  background: #f8fafc;
  color: var(--color-text);
  font-weight: 700;
  cursor: pointer;
  font-size: 1.05rem;
}

.take-btn:hover:not(:disabled) {
  border-color: var(--color-accent);
  color: var(--color-accent-text);
}

.take-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.status-next-btn {
  border: none;
  border-radius: var(--radius);
  padding: 12px 24px;
  font-weight: 700;
  font-size: 1.05rem;
  cursor: pointer;
  background: var(--color-accent);
  color: #fff;
  margin-top: 14px;
  animation: next-btn-pulse 1.6s ease-in-out infinite;
}

.status-next-btn:hover {
  background: var(--color-accent-text);
}

.status-next-btn:disabled {
  opacity: 0.5;
  cursor: default;
  animation: none;
}

@keyframes next-btn-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(5, 150, 105, 0.4); }
  50% { box-shadow: 0 0 0 6px rgba(5, 150, 105, 0); }
}

.action-box.status-human { border-left-color: var(--color-2); }
.action-box.status-ai { border-left-color: #8b5cf6; }
.action-box.status-system { border-left-color: var(--color-text-dim); }
.action-box.status-win { border-left-color: var(--color-accent); }
.action-box.status-loss { border-left-color: var(--color-danger); }
.action-box.status-train { border-left-color: #d97706; }

.action-box.status-win .action-status { background: var(--color-accent-dim); }
.action-box.status-loss .action-status { background: var(--color-danger-dim); }
.action-box.status-train .action-status { background: #fffbeb; }

.action-secondary {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--color-panel-border);
}

.time-travel-control {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 220px;
  font-size: 0.85rem;
  color: var(--color-text-dim);
  font-weight: 600;
}

.time-travel-control input[type="range"] {
  flex: 1;
  min-width: 100px;
}

.time-travel-control .slider-label {
  white-space: nowrap;
  color: var(--color-text-dim);
  font-weight: 400;
}

/* ---------- Panels layout ---------- */

.panels {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  align-items: start;
}

.panel {
  background: var(--color-panel);
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 4px;
  font-size: 1.15rem;
}

.panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}

.panel-header-row h2 {
  margin: 0;
}

.panel-hint {
  color: var(--color-text-dim);
  font-weight: 400;
  font-size: 0.8rem;
}

p.panel-hint {
  margin: 0 0 16px;
}

/* ---------- Game panel ---------- */

.how-to-play {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  background: #f8fafc;
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 16px;
}

.how-to-play-icon {
  font-size: 1.6rem;
  line-height: 1.3;
}

.how-to-play ol {
  margin: 0;
  padding-left: 20px;
  font-size: 1.05rem;
  color: var(--color-text);
  line-height: 1.9;
}

.how-to-play ol strong {
  color: var(--color-accent-text);
}

.action-pile-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.last-bead-callout {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--color-danger);
  font-weight: 700;
  font-size: 0.8rem;
  white-space: nowrap;
}

.last-bead-callout[hidden] {
  display: none;
}

.callout-arrow {
  font-size: 1.1rem;
  line-height: 1;
  animation: callout-bounce 1s ease-in-out infinite;
}

@keyframes callout-bounce {
  0%, 100% { transform: translateX(0); }
  50% { transform: translateX(4px); }
}

.pile-box-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.pile-box {
  border: 2px solid var(--color-panel-border);
  border-radius: var(--radius);
  background: #f8fafc;
  padding: 8px 10px;
  transition: border-color 0.3s ease, background 0.3s ease;
}

.pile-box.danger {
  border-color: var(--color-danger);
  background: var(--color-danger-dim);
}

.pile-display {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
  align-items: center;
  max-width: 260px;
}

.bead {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #fde68a, #d97706);
  box-shadow: 0 1px 2px rgba(217, 119, 6, 0.5);
  flex-shrink: 0;
}

.pile-count {
  text-align: center;
  color: var(--color-text-dim);
  font-size: 0.8rem;
  white-space: nowrap;
}

.new-game-btn,
.primary-btn {
  padding: 10px 18px;
  border-radius: var(--radius);
  border: none;
  background: var(--color-accent);
  color: #ffffff;
  font-weight: 700;
  cursor: pointer;
  font-size: 0.95rem;
}

.new-game-btn:hover,
.primary-btn:hover {
  background: var(--color-accent-text);
}

.narration-toggle {
  display: flex;
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.narration-btn {
  background: var(--color-panel);
  color: var(--color-text-dim);
  border: none;
  border-right: 1px solid var(--color-panel-border);
  padding: 10px 16px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 700;
}

.narration-btn:last-child {
  border-right: none;
}

.narration-btn.active {
  background: var(--color-accent-dim);
  color: var(--color-accent-text);
}


.training-cap-banner {
  margin-top: 14px;
  padding: 10px;
  border-radius: var(--radius);
  background: var(--color-warning-dim);
  color: var(--color-warning-text);
  font-size: 0.85rem;
  text-align: center;
}

/* ---------- Commentary log ---------- */

.log-panel {
  margin-top: 18px;
  border-top: 1px solid var(--color-panel-border);
  padding-top: 14px;
}

.log-panel h3 {
  margin: 0 0 8px;
  font-size: 0.9rem;
  color: var(--color-text-dim);
}

.game-log {
  height: 220px;
  overflow-y: auto;
  background: #f8fafc;
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.log-entry {
  font-size: 0.85rem;
  line-height: 1.4;
  padding: 6px 10px;
  border-radius: 8px;
  background: #fff;
  border-left: 4px solid var(--color-panel-border);
  white-space: pre-line;
}

.log-entry.log-human {
  border-left-color: var(--color-2);
}

.log-entry.log-ai {
  border-left-color: #8b5cf6;
}

.log-entry.log-system {
  border-left-color: var(--color-text-dim);
  color: var(--color-text-dim);
}

.log-entry.log-win {
  border-left-color: var(--color-accent);
  background: var(--color-accent-dim);
  font-weight: 700;
}

.log-entry.log-loss {
  border-left-color: var(--color-danger);
  background: var(--color-danger-dim);
  font-weight: 700;
}

.log-entry.log-train {
  border-left-color: #d97706;
  color: #92400e;
  background: #fffbeb;
}

/* ---------- Matchbox grid ---------- */

.legend {
  display: flex;
  gap: 16px;
  margin-bottom: 14px;
  font-size: 0.85rem;
  color: var(--color-text-dim);
}

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

.swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}

.swatch-1 { background: var(--color-1); }
.swatch-2 { background: var(--color-2); }
.swatch-3 { background: var(--color-3); }

.matchbox-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  gap: 8px;
}

.matchbox-card {
  background: #f8fafc;
  border: 1px solid var(--color-panel-border);
  border-radius: 8px;
  padding: 6px 8px;
  transition: box-shadow 0.3s ease, border-color 0.3s ease;
}

.matchbox-card.visited {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px var(--color-accent-dim);
}

.matchbox-card.active-move {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-dim);
  animation: glow-pulse-3 1.4s ease-in-out infinite;
}

/* Once a colour has actually been drawn from a box, the whole card takes on
   that colour instead of the neutral green - a box that hasn't been opened
   yet (or is still mid-open, colour unknown) keeps the neutral green above. */
.matchbox-card.visited.color-1,
.matchbox-card.active-move.color-1 {
  border-color: var(--color-1);
  box-shadow: 0 0 0 2px var(--color-danger-dim);
}
.matchbox-card.active-move.color-1 {
  box-shadow: 0 0 0 3px var(--color-danger-dim);
  animation-name: glow-pulse-1;
}

.matchbox-card.visited.color-2,
.matchbox-card.active-move.color-2 {
  border-color: var(--color-2);
  box-shadow: 0 0 0 2px #dbeafe;
}
.matchbox-card.active-move.color-2 {
  box-shadow: 0 0 0 3px #dbeafe;
  animation-name: glow-pulse-2;
}

.matchbox-card.visited.color-3,
.matchbox-card.active-move.color-3 {
  border-color: var(--color-3);
  box-shadow: 0 0 0 2px var(--color-accent-dim);
}
.matchbox-card.active-move.color-3 {
  box-shadow: 0 0 0 3px var(--color-accent-dim);
  animation-name: glow-pulse-3;
}

@keyframes glow-pulse-1 {
  0%, 100% { box-shadow: 0 0 0 3px var(--color-danger-dim); }
  50% { box-shadow: 0 0 0 6px rgba(220, 38, 38, 0.45); }
}

@keyframes glow-pulse-2 {
  0%, 100% { box-shadow: 0 0 0 3px #dbeafe; }
  50% { box-shadow: 0 0 0 6px rgba(59, 130, 246, 0.45); }
}

@keyframes glow-pulse-3 {
  0%, 100% { box-shadow: 0 0 0 3px var(--color-accent-dim); }
  50% { box-shadow: 0 0 0 6px rgba(5, 150, 105, 0.45); }
}

.matchbox-title {
  font-weight: 700;
  margin-bottom: 4px;
  font-size: 0.8rem;
}

.bead-color-row {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.75rem;
  color: var(--color-text-dim);
  line-height: 1.5;
  border-radius: 4px;
  padding: 0 3px;
  margin: 0 -3px;
  transition: background 0.2s ease, color 0.2s ease;
}

.bead-color-row.drawn {
  font-weight: 700;
  color: var(--color-text);
}

.bead-color-row.drawn[data-color="1"] { background: var(--color-danger-dim); }
.bead-color-row.drawn[data-color="2"] { background: #dbeafe; }
.bead-color-row.drawn[data-color="3"] { background: var(--color-accent-dim); }

.bead-count-text {
  font-variant-numeric: tabular-nums;
}

/* ---------- Track record chart ---------- */

.panel-stats {
  margin-top: 20px;
}

.stats-legend-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin: 0 2px;
}

.stats-legend-win { background: var(--color-accent); }
.stats-legend-loss { background: var(--color-danger); }

.stats-empty {
  background: #f8fafc;
  border: 1px dashed var(--color-panel-border);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  color: var(--color-text-dim);
  font-size: 0.9rem;
}

.win-rate-chart {
  width: 100%;
  height: 290px;
  display: block;
}

.stats-summary {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--color-text-dim);
}

.stats-summary strong {
  color: var(--color-accent-text);
}

.stats-line-legend {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  flex-wrap: wrap;
}

.stats-line-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stats-line-legend-swatch {
  display: inline-block;
  width: 18px;
  height: 3px;
  border-radius: 2px;
}

.time-travel-notice {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-left: 4px solid var(--color-2);
  border-radius: var(--radius);
  padding: 12px;
  font-size: 0.85rem;
  color: var(--color-text);
  margin-top: 14px;
}

.time-travel-notice strong {
  color: #1d4ed8;
}

.status-hint {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: var(--radius);
  padding: 10px 14px;
  margin: 14px 0;
  font-size: 0.95rem;
  color: #1d4ed8;
}

.status-hint[hidden] {
  display: none;
}

/* ---------- Welcome modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-card {
  background: #ffffff;
  border-radius: 16px;
  max-width: 900px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 32px 40px;
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.3);
}

.modal-card-small {
  max-width: 440px;
}

.reset-confirm-warning {
  font-weight: 700;
  color: var(--color-danger);
}

.danger-btn {
  padding: 10px 18px;
  border-radius: var(--radius);
  border: none;
  background: var(--color-danger);
  color: #ffffff;
  font-weight: 700;
  cursor: pointer;
  font-size: 0.95rem;
}

.danger-btn:hover {
  background: #b91c1c;
}

.modal-card h2 {
  margin-top: 0;
}

.modal-section {
  margin: 18px 0;
}

.modal-section h3 {
  margin: 0 0 6px;
  font-size: 1rem;
}

.modal-section p {
  margin: 6px 0;
  color: var(--color-text);
  line-height: 1.5;
}

/* Collapsible variant: only <details class="modal-section"> gets this look,
   so the plain <div class="modal-section"> used elsewhere is unaffected. */
details.modal-section {
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  background: #f8fafc;
  overflow: hidden;
}

details.modal-section + details.modal-section {
  margin-top: 12px;
}

details.modal-section > summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  list-style: none;
  cursor: pointer;
  padding: 14px 18px;
  user-select: none;
}

details.modal-section > summary::-webkit-details-marker {
  display: none;
}

details.modal-section > summary h3 {
  margin: 0;
}

details.modal-section > summary::after {
  content: "▸";
  flex-shrink: 0;
  color: var(--color-text-dim);
  font-size: 0.9rem;
  transition: transform 0.2s ease;
}

details.modal-section[open] > summary::after {
  transform: rotate(90deg);
}

details.modal-section > summary:hover h3 {
  color: var(--color-accent-text);
}

details.modal-section .modal-section-body {
  padding: 0 18px 18px;
}

.modal-illustration {
  display: block;
  width: 100%;
  max-width: 360px;
  height: auto;
  margin: 4px auto 14px;
}

.menace-acronym {
  font-size: 1.1rem;
  text-align: center;
  background: #f8fafc;
  border: 1px solid var(--color-panel-border);
  border-radius: var(--radius);
  padding: 14px;
  margin: 16px 0;
}

.menace-acronym strong {
  color: var(--color-accent-text);
  font-size: 1.25em;
}

.modal-legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 0.9rem;
  margin: 10px 0;
}

.modal-legend span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.modal-card .primary-btn {
  width: 100%;
  margin-top: 12px;
  font-size: 1.05rem;
  padding: 14px;
}

.welcome-punchy {
  font-size: 1.15rem;
  line-height: 1.6;
  color: var(--color-text);
  margin: 14px 0 0;
}

.welcome-cta-row {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}

.welcome-cta-row .primary-btn,
.welcome-cta-row .secondary-btn,
.welcome-cta-row .danger-btn {
  flex: 1;
  width: auto;
  margin-top: 0;
  font-size: 1.05rem;
  padding: 14px;
}

.secondary-btn {
  border-radius: var(--radius);
  border: 1px solid var(--color-panel-border);
  background: #fff;
  color: var(--color-text);
  font-weight: 700;
  cursor: pointer;
}

.secondary-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-accent-text);
}
