:root {
  --ink: #172026;
  --muted: #657078;
  --paper: #f4f1e9;
  --panel: #fffdf8;
  --line: #d9d4c8;
  --soft: #f3f0e8;
  --soft-hover: #e9e5dc;
  --inspector-bg: #f6f3eb;
  --canvas-bg: #faf9f5;
  --canvas-gradient-start: #fbfaf7;
  --canvas-gradient-end: #e9eef0;
  --control-border: #cbc6ba;
  --masthead: #182126;
  --red: #d94732;
  --red-dark: #a92e20;
  --teal: #1d6f72;
  --yellow: #e9b949;
  --shadow: 0 18px 45px rgb(31 38 42 / 10%);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

html {
  min-height: 100%;
  color-scheme: light;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --ink: #e8ecec;
  --muted: #9da9ad;
  --paper: #101619;
  --panel: #182126;
  --line: #344147;
  --soft: #222d32;
  --soft-hover: #2b383e;
  --inspector-bg: #141c20;
  --canvas-bg: #12191d;
  --canvas-gradient-start: #11191d;
  --canvas-gradient-end: #1c292f;
  --control-border: #3d4a50;
  --masthead: #0a1013;
  --red: #f06450;
  --red-dark: #ff806d;
  --teal: #58b9bc;
  --yellow: #efc45d;
  --shadow: 0 18px 50px rgb(0 0 0 / 32%);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(rgb(28 34 38 / 2%) 1px, transparent 1px),
    var(--paper);
  background-size: 100% 28px;
}

button, input, select { font: inherit; }
button, select { cursor: pointer; }

.masthead {
  height: 78px;
  padding: 0 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #fff;
  background: var(--masthead);
  border-bottom: 4px solid var(--red);
}

.brand {
  display: flex;
  align-items: center;
  gap: 13px;
  color: inherit;
  text-decoration: none;
}

.brand strong {
  display: block;
  font-size: 17px;
  line-height: 1;
  letter-spacing: .16em;
}

.brand small {
  display: block;
  margin-top: 6px;
  color: #aeb8bd;
  font-size: 10px;
  letter-spacing: .14em;
  text-transform: uppercase;
}

.brand-mark {
  width: 37px;
  height: 37px;
  padding: 7px 6px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border: 2px solid #fff;
  border-radius: 3px;
}

.brand-mark i {
  display: block;
  height: 2px;
  background: var(--red);
}

.dataset-status {
  display: flex;
  align-items: center;
  gap: 9px;
  color: #bdc5c9;
  font-size: 12px;
}

.header-actions { display: flex; align-items: center; gap: 20px; }

.theme-toggle {
  height: 34px;
  padding: 0 11px;
  display: flex;
  align-items: center;
  gap: 7px;
  color: #d9e0e2;
  background: rgb(255 255 255 / 6%);
  border: 1px solid rgb(255 255 255 / 16%);
  border-radius: 18px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .04em;
}
.theme-toggle:hover { background: rgb(255 255 255 / 11%); }
.theme-toggle svg {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.theme-toggle .sun-icon { display: none; }
html[data-theme="dark"] .theme-toggle .sun-icon { display: block; }
html[data-theme="dark"] .theme-toggle .moon-icon { display: none; }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--yellow);
  box-shadow: 0 0 0 4px rgb(233 185 73 / 12%);
}

main {
  width: 100%;
  min-height: 0;
  padding: 26px 32px 36px;
  display: flex;
  flex: 1;
  flex-direction: column;
}

.view-tabs {
  margin: -4px 0 22px;
  display: flex;
  gap: 4px;
  overflow-x: auto;
  border-bottom: 1px solid var(--line);
}

.view-tab {
  position: relative;
  padding: 11px 17px 13px;
  color: var(--muted);
  background: transparent;
  border: 0;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
  white-space: nowrap;
}

.view-tab::after {
  position: absolute;
  right: 12px;
  bottom: -1px;
  left: 12px;
  height: 3px;
  content: "";
  background: transparent;
}

.view-tab:hover { color: var(--ink); }
.view-tab.active { color: var(--ink); }
.view-tab.active::after { background: var(--red); }
.view-tab:focus-visible { outline: 2px solid var(--teal); outline-offset: -2px; }

.controls {
  position: relative;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(180px, .55fr) minmax(320px, 2fr);
  gap: 16px;
}

label > span, .search-wrap > label {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}

select, .search-field {
  width: 100%;
  height: 44px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--control-border);
  border-radius: 4px;
  box-shadow: 0 2px 0 rgb(29 34 37 / 4%);
}

select { padding: 0 38px 0 13px; }

.search-wrap { position: relative; }
.search-field { display: flex; align-items: center; padding: 0 11px; }
.search-field:focus-within { outline: 2px solid rgb(29 111 114 / 25%); border-color: var(--teal); }
.search-field svg { width: 19px; fill: none; stroke: var(--muted); stroke-width: 1.8; }
.search-field input { flex: 1; min-width: 0; padding: 0 10px; border: 0; outline: 0; background: transparent; }
kbd { padding: 2px 6px; color: var(--muted); background: var(--soft); border: 1px solid var(--line); border-radius: 3px; font: 11px/1.2 inherit; }

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: 0;
  max-height: 360px;
  overflow: auto;
  background: var(--panel);
  border: 1px solid #c8c3b6;
  border-radius: 4px;
  box-shadow: var(--shadow);
}

.search-result {
  width: 100%;
  padding: 11px 13px;
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 10px;
  text-align: left;
  color: inherit;
  background: transparent;
  border: 0;
  border-bottom: 1px solid #e4e0d7;
}

.search-result:hover, .search-result:focus { background: var(--soft); outline: 0; }
.search-result .type { color: var(--red-dark); font-size: 10px; font-weight: 800; text-transform: uppercase; }
.search-result strong, .search-result small { display: block; }
.search-result small { margin-top: 3px; color: var(--muted); }
.search-result .count { color: var(--muted); font-size: 11px; white-space: nowrap; }

.summary-band {
  padding: 33px 0 25px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 25px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--red-dark);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .15em;
  text-transform: uppercase;
}

h1 { margin: 0; font: 700 clamp(26px, 3vw, 40px)/1.05 Georgia, serif; letter-spacing: -.02em; }
#diagram-subtitle { margin: 9px 0 0; color: var(--muted); font-size: 13px; }

.metrics { margin: 0; display: flex; }
.metrics div { min-width: 92px; padding: 4px 18px; border-left: 1px solid var(--line); }
.metrics dt { font: 700 23px/1 Georgia, serif; }
.metrics dd { margin: 6px 0 0; color: var(--muted); font-size: 9px; font-weight: 700; letter-spacing: .09em; text-transform: uppercase; }

.workspace {
  min-height: 610px;
  display: grid;
  flex: 1;
  grid-template-columns: minmax(0, 1fr) 290px;
  background: var(--panel);
  border: 1px solid #d1ccbf;
  box-shadow: var(--shadow);
}

.canvas-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 50px minmax(515px, 1fr) auto;
}
.canvas-toolbar {
  height: 50px;
  padding: 0 15px 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}

.legend { display: flex; gap: 18px; color: var(--muted); font-size: 10px; font-weight: 700; text-transform: uppercase; }
.legend span { display: flex; align-items: center; gap: 6px; }
.key { width: 14px; height: 10px; display: inline-block; border: 1.5px solid currentColor; }
.key-plug { color: var(--red); border-radius: 5px 0 0 5px; }
.key-socket { color: var(--teal); border-radius: 0 5px 5px 0; }
.key-earth { width: 13px; height: 8px; color: #737a70; border: 0; border-bottom: 5px double currentColor; }
.key-box { color: #a07922; box-shadow: inset 0 0 0 2px #fff, inset 0 0 0 3px currentColor; }
.key-wire { height: 0; color: #2b68a3; border: 0; border-top: 3px solid currentColor; }

.view-tools { display: flex; align-items: center; gap: 5px; }
.view-tools button {
  height: 28px;
  min-width: 28px;
  color: var(--ink);
  background: var(--soft);
  border: 1px solid #d2cdc1;
  border-radius: 3px;
}
.view-tools button:hover { background: var(--soft-hover); }
.view-tools output { min-width: 42px; color: var(--muted); text-align: center; font-size: 10px; }
.view-tools .text-button { margin-left: 4px; padding: 0 9px; font-size: 10px; font-weight: 700; text-transform: uppercase; }

.diagram-scroll {
  position: relative;
  min-height: 0;
  overflow: auto;
  cursor: grab;
  background: linear-gradient(145deg, var(--canvas-gradient-start), var(--canvas-gradient-end));
}
.diagram-scroll.dragging { cursor: grabbing; user-select: none; }
#diagram { display: block; overflow: visible; }
.canvas-hint { margin: 0; padding: 10px 16px; color: var(--muted); background: var(--soft); border-top: 1px solid var(--line); font-size: 10px; }

.inspector {
  position: sticky;
  top: 12px;
  min-height: 0;
  height: calc(100vh - 24px);
  height: calc(100dvh - 24px);
  padding: 22px;
  align-self: start;
  overflow-y: auto;
  overflow-wrap: anywhere;
  border-left: 1px solid var(--line);
  background: var(--inspector-bg);
}
.inspector-heading { display: flex; align-items: center; justify-content: space-between; }
.inspector-heading .eyebrow { margin: 0; }
#selection-type { padding: 4px 6px; color: #6d6b63; background: #e7e2d7; border-radius: 2px; font-size: 9px; font-weight: 800; text-transform: uppercase; }
.inspector h2 { margin: 18px 0 7px; font: 700 22px/1.1 Georgia, serif; }
.inspector > p { margin: 0 0 20px; color: var(--muted); font-size: 12px; line-height: 1.5; }
.detail-group { margin-top: 20px; }
.detail-group h3 { margin: 0 0 9px; padding-bottom: 7px; color: var(--red-dark); border-bottom: 1px solid #d6d0c3; font-size: 9px; letter-spacing: .12em; text-transform: uppercase; }
.detail-list { margin: 0; }
.detail-list div { padding: 4px 0; display: grid; grid-template-columns: 78px 1fr; gap: 8px; font-size: 11px; line-height: 1.4; }
.detail-list dt { color: #797b76; }
.detail-list dd { margin: 0; font-weight: 600; }
.detail-list .drawing-link {
  color: var(--teal);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}
.detail-list .drawing-link:hover { color: var(--red-dark); }
.trace-button { width: 100%; margin-top: 18px; padding: 9px; color: white; background: var(--teal); border: 0; border-radius: 3px; font-size: 11px; font-weight: 700; text-transform: uppercase; }
.trace-button:hover { background: #145b5d; }

.tooltip {
  position: fixed;
  z-index: 50;
  width: min(310px, calc(100vw - 24px));
  padding: 12px 13px;
  pointer-events: none;
  color: #f8f6f0;
  background: rgb(20 28 32 / 96%);
  border-top: 3px solid var(--red);
  border-radius: 2px;
  box-shadow: 0 12px 30px rgb(0 0 0 / 24%);
  font-size: 11px;
}
.tooltip strong { display: block; margin-bottom: 6px; font: 700 14px Georgia, serif; }
.tooltip dl { margin: 0; }
.tooltip dl div { display: grid; grid-template-columns: 72px 1fr; gap: 8px; padding: 2px 0; }
.tooltip dt { color: #aeb6b9; }
.tooltip dd { margin: 0; }

.empty-state { position: absolute; inset: 0; z-index: 2; display: grid; place-content: center; text-align: center; }
.empty-state strong, .empty-state span { display: block; }
.empty-state span { margin-top: 6px; color: var(--muted); font-size: 12px; }
.toast { position: fixed; right: 24px; bottom: 24px; z-index: 100; padding: 11px 15px; color: #fff; background: #222d31; border-radius: 3px; box-shadow: var(--shadow); font-size: 12px; }

html[data-theme="dark"] #diagram {
  filter: invert(.91) hue-rotate(180deg) brightness(1.08);
}

html[data-theme="dark"] .search-results,
html[data-theme="dark"] .workspace {
  border-color: var(--line);
}

html[data-theme="dark"] .inspector-heading #selection-type {
  color: #c5ced1;
  background: #2a363b;
}

html[data-theme="dark"] .tooltip {
  color: #f5f7f7;
  background: rgb(5 10 12 / 97%);
}

@media (max-width: 950px) {
  .workspace { grid-template-columns: 1fr; }
  .canvas-panel { grid-template-rows: 50px minmax(480px, 1fr) auto; }
  .inspector {
    position: static;
    height: auto;
    padding: 22px;
    overflow-y: visible;
    border-top: 1px solid var(--line);
    border-left: 0;
  }
}

@media (max-width: 700px) {
  .masthead { padding: 0 17px; }
  .dataset-status { display: none; }
  .header-actions { gap: 8px; }
  .theme-toggle span { display: none; }
  .theme-toggle { width: 36px; padding: 0; justify-content: center; }
  main { padding: 18px 14px 28px; }
  .controls { grid-template-columns: 1fr; }
  .summary-band { align-items: flex-start; flex-direction: column; }
  .metrics { width: 100%; }
  .metrics div { min-width: 0; flex: 1; padding: 3px 9px; }
  .legend { display: none; }
}
