:root {
  color-scheme: light;
  --bg: #f4f5f2;
  --panel: #ffffff;
  --panel-2: #f8f9f7;
  --ink: #172026;
  --muted: #5b6870;
  --line: #d8dedc;
  --line-strong: #b9c3c0;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e2f1ef;
  --danger: #b4233a;
  --plot-bg: #ffffff;
  --axis: #8e9a97;
  --grid-line: #e7ebe9;
  --empty-cell: #e3e7e5;
  --shadow: 0 10px 30px rgba(23, 32, 38, 0.07);
  --radius: 8px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #11161a;
    --panel: #192026;
    --panel-2: #1e262d;
    --ink: #e6ecea;
    --muted: #98a6a4;
    --line: #2b353c;
    --line-strong: #3d4a52;
    --accent: #2bb3a3;
    --accent-ink: #062320;
    --accent-soft: #173733;
    --danger: #f07c8e;
    --plot-bg: #141a1f;
    --axis: #5d6b70;
    --grid-line: #232c32;
    --empty-cell: #2a3238;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #11161a;
  --panel: #192026;
  --panel-2: #1e262d;
  --ink: #e6ecea;
  --muted: #98a6a4;
  --line: #2b353c;
  --line-strong: #3d4a52;
  --accent: #2bb3a3;
  --accent-ink: #062320;
  --accent-soft: #173733;
  --danger: #f07c8e;
  --plot-bg: #141a1f;
  --axis: #5d6b70;
  --grid-line: #232c32;
  --empty-cell: #2a3238;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--accent); }
code { font-size: 0.92em; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

h1, h2 { margin: 0; letter-spacing: 0; }
h1 { font-size: 18px; font-weight: 750; line-height: 1.2; }
h2 { font-size: 14px; font-weight: 720; }

/* ------------------------------------------------------------ top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand > div { min-width: 0; }
.brand h1 { overflow: hidden; text-overflow: ellipsis; }
.logo { width: 34px; height: 34px; flex: none; }
.tagline { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

/* ------------------------------------------------------------ buttons & inputs */
.btn, .segmented button, .menu-list button, .icon-btn {
  font: inherit;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 7px;
  height: 34px;
  padding: 0 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover, .segmented button:hover:not(:disabled), .icon-btn:hover { border-color: var(--line-strong); }
.btn:focus-visible, .segmented button:focus-visible, input:focus-visible, select:focus-visible, .result-main:focus-visible, .chip button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.large { height: 42px; padding: 0 20px; font-size: 15px; }
.btn.small { height: 28px; padding: 0 9px; font-size: 12.5px; }
.btn.ghost { background: transparent; }
.btn.icon { width: 34px; padding: 0; }
.btn.icon svg { width: 18px; height: 18px; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

input[type="number"], input[type="text"], select {
  font: inherit;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 7px;
  height: 32px;
  padding: 0 8px;
  width: 100%;
  min-width: 0;
}
input[type="checkbox"] { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }

.segmented { display: flex; flex-wrap: wrap; gap: 4px; }
.segmented button { height: 30px; padding: 0 10px; font-size: 13px; }
.segmented.small button { height: 26px; padding: 0 8px; font-size: 12px; }
.segmented button.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }

.menu { position: relative; }
.menu-list {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 4px;
  display: flex;
  flex-direction: column;
  min-width: 210px;
  z-index: 30;
}
.menu-list button { border: 0; justify-content: flex-start; height: 32px; }
.menu-list button:hover { background: var(--accent-soft); }

/* ------------------------------------------------------------ status bar */
.status-bar {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 40;
  background: var(--ink);
  color: var(--bg);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
}
.status-bar.error { background: var(--danger); color: #fff; }
.progress { width: 120px; height: 5px; background: rgba(255,255,255,0.25); border-radius: 3px; overflow: hidden; flex: none; }
#progressFill { height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }

/* ------------------------------------------------------------ welcome */
.welcome { flex: 1; display: grid; place-items: center; padding: 32px 16px; }
.dropzone {
  max-width: 720px;
  width: 100%;
  background: var(--panel);
  border: 2px dashed var(--line-strong);
  border-radius: 14px;
  padding: 36px 36px 28px;
  box-shadow: var(--shadow);
}
.dropzone h2 { font-size: 22px; margin-bottom: 8px; }
.dropzone p { color: var(--muted); line-height: 1.5; margin: 0 0 20px; }
.welcome-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.dropzone .demo-note { font-size: 12.5px; margin: 0 0 22px; }
.formats { display: grid; grid-template-columns: 110px 1fr; gap: 8px 16px; margin: 0; border-top: 1px solid var(--line); padding-top: 18px; font-size: 13px; }
.formats dt { font-weight: 700; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; padding-top: 1px; }
.formats dd { margin: 0; color: var(--muted); line-height: 1.45; }

/* ------------------------------------------------------------ layout */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 16px;
  padding: 16px 20px 8px;
  align-items: start;
}
.sidebar { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 76px; max-height: calc(100vh - 92px); overflow-y: auto; padding-bottom: 4px; }
.content {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(330px, 1fr);
  gap: 16px;
  align-items: start;
  min-width: 0;
}
.map-panel { grid-column: 1; }
.side-col { grid-column: 2; display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.spectrum-panel, .info-panel { grid-column: 1 / -1; }

.card, .panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 0;
}
.card { padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.card-head { display: flex; align-items: center; justify-content: space-between; }
.panel { padding: 12px 14px 14px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.panel-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; overflow-wrap: anywhere; }
.panel-tools { display: flex; align-items: center; gap: 8px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field.row { flex-direction: row; align-items: flex-end; gap: 10px; }
.field.row .grow { flex: 1; display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 11px; font-weight: 720; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; min-height: 24px; }
.check.small { font-size: 12.5px; }
.check:has(input:disabled) { color: var(--muted); cursor: not-allowed; }
.field.row .check { height: 32px; }
.hint { color: var(--muted); font-size: 12px; line-height: 1.45; margin: 0; }
.hint:empty { display: none; }
.inline-num { width: 56px !important; height: 24px !important; padding: 0 4px !important; display: inline-block; }
.limits { display: grid; grid-template-columns: 1fr auto 1fr; gap: 6px; align-items: center; }
.mge-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.mge-row .inline { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.mge-row .inline input { width: 64px; height: 28px; }

.quantity-buttons { display: flex; flex-direction: column; gap: 4px; }
.q-group-label { font-size: 11px; color: var(--muted); margin-top: 4px; }
.q-group-label:first-child { margin-top: 0; }

/* ------------------------------------------------------------ results list */
.result-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.result-item {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  border-left: 4px solid var(--result-color);
}
.result-item.active { border-color: var(--accent); border-left-color: var(--result-color); background: var(--accent-soft); }
.result-main {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 7px 9px;
  font: inherit;
  color: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.result-name { font-weight: 650; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-meta { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { border: 0; border-left: 1px solid var(--line); border-radius: 0; height: auto; width: 32px; padding: 0; background: transparent; font-size: 16px; color: var(--muted); }
.icon-btn:hover { color: var(--danger); }

/* ------------------------------------------------------------ map */
.map-shell { display: grid; grid-template-columns: minmax(0, 1fr) 70px; gap: 4px; }
.map-wrap { position: relative; min-width: 0; }
canvas { display: block; width: 100%; }
#mapCanvas { height: min(72vh, 740px); min-height: 420px; cursor: crosshair; touch-action: none; }
#mapCanvas.panning { cursor: grabbing; }
#colorbarCanvas { height: min(72vh, 740px); min-height: 420px; }
.map-status { color: var(--muted); font-size: 12px; min-height: 16px; margin-top: 4px; }
.tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--ink);
  color: var(--bg);
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 5px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------------ bins & LOSVD */
.bin-input { display: flex; gap: 4px; align-items: center; }
.bin-input input { width: 76px; height: 28px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.chips:empty { display: none; }
.chip {
  display: inline-flex;
  align-items: center;
  border: 1.5px solid var(--chip-color);
  border-radius: 999px;
  overflow: hidden;
  font-size: 12.5px;
}
.chip.active { background: var(--chip-color); }
.chip.active button { color: #fff; font-weight: 650; }
.chip button { border: 0; background: transparent; color: var(--ink); font: inherit; cursor: pointer; height: 24px; }
.chip-label { padding: 0 4px 0 10px; }
.chip-close { padding: 0 8px 0 4px; font-size: 14px; }
.metrics { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 6px; }
.metric { border: 1px solid var(--line); border-radius: 7px; padding: 6px 8px; min-width: 0; }
.metric-name { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metric-value { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; overflow-wrap: anywhere; }
#losvdCanvas { height: 300px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px; font-size: 12px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.legend-item::before { content: ""; width: 14px; height: 3px; border-radius: 2px; background: var(--swatch); }
.legend-item.box::before { height: 10px; width: 12px; border-radius: 2px; }
.legend-line { display: flex; gap: 16px; margin-top: 6px; }
.losvd-panel .hint { margin-top: 6px; }

/* ------------------------------------------------------------ spectrum & info */
#spectrumCanvas { height: 330px; cursor: ew-resize; touch-action: none; }
.info-panel summary { cursor: pointer; font-weight: 700; }
.info-panel dl { display: grid; grid-template-columns: minmax(120px, max-content) 1fr; gap: 4px 16px; margin: 12px 0 0; font-size: 12.5px; }
.info-panel dt { color: var(--muted); }
.info-panel dd { margin: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }

.footer { color: var(--muted); font-size: 12px; padding: 12px 20px 18px; text-align: center; }

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  pointer-events: none;
}
.drop-overlay div {
  background: var(--panel);
  border: 2px dashed var(--accent);
  border-radius: 14px;
  padding: 28px 44px;
  font-size: 18px;
  font-weight: 700;
}

/* ------------------------------------------------------------ responsive */
@media (max-width: 1280px) {
  .content { grid-template-columns: minmax(0, 1fr); }
  .map-panel, .side-col, .spectrum-panel, .info-panel { grid-column: 1; }
}
@media (max-width: 900px) {
  .workspace { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
  .sidebar { position: static; max-height: none; overflow: visible; order: 2; }
  .content { order: 1; }
  .topbar { padding: 10px 16px; }
  .tagline { display: none; }
  #mapCanvas, #colorbarCanvas { height: 70vw; min-height: 300px; }
  .map-shell { grid-template-columns: minmax(0, 1fr) 62px; }
  .dropzone { padding: 24px 20px; }
  .formats { grid-template-columns: 1fr; gap: 2px; }
  .formats dd { margin-bottom: 8px; }
}
@media (max-width: 520px) {
  #repoLink, .h1-suffix { display: none; }
  .topbar { gap: 8px; }
  .topbar-actions { gap: 6px; }
  .logo { width: 28px; height: 28px; }
  h1 { font-size: 15px; white-space: nowrap; }
  .topbar .btn { padding: 0 9px; }
}
