:root {
  color-scheme: dark;
  --text: #eaf6ff;
  --muted: #a8bece;
  --accent: #54d2ff;
  --border: #39586f;
  --panel: #13202c;
  --known: #6ee7ac;
  --calculated: #ff8e8e;
  --missing: #ffcf73;
  --mono: Consolas, "Courier New", monospace;
  --nav-height: 62px;
  --characteristics-width: min(460px, calc(100% - 52px));
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { scrollbar-gutter: stable; scroll-padding-top: calc(var(--nav-height) + 12px); }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: radial-gradient(ellipse at 18% 5%, #54d2ff24, transparent 54%),
    radial-gradient(ellipse at 94% 98%, #ffba521c, transparent 46%), linear-gradient(150deg, #061018, #111f2a);
  font: 1rem/1.45 Bahnschrift, "Trebuchet MS", "Segoe UI", sans-serif;
}
a { color: #9fd8ff; text-underline-offset: 3px; }
button, input, select, textarea { font: inherit; color: inherit; }
button, input, select, textarea { min-height: 44px; min-width: 0; border: 1px solid var(--border); border-radius: 8px; background: #111f2b; padding: 7px 10px; }
button, select, summary { cursor: pointer; }
button:hover:not(:disabled) { background: #234456; }
button:disabled { opacity: .45; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.sr-only, .memory-status:not([data-error]) { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

.tool-nav { position: sticky; z-index: 15; top: 0; display: flex; align-items: center; gap: 8px; padding: 8px clamp(12px, 3vw, 28px); background: #0d1821f7; border-bottom: 1px solid var(--border); font-size: .88rem; }
.tool-nav a { text-decoration: none; }
.tool-nav a:hover { text-decoration: underline; }
.tool-nav span { white-space: nowrap; }
.exports { margin-left: auto; display: flex; gap: 8px; }
.exports button { background: #183448; border-color: #4386a2; color: #b7eaff; }
.page { width: min(100%, 1480px); margin: 0 auto; padding: 24px clamp(16px, 4.5vw, 68px) 64px; }
h2 { margin: 0; font-size: 1rem; font-weight: 600; }
.workbench { display: grid; grid-template-columns: 168px minmax(0, 1fr); align-items: start; gap: 14px; }
.diagram { min-width: 0; background: var(--panel); border: 1px solid #304c61; border-radius: 14px; box-shadow: 0 14px 34px #010a1755; }
.mode-picker { position: relative; z-index: 1; display: grid; gap: 6px; margin: 24px -15px 0 0; }
.mode-choice {
  display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 5px;
  min-height: 64px; padding: 11px 12px; border: 1px solid var(--border); border-right: 3px solid transparent;
  border-radius: 10px 0 0 10px; background: #192c3b; text-align: left;
}
.mode-choice strong { font-size: .9rem; line-height: 1.25; font-weight: 600; }
.mode-choice span { color: var(--muted); font: .75rem/1.45 var(--mono); }
.mode-choice:hover:not(:disabled), .mode-choice:focus-visible { background: #29445a; border-color: var(--accent); }
.mode-choice[aria-pressed="true"] { background: #123d50; border-color: var(--accent); border-right-color: var(--panel); }
.mode-choice[aria-pressed="true"] strong { color: var(--accent); }
.mode-choice[aria-pressed="true"]:hover:not(:disabled) { background: #19516a; }
.field { display: grid; gap: 5px; min-width: 0; }
.field label { color: var(--muted); font-size: .875rem; }
[aria-invalid="true"] { border-color: var(--missing); }

.diagram { padding: 10px; }
.diagram-tools { display: flex; justify-content: flex-end; padding: 2px 2px 10px; }
.diagram-tools:has([hidden]) { display: none; }
.schematic-viewport { --title-editor-height: 34px; position: relative; overflow: auto; border-radius: 12px; background: #101823; }
.electron-flow {
  margin: 10px 0 0; padding: 10px 14px; border-radius: 8px; text-align: center; color: var(--muted);
  text-wrap: balance;
  background: linear-gradient(90deg, transparent, #54d2ff12, transparent);
}
.electron-flow strong { color: var(--accent); font: 700 1.0625rem/1.4 var(--mono); white-space: nowrap; }
#schematic { display: block; width: 100%; height: auto; }
.schematic-viewport.desktop-view #schematic { min-width: var(--drawing-width); }
.schematic-viewport:has(.schematic-title-editor:not([hidden])) [data-annotation="title"] { visibility: hidden; }
.schematic-title-editor { position: absolute; display: flex; gap: 3px; font: 14px var(--mono); }
.schematic-title-editor :is(input, button) { min-height: var(--title-editor-height); padding: 2px 7px; border-color: #75d5ff; border-radius: 6px; background: #1e3547; }
.schematic-title-editor input { flex: 1; width: 0; }
.schematic-title-editor button { min-width: var(--title-editor-height); }
#schematic [data-schematic-control] { cursor: pointer; outline: none; }
#schematic [data-field-readout] { visibility: hidden; }
#schematic .schematic-quantity { display: grid; grid-template-columns: minmax(0, 1fr) 58px; gap: 3px; height: 100%; }
#schematic .component-field .resistance-quantity { grid-template-columns: minmax(0, 1fr) min(45%, 76px); }
#schematic .schematic-input {
  display: block; width: 100%; height: 100%; min-height: 0;
  padding: 2px 5px; border-radius: 4px; background: #142535;
  font: 16px/1 var(--mono); color: var(--known);
}
#schematic .schematic-quantity .schematic-input { font-size: 24px; }
#schematic select.schematic-input { padding-right: 0; }
#schematic .component-field .schematic-input { padding-left: 8px; border-color: #6a8ca5; border-radius: 6px; background: #1e3547; }
#schematic .component-field select { padding-left: 5px; }
#schematic [data-source-info]:hover .source-info-box { stroke: #54d2ff74; }
#schematic .schematic-input[data-schematic-input^="tolerance-"] { color: #c6d7e5; }
#schematic .schematic-input[data-value-role="calculated"] { color: var(--calculated); }
#schematic .schematic-input[data-centered] { text-align: center; }
#schematic .schematic-input[aria-invalid="true"] { color: var(--missing); }
#schematic .schematic-input:focus { outline: none; border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
#schematic .schematic-input[aria-invalid="true"]:focus { border-color: var(--missing); box-shadow: inset 0 0 0 1px var(--missing); }
#schematic [data-part]:hover .part-info-box,
#schematic [data-part][aria-pressed="true"] .part-info-box { stroke: #54d2ff74; }
#schematic [data-part]:focus-visible .part-info-box { stroke: var(--accent); stroke-width: 2; }
#schematic .control-hit { fill: transparent; stroke: transparent; stroke-width: 1; }
#schematic [data-schematic-control]:hover > .control-hit { fill: #54d2ff12; stroke: #54d2ff74; }
#schematic [data-schematic-control]:focus-visible > .control-hit { stroke: var(--accent); stroke-width: 2; }
#schematic [data-annotation="title"]:is(:hover, :focus-visible) text { fill: #fff; }
#schematic [data-frame-download]:is(:hover, :focus-visible) rect { fill: #2b465b; }
#schematic [data-frame-download]:focus-visible rect { stroke: var(--text); stroke-width: 2; }
.construction-menu { width: max-content; max-width: calc(100vw - 24px); }
#annotationEditor, #calculatedResistanceDialog, #toleranceDialog, .frame-export-dialog { width: min(420px, calc(100% - 32px)); padding: 18px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); color: var(--text); }
#annotationEditor::backdrop, #calculatedResistanceDialog::backdrop, #toleranceDialog::backdrop, .frame-export-dialog::backdrop { background: #081016aa; }
#toleranceForm { gap: 8px; }
.frame-export-dialog { width: min(480px, calc(100% - 32px)); padding: 26px; }
.frame-export-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 22px; }
.frame-export-brand { display: block; width: 132px; height: auto; }
.frame-export-close { font-size: 22px; line-height: 1; }
.frame-export-title { margin: 9px 0 5px; overflow-wrap: anywhere; }
.frame-export-formats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 22px; }
.frame-export-format { display: grid; justify-items: center; gap: 7px; min-height: 140px; padding: 20px 12px; border-color: #365166; border-radius: 14px; background: #172a39; color: var(--format-color); }
.frame-export-format:is(:hover, :focus-visible):not(:disabled) { background: #223e50; border-color: var(--format-color); }
.frame-export-format svg { width: 44px; height: 44px; }
.frame-export-format strong { font-size: 18px; letter-spacing: .04em; }
.frame-export-format small { color: var(--muted); font-size: 12px; }
#annotationForm { display: grid; gap: 14px; }
.annotation-actions { display: flex; justify-content: flex-end; gap: 8px; }
.resistance-actions { flex-wrap: wrap; }
#applyCalculatedResistance { background: #17394b; border-color: #4386a2; }
.annotation-actions [type="submit"] { background: #17394b; border-color: #4386a2; }
.notice { margin: 10px 6px 2px; color: var(--missing); font-size: .875rem; }

.characteristics-drawer, .calculator-memory {
  position: fixed; z-index: 13; top: var(--nav-height); bottom: 0; min-height: 0;
  background: linear-gradient(150deg, #172d3d, #101c28 56%); transition: transform .2s ease;
}
.edge-tab {
  position: fixed; z-index: 14; display: flex; align-items: center; gap: 8px; min-width: 44px; min-height: 44px;
  padding: 12px; background: #132d3d; color: var(--accent); border: 1px solid #4289a6;
  box-shadow: 0 5px 18px #0005; writing-mode: vertical-rl; font-size: .875rem; white-space: nowrap; list-style: none;
}
.edge-tab:hover, .edge-tab[aria-expanded="true"] { background: #1c4b60; }
.edge-tab svg { flex: none; }
.edge-tab[aria-expanded="true"] svg { transform: rotate(180deg); }
.characteristics-tab { top: calc(var(--nav-height) + 24px); right: 0; border-radius: 10px 0 0 10px; border-right: 0; transition: right .2s ease; }
.characteristics-tab[aria-expanded="true"] { right: var(--characteristics-width); }
.characteristics-drawer { right: 0; width: var(--characteristics-width); border-left: 1px solid #4289a6; box-shadow: -16px 0 40px #0006; transform: translateX(100%); visibility: hidden; display: grid; grid-template-rows: auto minmax(0, 1fr); }
.characteristics-drawer.is-open { transform: translateX(0); visibility: visible; }
.drawer-header { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; padding: 12px; border-bottom: 1px solid var(--border); }
.drawer-header h2 { flex: 1 1 120px; }
.close { flex: 0 0 44px; padding: 0; font-size: 1.5rem; background: #234153; }
.characteristics-scroll { overflow: auto; padding: 0 14px 16px; overscroll-behavior: contain; }
.characteristics-scroll table { width: 100%; table-layout: fixed; border-collapse: collapse; }
.characteristics-scroll th, .characteristics-scroll td { padding: 10px 0; border-bottom: 1px solid #39586f80; font-weight: 400; overflow-wrap: anywhere; }
.characteristics-scroll th { width: 50%; padding-right: 8px; text-align: left; color: var(--muted); }
.characteristics-scroll td { text-align: right; font: .9rem/1.45 var(--mono); }
#partCharacteristics { margin-top: 14px; }
#partCharacteristics details { border-top: 1px solid var(--border); }
#partCharacteristics summary { padding: 10px 4px; min-height: 44px; color: #c6e3ff; }
#copyFallback { width: 100%; margin-top: 10px; }

.calculator-memory { left: 0; width: min(var(--memory-width, 300px), calc(100% - 52px)); border-right: 1px solid #4289a6; box-shadow: 16px 0 40px #0006; transform: translateX(-100%); }
.calculator-memory[open] { transform: translateX(0); }
.calculator-memory > summary { position: absolute; top: 24px; left: 100%; border-radius: 0 10px 10px 0; border-left: 0; -webkit-touch-callout: none; }
.calculator-memory > summary::-webkit-details-marker, .memory-group-heading::-webkit-details-marker { display: none; }
#memoryCount, .memory-group-count { color: var(--muted); }
.memory-body { position: absolute; inset: 0; display: flex; flex-direction: column; padding: 0 10px 12px; }
.memory-body .drawer-header { flex-wrap: nowrap; padding-inline: 2px; margin-bottom: 8px; }
.memory-body .drawer-header h2 { flex-basis: 0; }
.memory-status[data-error] { color: var(--missing); font-size: .875rem; }
.memory-list, .memory-group-list { display: grid; align-content: start; gap: 8px; min-height: 0; margin: 0; padding: 0; list-style: none; }
.memory-list { overflow-y: auto; overscroll-behavior: contain; }
.memory-group-heading { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 8px 2px; color: #c6e3ff; overflow-wrap: anywhere; list-style: none; }
.memory-group-heading::before { content: "›"; margin-right: 3px; }
.memory-group > details[open] > summary::before { transform: rotate(90deg); }
.memory-group-count { margin-left: auto; }
.memory-entry { min-width: 0; border-radius: 8px; background: #162a39; border-left: 3px solid transparent; }
.memory-entry[aria-current="true"] { background: #1b384a; border-left-color: var(--accent); }
.memory-entry-heading { display: flex; align-items: center; }
.memory-restore { flex: 1; text-align: left; overflow-wrap: anywhere; -webkit-touch-callout: none; }
.memory-restore, .memory-expand, .memory-rename { border: 0; background: transparent; }
.memory-expand, .memory-rename { display: grid; place-items: center; flex: 0 0 44px; padding: 0; }
.memory-expand svg, .memory-rename svg { width: 16px; height: 16px; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.memory-expand[aria-expanded="true"] svg { transform: rotate(90deg); }
.memory-changes { margin: 0; padding: 0 10px 10px 24px; color: #b7eaff; font-size: .875rem; overflow-wrap: anywhere; }
.memory-name-input { flex: 1; width: 0; }
.memory-menu, .construction-menu { position: fixed; z-index: 17; display: grid; gap: 3px; max-height: calc(100vh - 24px); overflow: auto; padding: 6px; border: 1px solid var(--border); border-radius: 8px; background: #152634; box-shadow: 0 12px 30px #0008; }
.memory-menu p, .construction-menu p { margin: 4px 8px; color: var(--muted); font-size: .875rem; overflow-wrap: anywhere; }
.construction-menu button { text-align: left; }
.memory-menu button { position: relative; text-align: left; padding-left: 32px; }
.memory-menu [aria-checked="true"]::before { content: "✓"; position: absolute; left: 10px; color: var(--accent); }
#memoryResize { display: none; }

@media (min-width: 781px) {
  .calculator-memory[open] #memoryResize { display: block; position: absolute; top: 172px; right: -8px; bottom: 8px; width: 16px; cursor: col-resize; touch-action: none; }
  #memoryResize::after { content: ""; position: absolute; inset: 0 7px; border-radius: 2px; background: var(--border); }
  #memoryResize:hover::after, #memoryResize:focus-visible::after { background: var(--accent); }
  .memory-resizing, .memory-resizing * { cursor: col-resize !important; user-select: none; }
}
@media (min-width: 781px) and (pointer: coarse) {
  .calculator-memory[open] #memoryResize { right: -22px; width: 44px; }
  #memoryResize::after { inset-inline: 21px; }
}
@media (max-width: 1080px) and (min-width: 781px) {
  .workbench { grid-template-columns: 150px minmax(0, 1fr); }
}
@media (max-width: 780px) {
  .schematic-viewport { --title-editor-height: 44px; }
  .schematic-title-editor { font-size: 16px; }
  .page { padding: 16px 12px 76px; }
  .workbench { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .mode-picker { grid-template-columns: repeat(3, minmax(0, 1fr)); margin: 0 0 -11px; }
  .mode-choice { align-items: center; border-right: 1px solid var(--border); border-bottom-width: 3px; border-radius: 8px 8px 0 0; padding: 8px 4px; text-align: center; }
  .mode-choice[aria-pressed="true"] { border-right-color: var(--accent); border-bottom-color: var(--panel); }
  .diagram { border-radius: 0 0 14px 14px; }
  .characteristics-tab, .calculator-memory > summary { position: fixed; top: auto; bottom: 8px; writing-mode: horizontal-tb; }
  .calculator-memory > summary { position: absolute; }
  .calculator-memory:not([open]) > summary { border-radius: 0 10px 10px 0; }
  .calculator-memory[open] > summary { left: 0; margin-left: 6px; border: 1px solid #4289a6; border-radius: 8px; }
  .characteristics-tab[aria-expanded="true"] { right: 6px; border: 1px solid #4289a6; border-radius: 8px; }
  .characteristics-scroll, .memory-body { padding-bottom: 68px; }
}
@media (max-width: 430px) {
  .tool-nav { flex-wrap: wrap; }
  .exports { flex: 1 1 100%; margin-left: 0; }
  .exports button { flex: 1; }
  .frame-export-dialog { padding: 18px; }
  .frame-export-format { min-height: 104px; padding: 10px 6px; }
}
@media (prefers-reduced-motion: reduce) {
  .calculator-memory, .characteristics-tab, .characteristics-drawer { transition: none; }
}
