:root {
  --bg: #0f1117;
  --panel: #1a1d27;
  --ink: #e7e9ee;
  --muted: #9aa0ad;
  --accent: #5b8cff;
  --accent-2: #ff8c5b;
  --error: #ff6b6b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Geist", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

header, main, footer { max-width: 880px; margin: 0 auto; padding: 1rem 1.25rem; }
header h1 { margin: 0.4rem 0 0.2rem; font-size: 2rem; letter-spacing: -0.5px; }
header p { color: var(--muted); margin: 0; }

section { background: var(--panel); border-radius: 12px; padding: 1.25rem; margin-bottom: 1rem; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.75rem; }
textarea, select, input {
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.6rem 0.7rem;
  background: #0d0f16;
  color: var(--ink);
  border: 1px solid #2a2f3d;
  border-radius: 8px;
  font: inherit;
}
textarea { resize: vertical; font-family: "Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace; }

.row { display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; }
.row > label { flex: 1 1 160px; margin-bottom: 0; }

button, .btn-upload {
  padding: 0.65rem 1.1rem;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
button:hover, .btn-upload:hover { filter: brightness(1.1); }
button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  filter: none;
}

/* Data source: example dataset / upload / column picker on one aligned grid. */
.source-grid {
  display: grid;
  grid-template-columns: 4fr 3fr 3fr;   /* ~40% / 30% / 30% */
  gap: 1rem;
  margin-bottom: 1rem;
}
.source-cell { display: flex; flex-direction: column; min-width: 0; }
.source-cell > label { margin-bottom: 0; }   /* caption; the control adds its own 0.3rem top gap */
/* Upload trigger shares the button style above; these only make it fill its cell. */
.btn-upload { display: block; width: 100%; margin-top: 0.3rem; text-align: center; }
.filename {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.4rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upload-limit { margin-top: 0.2rem; color: var(--muted); font-size: 0.72rem; }

/* CSV import settings and preview. Native <dialog> supplies focus trapping,
   Escape handling, and modal semantics without another frontend dependency. */
dialog {
  width: min(720px, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  overflow: auto;
  padding: 1.25rem;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid #2a2f3d;
  border-radius: 12px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.65);
}
dialog::backdrop { background: rgba(5, 6, 10, 0.78); backdrop-filter: blur(2px); }
.csv-import-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; }
.csv-import-header h2 { margin: 0; font-size: 1.35rem; }
.csv-import-header .filename { display: block; margin: 0.2rem 0 0; }
.dialog-close {
  flex: 0 0 auto;
  padding: 0.15rem 0.55rem;
  background: transparent;
  color: var(--muted);
  border: 1px solid #2a2f3d;
  font-size: 1.3rem;
  line-height: 1.2;
}
.dialog-close:hover { background: #0d0f16; color: var(--ink); filter: none; }
.csv-default-note { margin: 1rem 0; color: #c0c5d0; font-size: 0.9rem; }
.csv-import-settings { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; }
.csv-import-settings label { margin-bottom: 0; }
.csv-preview-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin: 1.25rem 0 0.45rem;
}
.csv-preview-wrap {
  max-height: 240px;
  overflow: auto;
  background: #0d0f16;
  border: 1px solid #2a2f3d;
  border-radius: 8px;
}
.csv-preview { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.csv-preview th,
.csv-preview td {
  max-width: 180px;
  padding: 0.45rem 0.6rem;
  overflow: hidden;
  border-right: 1px solid #2a2f3d;
  border-bottom: 1px solid #2a2f3d;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.csv-preview th { position: sticky; top: 0; background: #151924; color: var(--ink); }
.csv-preview tr:last-child td { border-bottom: 0; }
.csv-preview th:last-child,
.csv-preview td:last-child { border-right: 0; }
#csv-preview-error { margin: 0 0 0.45rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1rem; }

/* Keep the three controls on one row down to small windows; stack only on phones. */
@media (max-width: 420px) {
  .source-grid { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
  .csv-import-settings { grid-template-columns: 1fr; }
  .csv-preview-heading { display: block; }
  .csv-preview-heading .hint { display: block; margin-top: 0.2rem; }
}

.error { color: var(--error); margin: 0.75rem 0 0; }

/* Bottom-cell tabs. Underline-style nav above the result panels. */
.tabs {
  display: flex;
  gap: 0.25rem;
  border-bottom: 1px solid #2a2f3d;
  margin-bottom: 1.25rem;
}
.tab {
  background: transparent;
  color: var(--muted);
  border: 0;
  border-radius: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  padding: 0.55rem 0.9rem;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}
.tab:hover { color: var(--ink); filter: none; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab-panel-empty { color: var(--muted); font-size: 0.9rem; padding: 1rem 0; margin: 0; }
/* Compression method controls now live at the top of the Compress panel. */
.compress-row { margin-bottom: 0.5rem; }
/* Breathing room between the compression method description and the results below it. */
#compress-output { margin-top: 1.5rem; }

/* Download the compressed bytes / decompressed series the browser already holds.
   Secondary style: outlined, lighter than the primary Compress button. */
.download-row { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-bottom: 1rem; }
.btn-secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid #2a2f3d;
}
.btn-secondary:hover { background: #0d0f16; filter: none; }

/* Benchmark tab. Compression method picker: a wrapping grid of compact checkbox chips. */
.method-checks {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1rem;
  margin-top: 0.3rem;
}
.method-check {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0;
  color: var(--ink);
  font-size: 0.85rem;
  cursor: pointer;
}
.method-check input { width: auto; margin: 0; }
#bench-methods, #bench-bounds { margin-bottom: 1rem; }
#bench-output { margin-top: 1.5rem; }

/* Build tab: comparison table sits above the reconstruction plot. */
#build-output { margin-top: 1.5rem; }
#build-table { margin-bottom: 1.5rem; }

/* Optimize tab: compact progress and Pareto result table. */
.optimize-intro { margin: 0 0 1rem; }
.optimize-warning {
  margin: 0 0 1rem;
  padding: 0.7rem 0.9rem;
  border-left: 3px solid var(--accent-2);
  background: #17140d;
  color: #ded5bf;
  font-size: 0.9rem;
  line-height: 1.5;
}
.optimize-warning strong { color: var(--accent-2); }
.optimize-setup {
  display: grid;
  grid-template-columns: 210px minmax(0, 1fr);
  gap: 1rem;
  align-items: stretch;
  margin-bottom: 0.5rem;
}
.optimize-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.65rem;
  align-content: center;
  align-items: end;
}
.optimize-controls > label { min-width: 0; margin: 0; }
.optimize-controls input { padding-inline: 0.55rem; }
.optimize-controls button {
  grid-column: 1 / -1;
  width: 100%;
  white-space: nowrap;
}
.optimize-formula {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  padding: 0.8rem 0.95rem;
  border: 1px solid #304d78;
  border-left: 3px solid var(--accent);
  border-radius: 9px;
  background: linear-gradient(135deg, rgba(91, 140, 255, 0.13), rgba(13, 15, 22, 0.72));
}
.optimize-formula > strong {
  color: #b9ceff;
  font-size: 0.82rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.optimize-formula-equation {
  margin-top: 0.3rem;
  overflow-x: auto;
  color: var(--ink);
  font-family: "Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.86rem;
  font-weight: 600;
  line-height: 1.55;
  white-space: nowrap;
}
.optimize-formula p {
  margin: 0.35rem 0 0;
  color: #b8bfcc;
  font-size: 0.86rem;
  line-height: 1.45;
}
@media (max-width: 760px) {
  .optimize-setup { grid-template-columns: 1fr; }
}
@media (max-width: 420px) {
  .optimize-controls { grid-template-columns: 1fr 1fr; }
}
.optimize-message {
  margin: 0.9rem 0 0;
  padding: 0.75rem 0.9rem;
  border-left: 3px solid var(--accent);
  background: #0d0f16;
  color: #cbd1dc;
  font-size: 0.9rem;
  line-height: 1.5;
}
.optimize-message[data-tone="success"] { border-left-color: #48b889; }
.optimize-message[data-tone="warning"] { border-left-color: var(--accent-2); }
.optimize-message[data-tone="error"] { border-left-color: var(--error); }
.optimize-progress {
  margin: 1rem 0 0.75rem;
  color: var(--muted);
  font-size: 0.9rem;
}
.optimize-progress-copy {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.35rem 1rem;
  margin-bottom: 0.55rem;
}
#optimize-status { color: var(--ink); font-weight: 600; }
.optimize-progress-track {
  position: relative;
  height: 0.55rem;
  overflow: hidden;
  border: 1px solid #303647;
  border-radius: 999px;
  background: #0d0f16;
}
.optimize-progress-bar {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), #48b889);
  transition: width 0.45s ease;
}
.optimize-progress-track[data-state="indeterminate"] .optimize-progress-bar {
  position: absolute;
  left: -35%;
  width: 35%;
  animation: optimize-progress-loading 2.8s ease-in-out infinite;
}
@keyframes optimize-progress-loading {
  from { left: -35%; }
  to { left: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  .optimize-progress-bar { transition: none; }
  .optimize-progress-track[data-state="indeterminate"] .optimize-progress-bar {
    left: 0;
    animation: none;
  }
}
.optimize-table-wrap { margin-bottom: 0.75rem; overflow-x: auto; }
#optimize-table { min-width: 820px; }
#optimize-table td { vertical-align: top; }
#optimize-table .optimize-pipeline {
  max-width: 260px;
  font-family: "Geist Mono", monospace;
  font-size: 0.78rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
}
#optimize-table .optimize-parameters {
  min-width: 260px;
  color: #cbd1dc;
  font-size: 0.8rem;
  line-height: 1.45;
  text-align: left;
}
#optimize-table .optimize-parameters span { display: block; }
#optimize-table .optimize-best-row td { background: #121a1a; }
#optimize-table .optimize-best-row td:first-child { color: #48b889; font-weight: 700; }
.optimize-summary {
  margin: 0 0 0.25rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid #294f43;
  border-left: 3px solid #48b889;
  border-radius: 8px;
  background: #101a18;
  color: #cce9df;
  font-size: 0.88rem;
}

/* Lossless results table: matches the dark stat-card palette. */
.bench-table {
  width: 100%;
  border-collapse: collapse;
  background: #0d0f16;
  border-radius: 8px;
  overflow: hidden;
  font-size: 0.9rem;
}
.bench-table th, .bench-table td {
  padding: 0.55rem 0.7rem;
  text-align: right;
  border-bottom: 1px solid #2a2f3d;
}
.bench-table th { color: var(--muted); font-weight: 600; }
.bench-table th:first-child, .bench-table td:first-child { text-align: left; }
.bench-table tbody tr:last-child td { border-bottom: 0; }
.bench-table tbody td:nth-child(2) { color: var(--accent); font-weight: 600; }

/* Plot range picker: two compact index inputs + a hint, above the chart. */
.plot-range { margin-bottom: 1rem; }
.plot-range > label { flex: 0 0 130px; }
.hint { flex: 1 1 auto; color: var(--muted); font-size: 0.8rem; }
.doc-text {
  color: #c0c5d0;
  font-size: 0.95rem;
  line-height: 1.6;
  max-width: 74ch;
}
.method-desc { margin: 0.4rem 0 0; }
#build-logical-desc,
#build-indices-desc,
#build-coeff-desc {
  margin-top: 0.55rem;
}

/* Compression summary on the first row; reconstruction metrics on the second. */
.stats {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.stats-row {
  display: grid;
  gap: 0.75rem;
}
.stats-summary {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.65fr) minmax(0, 1fr) minmax(0, 1fr);
}
.stats-errors { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stats-row > div {
  min-width: 0;
  background: #0d0f16;
  border-radius: 8px;
  padding: 0.8rem 0.65rem;
  text-align: center;
}
.stats span {
  display: block;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#sizes { font-size: 1.08rem; }
.stats small {
  display: block;
  min-height: 2.5em;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 500;
  line-height: 1.25;
}

@media (max-width: 700px) {
  .stats-summary,
  .stats-errors { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

footer { color: var(--muted); font-size: 0.9rem; }
footer a { color: var(--accent); text-decoration: none; }

/* ------------------------------------------------------------------ */
/* Hover/focus hints. Add `data-tip="..."` to any non-replaced element */
/* (label, button, div) and a styled bubble appears above it on hover  */
/* or keyboard focus. Pure CSS, no JS. Put `data-tip` on a wrapping    */
/* <label>/<div>, never on a bare <input>/<select>/<textarea> —        */
/* pseudo-elements do not render on those replaced elements.           */
/* ------------------------------------------------------------------ */
[data-tip] { position: relative; }
[data-tip]:hover::after,
[data-tip]:focus-within::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 9px);
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: 240px;
  padding: 0.5rem 0.65rem;
  background: #0d0f16;
  color: var(--ink);
  border: 1px solid #2a2f3d;
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  font-size: 0.78rem;
  font-weight: 400;
  line-height: 1.4;
  white-space: normal;
  text-align: left;
  z-index: 20;
  pointer-events: none;
}
/* Little arrow connecting the bubble to its element. */
[data-tip]:hover::before,
[data-tip]:focus-within::before {
  content: "";
  position: absolute;
  bottom: calc(100% + 3px);
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: #2a2f3d;
  z-index: 20;
  pointer-events: none;
}
