:root {
  --brand: #019a07;
  --brand-dark: #017305;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --border: #e6e6e6;
  --bg-soft: #f7f8f7;
}

* { box-sizing: border-box; }

body {
  font-family: Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: white;
  margin: 0;
}

.app-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
}

.brand {
  color: var(--brand);
  font-size: 22px;
  font-weight: 700;
  text-decoration: none;
}

.view-root {
  max-width: 960px;
  margin: 0 auto;
  padding: 32px 24px 64px;
}

h1 { font-size: 28px; margin: 0 0 4px; font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 18px; margin: 0 0 12px; font-weight: 600; }
p.subtitle { color: var(--muted); margin: 0 0 28px; }

.field {
  margin-bottom: 20px;
  padding-left: 12px;
  border-left: 3px solid var(--border);
}
/* PCB and schematic are the two required inputs — give them a stronger, distinct accent from the
   optional drill file so the two required fields read as visually related but not identical. */
.field-pcb { border-left-color: var(--brand); }
.field-sch { border-left-color: #0033ff; }
.field-drl { border-left-color: var(--border); }

.field label {
  display: block;
  font-weight: 600;
  margin-bottom: 6px;
}

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

input[type="file"] {
  display: block;
  width: 100%;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 6px;
  background: var(--bg-soft);
}

/* drag-and-drop target state — the drop itself just forwards to the same <input type=file>'s
   native change handling (see wireDropzone in app.js), so this is visual-only */
.field.drag-active input[type="file"] {
  border-color: var(--brand);
  background: #eef7ee;
}

.file-name {
  font-size: 13px;
  color: var(--brand);
  margin-top: 6px;
  word-break: break-all;
}

.actions {
  display: flex;
  gap: 12px;
  margin-top: 28px;
  flex-wrap: wrap;
}

button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 12px 20px;
  font-weight: 600;
  transition: background-color 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

button.primary {
  background: var(--ink);
  color: white;
}
button.primary:hover { opacity: 0.85; }
button.primary:disabled { background: #b5b5b5; opacity: 1; cursor: not-allowed; }

button.secondary {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--border);
}
button.secondary:hover { border-color: #ccc; background: var(--bg-soft); }
button.secondary:disabled { color: var(--muted); cursor: not-allowed; }
button.secondary:disabled:hover { background: var(--bg-soft); }

button.link {
  background: none;
  color: var(--brand);
  padding: 8px 0;
  font-weight: 600;
}

/* loading */
.loading-panel {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  background: var(--bg-soft);
  border-radius: 8px;
  padding: 18px 20px;
  margin: 20px 0;
}
.loading-body { flex: 1; min-width: 0; }
.spinner {
  width: 22px;
  height: 22px;
  margin-top: 2px;
  border: 3px solid #d5ded5;
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
/* the stage label is the ONLY thing allowed to claim "what's happening overall" — it changes only
   on an explicit stage transition or when the worker posts a terminal result, never implied by
   .bar-fill reaching 100% (that bar is always scoped to one sub-step, see app.js's loading-view
   comments) */
.loading-stage {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 4px;
}
.loading-detail {
  font-family: ui-monospace, monospace;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}
.bar-track {
  height: 8px;
  background: #d5ded5;
  border-radius: 4px;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  width: 0%;
  background: var(--brand);
  transition: width 0.15s linear;
}

/* component/net list */
.pick-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.pick-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.pick-list li:last-child { border-bottom: none; }
.pick-list li:hover { background: var(--bg-soft); }
.pick-list .ref { font-weight: 700; }
.pick-list .footprint { color: var(--muted); font-size: 13px; }

/* results */
.board-view {
  position: relative;
  display: inline-block;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  max-width: 100%;
}
/* boards render at native pixel size (often well over 1000px tall) — cap the displayed height so
   a result never floods multiple screens of vertical scroll; width:auto+height:auto lets the
   browser fit the intrinsic ratio within both constraints, same as object-fit:contain on an img.
   Hit-testing in hitTestSteps() already derives its own scale from getBoundingClientRect(), so
   this display-size change doesn't affect step selection accuracy. */
.board-view canvas { display: block; max-width: 100%; max-height: 65vh; width: auto; height: auto; }
.board-view .overlay { position: absolute; inset: 0; pointer-events: none; max-width: 100%; }

.result-meta {
  margin-top: 16px;
  font-size: 14px;
}
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  margin-right: 6px;
}
.badge.ok { background: #d9f2da; color: var(--brand-dark); }
.badge.warn { background: #fff2cc; color: #8a6d00; }

.net-list { margin-top: 12px; }
.net-list .net-row {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  border-left: 3px solid transparent;
  font-size: 14px;
}
/* net-row status accents mirror the same satisfied/needs-intervention/unmatched classification
   already computed for the inline badge (renderCircuitResultView) — purely a visual restatement
   of that same data, nothing new derived here */
.net-list .net-row.status-ok { border-left-color: var(--brand); }
.net-list .net-row.status-needs-intervention { border-left-color: #e6c800; background: #fffdf5; }
.net-list .net-row.status-unmatched { border-left-color: #d33; background: #fef7f7; }

/* intervention panel */
.intervention-layout {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  align-items: flex-start;
  margin-top: 16px;
}
/* flex-basis values sized so a standard 768px-wide portrait tablet (720px content area after
   main's own padding) still fits both columns side by side (400+280+24 gap = 704px) rather than
   wrapping to the mobile stacked layout one viewport class early. */
.intervention-layout .board-view { flex: 1 1 400px; min-width: 280px; }
/* the intervention overlay is interactive (hover/click to select a step) — override the
   click-through .overlay default used by the non-interactive result views */
.intervention-layout .board-view .overlay { pointer-events: auto; cursor: pointer; }
.instruction-panel {
  flex: 1 1 280px;
  min-width: 260px;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  background: var(--bg-soft);
  /* keeps the panel from growing taller than the viewport on boards with many steps — the list
     itself scrolls (below), the header/legend stay pinned and visible */
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.intervention-panel-header { flex-shrink: 0; }
.instruction-panel h2 { font-size: 16px; margin-bottom: 6px; }
.intervention-summary { font-size: 13px; color: var(--muted); margin: 0 0 14px; line-height: 1.5; }

.overlay-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  margin-bottom: 14px;
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}
.legend-item { display: flex; align-items: center; gap: 6px; }
.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
  flex-shrink: 0;
}
.legend-swatch.outline { background: #0033ff; }
.legend-swatch.addwire { background: #00cc66; }
.legend-swatch.tracecut { background: #e6c800; box-shadow: inset 0 0 0 2px #ff2b2b; }
.legend-swatch.active { background: #ff5a00; }

#instruction-list-slot {
  overflow-y: auto;
  min-height: 0; /* let flex shrink this below its content height so it actually scrolls */
}
.instruction-list {
  list-style: none;
  margin: 0;
  padding: 0;
  counter-reset: none;
}
.instruction-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  border: 1px solid transparent;
}
.instruction-item:hover { background: #eef1ee; }
.instruction-item.active {
  background: #fff2e8;
  border-color: #ff5a00;
}
.instruction-item:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.instruction-required {
  flex-shrink: 0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  text-transform: uppercase;
}
.instruction-order {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--ink);
  color: white;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.instruction-item.active .instruction-order { background: #ff5a00; }
.instruction-type-dot {
  flex-shrink: 0;
  width: 10px;
  height: 10px;
  border-radius: 3px;
}
.instruction-item.type-addWire .instruction-type-dot { background: #00cc66; }
.instruction-item.type-traceCut .instruction-type-dot { background: #e6c800; box-shadow: inset 0 0 0 2px #ff2b2b; }
.instruction-label { flex: 1; }

/* narrow viewports: stack the board above the instruction list (flex-wrap already does this at
   any width once the panel's min-width no longer fits beside the board — this just tightens
   spacing/sizing so it doesn't feel like a squeezed desktop layout) */
@media (max-width: 640px) {
  .view-root { padding: 20px 14px 48px; }
  .intervention-layout { gap: 16px; }
  .intervention-layout .board-view,
  .instruction-panel { flex-basis: 100%; min-width: 0; }
  .instruction-panel { max-height: 60vh; padding: 14px; }
  .instruction-item { font-size: 13px; padding: 12px 6px; min-height: 44px; }
  .overlay-legend { font-size: 11px; gap: 8px 12px; }
}

.error-box {
  background: #fde8e8;
  color: #8a1f1f;
  border: 1px solid #f3b8b8;
  border-radius: 8px;
  padding: 14px 16px;
  margin: 16px 0;
  font-size: 14px;
  line-height: 1.5;
}

/* Home — three tool entry points */
.tool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  margin: 20px 0;
}
.tool-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 18px;
  background: #fff;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.tool-card:hover, .tool-card:focus-visible {
  border-color: var(--brand);
  box-shadow: 0 0 0 1px var(--brand);
  outline: none;
}
.tool-card h3 { margin: 0 0 8px; font-size: 16px; }
.tool-card p { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.5; }

/* future-feature placeholders — deliberately still, no algorithm behind them yet */
.placeholder-panel {
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 20px;
  background: var(--bg-soft);
  margin: 16px 0;
}
.placeholder-panel p { margin: 10px 0 0; color: var(--muted); font-size: 14px; line-height: 1.5; }

/* PCB inventory — folded into whichever tool built it, behind a plain <details> disclosure */
.inventory-details { margin: 16px 0; border-top: 1px solid var(--border); padding-top: 12px; }
.inventory-details summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--muted); }
.inventory-summary { margin: 16px 0; }
.inventory-count { font-size: 18px; font-weight: 700; margin: 0 0 8px; }
.inventory-breakdown {
  margin: 0;
  padding-left: 20px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.7;
}
.inventory-table-wrap { overflow-x: auto; margin-top: 16px; }
.inventory-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.inventory-table th, .inventory-table td {
  text-align: left;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.inventory-table th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.02em; }

/* Find Recycling Projects — ranked recommendation list */
.filter-bar { display: flex; gap: 8px; flex-wrap: wrap; margin: 16px 0; }
.filter-btn {
  background: var(--bg-soft);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.filter-btn.active { background: var(--brand); color: #fff; border-color: var(--brand); }

.recommendation-grid { display: flex; flex-direction: column; gap: 14px; margin: 16px 0; }
.recommendation-card {
  display: flex;
  gap: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  background: #fff;
  transition: border-color 0.15s ease;
}
.recommendation-card:hover { border-color: #ccc; }
.recommendation-rank {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bg-soft);
  color: var(--muted);
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.recommendation-body { flex: 1; min-width: 0; }
.recommendation-body h3 { margin: 0 0 8px; font-size: 16px; }
.reuse-coverage-bar-track {
  height: 8px;
  background: #d5ded5;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 6px;
}
.reuse-coverage-bar-fill { height: 100%; background: var(--brand); }
.reuse-coverage-label { margin: 0 0 4px; font-weight: 700; font-size: 14px; }
.reuse-parts-label { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
.recommendation-body button { padding: 8px 16px; font-size: 13px; }
.library-disclosure { margin-top: 20px; font-size: 12px; color: var(--muted); font-style: italic; }

/* Recycling-project detail / explanation view */
.reuse-section { margin: 20px 0; }
.reuse-section h2 { font-size: 16px; margin: 0 0 10px; }
.reuse-checklist { list-style: none; margin: 0; padding: 0; }
.reuse-checklist-item {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 6px 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.reuse-checklist-item:last-child { border-bottom: none; }
.reuse-check { color: var(--brand-dark); font-weight: 700; }
.reuse-cross { color: #b33; font-weight: 700; }
.reuse-ref-badge {
  margin-left: auto;
  font-family: ui-monospace, monospace;
  font-size: 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
}
.reuse-mounting { font-size: 12px; color: var(--muted); }
.reuse-section.bonus .reuse-checklist-item { color: var(--muted); }
.reuse-source { margin: 20px 0; }
.reuse-source h2 { font-size: 16px; margin: 0 0 10px; }
.reuse-source p { margin: 0 0 6px; font-size: 14px; color: var(--muted); }
.reuse-source a { color: var(--brand); font-weight: 600; }

/* Future Reusability Score */
.score-disclosure { margin: 0 0 4px; font-size: 12px; color: var(--muted); }

.score-headline { margin: 8px 0 4px; }
.score-headline-number { font-size: 64px; font-weight: 800; margin: 0; line-height: 1; letter-spacing: -0.02em; }
.score-headline-label { margin: 6px 0 16px; font-size: 15px; color: var(--muted); }
.score-visual-bar-track { height: 4px; background: var(--bg-soft); border-radius: 2px; overflow: hidden; max-width: 320px; }
.score-visual-bar-fill { height: 100%; background: var(--ink); }

.score-delta { margin: 12px 0 0; font-size: 14px; color: var(--muted); }
.score-delta-up { color: var(--brand-dark); font-weight: 700; }
.score-delta-down { color: #b33; font-weight: 700; }
.score-delta-none { font-weight: 700; }

.score-calculated-at { margin: 8px 0 32px; font-size: 12px; color: var(--muted); }

.score-stale-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  background: #fff8e6;
  border: 1px solid #e8d28a;
  border-radius: 8px;
  padding: 12px 16px;
  margin: 16px 0;
}
.score-stale-banner p { margin: 0; font-size: 14px; }

/* Shared compact "label ... value" row, used both for the main score breakdown (with a bar) and
   the methodology weight list inside <details> (without one) — typography and spacing carry the
   hierarchy, not a border around each row. */
.score-breakdown { margin: 8px 0 32px; }
.score-row {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.score-row:last-child { border-bottom: none; }
.score-row-label { flex: 1 1 auto; color: var(--ink); }
.score-row-bar-track { flex: 1 1 120px; height: 4px; background: var(--bg-soft); border-radius: 2px; overflow: hidden; }
.score-row-bar-fill { height: 100%; background: var(--brand); }
.score-row-value { flex: 0 0 auto; width: 2.5em; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

.score-facts { list-style: none; margin: 0 0 32px; padding: 0; }
.score-fact { display: flex; gap: 10px; padding: 6px 0; font-size: 14px; }
.score-fact span { flex-shrink: 0; font-weight: 700; width: 1em; }
.score-fact-plus span { color: var(--brand-dark); }
.score-fact-minus span { color: var(--muted); }

.score-best-match-list { list-style: none; margin: 0 0 24px; padding: 0; }
.score-best-match-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.score-best-match-item:last-child { border-bottom: none; }
.score-best-match-item a { color: var(--ink); font-weight: 600; text-decoration: none; }
.score-best-match-item a:hover { color: var(--brand-dark); }
.score-best-match-item span { font-weight: 700; color: var(--muted); }

.score-methodology { margin: 24px 0; padding-top: 8px; border-top: 1px solid var(--border); }
.score-methodology summary { cursor: pointer; font-weight: 600; font-size: 14px; }
.score-methodology-body { margin-top: 16px; }
.score-methodology-body .score-breakdown { margin-bottom: 16px; }

@media (max-width: 640px) {
  .tool-grid { grid-template-columns: 1fr; }
  .recommendation-card { flex-direction: column; }
  .reuse-ref-badge { margin-left: 0; }
  .score-headline-number { font-size: 48px; }
}
