:root {
  --bg: #f3f6fb;
  --panel: #ffffff;
  --panel-soft: #f8fbff;
  --line: #d6e2f0;
  --line-strong: #bdd0e7;
  --text: #1b2b42;
  --text-dim: #59708d;
  --brand: #2f80ed;
  --brand-strong: #1f6ad1;
  --mint: #23c7a6;
  --danger: #dd4c4c;
  --shadow: 0 14px 30px rgba(31, 56, 91, 0.08);
}

* {
  box-sizing: border-box;
}

.hidden {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(980px 460px at 8% -25%, rgba(47, 128, 237, 0.12), transparent 60%),
    radial-gradient(900px 420px at 100% -20%, rgba(35, 199, 166, 0.12), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: "Plus Jakarta Sans", "Segoe UI", sans-serif;
}

.page {
  width: min(1660px, calc(100% - 28px));
  margin: 14px auto 24px;
  display: grid;
  gap: 14px;
}

.topbar,
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 16px 18px;
}

.back-btn {
  border: 1px solid var(--line-strong);
  color: var(--text);
  text-decoration: none;
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 600;
  font-size: 14px;
  background: #ffffff;
  transition: border-color 0.18s ease, color 0.18s ease, transform 0.18s ease;
  flex-shrink: 0;
}

.back-btn:hover {
  border-color: var(--brand);
  color: var(--brand-strong);
  transform: translateY(-1px);
}

.title-wrap h1 {
  margin: 0;
  font-size: clamp(28px, 4vw, 40px);
  letter-spacing: -0.02em;
  color: #18304f;
}

.title-wrap p {
  margin: 6px 0 0;
  color: var(--text-dim);
  font-size: 15px;
}

.panel {
  padding: 16px;
}

.controls {
  display: grid;
  gap: 12px;
}

.control-head h2 {
  margin: 0;
  font-size: 22px;
  color: #203958;
}

.control-head p {
  margin: 6px 0 0;
  font-size: 14px;
  color: var(--text-dim);
}

.mini-explain {
  margin: -2px 0 0;
  font-size: 14px;
  color: #3d5d83;
  background: #f3f8ff;
  border: 1px solid #d9e7f7;
  border-radius: 10px;
  padding: 10px 12px;
}

.control-grid {
  display: grid;
  gap: 12px;
}

.run-setup-grid {
  grid-template-columns: 1.2fr 1fr 1fr;
}

.control-grid-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.control-grid-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.rule-builder-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.control-block {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-soft);
  padding: 12px;
  display: grid;
  gap: 8px;
}

.control-block h3 {
  margin: 0;
  font-size: 15px;
  color: #214468;
}

.control-note {
  margin: 0;
  font-size: 13px;
  color: var(--text-dim);
}

label {
  display: grid;
  gap: 7px;
}

label span {
  font-size: 12px;
  color: #516c8a;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 700;
}

input,
select,
button {
  font: inherit;
}

input,
select {
  width: 100%;
  border: 1px solid #cddced;
  border-radius: 10px;
  background: #ffffff;
  color: var(--text);
  padding: 11px 12px;
  font-family: "Source Sans 3", "Segoe UI", sans-serif;
  font-size: 16px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

input:focus,
select:focus {
  border-color: var(--brand);
  outline: none;
  box-shadow: 0 0 0 3px rgba(47, 128, 237, 0.16);
}

.speed-wrap {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
}

.speed-wrap strong {
  color: var(--brand-strong);
  font-size: 14px;
  font-weight: 700;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.btn {
  border: 1px solid #c3d5ea;
  color: #214061;
  background: #ffffff;
  padding: 10px 14px;
  border-radius: 11px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.btn:hover {
  border-color: var(--brand);
  transform: translateY(-1px);
  box-shadow: 0 8px 18px rgba(47, 128, 237, 0.16);
}

.btn.primary {
  border-color: transparent;
  color: #ffffff;
  background: linear-gradient(90deg, #2f80ed, #27ae60);
}

.btn.primary:hover {
  filter: brightness(1.04);
}

.btn.ghost {
  border-style: dashed;
}

.btn.danger {
  color: #8f1e1e;
  border-color: rgba(221, 76, 76, 0.48);
  background: #fff1f1;
}

.btn.danger:hover {
  border-color: var(--danger);
}

.btn.running {
  border-color: #ffa62b;
  background: #fff7ea;
  color: #8f5a00;
}

.queue-line,
.status-line,
.mode-help,
.safety-hint {
  margin: 0;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
}

.queue-line {
  background: #f1f7ff;
  border: 1px solid #d4e5f9;
  color: #2a4f76;
}

.status-line {
  background: #eef8f7;
  border: 1px solid #d1ece8;
  color: #1f5350;
}

.mode-help {
  background: #f5f9ff;
  border: 1px solid #dce8f6;
  color: #2a547f;
}

.safety-hint {
  background: #f9fbfe;
  border: 1px solid #dce6f3;
  color: #4d6784;
}

.accordion {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-soft);
  overflow: hidden;
}

.accordion summary {
  list-style: none;
  cursor: pointer;
  font-weight: 700;
  color: #1f3f64;
  padding: 12px 14px;
  border-bottom: 1px solid transparent;
  user-select: none;
}

.accordion summary::-webkit-details-marker {
  display: none;
}

.accordion[open] summary {
  border-bottom-color: var(--line);
  background: #f0f6ff;
}

.accordion summary::after {
  content: "+";
  float: right;
  color: #3a6ba1;
  font-weight: 700;
}

.accordion[open] summary::after {
  content: "−";
}

.accordion-body {
  padding: 12px;
  display: grid;
  gap: 12px;
}

.help-copy p {
  margin: 0;
  color: #355476;
  line-height: 1.5;
}

.formula-box {
  border: 1px solid #d7e4f2;
  border-radius: 12px;
  background: #ffffff;
  padding: 12px;
  display: grid;
  gap: 8px;
}

.formula-title {
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  font-weight: 700;
  color: #4f6b8a;
}

.formula-human {
  font-size: 14px;
  color: #335579;
}

.formula-latex {
  min-height: 26px;
  overflow-x: auto;
}

.formula-latex .katex {
  color: #193b63;
  font-size: 1.05em;
}

.scene-panel {
  padding: 0;
  overflow: hidden;
  position: relative;
}

#scene {
  width: 100%;
  height: min(72vh, 860px);
  display: block;
  background:
    radial-gradient(920px 420px at 50% -25%, rgba(114, 181, 247, 0.12), transparent 65%),
    #081a2d;
}

.hover-tooltip {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(-50%, calc(-100% - 10px));
  pointer-events: none;
  background: rgba(250, 253, 255, 0.98);
  border: 1px solid #bfd4ea;
  color: #1f3e63;
  border-radius: 8px;
  padding: 8px 10px;
  font-family: "Source Sans 3", "Segoe UI", sans-serif;
  font-size: 14px;
  white-space: nowrap;
  z-index: 5;
  box-shadow: 0 8px 24px rgba(18, 35, 57, 0.2);
}

.hover-tooltip strong {
  color: var(--brand-strong);
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.stats article {
  border: 1px solid #d6e3f3;
  border-radius: 10px;
  background: #f8fbff;
  padding: 10px;
}

.stats h3 {
  margin: 0 0 7px;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #5d7998;
}

.stats p {
  margin: 0;
  font-size: 24px;
  color: #17385e;
  font-weight: 700;
  font-family: "Source Sans 3", "Segoe UI", sans-serif;
}

.runs-panel h2 {
  margin: 0 0 10px;
  font-size: 16px;
  color: #28486e;
}

.runs-list {
  display: grid;
  gap: 8px;
  max-height: 220px;
  overflow: auto;
  padding-right: 4px;
}

.runs-list::-webkit-scrollbar {
  width: 8px;
}

.runs-list::-webkit-scrollbar-thumb {
  background: #c9daec;
  border-radius: 6px;
}

.run-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  border: 1px solid #d5e2f0;
  border-radius: 10px;
  background: #ffffff;
  padding: 10px;
  font-size: 14px;
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.run-item:hover {
  border-color: #9ec0e6;
  box-shadow: 0 8px 20px rgba(41, 96, 160, 0.12);
}

.run-item.active {
  border-color: #ffa82d;
  box-shadow: inset 0 0 0 1px rgba(255, 168, 45, 0.6);
}

.run-swatch {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.78);
}

.run-main {
  color: #2a496d;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.run-state {
  color: #557496;
  font-size: 13px;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 999;
  background: rgba(15, 26, 42, 0.45);
  display: grid;
  place-items: center;
  padding: 16px;
}

.modal-card {
  width: min(1200px, 100%);
  max-height: calc(100vh - 40px);
  border: 1px solid #c5d8ed;
  border-radius: 14px;
  background: #ffffff;
  padding: 14px;
  display: grid;
  gap: 10px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-head h2 {
  margin: 0;
  font-size: 18px;
  color: #1d4068;
}

.modal-meta {
  margin: 0;
  color: #4f6f91;
  font-size: 14px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
}

.history-text {
  width: 100%;
  min-height: 58vh;
  border: 1px solid #d1e0f1;
  border-radius: 10px;
  background: #fbfdff;
  color: #1d3a5d;
  padding: 10px;
  font-family: "Source Sans 3", "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  resize: vertical;
}

@media (max-width: 1220px) {
  .run-setup-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 860px) {
  .page {
    width: calc(100% - 12px);
    margin-top: 10px;
  }

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

  .run-setup-grid,
  .control-grid-2,
  .control-grid-3,
  .rule-builder-grid {
    grid-template-columns: 1fr;
  }

  .stats {
    grid-template-columns: 1fr;
  }

  #scene {
    height: 62vh;
  }
}
