:root {
  --paper: #e6ebed;
  --panel: #f7f9f9;
  --stage: #fcfdfd;
  --ink: #10242e;
  --muted: #566b77;
  --rule: #c3cfd5;
  --accent: #0b7a85;
  --accent-dark: #075c65;
  --warn: #7a4300;
  --warn-bg: #fff1d6;
  --err: #a3122e;
  --err-bg: #fde8ec;
  --radius: 6px;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- header ---------- */
.top {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 20px;
  padding: 14px 20px 10px;
}
.top h1 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
.tag { margin: 0; color: var(--muted); }
.status { margin: 0 0 0 auto; color: var(--muted); font-size: 13px; min-height: 1.4em; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(250px, 300px) minmax(0, 1fr) minmax(320px, 380px);
  gap: 12px;
  padding: 0 12px 12px;
  align-items: start;
}

.controls, .results, .stage {
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
}

.controls { padding: 14px; }
.results { padding: 14px; }

.stage {
  position: relative;
  background: var(--stage);
  overflow: hidden;
  min-height: 520px;
  height: calc(100vh - 120px);
  max-height: 880px;
  display: flex;
  flex-direction: column;
}
#scene {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: block;
  cursor: grab;
  touch-action: none;
}
#scene.dragging { cursor: grabbing; }

.stage-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 8px 12px;
  border-top: 1px solid var(--rule);
  background: var(--panel);
}
.hint { margin-left: auto; color: var(--muted); font-size: 13px; }

.error {
  position: absolute;
  left: 12px; right: 12px; top: 12px;
  padding: 10px 12px;
  background: var(--err-bg);
  color: var(--err);
  border: 1px solid var(--err);
  border-radius: var(--radius);
  font-weight: 500;
}
.error[hidden] { display: none; }

/* ---------- controls ---------- */
.field { margin-bottom: 16px; }
.field:last-child { margin-bottom: 0; }
.label { display: block; font-weight: 600; margin-bottom: 4px; }
.row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.row .label { margin-bottom: 4px; }
output { color: var(--muted); }

select, input[type="number"] {
  width: 100%;
  padding: 8px 10px;
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
}

input[type="range"] {
  width: 100%;
  margin: 2px 0 0;
  accent-color: var(--accent);
  min-height: 28px;
}

.note { margin: 6px 0 0; color: var(--muted); font-size: 13px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.seg button {
  padding: 8px 6px;
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  cursor: pointer;
}
.seg button[aria-checked="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn {
  width: 100%;
  padding: 9px 12px;
  font: inherit;
  font-weight: 600;
  color: var(--accent-dark);
  background: #fff;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  cursor: pointer;
}
.btn:hover { background: #e8f4f5; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.small { width: auto; padding: 5px 10px; font-weight: 500; }

.more { border-top: 1px solid var(--rule); padding-top: 10px; }
.more summary { cursor: pointer; font-weight: 600; margin-bottom: 12px; }

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

/* ---------- results ---------- */
.headline {
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 12px;
}
.big { display: flex; align-items: baseline; gap: 6px; }
.big-num { font-size: 40px; font-weight: 650; line-height: 1.05; letter-spacing: -0.02em; }
.big-unit { color: var(--muted); font-size: 15px; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.facts { margin: 0 0 14px; }
.facts div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 5px 0;
  border-bottom: 1px solid var(--rule);
}
.facts dt { color: var(--muted); }
.facts dd { margin: 0; text-align: right; font-weight: 600; }

h2 { font-size: 15px; margin: 6px 0 6px; }

.plot { margin: 0 0 12px; }
.plot figcaption { font-weight: 600; margin-bottom: 4px; font-size: 14px; }
.c-swr { width: 100%; aspect-ratio: 16 / 9; display: block; background: #fff; border: 1px solid var(--rule); border-radius: var(--radius); }
.c-polar { width: 100%; aspect-ratio: 1 / 1; display: block; background: #fff; border: 1px solid var(--rule); border-radius: var(--radius); }
.polars { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.warn {
  margin: 0 0 12px;
  padding: 8px 10px;
  background: var(--warn-bg);
  color: var(--warn);
  border-radius: var(--radius);
  font-size: 13px;
}

.foot { padding: 4px 20px 20px; color: var(--muted); font-size: 13px; max-width: 80ch; }
.foot p { margin: 0; }

body.busy .results { opacity: 0.72; }

/* ---------- responsive ---------- */
@media (max-width: 1150px) {
  .layout { grid-template-columns: minmax(240px, 290px) minmax(0, 1fr); }
  .results { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 0 20px; }
  .results .headline { grid-column: 1 / -1; }
}

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; padding: 0 8px 8px; }
  .stage { height: 62vh; min-height: 380px; order: -1; }
  .hint { display: none; }
  .top { padding: 12px 12px 8px; }
  .status { margin-left: 0; width: 100%; }
}

/* ---------- garden ---------- */
.file-actions { display: flex; gap: 8px; margin-left: auto; }
.top .status { margin-left: 0; }
.garden { padding-bottom: 12px; border-bottom: 1px solid var(--rule); margin-bottom: 14px; }
.garden h2, .editor h2 { margin: 0 0 8px; font-size: 16px; }
.items { list-style: none; margin: 0 0 10px; padding: 0; display: grid; gap: 4px; }
.item { width: 100%; text-align: left; padding: 7px 10px; font: inherit; color: var(--ink); background: #fff; border: 1px solid var(--rule); border-radius: var(--radius); cursor: pointer; }
.item[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); font-weight: 600; }
.adders { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 14px; }
.adders .btn.small { width: 100%; padding: 6px 4px; }
.btn.danger { color: var(--err); border-color: var(--err); }
.btn.danger:hover { background: var(--err-bg); }
.results-title { margin: 0 0 8px; font-size: 16px; }
#scene.over-item { cursor: move; }
#scene.dragging.over-item { cursor: grabbing; }
@media (max-width: 1150px) { .results-title { grid-column: 1 / -1; } }

/* ---------- custom antenna editor ---------- */
.custom { border-top: 1px solid var(--rule); padding-top: 12px; margin-bottom: 16px; }
.custom h3 { margin: 0 0 4px; font-size: 15px; }
.custom input[type="text"] { width: 100%; padding: 8px 10px; font: inherit; color: var(--ink); background: #fff; border: 1px solid var(--rule); border-radius: var(--radius); }
.pills { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 10px; }
.pill { padding: 5px 10px; font: inherit; font-size: 13px; color: var(--ink); background: #fff; border: 1px solid var(--rule); border-radius: 999px; cursor: pointer; }
.pill[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.pill.add { color: var(--accent-dark); border-color: var(--accent); }
.pill:disabled { opacity: 0.5; cursor: default; }
.pts { width: 100%; border-collapse: collapse; margin-bottom: 8px; font-size: 13px; }
.pts th { text-align: left; font-weight: 600; color: var(--muted); padding: 0 3px 3px; }
.pts tbody th { color: var(--muted); font-weight: 500; padding: 0 4px 0 0; width: 1%; }
.pts td { padding: 2px 2px; }
.pts input { width: 100%; min-width: 0; padding: 5px 4px; font: inherit; font-size: 13px; color: var(--ink); background: #fff; border: 1px solid var(--rule); border-radius: 4px; }
.pts input[aria-invalid="true"] { border-color: var(--err); background: var(--err-bg); }
.pts tr.sel input { border-color: var(--accent); }
.pts tr.sel th { color: var(--accent-dark); font-weight: 700; }
.pts .x { width: 26px; height: 26px; padding: 0; font: inherit; font-size: 16px; line-height: 1; color: var(--muted); background: #fff; border: 1px solid var(--rule); border-radius: 4px; cursor: pointer; }
.pts .x:disabled { opacity: 0.4; cursor: default; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 10px; }
.pair .btn.small, .pair select { width: 100%; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* ---------- designer window ---------- */
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent-dark); }
.btn.primary:disabled { opacity: 0.55; }
.design-btn { width: 100%; margin: 0 0 8px; padding: 10px; }
.sub-h { margin: 14px 0 6px; font-size: 14px; }
.library { list-style: none; margin: 0 0 6px; padding: 0; display: grid; gap: 6px; }
.library li { display: grid; grid-template-columns: 1fr auto auto auto; gap: 4px; align-items: center; padding: 4px 6px; background: #fff; border: 1px solid var(--rule); border-radius: var(--radius); }
.library .lib-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.library .btn.small { padding: 3px 8px; font-size: 12px; }
.designer { position: fixed; inset: 0; z-index: 20; display: flex; align-items: stretch; justify-content: center; background: rgba(16,36,46,0.55); padding: 16px; }
.designer[hidden] { display: none; }
.designer-card { display: flex; flex-direction: column; width: min(1200px, 100%); background: var(--panel); border-radius: 10px; overflow: hidden; box-shadow: 0 10px 40px rgba(0,0,0,0.35); }
.designer-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--rule); }
.designer-head h2 { margin: 0; font-size: 20px; }
.designer-head .note { margin: 2px 0 0; }
.designer-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.designer-actions .btn { width: auto; padding: 8px 14px; }
#d-msg { margin: 8px 16px 0; }
.designer-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 340px 1fr; }
.d-form { overflow-y: auto; padding: 14px 16px; border-right: 1px solid var(--rule); }
.d-form .custom { border-top: 0; padding-top: 0; }
.d-stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--stage); }
.d-canvas { flex: 1; min-height: 240px; width: 100%; display: block; touch-action: none; cursor: grab; }
.d-canvas.over-point { cursor: pointer; }
.d-canvas.dragging { cursor: grabbing; }
.d-result { padding: 10px 16px 14px; border-top: 1px solid var(--rule); display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px 16px; font-size: 13px; }
.d-result .stat b { display: block; font-size: 18px; }
.d-result .stat span { color: var(--muted); }
.d-result .wide { grid-column: 1 / -1; }
.d-result .warn { color: var(--warn); background: var(--warn-bg); padding: 6px 8px; border-radius: var(--radius); }
.d-result .bad { color: var(--err); background: var(--err-bg); padding: 6px 8px; border-radius: var(--radius); }
body.modal-open { overflow: hidden; }
@media (max-width: 800px) {
  .designer { padding: 0; }
  .designer-card { border-radius: 0; overflow-y: auto; }
  .designer-body { grid-template-columns: 1fr; }
  .d-form { border-right: 0; overflow: visible; order: 2; }
  .d-stage { order: 1; }
  .d-canvas { min-height: 300px; flex: none; height: 300px; }
}
