:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --ink: #0f172a;
  --ink-soft: #475569;
  --muted: #94a3b8;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-2: #7c3aed;
  --good: #10b981;
  --warn: #f59e0b;
  --bad: #ef4444;
  --rope: #334155;
  --pulley: #64748b;
  --pulley-dark: #1e293b;
  --load: #b45309;
  --highlight: #f59e0b;
  --seg-colors: #ef4444, #f59e0b, #10b981, #06b6d4, #3b82f6, #8b5cf6, #ec4899, #14b8a6;
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.08);
  --radius: 14px;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
}
body.dark {
  --bg: #0b1220;
  --panel: #111a2e;
  --panel-2: #0f1729;
  --ink: #e2e8f0;
  --ink-soft: #cbd5e1;
  --muted: #64748b;
  --border: #1e293b;
  --rope: #cbd5e1;
  --pulley: #94a3b8;
  --pulley-dark: #e2e8f0;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); height: 100%; }
button { font-family: inherit; cursor: pointer; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo {
  width: 36px; height: 36px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white; border-radius: 10px; font-size: 20px;
}
.brand h1 { font-size: 17px; margin: 0; letter-spacing: -0.01em; }
.top-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.menu-toggle {
  display: none; flex-direction: column; justify-content: center; align-items: center;
  width: 38px; height: 38px; gap: 5px; background: transparent; border: 1px solid var(--border);
  border-radius: 10px; cursor: pointer; padding: 0;
}
.menu-toggle span {
  display: block; width: 20px; height: 2.5px; background: var(--ink); border-radius: 2px;
  transition: transform 0.2s, opacity 0.2s;
}
.menu-toggle[aria-expanded="true"] span:nth-child(1) { transform: translateY(7.5px) rotate(45deg); }
.menu-toggle[aria-expanded="true"] span:nth-child(2) { opacity: 0; }
.menu-toggle[aria-expanded="true"] span:nth-child(3) { transform: translateY(-7.5px) rotate(-45deg); }
.mobile-menu {
  position: fixed; top: 61px; right: 12px; z-index: 20;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 10px; display: flex; flex-direction: column; gap: 6px;
  min-width: 160px;
}
.mobile-menu .btn { width: 100%; text-align: left; }
.btn {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--ink);
  padding: 8px 14px; border-radius: 10px; font-size: 13px; font-weight: 500;
  transition: all 0.15s; white-space: nowrap;
}
.btn:hover { background: var(--bg); border-color: var(--accent); }
.btn.primary { background: var(--accent); color: white; border-color: var(--accent); }
.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.active { background: var(--accent); color: white; border-color: var(--accent); }

.layout {
  display: grid;
  grid-template-columns: 260px 1fr 300px;
  gap: 16px; padding: 16px;
  height: calc(100vh - 61px);
}
.sidebar, .rightpanel {
  background: var(--panel); border-radius: var(--radius); padding: 18px;
  overflow-y: auto; box-shadow: var(--shadow); border: 1px solid var(--border);
}
.stage {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border-radius: var(--radius); position: relative; overflow: hidden;
  box-shadow: var(--shadow); border: 1px solid var(--border);
  display: flex; flex-direction: column;
}
#canvas { flex: 1; width: 100%; height: 100%; display: block; touch-action: none; }
.stage-caption {
  padding: 10px 16px; font-size: 13px; color: var(--ink-soft); text-align: center;
  border-top: 1px solid var(--border); background: var(--panel);
}
.zoom-controls {
  position: absolute; top: 12px; right: 12px; z-index: 5;
  display: flex; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px 8px; box-shadow: var(--shadow);
}
.zoom-controls .btn { min-width: 28px; padding: 4px 8px; text-align: center; }
.zoom-value {
  font-size: 12px; font-weight: 600; color: var(--ink-soft);
  min-width: 40px; text-align: center; font-variant-numeric: tabular-nums;
}

.section-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--muted); margin: 0 0 10px; font-weight: 600;
}
.section-title:not(:first-child) { margin-top: 22px; }

.sim-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.sim-list li {
  padding: 10px 12px; border-radius: 8px; font-size: 13.5px; cursor: pointer;
  border: 1px solid transparent; color: var(--ink-soft);
  transition: all 0.15s;
}
.sim-list li:hover { background: var(--panel-2); color: var(--ink); }
.sim-list li.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white; border-color: transparent; font-weight: 500;
}

.control-group { margin-bottom: 16px; }
.control-group label { display: block; font-size: 12px; color: var(--ink-soft); margin-bottom: 6px; font-weight: 500; }
.control-group input[type=range] { width: 100%; accent-color: var(--accent); }
.control-group .value-inline { font-size: 12px; color: var(--muted); margin-top: 4px; }
.checkbox { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink); margin: 6px 0; }

.metric {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; background: var(--panel-2); border-radius: 8px;
  margin-bottom: 6px; font-size: 13px;
}
.metric span { color: var(--ink-soft); }
.metric b { color: var(--ink); font-variant-numeric: tabular-nums; }
.metric.total-rope { border: 1px solid var(--accent); background: color-mix(in oklab, var(--accent) 8%, var(--panel)); }
.metric.total-rope b { color: var(--accent); font-weight: 700; }

.segments { display: flex; flex-direction: column; gap: 4px; }
.seg-row {
  display: flex; justify-content: space-between; align-items: center; font-size: 12px;
  padding: 6px 10px; background: var(--panel-2); border-radius: 6px;
  border-left: 4px solid; font-variant-numeric: tabular-nums;
}
.seg-row .name { color: var(--ink-soft); }
.seg-row .len { color: var(--ink); font-weight: 600; }

.eq {
  font-family: 'JetBrains Mono', 'Fira Code', ui-monospace, monospace;
  font-size: 12.5px; padding: 8px 10px; background: var(--panel-2);
  border-radius: 6px; margin-bottom: 6px; color: var(--ink);
}
.rule {
  margin-top: 10px; padding: 8px 10px; border-radius: 6px;
  background: color-mix(in oklab, var(--good) 12%, var(--panel));
  color: var(--good); font-size: 12px; font-weight: 600; text-align: center;
}
.rule.violation {
  background: color-mix(in oklab, var(--bad) 15%, var(--panel));
  color: var(--bad);
}

.challenge-banner {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--accent);
  padding: 10px 16px; border-radius: 10px; z-index: 5;
  display: flex; flex-direction: column; align-items: stretch;
  gap: 12px; box-shadow: var(--shadow); max-width: 90%; min-width: 320px;
}
.ch-q { font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.ch-opts { display: flex; gap: 6px; flex-wrap: wrap; }
.ch-opts button { flex: 1; min-width: 60px; }
.ch-fb { font-size: 12px; margin-top: 8px; color: var(--ink-soft); min-height: 16px; }
.ch-fb.correct { color: var(--good); }
.ch-fb.wrong { color: var(--bad); }

.modal {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.6);
  display: grid; place-items: center; z-index: 100;
}
.modal-inner {
  background: var(--panel); padding: 28px 32px; border-radius: var(--radius);
  max-width: 480px; box-shadow: var(--shadow);
}
.modal-inner h2 { margin: 0 0 14px; }
.modal-inner ol { padding-left: 20px; line-height: 1.7; font-size: 14px; color: var(--ink-soft); }
.modal-inner button { margin-top: 16px; }

/* Simulation dropdown (mobile) */
.mobile-sim-bar { display: none; }
.mobile-sim-bar .sim-select {
  width: 100%; padding: 10px 12px; font: inherit; font-size: 14px;
  color: var(--ink); background: var(--panel-2); border: 1px solid var(--pulley);
  border-radius: 10px;
}

/* Slider steppers */
.slider-row { display: flex; align-items: center; gap: 8px; }
.slider-row input[type=range] { flex: 1; min-width: 0; }
.step-btn {
  flex: none; width: 30px; height: 30px; display: grid; place-items: center;
  font-size: 17px; line-height: 1; cursor: pointer; user-select: none;
  color: var(--ink); background: var(--panel-2);
  border: 1px solid var(--pulley); border-radius: 8px;
}
.step-btn:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.step-btn:active { transform: scale(0.94); }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 220px 1fr; }
  .rightpanel { grid-column: 1 / -1; grid-row: 2; max-height: 240px; }
  .layout { height: auto; }
  .stage { min-height: 500px; }
  .top-actions { display: none; }
  .menu-toggle { display: flex; }
}
@media (max-width: 720px) {
  .mobile-sim-bar { display: block; padding: 10px 10px 0; }
  .layout {
    display: flex; flex-direction: column;
    padding: 10px; gap: 10px;
  }
  .stage { order: -1; min-height: 360px; }
  .sidebar { order: 1; max-height: none; }
  .rightpanel { order: 2; max-height: none; }
  .sim-section-title, .sim-list { display: none; }
  .brand h1 { font-size: 14px; }
  .top-actions { display: none; }
  .menu-toggle { display: flex; }
  .step-btn { width: 40px; height: 40px; font-size: 20px; }
}



/* SVG element styles */
.rope-seg { fill: none; stroke-width: 3; stroke-linecap: round; }
.rope-seg.highlight { animation: pulseGlow 1.4s ease-in-out infinite; filter: drop-shadow(0 0 4px currentColor); }
@keyframes pulseGlow {
  0%, 100% { opacity: 0.7; stroke-width: 3; }
  50% { opacity: 1; stroke-width: 4.5; }
}
.pulley-body { fill: var(--panel); stroke: var(--pulley-dark); stroke-width: 2; }
.pulley-groove { fill: none; stroke: var(--pulley); stroke-width: 1.5; stroke-dasharray: 2 2; }
.pulley-hub { fill: var(--pulley-dark); }
.load-block { fill: var(--load); stroke: #78350f; stroke-width: 2; }
.effort-hand {
  fill: url(#handGrad); stroke: #78350f; stroke-width: 2;
  cursor: grab;
}
.effort-hand.grabbing { cursor: grabbing; }
.ceiling { fill: var(--pulley-dark); }
.ruler { stroke: var(--muted); stroke-width: 1; }
.ruler-tick { stroke: var(--muted); stroke-width: 1; }
.ruler-label { fill: var(--ink-soft); font-size: 10px; font-family: monospace; }
.dist-label { fill: var(--accent); font-size: 13px; font-weight: 700; font-family: monospace; }
.trail { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-dasharray: 3 3; opacity: 0.5; }
.arrow { fill: var(--accent); }
.text-label { fill: var(--ink); font-size: 12px; font-family: sans-serif; }
