:root {
  color-scheme: light;
  --ink: #17242d;
  --muted: #60727e;
  --line: #d5dfe4;
  --surface: #ffffff;
  --canvas: #f3f6f7;
  --blue: #12679d;
  --blue-soft: #d8eaf4;
  --green: #167b61;
  --green-soft: #e1f2ec;
  --red: #ad4039;
  --red-soft: #f7e6e3;
  --amber: #9a6515;
  --amber-soft: #f6edd7;
  --violet: #71315e;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.auth-loading { min-height: 70vh; display: grid; place-items: center; }
.auth-spinner { width: 32px; height: 32px; border: 3px solid var(--line); border-top-color: var(--blue); border-radius: 50%; animation: auth-spin .8s linear infinite; }
@keyframes auth-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .auth-spinner { animation: none; } }
html { background: var(--canvas); }
body {
  margin: 0;
  color: var(--ink);
  background: var(--canvas);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
.header-actions { display: flex; align-items: center; gap: 9px; }
.current-user { color: var(--muted); font-size: 12px; font-weight: 700; }
.logout-button { min-height: 30px; padding: 4px 9px; border: 1px solid #b9c7ce; border-radius: 3px; color: var(--ink); background: white; font-weight: 700; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 74px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.96);
  backdrop-filter: blur(10px);
}

.eyebrow, .step, .repeat-index {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 24px; line-height: 1.1; }
h2 { margin-bottom: 0; font-size: 18px; }
h3 { margin-bottom: 0; font-size: 14px; overflow-wrap: anywhere; }

.branch-tag, .decision-badge, .quiet-status {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 9px;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--muted);
  background: white;
  font-size: 12px;
  font-weight: 700;
}
.branch-tag.validated, .decision-badge.included { border-color: #9acdbd; color: var(--green); background: var(--green-soft); }
.branch-tag.disagreement { border-color: #e6b1ac; color: var(--red); background: var(--red-soft); }
.decision-badge.excluded { border-color: #e6b1ac; color: var(--red); background: var(--red-soft); }
.decision-badge.corrected { border-color: #dfc88f; color: var(--amber); background: var(--amber-soft); }

main { width: min(1500px, 100%); margin: 0 auto; padding: 22px 28px 64px; }
section { margin-bottom: 28px; }
.tree-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.tree-legend { display: flex; gap: 18px; margin-bottom: 18px; color: var(--muted); font-size: 12px; font-weight: 700; }
.tree-legend span { display: inline-flex; align-items: center; gap: 7px; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #9aa8af; }
.status-dot.validated { background: var(--green); }
.status-dot.disagreement { background: var(--red); }
.tree-root { display: grid; gap: 16px; max-height: 640px; padding-right: 5px; overflow: auto; }
.tree-surfactant { border: 1px solid var(--line); border-radius: 5px; background: white; }
.tree-surfactant > h3 { padding: 13px 15px; border-bottom: 1px solid var(--line); font-size: 16px; }
.tree-nanoparticles { display: grid; gap: 0; padding: 0 15px 12px 30px; }
.tree-nanoparticle { position: relative; padding: 13px 0 0 18px; border-left: 2px solid #c7d3d9; }
.tree-nanoparticle::before { position: absolute; top: 24px; left: 0; width: 14px; border-top: 2px solid #c7d3d9; content: ""; }
.tree-nanoparticle > h4 { margin: 0 0 8px; font-size: 13px; }
.tree-leaves { display: flex; flex-wrap: wrap; gap: 8px; }
.tree-leaf { display: grid; grid-template-columns: auto auto; align-items: center; gap: 3px 8px; min-width: 185px; padding: 9px 10px; border: 1px solid #c5d0d6; border-radius: 4px; background: #f8fafb; color: var(--ink); text-align: left; }
.tree-leaf:hover { border-color: var(--blue); background: var(--blue-soft); }
.tree-leaf.selected { box-shadow: 0 0 0 2px var(--blue); }
.tree-leaf.validated { border-color: #9acdbd; background: var(--green-soft); }
.tree-leaf.disagreement { border-color: #d88f89; background: var(--red-soft); }
.tree-leaf.disagreement .leaf-status { color: var(--red); }
.tree-leaf .leaf-concentration { font-weight: 800; font-variant-numeric: tabular-nums; }
.tree-leaf .leaf-status { color: var(--muted); font-size: 11px; font-weight: 700; }
.tree-leaf .leaf-auditors { grid-column: 1 / -1; color: var(--muted); font-size: 10px; overflow-wrap: anywhere; }
.empty-tree { color: var(--muted); }
.source-band, .metadata-band, .review-band, .average-band, .finalize-band {
  border-top: 3px solid var(--ink);
  background: var(--surface);
  padding: 18px;
}
.metadata-band { border-top-color: var(--green); }
.review-band { border-top-color: var(--blue); }
.average-band { border-top-color: var(--violet); }
.finalize-band { border-top-color: var(--amber); }

.section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 16px;
}
.section-side { display: flex; align-items: center; justify-content: flex-end; gap: 9px; }
.chart-export-actions { display: flex; align-items: center; gap: 6px; }
.chart-export-actions button {
  min-width: 44px;
  min-height: 30px;
  padding: 4px 8px;
  border: 1px solid #9aabb4;
  border-radius: 3px;
  color: var(--ink);
  background: white;
  font-size: 11px;
  font-weight: 800;
}
.chart-export-actions button:hover:not(:disabled) { border-color: var(--blue); color: var(--blue); background: var(--blue-soft); }
.chart-export-actions button:disabled { cursor: not-allowed; color: #8a989f; background: #edf1f3; }
.repeat-export-actions { justify-content: flex-end; padding: 7px 12px; border-top: 1px solid var(--line); }
.path-row, .project-row, .finalize-row { display: flex; align-items: end; gap: 12px; }
.cloud-source-controls { display: grid; gap: 12px; }
.project-row > button:not(.primary-button) { min-height: 40px; padding: 8px 13px; border: 1px solid #9aabb4; border-radius: 3px; color: var(--ink); background: white; font-weight: 750; }
.import-row { padding-top: 12px; border-top: 1px solid var(--line); }
.storage-note { color: var(--muted); line-height: 1.45; }
.grow { flex: 1; }
.field { display: grid; gap: 6px; min-width: 0; }
.field > span { color: var(--muted); font-size: 12px; font-weight: 700; }
input, select, textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid #b9c7ce;
  border-radius: 3px;
  color: var(--ink);
  background: white;
  outline: none;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(18,103,157,.12); }
.primary-button {
  min-height: 40px;
  padding: 8px 15px;
  border: 1px solid #0d527e;
  border-radius: 3px;
  color: white;
  background: var(--blue);
  font-weight: 750;
  white-space: nowrap;
}
.primary-button:disabled { cursor: not-allowed; border-color: #aebbc2; background: #aebbc2; }
.secondary-button { min-height: 32px; padding: 5px 10px; border: 1px solid #9aabb4; border-radius: 3px; color: var(--ink); background: white; font-weight: 750; white-space: nowrap; }
.secondary-button:hover:not(:disabled) { border-color: var(--blue); color: var(--blue); background: var(--blue-soft); }
.secondary-button:disabled { cursor: not-allowed; color: #8a989f; background: #edf1f3; }
.metadata-grid { display: grid; grid-template-columns: 1.2fr 1fr .75fr 1fr; gap: 14px; }
.audit-history { margin-top: 14px; padding: 12px 14px; border-left: 3px solid var(--green); background: var(--green-soft); }
.audit-history strong { display: block; margin-bottom: 5px; }
.audit-history p { margin: 3px 0 0; color: #155b49; }
.prior-decision { margin: 5px 0 0; color: var(--green); font-size: 11px; font-weight: 700; }

.repeat-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.repeat-panel { min-width: 0; border: 1px solid var(--line); border-radius: 5px; background: white; }
.repeat-header { display: flex; justify-content: space-between; gap: 14px; padding: 14px 14px 8px; }
.repeat-chart-shell, .average-chart-shell { position: relative; min-width: 0; }
.repeat-chart { display: block; width: 100%; height: 300px; }
#average-chart { display: block; width: 100%; height: 500px; }
.chart-tooltip {
  position: absolute;
  pointer-events: none;
  padding: 7px 9px;
  border: 1px solid #aebcc4;
  border-radius: 3px;
  background: rgba(255,255,255,.96);
  color: var(--ink);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 3px 10px rgba(23,36,45,.12);
}
.repeat-metrics { display: grid; grid-template-columns: repeat(3, 1fr); margin: 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.repeat-metrics div { padding: 9px 12px; border-right: 1px solid var(--line); }
.repeat-metrics div:last-child { border-right: 0; }
.repeat-metrics dt { color: var(--muted); font-size: 11px; font-weight: 700; }
.repeat-metrics dd { margin: 2px 0 0; font-weight: 750; font-variant-numeric: tabular-nums; }
.decision-control { display: grid; grid-template-columns: repeat(3, 1fr); padding: 12px; gap: 7px; }
.decision-control button { min-height: 36px; border: 1px solid #b9c7ce; border-radius: 3px; background: white; color: var(--muted); font-weight: 750; }
.decision-control button.active[data-status="included"] { border-color: var(--green); color: var(--green); background: var(--green-soft); }
.decision-control button.active[data-status="excluded"] { border-color: var(--red); color: var(--red); background: var(--red-soft); }
.decision-control button.active[data-status="corrected"] { border-color: var(--amber); color: var(--amber); background: var(--amber-soft); }
.correction-fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; padding: 0 12px 12px; }
.upload-correction { grid-column: 1 / -1; padding-top: 4px; }
.corrected-file { padding: 6px; }
.upload-feedback { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 5px; }
.upload-status { color: var(--muted); font-size: 11px; }
.upload-status.loaded { color: var(--green); font-weight: 700; }
.upload-status.error { color: var(--red); font-weight: 700; }
.remove-upload { padding: 4px 7px; border: 1px solid #b9c7ce; border-radius: 3px; color: var(--red); background: white; font-size: 11px; font-weight: 700; }
.image-correction { grid-column: 1 / -1; margin: 4px 0 0; padding: 14px 0 0; border-top: 1px solid var(--line); }
.image-correction-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 11px; }
.image-algorithm-badge { color: var(--blue); font-size: 11px; font-weight: 750; }
.image-correction-controls { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); align-items: end; gap: 9px; }
.image-file-field { grid-column: span 2; min-width: 180px; }
.instrument-image-file { padding: 6px; }
.process-image-button, .remove-image-correction, .export-image-png, .export-image-csv {
  min-height: 40px;
  padding: 7px 11px;
  border: 1px solid #9aabb4;
  border-radius: 3px;
  color: var(--ink);
  background: white;
  font-weight: 750;
  white-space: nowrap;
}
.process-image-button:disabled { cursor: not-allowed; color: #8a989f; background: #edf1f3; }
.image-correction-feedback { margin-top: 7px; color: var(--muted); }
.image-status.processing { color: var(--blue); font-weight: 700; }
.image-status.error { color: var(--red); font-weight: 700; }
.image-status.ready { color: var(--green); font-weight: 700; }
.image-preview { margin-top: 12px; }
.image-overlay-canvas { display: block; width: 100%; aspect-ratio: 16 / 10; border: 1px solid var(--line); background: #eef2f4; object-fit: contain; }
.image-result-metrics { display: grid; grid-template-columns: repeat(4, 1fr); border-bottom: 1px solid var(--line); }
.image-result-metrics div { padding: 9px 10px; border-right: 1px solid var(--line); }
.image-result-metrics div:last-child { border-right: 0; }
.image-result-metrics span { display: block; color: var(--muted); font-size: 10px; font-weight: 700; }
.image-result-metrics strong { display: block; margin-top: 2px; font-size: 13px; font-variant-numeric: tabular-nums; }
.image-review-actions { display: flex; justify-content: flex-end; gap: 8px; padding-top: 10px; }
.use-image-correction.selected { border-color: #0f634e; background: var(--green); }
.audit-note { padding: 0 12px 12px; }

.average-layout { display: grid; grid-template-columns: minmax(0, 1fr) 220px; gap: 16px; }
.average-chart-shell { border: 1px solid var(--line); }
.metric-rail { display: grid; align-content: start; border-top: 1px solid var(--line); }
.metric { padding: 13px 4px; border-bottom: 1px solid var(--line); }
.metric span { display: block; color: var(--muted); font-size: 12px; }
.metric strong { display: block; margin-top: 3px; font-size: 19px; font-variant-numeric: tabular-nums; }
.finalize-row { align-items: center; justify-content: space-between; }
.finalize-row p { margin: 4px 0 0; color: var(--muted); }
.final-result { margin-top: 14px; padding: 12px; border: 1px solid #9acdbd; background: var(--green-soft); color: #155b49; overflow-wrap: anywhere; }
.error-list { margin-top: 12px; padding: 10px 12px; border-left: 3px solid var(--red); background: var(--red-soft); color: #782d28; }

.login-page { min-height: 100vh; background: #edf2f4; }
.login-shell { display: grid; min-height: 100vh; place-items: center; padding: 24px; }
.login-panel { width: min(420px, 100%); margin: 0; padding: 28px; border-top: 4px solid var(--blue); background: white; box-shadow: 0 12px 36px rgba(23,36,45,.12); }
.login-panel h1 { margin-bottom: 24px; }
.login-panel form { display: grid; gap: 15px; }
.login-error { margin: 0; padding: 9px 11px; border-left: 3px solid var(--red); color: var(--red); background: var(--red-soft); }

@media (max-width: 980px) {
  .repeat-grid { grid-template-columns: 1fr; }
  .metadata-grid { grid-template-columns: repeat(2, 1fr); }
  .average-layout { grid-template-columns: 1fr; }
  .metric-rail { grid-template-columns: repeat(5, 1fr); }
  .metric { padding: 10px; border-right: 1px solid var(--line); }
  .image-correction-controls { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 640px) {
  .app-header { position: static; padding: 14px 16px; align-items: flex-start; }
  .header-actions { flex-wrap: wrap; justify-content: flex-end; }
  h1 { font-size: 21px; }
  main { padding: 16px 12px 42px; }
  .source-band, .metadata-band, .review-band, .average-band, .finalize-band { padding: 14px 10px; }
  .path-row, .project-row, .finalize-row { align-items: stretch; flex-direction: column; }
  .section-side { align-items: flex-end; flex-direction: column; }
  .metadata-grid, .correction-fields { grid-template-columns: 1fr; }
  .repeat-chart { height: 250px; }
  #average-chart { height: 330px; }
  .metric-rail { grid-template-columns: repeat(2, 1fr); }
  .repeat-metrics { grid-template-columns: 1fr; }
  .repeat-metrics div { border-right: 0; border-bottom: 1px solid var(--line); }
  .decision-control { grid-template-columns: 1fr; }
  .image-correction-controls, .image-result-metrics { grid-template-columns: 1fr; }
  .image-file-field { grid-column: auto; }
  .image-result-metrics div { border-right: 0; border-bottom: 1px solid var(--line); }
  .image-review-actions { align-items: stretch; flex-direction: column; }
}
[hidden] { display: none !important; }
.login-panel { max-width: 460px; margin: 12vh auto; padding: 32px; background: white; border: 1px solid #dbe3ec; border-radius: 16px; }
.login-panel form { display: grid; gap: 18px; margin-top: 24px; }
#login-message { min-height: 1.5em; margin-top: 20px; }
.password-label { color: var(--muted); font-size: 12px; font-weight: 700; }
.password-control { position: relative; }
.password-control input { width: 100%; padding-right: 52px; }
#toggle-password { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); display: grid; place-items: center; width: 36px; height: 36px; padding: 8px; border: 0; border-radius: 4px; background: transparent; color: var(--blue); }
#toggle-password:hover { background: var(--canvas); }
#toggle-password[aria-pressed="false"] .password-eye-slash { display: none; }
#toggle-password:focus-visible { outline: 2px solid var(--blue); outline-offset: 1px; }
