:root {
  --bg-0: #f3f1ee;
  --bg-1: #e8edf4;
  --panel: #ffffff;
  --panel-border: #d3d7df;
  --text: #1c1d22;
  --muted: #4f5763;
  --input-bg: #ffffff;
  --input-border: #c6ccd6;
  --input-text: #1c1d22;
  --input-muted: #8a93a3;
  --button-bg: #f7f8fb;
  --button-text: #1c1d22;
  --checkbox-bg: #ffffff;
  --status-bg: #e9f1ff;
  --status-text: #243658;
  --accent: #254b7f;
  --accent-2: #c9622a;
  --chip-synchrone: #2f855a;
  --chip-syndyne: #b45309;
  --chip-sun: #b7791f;
  --chip-comet: #2c5282;
  --chip-velocity: #1d4ed8;
  --shadow: 0 18px 36px rgba(17, 23, 35, 0.08);
}

body.dark {
  --bg-0: #0f1012;
  --bg-1: #0b0c0e;
  --panel: #15171b;
  --panel-border: #2c3036;
  --text: #e6e6e6;
  --muted: #a4a9b1;
  --input-bg: #121418;
  --input-border: #31363e;
  --input-text: #e6e6e6;
  --input-muted: #7a808a;
  --button-bg: #1a1d22;
  --button-text: #e6e6e6;
  --checkbox-bg: #121418;
  --status-bg: #1b1e24;
  --status-text: #cfd3da;
  --accent: #6aa0ff;
  --accent-2: #f0a45c;
  --chip-synchrone: #5fbd8a;
  --chip-syndyne: #9ea4ad;
  --chip-sun: #d8a85f;
  --chip-comet: #8aa8ff;
  --chip-velocity: #8bb0ff;
  --shadow: 0 18px 36px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  color: var(--text);
  background: radial-gradient(1200px 600px at 10% -10%, #ffffff 0%, var(--bg-0) 45%, var(--bg-1) 100%);
  min-height: 100vh;
}

body.dark {
  background: radial-gradient(1200px 600px at 10% -10%, #1b1d21 0%, var(--bg-0) 45%, var(--bg-1) 100%);
}

.bg {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(800px 400px at 80% 0%, rgba(37, 75, 127, 0.08), transparent 60%),
    radial-gradient(600px 400px at 0% 100%, rgba(201, 98, 42, 0.08), transparent 60%);
  z-index: -1;
}

body.dark .bg {
  background:
    radial-gradient(800px 400px at 80% 0%, rgba(130, 130, 130, 0.12), transparent 60%),
    radial-gradient(600px 400px at 0% 100%, rgba(150, 150, 150, 0.08), transparent 60%);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 24px 8px;
  border-bottom: 1px solid var(--panel-border);
  background: var(--panel);
}

.brand {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background: #f2f2f2;
  color: #333;
  font-weight: 700;
  display: grid;
  place-items: center;
  letter-spacing: 0.5px;
  border: 1px solid var(--panel-border);
}

.title {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.2px;
  font-family: "Space Grotesk", "IBM Plex Sans", sans-serif;
}
.subtitle { font-size: 0.72rem; color: var(--muted); letter-spacing: 0.4px; }

.actions { display: flex; gap: 12px; }

button {
  font-family: inherit;
  font-weight: 600;
  border: 1px solid var(--panel-border);
  padding: 6px 10px;
  border-radius: 4px;
  cursor: pointer;
  background: var(--button-bg);
  color: var(--button-text);
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  text-shadow: none;
  box-shadow: none;
}

button.primary:hover {
  border-color: #245a92;
}

button.ghost {
  background: var(--button-bg);
  color: var(--button-text);
  border: 1px solid var(--panel-border);
  box-shadow: none;
}

button.ghost:hover {
  border-color: rgba(255, 255, 255, 0.3);
}

button.small { padding: 6px 12px; font-size: 0.85rem; }

.json-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  border-radius: 6px;
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--button-text);
  text-decoration: none;
}

.json-link.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.grid {
  display: grid;
  gap: 18px;
  padding: 18px 24px 32px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-areas:
    "mini mini mini"
    "steps steps steps"
    "image observer elements"
    "manual manual manual"
    "plot plot plot"
    "results results results"
    "citation citation citation";
  max-width: 1400px;
  margin: 0 auto;
}

.mini-header {
  grid-area: mini;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 4px;
}

.title-card {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.title-actions {
  display: inline-flex;
  align-items: center;
  gap: 14px;
}

.mini-title {
  font-family: "Space Grotesk", "IBM Plex Sans", sans-serif;
  font-weight: 600;
  font-size: 1.15rem;
  color: var(--text);
}

.title-link {
  font-size: 0.8rem;
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px dashed rgba(20, 24, 32, 0.35);
}

.title-link:hover {
  border-bottom-color: rgba(20, 24, 32, 0.7);
}

.title-details {
  margin-top: 6px;
  font-size: 0.82rem;
  color: var(--muted);
}

.title-details summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  list-style: none;
  margin-bottom: 4px;
}

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

.title-details summary::before {
  content: "▶";
  display: inline-block;
  margin-right: 6px;
  font-size: 0.7rem;
  transform: translateY(-1px);
}

.title-details[open] summary::before {
  transform: rotate(90deg) translateY(-1px);
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 600;
}

.theme-toggle input {
  display: none;
}

.toggle-track {
  width: 36px;
  height: 20px;
  border-radius: 999px;
  background: rgba(26, 34, 46, 0.15);
  border: 1px solid var(--panel-border);
  position: relative;
  transition: background 0.2s ease, border 0.2s ease;
}

.toggle-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  position: absolute;
  top: 2px;
  left: 2px;
  transition: transform 0.2s ease;
}

.theme-toggle input:checked + .toggle-track {
  background: rgba(106, 160, 255, 0.35);
  border-color: rgba(106, 160, 255, 0.6);
}

.theme-toggle input:checked + .toggle-track .toggle-thumb {
  transform: translateX(16px);
}

.mini-actions {
  display: flex;
  gap: 8px;
}

.citation {
  grid-area: citation;
  font-size: 0.78rem;
  color: var(--muted);
  padding: 4px 6px 10px;
}

.citation a {
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px dashed rgba(20, 24, 32, 0.35);
}

.cite-tool {
  margin-top: 6px;
  color: var(--muted);
}

.cite-bibtex-link {
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px dashed rgba(20, 24, 32, 0.35);
}

.bibtex {
  margin-top: 6px;
  font-size: 0.75rem;
  color: var(--muted);
}

.bibtex summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  list-style: none;
  margin-bottom: 4px;
}

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

.bibtex summary::before {
  content: "▶";
  display: inline-block;
  margin-right: 6px;
  font-size: 0.7rem;
  transform: translateY(-1px);
}

.bibtex[open] summary::before {
  transform: rotate(90deg) translateY(-1px);
}

.bibtex pre {
  margin: 0;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--input-border);
  background: var(--input-bg);
  color: var(--input-text);
  white-space: pre-wrap;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 12px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 0.9rem;
  letter-spacing: 0.3px;
  text-transform: none;
  border-bottom: 1px solid var(--panel-border);
  padding-bottom: 6px;
  font-family: "Space Grotesk", "IBM Plex Sans", sans-serif;
}

.panel.steps { grid-area: steps; }
.panel.image { grid-area: image; }
.panel.observer { grid-area: observer; }
.panel.elements { grid-area: elements; }
.panel.manual { grid-area: manual; }
.panel.plot { grid-area: plot; }
.panel.plot h2 {
  margin-bottom: 8px;
}
.panel.results { grid-area: results; }
.panel.results {
  display: flex;
  flex-direction: column;
  min-height: 420px;
}

.panel.manual {
  padding: 0;
}

.panel.manual > summary {
  list-style: none;
  cursor: pointer;
  padding: 12px;
  font-weight: 600;
  font-family: "Space Grotesk", "IBM Plex Sans", sans-serif;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.panel.manual .panel-body {
  padding: 12px;
}

.summary-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
}

.summary-left {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.summary-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.panel.manual .target-library {
  min-width: 200px;
}

.summary-arrow::before {
  content: "▶";
  display: inline-block;
  transition: transform 0.2s ease;
  color: var(--muted);
}

.panel.manual[open] .summary-arrow::before {
  transform: rotate(90deg);
}

.manual-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.steps-header {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  margin-bottom: 12px;
}

.status-box {
  padding: 8px 12px;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background: var(--status-bg);
  font-size: 0.82rem;
  color: var(--status-text);
  min-width: 240px;
  width: 100%;
  text-align: left;
}

.steps-row {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  align-items: end;
}

.step-btn {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.step-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.step-check {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  font-size: 0.8rem;
  color: #1c7f4c;
}

.step-btn.done .step-check {
  opacity: 1;
}

body.dark .step-check {
  color: #74d59b;
}

.step-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(36, 54, 88, 0.25);
  border-top-color: rgba(36, 54, 88, 0.9);
  border-radius: 50%;
  display: none;
  animation: step-spin 0.8s linear infinite;
}

.step-btn.loading .step-spinner {
  display: inline-block;
}

.step-btn.loading .step-check {
  opacity: 0;
}

@keyframes step-spin {
  to { transform: rotate(360deg); }
}

.step-target input {
  width: 100%;
}


.fit-details {
  margin-top: 12px;
}

.fit-title {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 600;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

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

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

.meta-box {
  margin: 0;
  padding: 10px;
  border: 1px solid var(--input-border);
  border-radius: 6px;
  background: var(--input-bg);
  min-height: 72px;
  white-space: pre-wrap;
  font-size: 0.82rem;
  color: var(--input-text);
}

label {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: none;
  letter-spacing: 0.2px;
}

.inline-help {
  margin-left: 6px;
  padding: 2px 6px;
  font-size: 0.72rem;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.inline-help svg {
  width: 16px;
  height: 16px;
  display: block;
}

input, textarea, select {
  background: var(--input-bg);
  color: var(--input-text);
  border: 1px solid var(--input-border);
  border-radius: 4px;
  padding: 7px 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.88rem;
  box-shadow: none;
}

input:disabled, textarea:disabled, select:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

body.dark input:disabled,
body.dark textarea:disabled,
body.dark select:disabled {
  color: var(--input-muted);
}

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(43, 108, 176, 0.15);
}

input::placeholder,
textarea::placeholder {
  color: var(--input-muted);
}

input[type="file"] {
  color: var(--input-text);
}

input[type="file"]::-webkit-file-upload-button {
  background: var(--button-bg);
  color: var(--button-text);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 6px 10px;
  margin-right: 8px;
  cursor: pointer;
}

textarea {
  min-height: 80px;
  resize: vertical;
}

.inline {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.note {
  color: var(--muted);
  font-size: 0.72rem;
}

.note.small { font-size: 0.78rem; }

body.dark .leaflet-container,
body.dark .leaflet-control-zoom a {
  filter: grayscale(100%) contrast(1.05) brightness(0.8);
}

.hidden { display: none; }

.thumb-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-items: start;
}

.nucleus-block {
  margin-top: 8px;
}

#fits-thumb {
  width: 240px;
  height: 120px;
  max-width: 100%;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background: #0f1115;
  object-fit: contain;
  align-self: start;
}

.plot-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}

.tilt-readout {
  margin-top: 6px;
  font-size: 12px;
  color: #1f1f1f;
  font-weight: 600;
}

.legend {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text);
}

.legend-line {
  width: 22px;
  height: 2px;
  display: inline-block;
  background: var(--panel-border);
  border-radius: 2px;
}

.legend-line.synchrone { background: var(--chip-synchrone); }
.legend-line.syndyne {
  background: repeating-linear-gradient(
    90deg,
    rgba(140, 146, 154, 0.95) 0 2px,
    rgba(140, 146, 154, 0) 2px 6px
  );
}
.legend-line.sun { background: var(--chip-sun); }
.legend-line.velocity { background: var(--chip-velocity); }

.plot-stage {
  position: relative;
}

.plot-hover {
  position: absolute;
  min-width: 160px;
  max-width: 240px;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.72rem;
  line-height: 1.35;
  pointer-events: none;
  box-shadow: 0 6px 18px rgba(14, 20, 32, 0.12);
  white-space: pre-line;
  z-index: 4;
}

.plot-hover.hidden {
  display: none;
}

#plot {
  width: 100%;
  height: min(65vh, 640px);
  border-radius: 6px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  display: block;
  margin: 0 auto;
}

body.dark #plot {
  background: #0f1216;
}

.plot-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.85rem;
}

#status {
  font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.78rem;
  letter-spacing: 0.1px;
}

#json-output {
  flex: 1;
  width: 100%;
  min-height: 520px;
  resize: vertical;
  padding: 12px;
  font-size: 0.85rem;
  border-radius: 6px;
  border: 1px solid var(--input-border);
  background: var(--input-bg);
}

.fit-block {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.fit-block h3 {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}

#fit-output {
  width: 100%;
  min-height: 220px;
  resize: vertical;
  padding: 12px;
  font-size: 0.82rem;
  border-radius: 6px;
  border: 1px solid var(--input-border);
  background: var(--input-bg);
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 50;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.modal-card {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 16px;
  width: min(520px, 92vw);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.modal-body {
  color: var(--text);
  font-size: 0.9rem;
  line-height: 1.4;
}

.modal-body ul {
  margin: 8px 0 0 18px;
  padding: 0;
}

#map {
  width: 100%;
  height: 210px;
  border-radius: 4px;
  border: 1px solid var(--panel-border);
  overflow: hidden;
  margin-top: 10px;
}

#map .leaflet-tile {
  filter: grayscale(100%) contrast(1.05) brightness(0.8);
}

@media (max-width: 1200px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-areas:
      "mini"
      "steps"
      "image"
      "observer"
      "elements"
      "manual"
      "plot"
      "results"
      "citation";
  }
  .cols { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .steps-row { grid-template-columns: 1fr; }
  .thumb-row { grid-template-columns: 1fr; }
  #json-output { min-height: 320px; }
}

@media (max-width: 720px) {
  .topbar { flex-direction: column; align-items: flex-start; gap: 12px; }
  .actions { width: 100%; flex-wrap: wrap; }
  .cols { grid-template-columns: 1fr; }
  #plot { height: 420px; }
}
