/* kicad-web — one stylesheet for the whole app.
   Sections: tokens · base · components (.btn/.badge/.card/.table/.chip/.segmented)
             · plain pages (.page) · viewer shell · panel · toolbar · viewers
             · detail card · responsive.
   Sized for reading at arm's length on an iPad: 16px base, >=44px touch targets. */

/* ------------------------------------------------------------------ tokens */
:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef1;
  --panel: #fbfbfc;
  --text: #16191d;
  --muted: #6b7280;
  --border: #d8dce1;
  --border-strong: #b9c0c8;
  --accent: #0a66c2;
  --accent-soft: rgba(10, 102, 194, 0.14);
  --accent-text: #ffffff;
  --warn: #b45309;
  --danger: #b91c1c;
  --ok: #15803d;
  --canvas: #ffffff;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px rgba(16, 24, 40, 0.09);
  --tap: 44px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f24;
    --surface-2: #262a31;
    --panel: #191c21;
    --text: #e8eaed;
    --muted: #9aa3ad;
    --border: #2f343c;
    --border-strong: #454c56;
    --accent: #58a6ff;
    --accent-soft: rgba(88, 166, 255, 0.18);
    --accent-text: #08121d;
    --warn: #f0b429;
    --danger: #f87171;
    --ok: #4ade80;
    --canvas: #f7f7f4; /* KiCad SVGs draw on a light background */
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.45);
  }
}

/* -------------------------------------------------------------------- base */
* { box-sizing: border-box; }

/* Several component classes set `display`, which would otherwise beat the UA's
   `[hidden] { display: none }`. The JS toggles `hidden` a lot, so make it stick. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent); }

h1 { font-size: 1.6rem; margin: 0 0 0.6rem; }
h2 { font-size: 1.2rem; margin: 1.6rem 0 0.6rem; }
h3 { font-size: 1rem; margin: 1.2rem 0 0.4rem; }
p { margin: 0.5rem 0; }
code, .mono { font-family: var(--mono); font-size: 0.92em; }
.muted { color: var(--muted); }

/* ---------------------------------------------------------------- topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  padding-top: calc(8px + env(safe-area-inset-top));
  min-height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 650;
  text-decoration: none;
  color: var(--text);
  white-space: nowrap;
}

.crumbs {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
}
.crumbs a { text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
.crumb-sep { color: var(--border-strong); }
.crumb-current { color: var(--text); font-weight: 550; white-space: nowrap; }

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

/* ------------------------------------------------------------- components */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap);
  padding: 0 14px;
  font: inherit;
  font-weight: 550;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary { color: var(--accent-text); background: var(--accent); border-color: var(--accent); }
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.inline-form { display: inline; }
.btn-small { min-height: 30px; padding: 0 8px; font-size: 0.85rem; }
.btn-danger { color: #fff; background: #c62828; border-color: #c62828; }
.btn-danger:hover { background: #b71c1c; }
.btn-quiet { background: transparent; border-color: transparent; color: var(--muted); }
.btn-quiet:hover { background: var(--surface-2); }
.btn-icon { min-width: var(--tap); padding: 0 10px; font-size: 1.05rem; }
.btn.is-on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px;
  gap: 2px;
}
.segmented .seg {
  appearance: none;
  font: inherit;
  font-weight: 550;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 5px;
  min-height: 38px;
  padding: 0 12px;
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
}
.segmented .seg:disabled { opacity: 0.4; cursor: default; }
.segmented .seg[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.14);
}

.chip {
  appearance: none;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 550;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  min-height: 34px;
  padding: 0 12px;
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
}
.chip.is-on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip.is-selected { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip.is-dnp { text-decoration: line-through; }
.chips { display: flex; gap: 6px; flex-wrap: nowrap; overflow-x: auto; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 650;
  letter-spacing: 0.01em;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge-ready { background: rgba(21, 128, 61, 0.14); color: var(--ok); border-color: transparent; }
.badge-processing { background: rgba(180, 83, 9, 0.14); color: var(--warn); border-color: transparent; }
.badge-pending { background: var(--surface-2); color: var(--muted); }
.badge-failed { background: rgba(185, 28, 28, 0.14); color: var(--danger); border-color: transparent; }
.badge-latest { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge-dnp { background: rgba(185, 28, 28, 0.14); color: var(--danger); border-color: transparent; }
.badge-side-F { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge-side-B { background: rgba(180, 83, 9, 0.16); color: var(--warn); border-color: transparent; }
.badge-muted { background: var(--surface-2); color: var(--muted); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}
.table th, .table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.table th { font-weight: 600; color: var(--muted); }
.table tr:last-child th, .table tr:last-child td { border-bottom: 0; }
.table a { word-break: break-all; }

.form-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.form-inline .label { color: var(--muted); font-size: 0.85rem; }

input[type="text"], input[type="search"], input[type="file"], select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  min-height: var(--tap);
  padding: 0 10px;
  max-width: 100%;
}
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ------------------------------------------------------- plain HTML pages */
main.page {
  max-width: 960px;
  margin: 0 auto;
  padding: 20px max(16px, env(safe-area-inset-left)) 48px max(16px, env(safe-area-inset-right));
}
main.page .card + .card { margin-top: 16px; }
main.page .table { margin-top: 8px; }
.error-text { color: var(--danger); white-space: pre-wrap; font-family: var(--mono); font-size: 0.85rem; }

/* ------------------------------------------------------------ viewer shell */
body.viewer-body {
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  display: flex;
  flex-direction: column;
}
body.viewer-body .topbar { flex: 0 0 auto; }

main.viewer-main {
  flex: 1 1 auto;
  min-height: 0;
  padding: 0;
}

#app { height: 100%; }

.app-shell {
  display: flex;
  height: 100%;
  min-height: 0;
  position: relative;
}

/* ------------------------------------------------------------------ panel */
.panel {
  flex: 0 0 320px;
  width: 320px;
  min-height: 0;
  display: none;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding-left: env(safe-area-inset-left);
}
body.panel-open .panel { display: flex; }

.panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px 8px;
  border-bottom: 1px solid var(--border);
}
.panel-head .segmented { flex: 1; }
.panel-head .segmented .seg { flex: 1; }
.panel-close { display: none; }

.panel-pane {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.panel-pane[hidden] { display: none; }

.search-box { padding: 10px; }
.search-box input { width: 100%; }

.list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding-bottom: env(safe-area-inset-bottom);
}
.list-note {
  padding: 8px 12px;
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: var(--tap);
  padding: 8px 12px;
  font: inherit;
  text-align: left;
  color: inherit;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  touch-action: manipulation;
}
.row:hover { background: var(--surface-2); }
.row.is-selected { background: var(--accent-soft); }
.row-ref, .row-qty {
  font-family: var(--mono);
  font-weight: 650;
  font-size: 0.9rem;
  flex: 0 0 auto;
  min-width: 3.6em;
}
.row-qty { color: var(--muted); text-align: right; }
.row-main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.row-value { font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-fp {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  direction: rtl; /* keep the useful tail of long footprint names visible */
  text-align: left;
}
.row-badges { flex: 0 0 auto; display: flex; align-items: center; gap: 4px; }
.chev { color: var(--muted); width: 1em; text-align: center; }

.bom-group.is-open { background: var(--surface); }
.bom-refs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px 12px;
  border-bottom: 1px solid var(--border);
}

.panel-scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 4;
}

/* ---------------------------------------------------------------- toolbar */
.stage {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  padding-right: max(10px, env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}
.toolbar > * { flex: 0 0 auto; }
.toolbar-spacer { flex: 1 1 auto; min-width: 8px; }
.sheet-picker select { max-width: 220px; }
/* The layer chips are the one thing allowed to shrink and scroll. */
.toolbar .chips { flex: 0 1 auto; min-width: 90px; }

/* ---------------------------------------------------------------- viewers */
.views {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.view {
  flex: 1 1 0;
  min-height: 0;
  position: relative;
}
.view[hidden] { display: none; }
.views.is-both .view + .view:not([hidden]) { border-top: 2px solid var(--border-strong); }
.view-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 2;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  border: 1px solid var(--border);
  pointer-events: none;
  display: none;
}
.views.is-both .view-tag { display: block; }

.viewer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--canvas);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  contain: strict;
}
.viewer svg {
  pointer-events: none; /* hit testing is geometric, see viewer.js */
  transform-origin: 0 0;
}
.viewer svg.gesturing { will-change: transform; }

.overlay .hit {
  fill: transparent;
  stroke: none;
  pointer-events: none;
}
.overlay .hit.selected {
  fill: var(--accent-soft);
  stroke: var(--accent);
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
}
.overlay .hit.pulse { animation: hit-pulse 900ms ease-out 1; }
@keyframes hit-pulse {
  0% { fill-opacity: 0.9; stroke-width: 6; }
  100% { fill-opacity: 1; stroke-width: 2; }
}
@media (prefers-reduced-motion: reduce) {
  .overlay .hit.pulse { animation: none; }
}

/* ------------------------------------------------------------ detail card */
.detail-card {
  position: absolute;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  width: min(400px, calc(100% - 24px));
  max-height: min(70%, 560px);
  display: flex;
  flex-direction: column;
  padding: 12px 14px;
  z-index: 3;
}
.detail-card[hidden] { display: none; }
.detail-head { display: flex; align-items: flex-start; gap: 8px; }
.detail-title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; flex: 1 1 auto; min-width: 0; }
.detail-ref { font-family: var(--mono); font-size: 1.5rem; font-weight: 700; line-height: 1.1; }
.detail-sub { display: flex; flex-direction: column; margin: 2px 0 10px; min-width: 0; }
.detail-value { font-size: 1.05rem; }
.detail-fp { font-size: 0.82rem; color: var(--muted); word-break: break-all; }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.detail-actions .btn { flex: 1 1 auto; }
.detail-body { overflow-y: auto; overscroll-behavior: contain; margin-top: 10px; }
.detail-section {
  margin: 12px 0 4px;
  font-size: 0.75rem;
  font-weight: 650;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}
.detail-table th { width: 34%; white-space: nowrap; }

/* ------------------------------------------------------------------ toast */
.status-toast {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  z-index: 5;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  pointer-events: none;
  max-width: 80%;
  text-align: center;
}
.status-toast[hidden] { display: none; }

/* ------------------------------------------------------------- responsive */
@media (max-width: 899px) {
  .panel {
    position: absolute;
    z-index: 5;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(88vw, 380px);
    flex-basis: min(88vw, 380px);
    box-shadow: var(--shadow);
  }
  .panel-close { display: inline-flex; }
  .detail-card {
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 55%;
    border-radius: var(--radius) var(--radius) 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  /* Not enough room for the layer chips beside everything else: give them a row. */
  .toolbar { flex-wrap: wrap; overflow-x: hidden; }
  .toolbar .chips { order: 10; flex: 1 1 100%; max-width: 100%; }
  .crumbs { font-size: 0.9rem; }
}

@media (max-width: 599px) {
  .topbar-actions { display: none; }
  .segmented .seg { padding: 0 9px; font-size: 0.9rem; }
}
