:root {
  color-scheme: light;
  --ink: #152321;
  --muted: #61706d;
  --line: #d8e0dc;
  --line-strong: #aebdb7;
  --paper: #ffffff;
  --surface: #f3f6f4;
  --surface-2: #e9efeb;
  --teal: #087f73;
  --teal-dark: #075d56;
  --blue: #2864b8;
  --red: #b9413f;
  --amber: #a76513;
  --shadow: 0 8px 24px rgba(31, 55, 49, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--surface);
  color: var(--ink);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  letter-spacing: 0;
}

button, input, select { font: inherit; }

button { cursor: pointer; }

.app-header {
  min-height: 74px;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 0 0 auto;
}

.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  background: var(--teal-dark);
  color: #fff;
  font-weight: 800;
  font-size: 15px;
  border-radius: 6px;
}

h1, h2, p { margin: 0; }

.brand h1 { font-size: 20px; line-height: 1.2; }
.brand p { margin-top: 3px; font-size: 11px; color: var(--muted); }

.solver-controls {
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.solver-controls label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
}

.solver-controls label span { white-space: nowrap; }

input, select {
  min-height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  padding: 6px 8px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

input:focus, select:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 2px rgba(8, 127, 115, 0.14);
}

#base-mva { width: 82px; }
#tolerance { width: 84px; }
#max-iter { width: 64px; }

.button {
  min-height: 35px;
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 7px 13px;
  font-weight: 700;
  white-space: nowrap;
}

.button.primary { background: var(--teal); color: #fff; }
.button.primary:hover { background: var(--teal-dark); }
.button.secondary { background: #fff; color: var(--ink); border-color: var(--line-strong); }
.button.secondary:hover { background: var(--surface-2); }
.button.compact { min-height: 30px; padding: 4px 10px; font-size: 12px; }

.status {
  min-height: 34px;
  padding: 7px 20px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: #edf7f4;
  border-bottom: 1px solid #cce5de;
  color: var(--teal-dark);
  font-size: 13px;
}

.status.error { background: #fff0ef; border-color: #efcecc; color: #8e2f2c; }
.status.warning { background: #fff7e8; border-color: #ead7ae; color: #805016; }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  flex: 0 0 auto;
}

main { padding: 14px; }

.workspace {
  height: min(680px, calc(100vh - 142px));
  min-height: 520px;
  display: grid;
  grid-template-columns: minmax(420px, 34%) minmax(0, 1fr);
  overflow: hidden;
  background: var(--paper);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  border-radius: 7px;
}

.editor-pane {
  min-width: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
}

.tabs {
  height: 45px;
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.tab {
  min-width: 72px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.tab:hover { color: var(--ink); background: #eef2ef; }
.tab.active { color: var(--teal-dark); border-bottom-color: var(--teal); background: #fff; }

.tab-panel { display: none; min-height: 0; flex: 1; flex-direction: column; }
.tab-panel.active { display: flex; }

.panel-toolbar {
  min-height: 44px;
  padding: 7px 10px 7px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.icon-button {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: #fff;
  color: var(--teal-dark);
  font-size: 20px;
  line-height: 1;
}

.icon-button:hover { background: #e7f3ef; border-color: var(--teal); }
.icon-button.danger { color: var(--red); width: 26px; height: 26px; font-size: 16px; }

.table-scroll {
  min-width: 0;
  overflow: auto;
  flex: 1;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

th {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 9px 8px;
  background: #eaf0ec;
  border-bottom: 1px solid var(--line-strong);
  color: #384743;
  text-align: right;
  white-space: nowrap;
}

th:first-child, td:first-child { text-align: left; }

td {
  padding: 7px 8px;
  border-bottom: 1px solid #e6ebe8;
  text-align: right;
  white-space: nowrap;
}

tbody tr:nth-child(even) { background: #fafcfb; }
tbody tr:hover { background: #eef7f4; }

td input, td select {
  width: 76px;
  min-height: 28px;
  padding: 3px 5px;
  border-color: transparent;
  border-radius: 3px;
  background: transparent;
  text-align: right;
}

td input:hover, td select:hover { border-color: var(--line-strong); background: #fff; }
td input:focus, td select:focus { background: #fff; }
td .text-input { width: 96px; text-align: left; }
td .small-input { width: 56px; }
td .bool-select { width: 64px; }
td.actions { width: 36px; padding-left: 3px; padding-right: 7px; }

.bpa-text {
  margin: 0;
  padding: 14px;
  flex: 1;
  overflow: auto;
  background: #fbfcfb;
  color: #263632;
  font: 12px/1.65 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre;
}

.diagram-pane {
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: #fff;
}

.diagram-heading {
  min-height: 58px;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 1px solid var(--line);
}

.diagram-heading h2, .result-section h2 { font-size: 14px; }
.diagram-heading p { margin-top: 3px; color: var(--muted); font-size: 11px; }

.legend { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 11px; }
.legend span { display: flex; align-items: center; gap: 5px; white-space: nowrap; }
.legend-line { width: 18px; height: 2px; background: var(--teal); }
.legend-line.transformer { border-top: 2px dashed var(--amber); background: transparent; height: 0; }
.legend-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--blue); }
.legend-dot.pq { background: var(--teal); }

.canvas-wrap { position: relative; min-height: 0; flex: 1; overflow: hidden; }
#network-canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.result-band { margin-top: 14px; }

.metrics {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 7px;
  overflow: hidden;
}

.metrics article { padding: 13px 16px; border-right: 1px solid var(--line); }
.metrics article:last-child { border-right: 0; }
.metrics span { display: block; color: var(--muted); font-size: 11px; }
.metrics strong { display: block; margin-top: 5px; font-size: 17px; font-variant-numeric: tabular-nums; }
.metrics strong.good { color: var(--teal-dark); }
.metrics strong.bad { color: var(--red); }

.result-grid {
  margin-top: 14px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
}

.result-section {
  min-width: 0;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
}

.result-section h2 { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.result-section .table-scroll { max-height: 380px; }

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  padding: 11px 14px;
  border-radius: 5px;
  background: #152321;
  color: #fff;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.2);
  font-size: 13px;
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: 160ms ease;
}

.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 980px) {
  .app-header { align-items: flex-start; flex-direction: column; gap: 10px; }
  .solver-controls { justify-content: flex-start; }
  .workspace { height: auto; min-height: 0; grid-template-columns: 1fr; overflow: visible; }
  .editor-pane { height: 480px; border-right: 0; border-bottom: 1px solid var(--line); }
  .diagram-pane { height: 520px; }
  .metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .metrics article:nth-child(3) { border-right: 0; }
  .metrics article:nth-child(n+4) { border-top: 1px solid var(--line); }
  .result-grid { grid-template-columns: 1fr; }
}

@media (max-width: 620px) {
  main { padding: 8px; }
  .app-header { padding: 12px; }
  .solver-controls { display: grid; width: 100%; grid-template-columns: 1fr 1fr; align-items: end; }
  .solver-controls label { min-width: 0; }
  .solver-controls input { width: 100% !important; }
  .workspace { border-radius: 5px; }
  .editor-pane { height: 450px; }
  .diagram-pane { height: 430px; }
  .diagram-heading { align-items: flex-start; flex-direction: column; gap: 8px; }
  .legend { flex-wrap: wrap; }
  .tabs { overflow-x: auto; }
  .metrics { grid-template-columns: 1fr 1fr; }
  .metrics article { border-top: 1px solid var(--line); }
  .metrics article:nth-child(1), .metrics article:nth-child(2) { border-top: 0; }
  .metrics article:nth-child(even) { border-right: 0; }
}
