/* KiCad Library Manager
 *
 * The panel renders inside KiCad's WebView, which may be light or dark and is
 * often narrow, so everything below is driven by tokens and follows
 * prefers-color-scheme rather than assuming either.
 */

/* ---------------------------------------------------------------- tokens */

:root {
  color-scheme: light dark;

  --bg: #ffffff;
  --bg-sunken: #f5f6f8;
  --bg-raised: #ffffff;
  --fg: #14171c;
  --fg-muted: #5c6672;
  --fg-subtle: #8a94a6;
  --border: #e2e5ea;
  --border-strong: #cbd1d9;

  --accent: #2f6fd0;
  --accent-fg: #ffffff;
  --accent-soft: #e8f0fc;

  --ok: #1f7a45;
  --ok-soft: #e3f5ea;
  --warn: #9a6400;
  --warn-soft: #fdf1dc;
  --danger: #b3261e;
  --danger-soft: #fbe9e8;

  --radius: 8px;
  --radius-lg: 12px;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "Cascadia Mono", Menlo, monospace;

  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 1px 3px rgb(0 0 0 / 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --bg-sunken: #0f1216;
    --bg-raised: #1b1f26;
    --fg: #e8eaed;
    --fg-muted: #99a2ad;
    --fg-subtle: #6b7480;
    --border: #2a2f38;
    --border-strong: #3a414c;

    --accent: #6fa4f0;
    --accent-fg: #0d1117;
    --accent-soft: #1c2b41;

    --ok: #5fcf8e;
    --ok-soft: #17301f;
    --warn: #e5a441;
    --warn-soft: #332711;
    --danger: #f2867d;
    --danger-soft: #3a1b19;

    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
  }
}

/* ----------------------------------------------------------------- reset */

*,
*::before,
*::after { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.55 var(--font);
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; line-height: 1.25; }
p { margin: 0; }
a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

:where(button, a, input, select, summary):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius);
}

/* ---------------------------------------------------------------- layout */

.page {
  max-width: 72rem;
  margin: 0 auto;
  padding: var(--space-5) var(--space-4) var(--space-6);
}

.page--narrow { max-width: 56rem; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  align-items: center;
  justify-content: space-between;
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--space-4);
}

.topbar__title { display: flex; align-items: baseline; gap: var(--space-3); }
.topbar__title h1 { font-size: 1.0625rem; }
.topbar__actions { display: flex; align-items: center; gap: var(--space-2); }

.stack { display: flex; flex-direction: column; gap: var(--space-4); }
.row { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }

/* -------------------------------------------------------------- controls */

.button {
  font: inherit;
  font-size: 0.875rem;
  line-height: 1.2;
  padding: 0.4rem 0.75rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--bg-raised);
  color: var(--fg);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}

.button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.button:disabled { opacity: 0.5; cursor: default; }

/* Links styled as buttons: the button is the affordance, the underline is a
   second one saying the same thing. */
a.button { display: inline-block; text-decoration: none; }

.button--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}

.button--primary:hover:not(:disabled) {
  filter: brightness(1.08);
  color: var(--accent-fg);
}

/* Search and filters travel together: the filters describe the list the
   search produces, so scrolling one away from the other leaves the reader
   unable to tell why a result set looks the way it does. */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg);
  padding: var(--space-2) 0 var(--space-3);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--space-2);
}

.search {
  display: flex;
  gap: var(--space-3);
  align-items: center;
}

.search input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--bg-raised);
  color: var(--fg);
}

.search input::placeholder { color: var(--fg-subtle); }
.search input:focus { outline: none; border-color: var(--accent); }

/* --------------------------------------------------------------- filters */

.filters { display: flex; flex-direction: column; gap: var(--space-1); }

.filters:not([hidden]) { margin-top: var(--space-3); }

.filter {
  display: flex;
  align-items: baseline;
  gap: var(--space-3);
  min-width: 0;
}

.filter__label {
  flex: none;
  width: 4.5rem;
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--fg-subtle);
}

/* Wraps rather than scrolls, so a narrow panel gets taller instead of hiding
   half the statuses off its right edge. It only ever holds the three of them;
   the open-ended facet is a dropdown. */
.filter__chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  min-width: 0;
}

.chip {
  font: inherit;
  font-size: 0.75rem;
  line-height: 1.4;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  border: 1px solid transparent;
  background: var(--bg-sunken);
  color: var(--fg-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}

.chip:hover { border-color: var(--border-strong); color: var(--fg); }

.chip--on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 500;
}

.chip__count {
  font-size: 0.6875rem;
  font-variant-numeric: tabular-nums;
  opacity: 0.7;
}

/* The category chooser. A row of chips is the better control while there are
   six categories and the worse one at thirty, and a library only ever grows,
   so the height this costs is one line whichever it holds. */
.select {
  position: relative;
  display: inline-flex;
  max-width: 100%;
  min-width: 0;
}

.select select {
  font: inherit;
  font-size: 0.75rem;
  line-height: 1.4;
  appearance: none;
  max-width: 100%;
  padding: 0.15rem 1.5rem 0.15rem 0.55rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--bg-raised);
  color: var(--fg);
  cursor: pointer;
  text-overflow: ellipsis;
}

.select select:hover { border-color: var(--fg-subtle); }

/* Reads as set from across the room, the way an active chip does: a filter you
   cannot see is on is a list you cannot explain. */
.select--on select {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 500;
}

/* Drawn rather than a background image, so it follows the text through both
   themes and through the accent the chosen state paints over it. */
.select::after {
  content: "";
  position: absolute;
  top: 50%;
  right: 0.55rem;
  width: 0.35rem;
  height: 0.35rem;
  margin-top: -0.28rem;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg);
  color: var(--fg-subtle);
  pointer-events: none;
}

.select--on::after { color: var(--accent); }

/* --------------------------------------------------------------- badges */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1.4;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: var(--bg-sunken);
  color: var(--fg-muted);
  white-space: nowrap;
}

.badge--ok { background: var(--ok-soft); color: var(--ok); }
.badge--warn { background: var(--warn-soft); color: var(--warn); }
.badge--danger { background: var(--danger-soft); color: var(--danger); }
.badge--accent { background: var(--accent-soft); color: var(--accent); }

.badge--link { text-decoration: none; cursor: pointer; }
.badge--link:hover { text-decoration: underline; }

/* ----------------------------------------------------------- part list */

.parts { list-style: none; margin: 0; padding: 0; }

.part {
  display: grid;
  grid-template-columns: 3rem minmax(6rem, 11rem) 1fr auto auto;
  grid-template-areas: "thumb name desc source action" "note note note note note";
  gap: var(--space-1) var(--space-3);
  align-items: center;
  padding: var(--space-3) var(--space-2);
  border-bottom: 1px solid var(--border);
  outline: none;
}

.part:hover { background: var(--bg-sunken); }

/* The keyboard selection. Marked with a bar rather than only a tint, so it
   survives next to the hover state without the two being confusable. */
.part--on {
  background: var(--accent-soft);
  box-shadow: inset 2px 0 0 var(--accent);
}

/* A fixed box every drawing is fitted into. Symbols vary wildly in aspect --
   a resistor is tall and narrow, an op-amp wide -- and sizing each to its own
   extent left the column ragged and the tall ones as unreadable slivers. */
.part__thumb {
  grid-area: thumb;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 3rem;
  height: 2.75rem;
}

.part__thumb img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.part__name {
  grid-area: name;
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  min-width: 0;
}

.part__ipn {
  font-family: var(--font-mono);
  font-size: 0.875rem;
  text-decoration: none;
}

.part__ipn:hover { text-decoration: underline; }

.part__desc {
  grid-area: desc;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.part__source {
  grid-area: source;
  font-size: 0.8125rem;
  color: var(--fg-muted);
  font-family: var(--font-mono);
  white-space: nowrap;
}

.part__action { grid-area: action; }

/* Why a place failed, on the row that failed. Spans the grid because the
   message is a sentence -- "the extends chain is broken at X" -- and squeezing
   it into a cell would truncate the part that says what to do. */
.part__note {
  grid-area: note;
  font-size: 0.8125rem;
  line-height: 1.45;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  background: var(--bg-sunken);
  color: var(--fg-muted);
}

.part__note--error { background: var(--danger-soft); color: var(--danger); }
.part__note--warn { background: var(--warn-soft); color: var(--warn); }

@media (max-width: 40rem) {
  .part {
    grid-template-columns: 2.5rem 1fr auto;
    grid-template-areas:
      "thumb name   action"
      "thumb desc   action"
      "thumb source action"
      "note  note   note";
  }
  .part__desc { white-space: normal; }
}

.shortcuts { margin-top: var(--space-4); }

/* Hidden where they are a lie: a touch device has no keyboard to press. */
@media (hover: none) {
  .shortcuts { display: none; }
}

kbd {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  padding: 0.05rem 0.3rem;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--bg-sunken);
  color: var(--fg-muted);
}

/* ---------------------------------------------------------------- states */

.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-6) var(--space-4);
  text-align: center;
  color: var(--fg-muted);
}

.state__title { font-weight: 600; color: var(--fg); }
.state__hint { font-size: 0.875rem; max-width: 28rem; }

.skeleton { list-style: none; margin: 0; padding: 0; }

.skeleton li {
  display: grid;
  grid-template-columns: 3rem 9rem 1fr;
  gap: var(--space-3);
  align-items: center;
  padding: var(--space-3) var(--space-2);
  border-bottom: 1px solid var(--border);
}

.skeleton span {
  display: block;
  height: 0.7rem;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--bg-sunken), var(--border), var(--bg-sunken));
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

.skeleton li span:first-child { height: 2rem; }

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .skeleton span { animation: none; }
  * { transition: none !important; }
}

/* --------------------------------------------------------------- details */

.previews {
  display: flex;
  gap: var(--space-4);
  flex-wrap: wrap;
  /* Equal heights: the symbol is tall and narrow, the footprint short and
     wide, and top-aligning them leaves the row looking ragged. */
  align-items: stretch;
  margin: var(--space-4) 0 var(--space-2);
}

.preview {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  min-width: 9rem;
  gap: var(--space-3);
  padding: var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-raised);
}

.preview svg { max-height: 12rem; width: auto; }
.preview figcaption { font-size: 0.8125rem; color: var(--fg-muted); }

.previews [data-pin],
.previews [data-pad] { cursor: pointer; outline: none; }

.previews [data-pin]:focus-visible,
.previews [data-pad]:focus-visible { outline: 2px solid var(--accent); }

.section {
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--fg-subtle);
  margin: var(--space-5) 0 var(--space-2);
}

.fields {
  display: grid;
  grid-template-columns: minmax(6rem, max-content) 1fr;
  gap: var(--space-2) var(--space-5);
  align-items: baseline;
}

.fields dt { color: var(--fg-muted); font-size: 0.875rem; }
.fields dd { margin: 0; }
.fields dd.mono { font-family: var(--font-mono); font-size: 0.875rem; }

.table { width: 100%; border-collapse: collapse; font-size: 0.9375rem; }

.table th {
  text-align: left;
  font-weight: 500;
  font-size: 0.8125rem;
  color: var(--fg-subtle);
  padding: 0 var(--space-4) var(--space-2) 0;
  border-bottom: 1px solid var(--border);
}

.table td {
  padding: var(--space-3) var(--space-4) var(--space-3) 0;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.table td:last-child, .table th:last-child { padding-right: 0; }
.table .mono { font-family: var(--font-mono); font-size: 0.875rem; }

.table-scroll { overflow-x: auto; }

/* ------------------------------------------------------------------- log */

.log { margin-top: var(--space-5); }

.log summary {
  cursor: pointer;
  font-size: 0.8125rem;
  color: var(--fg-muted);
  padding: var(--space-2) 0;
}

.log pre {
  margin: var(--space-2) 0 0;
  padding: var(--space-3);
  border-radius: var(--radius);
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.6;
  max-height: 12rem;
  overflow: auto;
  white-space: pre-wrap;
  color: var(--fg-muted);
}

/* --------------------------------------------------------------- metrics */

.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
  gap: var(--space-3);
}

.metric {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-raised);
  color: inherit;
  text-decoration: none;
}

a.metric:hover { border-color: var(--accent); }

.metric__value {
  font-size: 1.5rem;
  font-weight: 600;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.metric__label {
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--fg-subtle);
}

/* Coloured only when the number is not zero: a red "0 errors" trains the
   reader to ignore the colour. */
.metric--danger { border-color: var(--danger); }
.metric--danger .metric__value { color: var(--danger); }
.metric--warn { border-color: var(--warn); }
.metric--warn .metric__value { color: var(--warn); }

/* --------------------------------------------------------------- notices */

.notice {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--bg-sunken);
  font-size: 0.875rem;
}

.notice__title { font-weight: 600; margin-bottom: var(--space-1); }
.notice ul { margin: 0; padding-left: var(--space-4); }
.notice li + li { margin-top: var(--space-1); }

.notice--danger {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
}

/* ---------------------------------------------------------------- issues */

.issue-groups { display: flex; flex-direction: column; gap: var(--space-2); }

.issue-group {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-raised);
  overflow: hidden;
}

.issue-group__head {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  cursor: pointer;
  list-style: none;
}

.issue-group__head::-webkit-details-marker { display: none; }
.issue-group__head:hover { background: var(--bg-sunken); }

.issue-group__kind {
  font-family: var(--font-mono);
  font-size: 0.875rem;
  flex: 1;
  min-width: 0;
}

.issue-group__count {
  font-size: 0.8125rem;
  color: var(--fg-subtle);
  font-variant-numeric: tabular-nums;
}

.table--issues { font-size: 0.875rem; }
.table--issues td { padding: var(--space-2) var(--space-4); vertical-align: top; }
.table--issues tr:last-child td { border-bottom: none; }

.issue__message { width: 60%; }
.issue__path { color: var(--fg-subtle); font-size: 0.8125rem; white-space: nowrap; }

/* --------------------------------------------------------------- related */

.related { list-style: none; margin: 0; padding: 0; }

.related li {
  display: flex;
  gap: var(--space-3);
  align-items: baseline;
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--border);
}

.related .muted {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.875rem;
}

/* ----------------------------------------------------------------- misc */

.muted { color: var(--fg-muted); }
.small { font-size: 0.875rem; }
.mono { font-family: var(--font-mono); }
.block { display: block; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ------------------------------------------------------------- 3D model */

.preview--model { justify-content: flex-start; }

.model {
  width: 15rem;
  height: 12rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  touch-action: none;   /* the canvas handles its own drag */

  /* A neutral mid-tone rather than the page background. Component bodies are
     frequently near-black and would otherwise vanish in dark mode, and the
     silver of a termination vanishes in light. */
  background: radial-gradient(circle at 50% 40%, #8d949e 0%, #5d636c 100%);
}

.model canvas { display: block; border-radius: var(--radius); cursor: grab; }
.model canvas:active { cursor: grabbing; }

.model__status {
  display: block;
  font-size: 0.75rem;
  color: var(--fg-subtle);
  margin-top: var(--space-1);
  min-height: 1rem;
}

@media (max-width: 40rem) {
  .model { width: 100%; min-width: 12rem; }
}
