:root {
  --ink: #1d2329;
  --muted: #5d6873;
  --line: #d9dee3;
  --surface: #ffffff;
  --backdrop: #eef0f2;
  --accent: #b80000;
  --accent-ink: #ffffff;
  --selected: #fbeaea;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; background: var(--backdrop); overflow: hidden; }

#stage { position: fixed; inset: 0; }
#stage canvas { display: block; }

body[data-loading] #bar, body[data-loading] #panel { visibility: hidden; }
#loading {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 16px;
  color: var(--muted);
  font-size: 15px;
  text-align: center;
  pointer-events: none;
}
#loading[hidden], #loading .spinner[hidden] { display: none; }
#loading .spinner {
  width: 44px;
  height: 44px;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { #loading .spinner { animation: none; } }

button {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { background: #f6f7f8; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover { background: #9a0000; }

#bar {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  pointer-events: none;
}
#bar > * { pointer-events: auto; }
#title {
  margin: 0;
  padding: 6px 12px;
  font-size: 16px;
  font-weight: 600;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
}
#panel-toggle { margin-left: auto; }

.segmented { display: flex; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; border-right: 1px solid var(--line); }
.segmented button:last-child { border-right: 0; }
.segmented button[aria-pressed="true"] { background: var(--ink); color: #fff; }

#panel {
  position: fixed;
  top: 60px;
  right: 12px;
  bottom: 12px;
  width: 340px;
  padding: 14px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
}
#panel[hidden] { display: none; }
#panel section[hidden] { display: none; }
#panel section:not([hidden]) ~ section:not([hidden]) { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
#panel h2 { margin: 14px 0 8px; font-size: 13px; font-weight: 600; color: var(--muted); }
#panel section > h2:first-child { margin-top: 0; }
#panel p { margin: 0; }

.check { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; cursor: pointer; }

#legend .row {
  display: grid;
  grid-template-columns: 18px 18px 1fr auto;
  align-items: center;
  gap: 6px;
  padding: 2px 0;
  cursor: pointer;
}
#legend .swatch { width: 14px; height: 14px; border-radius: 3px; border: 1px solid rgba(0, 0, 0, .25); }
#legend .count { color: var(--muted); font-variant-numeric: tabular-nums; }

#circuits details { border: 1px solid var(--line); border-radius: 6px; margin-bottom: 6px; }
#circuits summary { padding: 6px 10px; cursor: pointer; font-weight: 600; }
#circuits .list { border-top: 1px solid var(--line); }
#circuits .circuit {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 4px;
  width: 100%;
  text-align: left;
  border: 0;
  border-radius: 0;
  padding: 4px 10px;
  background: transparent;
}
#circuits .circuit:hover { background: #f6f7f8; }
#circuits .circuit.selected { background: var(--selected); font-weight: 600; }
#circuits .circuit .id { font-variant-numeric: tabular-nums; font-weight: 600; }
#circuits .circuit.spare { color: var(--muted); }

#details .title { font-size: 15px; font-weight: 600; margin: 0 28px 8px 0; }
#details { position: relative; }
#details .close { position: absolute; top: -4px; right: -4px; border: 0; background: transparent; font-size: 18px; padding: 2px 8px; }
#details dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; }
#details dt { color: var(--muted); }
#details dd { margin: 0; font-variant-numeric: tabular-nums; }
#details .tag {
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 6px;
  margin: 0 3px 3px 0;
  background: var(--surface);
  font-size: 13px;
}
#details .wires { margin-top: 8px; }
#details .hint { margin-top: 8px; color: var(--muted); font-size: 13px; }

#warnings { margin: 0; padding-left: 16px; color: var(--muted); font-size: 13px; }
#warnings li + li { margin-top: 4px; }

#tooltip {
  position: fixed;
  z-index: 10;
  max-width: 260px;
  padding: 4px 8px;
  border-radius: 4px;
  background: rgba(20, 24, 28, .92);
  color: #fff;
  font-size: 13px;
  pointer-events: none;
}

@media (max-width: 720px) {
  #bar { top: 8px; left: 8px; right: 8px; gap: 6px; }
  #title { display: none; }
  #panel { top: auto; left: 8px; right: 8px; bottom: 8px; width: auto; max-height: 45vh; }
  .segmented button { padding: 6px 8px; font-size: 13px; }
}
