:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-soft: #eef3f8;
  --ink: #18202b;
  --muted: #667080;
  --line: #dbe3ec;
  --blue: #2563eb;
  --teal: #0f9f8f;
  --red: #df3f3f;
  --amber: #b7791f;
  --shadow: 0 18px 50px rgba(24, 32, 43, 0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.72), rgba(245,247,251,0.92)),
    radial-gradient(circle at top left, rgba(15,159,143,0.14), transparent 32rem),
    var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input { font: inherit; }

button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
}
button:hover { border-color: #b9c6d4; background: #f8fafc; }

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 20px;
}
.workspace { min-height: calc(100vh - 40px); }

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-end;
  margin-bottom: 16px;
}
.eyebrow {
  margin: 0 0 4px;
  color: var(--teal);
  font-weight: 800;
  text-transform: uppercase;
  font-size: 0.76rem;
}
h1 { margin: 0; font-size: clamp(1.65rem, 4vw, 2.7rem); line-height: 1.05; }

.status-card {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 6px 20px rgba(24, 32, 43, 0.06);
  color: var(--muted);
  white-space: nowrap;
}
.status-dot { width: 9px; height: 9px; border-radius: 99px; background: var(--teal); }
.status-card.warning .status-dot { background: var(--amber); }
.status-card.error .status-dot { background: var(--red); }

.sim-layout {
  display: grid;
  grid-template-columns: 320px minmax(360px, 1fr) 280px;
  gap: 16px;
  align-items: start;
}

.control-panel,
.canvas-panel,
.results-panel {
  background: rgba(255,255,255,0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}
.control-panel, .results-panel { padding: 14px; }
.viewer-stack {
  display: grid;
  gap: 10px;
}

.panel-section + .panel-section {
  border-top: 1px solid var(--line);
  margin-top: 14px;
  padding-top: 14px;
}

.section-head,
.canvas-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.section-title { margin-bottom: 10px; font-weight: 800; font-size: 0.86rem; }
.unit-label, .hint, .canvas-toolbar span, .mini-card p { color: var(--muted); font-size: 0.86rem; }

.segmented {
  display: grid;
  gap: 6px;
  padding: 4px;
  border-radius: 8px;
  background: var(--panel-soft);
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: grid;
  min-height: 36px;
  place-items: center;
  border-radius: 7px;
  color: var(--muted);
  cursor: pointer;
  font-weight: 700;
}
.segmented input:checked + span {
  background: var(--panel);
  color: var(--blue);
  box-shadow: 0 4px 16px rgba(24, 32, 43, 0.1);
}

/* Mode selector for IK 2D / FK / IK 3D / DH */
.mode-seg {
  grid-template-columns: repeat(4, 1fr);
}
.mode-seg span { padding-inline: 3px; font-size: 0.78rem; }
.link-count-seg { grid-template-columns: repeat(4, 1fr); }
.link-count-seg span { padding-inline: 4px; font-size: 0.78rem; }

.field-grid, .field-grid-3 { display: grid; gap: 8px; }
.field-grid { grid-template-columns: repeat(2, 1fr); }
.field-grid-3 { grid-template-columns: repeat(3, 1fr); }

.field {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}
.field input, .slider-field input[type="number"] {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  background: #fbfdff;
  color: var(--ink);
}
.axis-field {
  --axis-color: var(--ink);
  --axis-border: var(--line);
  --axis-bg: #fbfdff;
  --axis-focus: rgba(24, 32, 43, 0.14);
  color: var(--axis-color);
}
.axis-field span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.axis-field span::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--axis-color);
}
.axis-field input {
  border-color: var(--axis-border);
  background: var(--axis-bg);
  color: var(--axis-color);
  font-weight: 800;
}
.axis-field input:focus {
  outline: none;
  border-color: var(--axis-color);
  box-shadow: 0 0 0 2px var(--axis-focus);
}
.axis-x { --axis-color: #df3f3f; --axis-border: #efb3b3; --axis-bg: #fff7f7; --axis-focus: rgba(223, 63, 63, 0.18); }
.axis-y { --axis-color: #2563eb; --axis-border: #a9c4ff; --axis-bg: #f4f8ff; --axis-focus: rgba(37, 99, 235, 0.18); }
.axis-z { --axis-color: #0f9f8f; --axis-border: #9bd9d1; --axis-bg: #f2fffd; --axis-focus: rgba(15, 159, 143, 0.18); }

.radio-stack { display: grid; gap: 8px; margin-top: 12px; color: var(--muted); }

.text-button { min-height: 30px; padding: 0 9px; color: var(--blue); font-weight: 800; }

.slider-field {
  display: grid;
  grid-template-columns: 62px minmax(90px, 1fr) 72px;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}
input[type="range"] { width: 100%; accent-color: var(--blue); }

.button-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.wide-button { width: 100%; font-weight: 800; }

/* ── Joint Counter ── */
.joint-counter {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 6px;
}
.counter-btn {
  width: 44px; height: 44px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 1.3rem; font-weight: 800; color: var(--blue);
  cursor: pointer;
  display: grid; place-items: center;
  transition: background 0.15s;
}
.counter-btn:first-child { border-radius: 8px 0 0 8px; }
.counter-btn:last-child { border-radius: 0 8px 8px 0; }
.counter-btn:hover { background: #f0f7ff; border-color: var(--blue); }
.counter-val {
  width: 52px; height: 44px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: grid; place-items: center;
  font-size: 1.3rem; font-weight: 800; color: var(--ink);
  background: var(--panel-soft);
}

/* ── DH Table ── */
.dh-table-wrap { overflow-x: auto; margin-bottom: 8px; }
.dh-table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.dh-table th {
  text-align: center;
  font-weight: 800;
  color: var(--muted);
  padding: 6px 4px;
  border-bottom: 2px solid var(--line);
  white-space: nowrap;
}
.dh-table td { padding: 4px 2px; text-align: center; border-bottom: 1px solid var(--line); }
.dh-table .dh-label { font-weight: 800; color: var(--blue); width: 28px; }
.dh-table .dh-cell input {
  width: 78px; min-height: 32px;
  border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 6px; text-align: center;
  background: #fbfdff; color: var(--ink); font-size: 0.82rem;
}
.dh-table .dh-cell input:focus {
  outline: none; border-color: var(--blue);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
}
.dh-table .joint-var input {
  background: #f0f7ff; border-color: #b8d4fe;
  font-weight: 700; color: var(--blue);
}
.dh-note { color: var(--muted); font-size: 0.78rem; margin-top: 4px; }

/* ── Canvas & Results ── */
.canvas-panel { overflow: hidden; }
.canvas-toolbar {
  min-height: 58px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.canvas-toolbar strong { display: block; }

.toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-weight: 700;
  white-space: nowrap;
}
.toolbar-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}
.download-menu {
  position: relative;
  z-index: 5;
}
.download-menu summary {
  display: grid;
  min-height: 34px;
  min-width: 106px;
  place-items: center;
  padding: 0 12px;
  border: 1px solid var(--blue);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 800;
  list-style: none;
  user-select: none;
  box-shadow: 0 0 0 1px rgba(37, 99, 235, 0.08);
}
.download-menu summary::-webkit-details-marker { display: none; }
.download-menu summary:hover {
  background: #eff6ff;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.12);
}
.download-menu[open] summary {
  color: var(--blue);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.16);
}
.download-options {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  display: grid;
  width: 132px;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 14px 35px rgba(24, 32, 43, 0.14);
}
.download-options button {
  min-width: 0;
  min-height: 34px;
  padding: 0 8px;
  font-size: 0.82rem;
  font-weight: 800;
  text-align: left;
}

.canvas-wrap { padding: 14px; }
canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  height: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
  cursor: crosshair;
}

.three-container {
  width: 100%;
  height: 560px;
  cursor: grab;
  background: #f8faff;
}
.three-container:active { cursor: grabbing; }
.three-container canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  border: none;
  border-radius: 0 0 8px 8px;
  cursor: grab;
}

.metric-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.metric, .joint-table, .mini-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
}
.axis-metric {
  --axis-color: var(--ink);
  --axis-border: var(--line);
  --axis-bg: #fbfdff;
  border-color: var(--axis-border);
  background: var(--axis-bg);
}
.axis-metric span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--axis-color);
}
.axis-metric span::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--axis-color);
}
.axis-metric strong { color: var(--axis-color); }
.axis-metric.axis-x { --axis-color: #df3f3f; --axis-border: #efb3b3; --axis-bg: #fff7f7; }
.axis-metric.axis-y { --axis-color: #2563eb; --axis-border: #a9c4ff; --axis-bg: #f4f8ff; }
.axis-metric.axis-z { --axis-color: #0f9f8f; --axis-border: #9bd9d1; --axis-bg: #f2fffd; }
.hidden-metric { display: none !important; }
.singularity-card .section-title { margin-bottom: 0; }
.singularity-badge {
  padding: 4px 7px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--teal);
  background: rgba(15, 159, 143, 0.1);
}
.singularity-bar {
  height: 8px;
  margin: 12px 0 10px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--panel-soft);
}
.singularity-bar span {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: var(--teal);
  transition: width 0.2s ease, background 0.2s ease;
}
.singularity-values {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 0.78rem;
}
.dh-singularity-values {
  margin-top: 12px;
}
.singularity-values strong { color: var(--ink); }
.dh-singularity-values { flex-wrap: wrap; }
.singularity-card.near .singularity-badge { color: var(--amber); background: rgba(183, 121, 31, 0.12); }
.singularity-card.near .singularity-bar span { background: var(--amber); }
.singularity-card.singular .singularity-badge { color: var(--red); background: rgba(223, 63, 63, 0.11); }
.singularity-card.singular .singularity-bar span { background: var(--red); }
#singularityState.near { color: var(--amber); }
#singularityState.singular { color: var(--red); }
#singularityState.stable { color: var(--teal); }
.metric { padding: 10px; }
.metric span { display: block; color: var(--muted); font-size: 0.78rem; font-weight: 800; }
.metric strong { display: block; margin-top: 4px; font-size: 1.1rem; }
.axis-metric strong { color: var(--axis-color); }
.joint-table, .mini-card { margin-top: 12px; padding: 12px; }
.joint-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 0;
  border-top: 1px solid var(--line);
  color: var(--muted);
}
.joint-row strong { color: var(--ink); }
.position-error-row {
  margin-top: 4px;
  border-top-color: rgba(15, 159, 143, 0.35);
  color: var(--ink);
}
.position-error-row strong { color: var(--teal); }

.matrix-display {
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  margin-top: 8px;
  overflow-x: auto;
}
.matrix-display pre {
  margin: 0;
  font-family: "SF Mono", "Fira Code", "Consolas", monospace;
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--ink);
  white-space: pre;
}
.jacobian-table-wrap { padding: 0; }
.jacobian-table {
  width: 100%;
  border-collapse: collapse;
  font-family: "SF Mono", "Fira Code", "Consolas", monospace;
  font-size: 0.74rem;
  white-space: nowrap;
}
.jacobian-table th, .jacobian-table td {
  padding: 7px 9px;
  text-align: right;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.jacobian-table thead th {
  color: var(--blue);
  background: rgba(37, 99, 235, 0.06);
}
.jacobian-table tbody th {
  color: var(--teal);
  text-align: left;
  background: rgba(15, 159, 143, 0.05);
}
.jacobian-table th:last-child, .jacobian-table td:last-child { border-right: 0; }
.jacobian-table tbody tr:last-child th, .jacobian-table tbody tr:last-child td { border-bottom: 0; }

.report-actions {
  display: grid;
  grid-template-columns: 0.85fr 0.9fr 1.35fr;
  gap: 8px;
  margin-top: 12px;
}
.report-actions button {
  min-width: 0;
  font-weight: 800;
}
.toolbar-report-actions {
  width: min(100%, 420px);
  margin-top: 0;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
.toolbar-report-actions button {
  min-height: 34px;
  font-size: 0.78rem;
  padding: 0 6px;
}
.hidden { display: none !important; }

@media (min-width: 761px) {
  body.is-3d-mode .viewer-stack {
    position: sticky;
    top: 20px;
  }

  body.is-3d-mode #panel3D {
    display: flex !important;
    flex-direction: column;
    height: calc(100vh - 40px);
  }

  body.is-3d-mode #panel3D .three-container {
    flex: 1;
    height: auto;
    min-height: 0;
  }
}

@media (max-width: 1100px) {
  .sim-layout { grid-template-columns: 300px 1fr; }
  .results-panel { grid-column: 1 / -1; }
}
@media (max-width: 760px) {
  .app-shell { padding: 12px; }
  .topbar, .sim-layout { display: grid; grid-template-columns: 1fr; }
  .status-card { width: fit-content; }
  .control-panel, .results-panel { padding: 12px; }
  .field-grid, .metric-grid { grid-template-columns: 1fr; }
  .canvas-toolbar {
    align-items: stretch;
  }
  .toolbar-actions,
  .toolbar-report-actions {
    width: 100%;
  }
  .download-menu { width: 100%; }
  .download-menu summary { width: 100%; }
  .download-options {
    left: 0;
    right: auto;
    width: 100%;
  }
}
