:root {
  color-scheme: light;
  --bg: #f7f7f4;
  --surface: #ffffff;
  --surface-2: #ecefeb;
  --text: #1c2422;
  --muted: #66716d;
  --border: #d8ded9;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --line: #243b53;
  --grid: #e6eae6;
  --danger: #b42318;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
}

button, input { font: inherit; }

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

h1, h2, p { margin: 0; }

h1 {
  font-size: 22px;
  line-height: 1.2;
  font-weight: 720;
  letter-spacing: 0;
}

h2 {
  font-size: 16px;
  line-height: 1.2;
  font-weight: 700;
  letter-spacing: 0;
}

.file-status {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.file-button, button {
  min-height: 38px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 8px 12px;
  color: #ffffff;
  background: var(--accent);
  cursor: pointer;
  white-space: nowrap;
}

#deleteSectionButton.active {
  background: var(--danger);
  border-color: var(--danger);
}

#fitProfileButton.active {
  background: #c2410c;
  border-color: #c2410c;
}

#fitProfileButton.active:hover {
  background: #9a3412;
  border-color: #9a3412;
}

#deleteSectionButton.active:hover {
  background: #9b1c12; /* Slightly darker shade of danger */
}

#spectrumCanvas.delete-mode-cursor,
#spectrumCanvas.fit-mode-cursor {
  cursor: copy;
}

.deleted-sections-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.fit-profiles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}

.delete-card {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  white-space: nowrap;
}

.delete-card .range-label {
  font-weight: 600;
  color: var(--text);
}

.delete-card .remove-btn,
.fit-card .remove-btn {
  min-height: 28px;
  padding: 4px 8px;
  font-size: 12px;
  background: transparent;
  color: var(--danger);
  border: 1px solid transparent;
}

.delete-card .remove-btn:hover,
.fit-card .remove-btn:hover {
  background: var(--danger);
  color: #ffffff;
}

.file-button:hover, button:hover { background: var(--accent-strong); }

.file-button input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

.workspace {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  align-items: start;
  gap: 18px;
  padding: 18px 24px;
}

.controls {
  min-height: 50vh;
  padding: 16px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.control-group {
  display: grid;
  gap: 7px;
  margin-bottom: 14px;
}

.control-group label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

input[type="number"], input[type="text"] {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  color: var(--text);
  background: var(--surface);
}

input[type="color"] {
  width: 100%;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px;
  background: var(--surface);
}

input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

.center-slider {
  width: 100%;
  accent-color: var(--accent);
}

.plot-panel {
  position: relative;
  width: 75vw;
  height: 50vh;
  min-width: 420px;
  min-height: 360px;
  max-width: calc(100vw - 316px);
  border: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
}

.plot-panel.drag-over {
  outline: 3px solid rgba(15, 118, 110, 0.38);
  outline-offset: -3px;
}

#spectrumCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.plot-toolbar {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 3;
  display: flex;
  gap: 6px;
  align-items: center;
}

.plot-toolbar button {
  min-height: 30px;
  padding: 5px 9px;
  border-color: rgba(28, 36, 34, 0.16);
  color: var(--text);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 2px 8px rgba(28, 36, 34, 0.08);
}

.plot-toolbar button:hover, .plot-toolbar button.active {
  color: #ffffff;
  border-color: var(--accent);
  background: var(--accent);
}

.mouse-readout {
  position: absolute;
  left: 12px;
  bottom: 10px;
  z-index: 3;
  max-width: calc(100% - 24px);
  border: 1px solid rgba(28, 36, 34, 0.12);
  border-radius: 6px;
  padding: 6px 8px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 2px 8px rgba(28, 36, 34, 0.08);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.delete-hint,
.fit-hint {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  display: none;
  padding: 8px 16px;
  background: rgba(180, 35, 24, 0.85);
  color: #ffffff;
  border-radius: 20px;
  font-weight: 600;
  font-size: 13px;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.delete-hint.visible,
.fit-hint.visible {
  display: block;
}

.fit-hint {
  background: rgba(194, 65, 12, 0.86);
}

.fit-card {
  display: grid;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
}

.fit-card-title {
  color: var(--text);
  font-weight: 700;
}

.fit-values {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 5px 12px;
  margin: 0;
}

.fit-values dt {
  color: var(--muted);
  font-weight: 700;
}

.fit-values dd {
  margin: 0;
  color: var(--text);
  overflow-wrap: anywhere;
}

.drop-hint {
  position: absolute;
  inset: 14px;
  z-index: 4;
  display: none;
  place-content: center;
  border: 2px dashed var(--accent);
  border-radius: 8px;
  color: var(--accent-strong);
  background: rgba(255, 255, 255, 0.82);
  font-weight: 700;
  pointer-events: none;
}

.plot-panel.drag-over .drop-hint { display: grid; }

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  padding: 28px;
  text-align: center;
  color: var(--muted);
  pointer-events: none;
}

.empty-state strong {
  color: var(--text);
  font-size: 18px;
}

.empty-state span {
  max-width: 560px;
  font-size: 14px;
  line-height: 1.5;
}

.empty-state.hidden { display: none; }

.line-table-section {
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 24px;
}

.section-header button {
  min-height: 34px;
  padding: 6px 10px;
}

.table-wrap {
  padding: 0 24px 20px;
  overflow: visible;
}

table, thead, tbody {
  display: block;
  width: 100%;
}

thead {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

tbody {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 520px), 1fr));
  gap: 10px;
}

.line-card {
  display: grid;
  grid-template-columns: 24px 12ch minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr) 34px 32px;
  gap: 8px;
  align-items: start;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  background: #fbfcfb;
}

.empty-line-row {
  display: block;
  border-top: 1px solid var(--border);
}

.empty-line-cell {
  display: block;
  padding: 12px 0;
  font-size: 13px;
}

td {
  display: grid;
  gap: 4px;
  min-width: 0;
  padding: 0;
  text-align: left;
  font-size: 13px;
  vertical-align: top;
}

.line-field-label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
}

.visibility-cell, .color-cell, .remove-cell {
  justify-items: center;
  text-align: center;
  align-self: start;
}

.species-cell input {
  width: 13ch;
  max-width: 13ch;
}

.color-cell input[type="color"] {
  width: 32px;
  min-width: 32px;
  padding: 2px;
}

.field-stack {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.numeric-cell input[type="number"] {
  min-width: 0;
}

.remove-line {
  width: 34px;
  min-height: 32px;
  padding: 0;
  border-color: transparent;
  color: var(--danger);
  background: transparent;
}

.remove-line:hover {
  color: #ffffff;
  background: var(--danger);
}

aside.controls button {
  margin: 3px 0;
  padding: 5px 5px;
}

@media (max-width: 900px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
    padding: 14px 16px;
  }

  .workspace {
    grid-template-columns: 1fr;
    padding: 14px 16px;
  }

  .controls {
    min-height: 0;
  }

  .plot-panel {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    height: 50vh;
  }

  .section-header { padding: 12px 16px; }

  .table-wrap { padding: 0 16px 18px; }

  tbody { grid-template-columns: 1fr; }

  .line-card {
    grid-template-columns: 24px minmax(10ch, 12ch) minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr) 34px 32px;
    gap: 6px;
  }
}

@media (max-width: 640px) {
  .visibility-cell, .color-cell, .remove-cell { align-self: start; }

  .line-card {
    grid-template-columns: 24px 12ch minmax(0, 1fr) 34px 32px;
  }

  .visibility-cell { grid-column: 1; grid-row: 1; }
  .species-cell { grid-column: 2; grid-row: 1; }
  .numeric-cell { grid-column: 1 / -1; }
  .color-cell { grid-column: 4; grid-row: 1; }
  .remove-cell { grid-column: 5; grid-row: 1; }
}

@media (max-width: 560px) {
  .line-card {
    grid-template-columns: 24px minmax(0, 1fr) 34px 32px;
  }

  .visibility-cell { grid-column: 1; grid-row: 1; }
  .species-cell { grid-column: 2; grid-row: 1; }
  .numeric-cell { grid-column: 1 / -1; }
  .color-cell { grid-column: 3; grid-row: 1; }
  .remove-cell { grid-column: 4; grid-row: 1; }
}

.fit-line-id-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.fit-field-wrap {
  display: grid;
  gap: 4px;
}

.fit-species-input,
.fit-rest-input {
  width: 100%;
  min-height: 30px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 7px;
  color: var(--text);
  background: var(--surface);
  font: inherit;
  font-size: 12px;
}

.fit-rest-input {
  /* number inputs look odd with spinners at small size */
  -moz-appearance: textfield;
}

.fit-rest-input::-webkit-outer-spin-button,
.fit-rest-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

/* ── Session buttons & topbar action group ─────────────────────────────── */

.topbar-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.session-button {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}

.session-button:hover {
  background: var(--accent);
  color: #ffffff;
}

/* ── Fit card header (title + visibility checkbox) ─────────────────────── */

.fit-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.fit-vis-checkbox {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ── Section header actions group ─────────────────────────────────────── */

.section-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.average-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.average-label input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--accent);
}

/* ── Tabulate table ────────────────────────────────────────────────────── */

/* Override the global display:block and hidden-thead rules for the tabulate table */
.tabulate-table {
  display: table;
  width: max-content;
  max-width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.tabulate-table thead {
  display: table-header-group;
  position: static;
  inline-size: auto;
  block-size: auto;
  overflow: visible;
  clip: auto;
  white-space: normal;
}

.tabulate-table tbody {
  display: table-row-group;
}

.tabulate-table tr {
  display: table-row;
}

.tabulate-table th,
.tabulate-table td {
  display: table-cell;
}

#tabulateTableWrap {
  overflow-x: auto;
  padding-bottom: 8px;
}

.tab-group-header {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 7px 12px;
  text-align: center;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}

.tab-field-header {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 5px 10px;
  text-align: center;
  font-weight: 600;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.tab-error-header {
  color: var(--accent);
}

.tab-value {
  border: 1px solid var(--border);
  padding: 5px 10px;
  text-align: right;
  white-space: nowrap;
  color: var(--text);
}

.tab-error-value {
  color: var(--muted);
}

/* ── Line display mode select ──────────────────────────────────────────── */

.line-display-select {
  min-height: 34px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  color: var(--text);
  background: var(--surface);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.line-display-select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ── Column selector popup ──────────────────────────────────────────────── */

.column-popup {
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  z-index: 100;
  min-width: 180px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 -4px 16px rgba(28, 36, 34, 0.13);
  padding: 10px 0 8px;
}

.column-popup-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0 14px 6px;
}

.column-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 14px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  min-height: unset;
  border: none;
  background: none;
}

.column-option:hover {
  background: var(--surface-2);
}

.column-option input[type="checkbox"] {
  width: 14px;
  height: 14px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.column-popup-divider {
  height: 1px;
  background: var(--border);
  margin: 6px 0;
}

.column-option-errors {
  color: var(--accent);
  font-weight: 600;
}