:root{
  --bg: #0f1117;
  --panel: #161a23;
  --text: #e6e8ef;
  --muted: #aab2c5;
  --border: #232635;

  /* --- spline visuals --- */
  --spline-color: #ffd480;      /* warm yellow for spline */
  --spline-width: 0.1;          /* tube radius in meters */

  /* --- control points --- */
  --ctrl-color: #66ccff;
  --ctrl-selected-color: #ff6699;
  --ctrl-size: 0.4;             /* sphere radius (m) */

  /* --- sample points --- */
  --sample-color: #ffffff;
  --sample-selected-color: #ffcc00;
  --sample-size: 0.25;          /* sphere radius (m) */

  /* --- trajectory overlay --- */
  --trajectory-color: #ff4d8d;
  --trajectory-groundtruth-color: #4dff88;
  --trajectory-point-size: 0.18;   /* sphere radius (m) */
  --trajectory-tube-radius: 0.07;  /* tube radius (m) */

  /* --- metric rectangles --- */
  --rect-gt-color: #4dff88;
  --rect-gt-opacity: 0.24;
  --rect-sample-color: #66ccff;
  --rect-sample-opacity: 0.18;
  --rect-line-width: 2;
}

* { box-sizing: border-box; }
.hidden { display: none !important; }
html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
}
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

header {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 4000;
  min-height: 58px;
}
.toolbar {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 12px;
}
.toolbar-mobile {
  display: none;
}
.toolbar-panels {
  display: flex;
  flex: 1 1 auto;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  min-width: 0;
}
.toolbar-panels .left,
.toolbar-panels .middle,
.toolbar-panels .right {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}
.toolbar-panels .pcd-main {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.toolbar-divider {
  color: var(--muted);
  opacity: 0.75;
  user-select: none;
}
.toolbar-panels .pcd-views {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.menu-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  background: #111420;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  flex-shrink: 0;
}
.menu-toggle:focus-visible {
  outline: 2px solid rgba(102, 204, 255, 0.6);
  outline-offset: 2px;
}
.menu-toggle .menu-icon {
  position: relative;
  display: inline-block;
  width: 18px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
  transition: background 0.2s ease;
  margin: 0 auto;
  flex-shrink: 0;
}
.menu-toggle .menu-icon::before,
.menu-toggle .menu-icon::after {
  content: "";
  position: absolute;
  left: 0;
  width: 100%;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
  transition: transform 0.2s ease, top 0.2s ease, opacity 0.2s ease, background 0.2s ease;
}
.menu-toggle .menu-icon::before { top: -6px; }
.menu-toggle .menu-icon::after { top: 6px; }
body.menu-open .menu-toggle .menu-icon { background: transparent; }
body.menu-open .menu-toggle .menu-icon::before { top: 0; transform: rotate(45deg); }
body.menu-open .menu-toggle .menu-icon::after { top: 0; transform: rotate(-45deg); }

#menuBackdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 10, 16, 0.55);
  z-index: 3500;
  display: none;
  cursor: pointer;
}
#menuBackdrop[hidden] { display: none !important; }
body.menu-open #menuBackdrop { display: block; }
body.menu-open { overflow: hidden; }

label { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); font-size: 14px; line-height: 1.2; }
label.checkbox { gap: 8px; }
label.file { padding: 6px 12px; background: #111420; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; color: var(--text); font-size: 14px; line-height: 1.2; height: 34px; display: inline-flex; align-items: center; }
label.file input { display: none; }

select, button {
  background: #111420; border: 1px solid var(--border); color: var(--text);
  padding: 6px 12px; border-radius: 8px; cursor: pointer;
  font-size: 14px; line-height: 1.2; height: 34px;
  display: inline-flex; align-items: center; gap: 6px;
}
button:disabled,
.toggle-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
input[type="range"] { cursor: ew-resize; height: 4px; }
.toggle-btn.active{
  background: #1d3b55;
  border-color: rgba(102, 204, 255, 0.4);
}
body.optimizing-cursor {
  cursor: progress !important;
}
#stage3d.optimizing-cursor,
#stage3d.optimizing-cursor canvas.threejs {
  cursor: progress !important;
}

#charts.hidden {
  display: none;
}

.front-image {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  width: 30vw;
  max-width: 95vw;
  max-height: 90vh;
  min-width: 220px;
  background: #0c0f18;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.45);
  z-index: 3800;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  resize: both;
  overflow: hidden;
}
.front-image.fullscreen-like {
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  left: 0;
  top: 0;
  transform: none;
  resize: none;
  border-radius: 0;
  box-shadow: none;
  z-index: 12000;
  max-width: none;
  max-height: none;
  min-width: 0;
  min-height: 0;
}
.front-image.fullscreen-like .front-image-body { background: #000; }
.front-image-body {
  position: relative;
  width: 100%;
  height: 100%;
  background: #0b0e16;
}
.front-image-body img {
  display: block;
  width: 100%;
  height: auto;
  object-fit: contain;
}
.front-image.fullscreen-like .front-image-body img {
  width: 100%;
  height: 100%;
}
.front-image-full { overflow: hidden; }
.front-image.hidden {
  display: none;
}

#optimizeBtn {
  position: relative;
  transition: background-color 0.3s ease, box-shadow 0.3s ease, transform 0.2s ease;
}

#optimizeBtn.optimizing {
  animation: optimize-pulse 1.1s ease-in-out infinite;
  cursor: progress;
}

#optimizeBtn[disabled] {
  opacity: 1;
}

#optimizeBtn.optimized-flash {
  animation: optimize-finish 0.7s ease-out;
}

@keyframes optimize-pulse {
  0% {
    background-color: #111420;
    box-shadow: 0 0 0 0 rgba(102, 204, 255, 0.0);
    border-color: var(--border);
  }
  50% {
    background-color: #1d3b55;
    box-shadow: 0 0 14px 0 rgba(102, 204, 255, 0.65);
    border-color: rgba(102, 204, 255, 0.7);
  }
  100% {
    background-color: #111420;
    box-shadow: 0 0 0 0 rgba(102, 204, 255, 0.0);
    border-color: var(--border);
  }
}

@keyframes optimize-finish {
  0% {
    background-color: #38bdf8;
    box-shadow: 0 0 18px 0 rgba(56, 189, 248, 0.75);
    border-color: rgba(56, 189, 248, 0.8);
  }
  100% {
    background-color: #111420;
    box-shadow: 0 0 0 0 rgba(56, 189, 248, 0);
    border-color: var(--border);
  }
}

main { flex: 1 1 auto; min-height: 0; }
#stage3d { position: relative; width: 100%; height: 100%; background: #0b0d13; }
#stage3d canvas.threejs { width: 100%; height: 100%; display: block; }

#scenarioInfo{
  position: absolute;
  top: 12px;
  left: 120px;
  background: rgba(17, 20, 32, 0.9);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 16px;
  color: var(--text);
  font-size: 15px;
  display: none;
  box-shadow: 0 10px 24px rgba(0,0,0,0.35);
  z-index: 3000;
  max-width: 80vw;
  min-width: 0;
  text-align: left;
  line-height: 1.35;
}
.scenario-info-line {
  display: block;
  white-space: nowrap;
  margin-bottom: 4px;
}
.scenario-info-label {
  display: inline-block;
  min-width: 100px;
  font-weight: 600;
  color: var(--text);
}
.scenario-info-text {
  color: var(--text);
  white-space: nowrap;
}
.scenario-info-line:last-child { margin-bottom: 0; }

#exportWarnDlg .dialog-body {
  width: auto;
  min-width: 280px;
  max-width: 80vw;
}
#exportWarnDlg .actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
#exportWarnDlg p {
  margin-bottom: 16px;
  color: var(--muted);
}
.dialog-close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 20px;
  cursor: pointer;
}
#exportWarnDlg .dialog-close { font-size: 22px; }
.dialog-close:hover { color: var(--text); }
#exportWarnDlg .export-warn-dialog {
  position: relative;
}

#weightsPanel{
  position: absolute;
  left: 12px;
  top: 120px;
  width: 540px;
  display: none;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  background: rgba(0,0,0,0.84);
  border: 1px solid #2a2f40;
  border-radius: 10px;
  pointer-events: auto;
  color: var(--text);
  box-shadow: 0 6px 20px rgba(0,0,0,0.35);
}
#weightsPanel.open{
  display: flex;
}
#weightsPanel .panel-title{
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
#weightsPanel .slider-row{
  display: grid;
  grid-template-columns: 90px 1fr 80px;
  align-items: center;
  gap: 10px;
}
#weightsPanel .slider-row label{
  color: var(--text);
  font-size: 13px;
}
#weightsPanel input[type="range"]{
  width: 100%;
}
#weightsPanel input[type="number"]{
  background: #111420;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 6px;
  border-radius: 6px;
  font-variant-numeric: tabular-nums;
  width: 100%;
}
#weightsPanel input[type="number"]::-webkit-outer-spin-button,
#weightsPanel input[type="number"]::-webkit-inner-spin-button{
  opacity: 0.2;
}
#weightsPanel input[type="number"]:focus{
  outline: none;
  border-color: rgba(102, 204, 255, 0.5);
  box-shadow: 0 0 0 1px rgba(102, 204, 255, 0.25);
}

footer {
  padding: 8px 12px; background: var(--panel); border-top: 1px solid var(--border);
  color: var(--muted);
  display: flex; align-items: center; gap: 12px;
}

#status { flex: 1 1 auto; min-width: 0; }

#statusExtra {
  flex: 0 0 auto;
  color: var(--muted);
  font-weight: 400;
  min-height: 1em;
  text-align: right;
  padding-left: 12px;
  border-left: 1px solid rgba(255, 255, 255, 0.08);
}

/* Mode badge (inside stage) */
.badge {
  position: absolute; left: 12px; top: 12px;
  padding: 4px 8px; border-radius: 10px; font-weight: 600; font-size: 15px;
  background: rgba(0,0,0,0.8); border: 1px solid var(--border); color: var(--text);
  pointer-events: auto;
  cursor: pointer;
}
.badge.two { background: rgba(0, 90, 255, 0.8); }
.badge.three { background: rgba(0, 180, 120, 0.8); }

/* Charts (top-right) */
#charts{
  position: absolute; right: 12px; top: 12px;
  width: min(30vw, 460px);
  height: 36vh;
  min-width: 220px;
  min-height: 220px;
  max-width: 75vw;
  max-height: 80vh;
  resize: both;
  overflow: hidden;
  box-sizing: border-box;
  pointer-events: auto;
  direction: rtl; /* move native resize handle to bottom-left */
}
#charts .charts-inner{
  direction: ltr;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  height: 100%;
}
#charts .charts-inner svg{
  flex: 1 1 0;
  width: 100%;
  height: 100%;
  min-height: 0;
  background: rgba(0,0,0,0.8);
  border: 1px solid #2a2f40; border-radius: 8px;
  display: block;
}


/* Legend (bottom-right, vertical) */
#legend{
  position: absolute; right: 12px; bottom: 12px;
  display: flex; align-items: flex-end; gap: 8px;
  background: rgba(0,0,0,0.8); padding: 6px 8px; border-radius: 8px;
  color: #e6e8ef; font: 12px/1.2 ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
  pointer-events: none;
}
#legendCanvas{
  width: 20px; height: 160px;
  border: 1px solid var(--border); border-radius: 6px; background:#0b0d13;
}
#legend .labels{ display: grid; row-gap: 4px; }
#legend .ticks{ display: flex; flex-direction: column; justify-content: space-between; height: 160px; }

body.toolbar-collapsed .menu-toggle {
  display: inline-flex;
}
body.toolbar-collapsed .toolbar {
  justify-content: space-between;
}
body.toolbar-collapsed .toolbar-mobile {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  overflow-x: auto;
  padding-right: 8px;
  min-width: 0;
}
body.toolbar-collapsed .toolbar-mobile > * {
  flex: 0 0 auto;
}
body.toolbar-collapsed .toolbar-mobile::-webkit-scrollbar {
  height: 4px;
}
body.toolbar-collapsed .toolbar-mobile::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.25);
  border-radius: 4px;
}
body.toolbar-collapsed .toolbar-panels {
  display: none;
  position: fixed;
  top: var(--toolbar-offset, 58px);
  left: 0;
  right: 0;
  background: rgba(16, 20, 32, 0.96);
  border-bottom: 1px solid var(--border);
  flex-direction: column;
  gap: 18px;
  padding: 18px 18px 28px;
  max-height: calc(100vh - var(--toolbar-offset, 58px));
  overflow-y: auto;
  z-index: 3600;
}
body.toolbar-collapsed.menu-open .toolbar-panels {
  display: flex;
}
body.toolbar-collapsed .toolbar-panels .left,
body.toolbar-collapsed .toolbar-panels .middle,
body.toolbar-collapsed .toolbar-panels .right {
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
}
body.toolbar-collapsed .toolbar-panels .pcd-main {
  width: 100%;
}
body.toolbar-collapsed .toolbar-panels .pcd-views {
  margin-left: 0;
  justify-content: center;
  width: 100%;
}
body.toolbar-collapsed .toolbar-panels label,
body.toolbar-collapsed .toolbar-panels select,
body.toolbar-collapsed .toolbar-panels button,
body.toolbar-collapsed .toolbar-panels .toggle-btn {
  width: 100%;
  justify-content: center;
}
body.toolbar-collapsed .toolbar-panels .mode-btn {
  display: none;
}
body.toolbar-collapsed #stage3d {
  min-height: 60vh;
}
body.toolbar-collapsed #charts {
  left: auto;
  right: 12px;
  top: 12px;
  bottom: auto;
  width: min(60vw, 320px);
  height: min(34vh, 220px);
  min-width: 160px;
  min-height: 140px;
}
body.toolbar-collapsed #legend {
  right: 12px;
  left: auto;
  bottom: 12px;
  top: auto;
  padding: 4px 6px;
  gap: 6px;
}
body.toolbar-collapsed #legendCanvas {
  width: 18px;
  height: 120px;
}
body.toolbar-collapsed #legend .ticks {
  height: 120px;
}
body.toolbar-collapsed footer {
  display: none;
}
body.toolbar-collapsed #statusExtra {
  border-left: none;
  padding-left: 0;
  text-align: left;
}
body.toolbar-collapsed #weightsPanel {
  left: 12px;
  right: 12px;
  width: auto;
  max-width: none;
}
body.toolbar-collapsed #scenarioInfo {
  left: 12px;
  right: 12px;
  top: 70px;
}

@media (max-width: 600px) {
  header {
    padding: 6px 10px;
  }
  body.toolbar-collapsed .toolbar-panels label,
  body.toolbar-collapsed .toolbar-panels select,
  body.toolbar-collapsed .toolbar-panels button {
    font-size: 13px;
    padding: 8px 10px;
    height: auto;
  }
  body.toolbar-collapsed #charts {
    left: auto;
    right: 8px;
    top: 8px;
    bottom: auto;
    width: min(70vw, 240px);
    height: min(38vh, 180px);
    min-width: 140px;
    min-height: 128px;
  }
  body.toolbar-collapsed #legend {
    right: 8px;
    bottom: 8px;
    padding: 3px 5px;
  }
  body.toolbar-collapsed #legendCanvas {
    height: 96px;
    width: 16px;
  }
  body.toolbar-collapsed #legend .ticks {
    height: 96px;
  }
}

/* Dialogs */
dialog#helpDlg,
dialog#maneuverDlg,
dialog#exportWarnDlg,
dialog#deviationDlg {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #111420;
  color: var(--text);
  max-width: 520px;
  width: calc(100% - 32px);
}
.dialog-body { padding: 16px; position: relative; }
.dialog-body h3 { margin: 0 0 8px; }
.dialog-body h4 { margin: 16px 0 8px; font-size: 14px; color: var(--muted); }
.dialog-body ul { margin: 8px 0 16px 18px; color: var(--muted); }
.help-grid {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 4px 12px;
  font-size: 14px;
  color: var(--muted);
}
.help-grid .help-key {
  font-weight: 600;
  color: var(--text);
}
.help-grid kbd {
  font-weight: 600;
}
.help-note {
  margin: 16px 0;
  color: var(--muted);
  font-size: 13px;
}
.dialog-body .actions { display: flex; justify-content: flex-end; }
.dialog-body .actions .primary {
  background: #2563eb;
  border-color: rgba(37, 99, 235, 0.85);
  color: #fff;
}
.dialog-body .actions .primary:hover {
  background: #1d4ed8;
  border-color: rgba(29, 78, 216, 0.9);
}
dialog::backdrop{ background: rgba(0,0,0,0.4); }

.maneuver-dialog {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.deviation-dialog {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.maneuver-intro {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}
.deviation-intro {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}
.deviation-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #121622;
  color: var(--text);
  font-size: 15px;
}
.deviation-input:focus {
  outline: 2px solid rgba(102, 204, 255, 0.5);
  border-color: rgba(102, 204, 255, 0.6);
}
.maneuver-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.maneuver-option {
  display: flex;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #121622;
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease;
}
.maneuver-option:hover {
  border-color: rgba(102, 204, 255, 0.6);
}
.maneuver-option.selected {
  border-color: rgba(102, 204, 255, 0.9);
  background: rgba(40, 80, 120, 0.35);
}
.maneuver-option input[type="radio"] {
  margin-top: 4px;
}
.maneuver-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.maneuver-title {
  font-weight: 600;
  color: var(--text);
}
.maneuver-desc {
  font-size: 13px;
  color: var(--muted);
  max-width: 420px;
}
.maneuver-dialog .actions {
  gap: 8px;
}

/* KBD styling in footer */
kbd {
  background: #151a25; border: 1px solid var(--border); border-bottom-color: #141722;
  padding: 1px 5px; border-radius: 5px; font-size: 12px; color: var(--text);
}
