/* ══════════════════════════════════════════════════════════════════
   COLLATZ CASCADE 2 — DARK GLASS UI
   ══════════════════════════════════════════════════════════════════ */

:root {
  --bg:        #070c16;
  --surf-0:    #0d1525;
  --surf-1:    #111e35;
  --surf-2:    #16253f;
  --surf-3:    #1c2e4e;
  --border:    rgba(100, 150, 220, 0.12);
  --border-hi: rgba(100, 160, 240, 0.28);
  --border-glow: rgba(80, 140, 255, 0.5);

  --text:      #ddeeff;
  --text-dim:  #7da0cc;
  --text-muted:#3f6080;

  --blue:      #4d9fff;
  --blue-dim:  #2563eb;
  --blue-glow: rgba(77, 159, 255, 0.35);
  --amber:     #fbbf24;
  --amber-dim: #d97706;
  --amber-glow:rgba(251, 191, 36, 0.35);
  --purple:    #c084fc;
  --purple-dim:#9333ea;
  --purple-glow:rgba(192, 132, 252, 0.35);
  --green:     #34d399;
  --red:       #f87171;

  --shadow-sm:  0 2px 8px rgba(0,0,0,0.5);
  --shadow-md:  0 6px 24px rgba(0,0,0,0.6);
  --shadow-lg:  0 12px 40px rgba(0,0,0,0.7);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
.hidden { display: none !important; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Plus Jakarta Sans", "Segoe UI", system-ui, sans-serif;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ── Page layout ── */
.page {
  width: min(1680px, calc(100% - 24px));
  margin: 12px auto 32px;
  display: grid;
  gap: 12px;
}

/* ── Glass panel base ── */
.topbar, .panel {
  background: var(--surf-1);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow-sm);
}

/* ══════════════════════════════════════════════════════════════════
   HEADER
   ══════════════════════════════════════════════════════════════════ */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 18px 22px;
  background: linear-gradient(135deg, #0d1a30 0%, #0f1f3a 100%);
  border-color: rgba(100, 160, 240, 0.18);
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-dim);
  text-decoration: none;
  border: 1px solid var(--border-hi);
  border-radius: 10px;
  padding: 9px 14px;
  font-weight: 600;
  font-size: 13px;
  background: rgba(255,255,255,0.04);
  transition: all 0.2s;
  flex-shrink: 0;
  white-space: nowrap;
}
.back-btn::before { content: "←"; }
.back-btn:hover {
  border-color: var(--blue);
  color: var(--blue);
  background: rgba(77,159,255,0.08);
  transform: translateX(-2px);
}

.title-wrap { flex: 1; min-width: 0; }
.title-wrap h1 {
  font-size: clamp(22px, 3vw, 36px);
  font-weight: 800;
  letter-spacing: -0.03em;
  background: linear-gradient(90deg, #7dc4ff 0%, #a78bfa 50%, #60e5c4 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  line-height: 1.15;
}
.title-wrap p {
  margin-top: 6px;
  color: var(--text-dim);
  font-size: 13.5px;
  line-height: 1.55;
  max-width: 860px;
}
.title-wrap strong { color: var(--text); font-weight: 700; }

.v2-badge {
  font-size: 0.42em;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #fff;
  background: linear-gradient(135deg, #4d9fff, #a855f7);
  border-radius: 6px;
  padding: 3px 9px 4px;
  vertical-align: middle;
  margin-left: 10px;
  -webkit-text-fill-color: #fff;
}

/* ══════════════════════════════════════════════════════════════════
   CONTROLS PANEL
   ══════════════════════════════════════════════════════════════════ */
.controls {
  padding: 20px;
  display: grid;
  gap: 16px;
}

.control-head { border-bottom: 1px solid var(--border); padding-bottom: 14px; }
.control-head h2 {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.01em;
}
.control-head p { margin-top: 4px; font-size: 13px; color: var(--text-muted); }

/* ── Grid helpers ── */
.control-grid { display: grid; gap: 12px; }
.run-setup-grid { grid-template-columns: 1.4fr 1fr 1fr; }
.control-grid-3 { grid-template-columns: repeat(3, 1fr); }

/* ── Labels / Inputs ── */
label { display: grid; gap: 7px; }
label > span, .field-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

input[type="text"],
input[type="number"],
select {
  font: inherit;
  width: 100%;
  background: var(--surf-0);
  border: 1px solid var(--border-hi);
  border-radius: 10px;
  color: var(--text);
  padding: 11px 13px;
  font-family: "Source Sans 3", monospace;
  font-size: 15px;
  transition: border-color 0.2s, box-shadow 0.2s;
  appearance: none;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-glow);
}
select { cursor: pointer; }
select option { background: var(--surf-2); }

input[type="range"] {
  width: 100%;
  accent-color: var(--blue);
  cursor: pointer;
}

.speed-wrap {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
}
.speed-wrap strong {
  font-size: 16px;
  font-weight: 800;
  color: var(--blue);
  font-family: "Source Sans 3", monospace;
  min-width: 42px;
  text-align: right;
}

/* ── Buttons ── */
.button-row { display: flex; flex-wrap: wrap; gap: 9px; }

button { font: inherit; cursor: pointer; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border-hi);
  color: var(--text-dim);
  background: var(--surf-2);
  padding: 9px 15px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 700;
  transition: all 0.18s;
  white-space: nowrap;
}
.btn:hover {
  border-color: var(--blue);
  color: var(--blue);
  background: rgba(77,159,255,0.08);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px var(--blue-glow);
}

.btn.primary {
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 4px 16px rgba(37,99,235,0.5);
  padding: 9px 20px;
}
.btn.primary:hover {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(37,99,235,0.65);
  color: #fff;
}

.btn.ghost {
  background: transparent;
  border-style: dashed;
  border-color: rgba(100,160,240,0.3);
}
.btn.ghost:hover { border-style: solid; }

.btn.danger {
  color: var(--red);
  border-color: rgba(248,113,113,0.3);
  background: rgba(248,113,113,0.06);
}
.btn.danger:hover {
  border-color: var(--red);
  background: rgba(248,113,113,0.12);
  color: var(--red);
  box-shadow: 0 4px 16px rgba(248,113,113,0.3);
}
.btn.running {
  border-color: var(--amber);
  color: var(--amber);
  background: rgba(251,191,36,0.08);
}

/* ── Status / queue lines ── */
.queue-line, .status-line, .safety-hint {
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  line-height: 1.45;
}
.queue-line {
  background: rgba(77,159,255,0.07);
  border: 1px solid rgba(77,159,255,0.18);
  color: #89b8f0;
}
.status-line {
  background: rgba(52,211,153,0.07);
  border: 1px solid rgba(52,211,153,0.2);
  color: #6edfc0;
}
.safety-hint {
  background: var(--surf-0);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

/* ══════════════════════════════════════════════════════════════════
   ACCORDIONS
   ══════════════════════════════════════════════════════════════════ */
.accordion {
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  background: var(--surf-0);
}
.accordion summary {
  list-style: none;
  cursor: pointer;
  font-weight: 700;
  font-size: 14px;
  color: var(--text-dim);
  padding: 13px 16px;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background 0.18s, color 0.18s;
}
.accordion summary::-webkit-details-marker { display: none; }
.accordion[open] summary {
  background: rgba(77,159,255,0.07);
  color: var(--text);
  border-bottom: 1px solid var(--border);
}
.accordion summary::after {
  content: "+";
  font-size: 18px;
  font-weight: 300;
  color: var(--text-muted);
  transition: transform 0.2s;
}
.accordion[open] summary::after { content: "−"; }
.accordion-body { padding: 16px; display: grid; gap: 14px; }

.mini-explain {
  font-size: 13px;
  color: var(--text-dim);
  background: rgba(77,159,255,0.06);
  border: 1px solid rgba(77,159,255,0.15);
  border-radius: 10px;
  padding: 11px 14px;
  line-height: 1.55;
}
.mini-explain strong { color: var(--blue); }

.help-copy p { color: var(--text-dim); font-size: 13.5px; line-height: 1.6; }
.help-copy strong { color: var(--text); }
.help-copy em { color: var(--amber); font-style: normal; font-weight: 600; }

/* ══════════════════════════════════════════════════════════════════
   MODULUS SELECTOR
   ══════════════════════════════════════════════════════════════════ */
.modulus-section { display: grid; gap: 10px; }

.modulus-label-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.modulus-pill-track {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 4px;
  background: var(--surf-0);
  border: 1px solid var(--border-hi);
  border-radius: 12px;
  padding: 4px;
}

.mod-btn {
  border: none;
  color: var(--text-muted);
  background: transparent;
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.16s;
  letter-spacing: 0.02em;
}
.mod-btn:hover { color: var(--text-dim); background: var(--surf-3); }
.mod-btn.active {
  background: linear-gradient(135deg, #2563eb, #4d9fff);
  color: #fff;
  box-shadow: 0 2px 10px rgba(37,99,235,0.5);
}

.custom-mod-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 600;
}
.custom-mod-input {
  width: 72px !important;
  padding: 7px 10px !important;
  font-size: 14px !important;
  text-align: center;
}

.coverage-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 8px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.coverage-badge.ok {
  background: rgba(52,211,153,0.1);
  border: 1px solid rgba(52,211,153,0.25);
  color: var(--green);
}
.coverage-badge.warn {
  background: rgba(251,191,36,0.1);
  border: 1px solid rgba(251,191,36,0.25);
  color: var(--amber);
}

/* ══════════════════════════════════════════════════════════════════
   RULES CONTAINER & CARDS
   ══════════════════════════════════════════════════════════════════ */
.rules-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(460px, 1fr));
  gap: 10px;
}

/* Rule card — left border color driven by data-active-cat */
.rule-card {
  background: var(--surf-1);
  border: 1px solid var(--border);
  border-radius: 13px;
  overflow: hidden;
  border-left: 3px solid var(--blue-dim);
  transition: border-color 0.25s, box-shadow 0.25s;
}
.rule-card[data-active-cat="basic"]  { border-left-color: var(--blue-dim);   box-shadow: -2px 0 18px rgba(37,99,235,0.2); }
.rule-card[data-active-cat="power"]  { border-left-color: var(--amber-dim);  box-shadow: -2px 0 18px rgba(217,119,6,0.2); }
.rule-card[data-active-cat="calc"]   { border-left-color: var(--purple-dim); box-shadow: -2px 0 18px rgba(147,51,234,0.2); }

.rule-card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px 9px;
  background: var(--surf-0);
  border-bottom: 1px solid var(--border);
}

.residue-badge {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  font-family: "Source Sans 3", monospace;
}
.residue-badge strong {
  font-size: 20px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--blue), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.rule-card[data-active-cat="power"] .residue-badge strong {
  background: linear-gradient(135deg, var(--amber), #fb923c);
  -webkit-background-clip: text;
  background-clip: text;
}
.rule-card[data-active-cat="calc"] .residue-badge strong {
  background: linear-gradient(135deg, var(--purple), #e879f9);
  -webkit-background-clip: text;
  background-clip: text;
}
.mod-label { font-size: 12px; color: var(--text-muted); font-weight: 500; }

/* active-op tag shown in header */
.active-op-tag {
  margin-left: auto;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 6px;
  white-space: nowrap;
  letter-spacing: 0.02em;
}
.rule-card[data-active-cat="basic"]  .active-op-tag { background: rgba(37,99,235,0.2); color: var(--blue); border: 1px solid rgba(77,159,255,0.25); }
.rule-card[data-active-cat="power"]  .active-op-tag { background: rgba(217,119,6,0.2); color: var(--amber); border: 1px solid rgba(251,191,36,0.25); }
.rule-card[data-active-cat="calc"]   .active-op-tag { background: rgba(147,51,234,0.2); color: var(--purple); border: 1px solid rgba(192,132,252,0.25); }

/* ── Active formula display (big KaTeX between header & op buttons) ── */
.active-formula-wrap {
  padding: 16px 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 66px;
  background: rgba(0, 0, 0, 0.22);
  border-bottom: 1px solid var(--border);
  transition: background 0.25s;
}

/* Category tinting */
.rule-card[data-active-cat="basic"] .active-formula-wrap {
  background: rgba(37, 99, 235, 0.09);
}
.rule-card[data-active-cat="power"] .active-formula-wrap {
  background: rgba(180, 83, 9, 0.11);
}
.rule-card[data-active-cat="calc"] .active-formula-wrap {
  background: rgba(126, 34, 206, 0.11);
}

.active-formula {
  text-align: center;
  line-height: 1;
  color: var(--text);
}

/* KaTeX sizing & tinting inside formula display */
.active-formula .katex {
  font-size: 1.45em;
  color: var(--text);
}
.rule-card[data-active-cat="basic"] .active-formula .katex  { color: #93c5fd; }
.rule-card[data-active-cat="power"] .active-formula .katex  { color: #fcd34d; }
.rule-card[data-active-cat="calc"]  .active-formula .katex  { color: #d8b4fe; }

/* ── Op type selector ── */
.op-type-selector {
  padding: 12px 14px 4px;
  display: grid;
  gap: 10px;
}

.op-cat-group { display: grid; gap: 6px; }

.op-cat-label {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 8px;
}
.op-cat-label::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.op-btn-row { display: flex; flex-wrap: wrap; gap: 5px; }

.op-btn {
  border: 1px solid transparent;
  background: var(--surf-2);
  color: var(--text-muted);
  padding: 5px 12px;
  border-radius: 7px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  font-family: "Source Sans 3", monospace;
  transition: all 0.16s;
  white-space: nowrap;
}

/* Basic category — blue */
.op-btn[data-cat="basic"]:hover {
  background: rgba(37,99,235,0.15);
  border-color: rgba(77,159,255,0.4);
  color: var(--blue);
}
.op-btn[data-cat="basic"].active {
  background: linear-gradient(135deg, #1d4ed8, #2563eb);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 2px 12px rgba(37,99,235,0.5);
}

/* Power category — amber */
.op-btn[data-cat="power"]:hover {
  background: rgba(217,119,6,0.15);
  border-color: rgba(251,191,36,0.4);
  color: var(--amber);
}
.op-btn[data-cat="power"].active {
  background: linear-gradient(135deg, #b45309, #d97706);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 2px 12px rgba(217,119,6,0.5);
}

/* Calc category — purple */
.op-btn[data-cat="calc"]:hover {
  background: rgba(147,51,234,0.15);
  border-color: rgba(192,132,252,0.4);
  color: var(--purple);
}
.op-btn[data-cat="calc"].active {
  background: linear-gradient(135deg, #7e22ce, #9333ea);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 2px 12px rgba(147,51,234,0.5);
}

/* ── Operation parameters ── */
.op-params-area {
  padding: 12px 14px 14px;
  border-top: 1px solid var(--border);
  background: rgba(0,0,0,0.15);
}

.params-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}
.params-grid label { min-width: 90px; flex: 1 1 auto; }

.param-input {
  background: var(--surf-0) !important;
  border-color: var(--border-hi) !important;
  font-size: 14px !important;
  padding: 9px 11px !important;
  color: var(--text) !important;
}
.param-input:focus { border-color: var(--blue) !important; }

.param-input.poly-input { font-family: "Source Sans 3", monospace !important; }

.no-params {
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
}

.poly-preview {
  margin-top: 5px;
  font-size: 12px;
  font-family: "Source Sans 3", monospace;
  padding: 6px 10px;
  border-radius: 7px;
  line-height: 1.5;
}
.poly-preview.ok {
  background: rgba(52,211,153,0.08);
  border: 1px solid rgba(52,211,153,0.2);
  color: #6edfc0;
}
.poly-preview.err {
  background: rgba(248,113,113,0.08);
  border: 1px solid rgba(248,113,113,0.2);
  color: var(--red);
}

.rational-diagram {
  margin-top: 10px;
  text-align: center;
}
.rational-formula {
  display: inline-block;
  font-size: 14px;
  font-family: "Source Sans 3", monospace;
  color: var(--purple);
  background: rgba(147,51,234,0.1);
  border: 1px solid rgba(192,132,252,0.2);
  border-radius: 8px;
  padding: 7px 18px;
  letter-spacing: 0.03em;
}

/* ── Formula summary box ── */
.formula-box {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surf-0);
  padding: 14px;
  display: grid;
  gap: 10px;
}
.formula-title {
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--text-muted);
}
.formula-preview-text {
  margin: 0;
  font-size: 13px;
  font-family: "Source Sans 3", monospace;
  color: var(--text-dim);
  background: rgba(0,0,0,0.25);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  white-space: pre;
  overflow-x: auto;
  line-height: 1.7;
}

/* ══════════════════════════════════════════════════════════════════
   CANVAS SCENE
   ══════════════════════════════════════════════════════════════════ */
.scene-panel {
  padding: 0;
  overflow: hidden;
  position: relative;
  border-color: rgba(77,159,255,0.2);
  box-shadow: 0 0 40px rgba(77,159,255,0.08), var(--shadow-md);
}

#scene {
  width: 100%;
  height: min(74vh, 900px);
  display: block;
  background: #081a2d;
}

.hover-tooltip {
  position: absolute;
  left: 0; top: 0;
  transform: translate(-50%, calc(-100% - 10px));
  pointer-events: none;
  background: rgba(13,21,37,0.97);
  border: 1px solid var(--border-hi);
  color: var(--text);
  border-radius: 9px;
  padding: 8px 12px;
  font-family: "Source Sans 3", monospace;
  font-size: 13.5px;
  white-space: nowrap;
  z-index: 5;
  box-shadow: var(--shadow-md);
}
.hover-tooltip strong { color: var(--blue); }

/* ══════════════════════════════════════════════════════════════════
   STATS
   ══════════════════════════════════════════════════════════════════ */
.stats {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 10px;
  padding: 14px;
  background: var(--surf-0);
}

.stats article {
  background: var(--surf-1);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 12px 14px;
  display: grid;
  gap: 6px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.stats article:hover {
  border-color: var(--border-hi);
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

.stats h3 {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats p {
  font-size: 20px;
  font-weight: 800;
  font-family: "Source Sans 3", monospace;
  color: var(--blue);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}

/* colorize specific stats */
#stat-peak   { color: var(--amber); }
#stat-longest { color: var(--green); }
#stat-current-node { color: var(--purple); font-size: 16px; }
#stat-hover-node   { color: var(--text-dim); font-size: 16px; }

/* ══════════════════════════════════════════════════════════════════
   RUNS PANEL
   ══════════════════════════════════════════════════════════════════ */
.runs-panel { padding: 16px; }
.runs-panel h2 {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 10px;
}

.runs-list {
  display: grid;
  gap: 7px;
  max-height: 230px;
  overflow-y: auto;
  padding-right: 4px;
}
.runs-list::-webkit-scrollbar { width: 5px; }
.runs-list::-webkit-scrollbar-track { background: transparent; }
.runs-list::-webkit-scrollbar-thumb { background: var(--surf-3); border-radius: 3px; }

.run-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  background: var(--surf-0);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.run-item:hover {
  border-color: var(--border-hi);
  background: var(--surf-1);
  transform: translateX(2px);
}
.run-item.active {
  border-color: var(--amber-dim);
  background: rgba(217,119,6,0.07);
  box-shadow: inset 3px 0 0 var(--amber-dim);
}

.run-swatch {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 0 8px currentColor;
}
.run-main { color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.run-state { color: var(--text-muted); font-size: 12px; white-space: nowrap; }

/* ══════════════════════════════════════════════════════════════════
   HISTORY MODAL
   ══════════════════════════════════════════════════════════════════ */
.modal {
  position: fixed;
  inset: 0;
  z-index: 999;
  background: rgba(4, 8, 16, 0.75);
  backdrop-filter: blur(8px);
  display: grid;
  place-items: center;
  padding: 16px;
}

.modal-card {
  width: min(1300px, 100%);
  max-height: calc(100vh - 40px);
  background: var(--surf-1);
  border: 1px solid var(--border-hi);
  border-radius: 16px;
  padding: 18px;
  display: grid;
  gap: 12px;
  box-shadow: var(--shadow-lg);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}
.modal-head h2 { font-size: 18px; font-weight: 700; color: var(--text); }
.modal-meta { font-size: 13px; color: var(--text-dim); }
.modal-actions { display: flex; justify-content: flex-end; }

.history-text {
  width: 100%;
  min-height: 58vh;
  background: var(--surf-0);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-dim);
  padding: 12px 14px;
  font-family: "Source Sans 3", monospace;
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
}
.history-text:focus { outline: none; border-color: var(--blue); }

/* ══════════════════════════════════════════════════════════════════
   RESPONSIVE
   ══════════════════════════════════════════════════════════════════ */
@media (max-width: 1400px) {
  .stats { grid-template-columns: repeat(4, 1fr); }
}
@media (max-width: 1100px) {
  .run-setup-grid { grid-template-columns: 1fr 1fr; }
  .control-grid-3 { grid-template-columns: 1fr 1fr; }
  .rules-container { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(4, 1fr); }
  .modulus-label-row { flex-direction: column; align-items: flex-start; }
}
@media (max-width: 700px) {
  .page { width: calc(100% - 12px); }
  .topbar { flex-direction: column; align-items: flex-start; }
  .run-setup-grid { grid-template-columns: 1fr; }
  .control-grid-3 { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  #scene { height: 60vh; }
  .modal-card { padding: 12px; }
}
