/* Тёмная тема, спокойная палитра */
:root {
  --bg: #0f1217;
  --surface: #161a21;
  --surface-2: #1d222b;
  --surface-3: #252b36;
  --border: #2a313d;
  --text: #d8dee8;
  --muted: #8b96a6;
  --faint: #5d6776;
  --accent: #8fb3ff;
  --accent-soft: rgba(143, 179, 255, 0.14);
  --teal: #6fcfbf;
  --ok: #8fcf9a;
  --warn: #e3c66d;
  --bad: #e88a8a;
  --edge: #7d8899;
  --edge-hl: #f0d27a;
  --vertex: #2b323e;
  --vertex-stroke: #8b96a6;
  --halo: rgba(143, 179, 255, 0.28);
  --ring: #e3c66d;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: dark;
}

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

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
}

.wrap { max-width: 1280px; margin: 0 auto; padding: 0 20px; }

code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--surface-3);
  padding: 1px 5px;
  border-radius: 4px;
  white-space: nowrap;
}

h1, h2, h3, h4 { line-height: 1.25; margin: 0; font-weight: 600; }
h1 { font-size: 20px; padding: 18px 0 12px; letter-spacing: 0.01em; }
h2 { font-size: 19px; margin-bottom: 10px; }
h3 { font-size: 15px; margin: 18px 0 8px; color: var(--text); }
h4 { font-size: 13px; margin: 0 0 8px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
.card > h3:first-child { margin-top: 0; }
p { margin: 0 0 10px; }
.lead { color: var(--text); }
.card p { color: #c2cad6; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }

/* ---------- шапка и вкладки ---------- */

.top {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }

.tab {
  flex: 0 0 auto;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  padding: 10px 14px 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-num {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--surface-3);
  font-size: 12px;
  font-weight: 600;
}
.tab.active .tab-num { background: var(--accent); color: var(--bg); }

/* ---------- раскладка ---------- */

main { padding-top: 20px; padding-bottom: 48px; }

.cols {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
.results { margin-top: 0; }
#res-5 { display: grid; gap: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  min-width: 0;
}

.block { margin-top: 18px; }

/* ---------- соглашения ---------- */

.conv {
  margin-top: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
}
.conv summary { cursor: pointer; color: var(--accent); font-weight: 500; }
.conv p { margin: 8px 0 4px; }
.conv ul { margin: 4px 0 2px; padding-left: 20px; }
.conv li { margin: 3px 0; }

/* ---------- ввод матрицы ---------- */

.mi-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px 16px;
  margin-bottom: 12px;
}

.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }

input[type="number"], select, textarea {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  padding: 6px 8px;
}
input[type="number"] { width: 76px; }
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.check { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); padding-bottom: 7px; cursor: pointer; }
.check input { accent-color: var(--accent); }

.segmented {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  margin-left: auto;
}
.seg {
  background: var(--surface-2);
  border: none;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  cursor: pointer;
}
.seg + .seg { border-left: 1px solid var(--border); }
.seg.on { background: var(--accent-soft); color: var(--accent); }

.table-wrap { overflow-x: auto; max-width: 100%; }

table.grid { border-collapse: collapse; }
table.grid th {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  padding: 2px 4px;
  text-align: center;
}
table.grid td { padding: 2px; }
table.grid input {
  width: 34px;
  height: 30px;
  text-align: center;
  font-family: var(--mono);
  font-size: 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0;
}
table.grid input.diag { background: var(--surface-3); }

.mi-text {
  width: 100%;
  font-family: var(--mono);
  font-size: 14px;
  resize: vertical;
}

.two-inputs { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.two-inputs .segmented { margin-left: 0; }

.actions { display: flex; gap: 10px; margin-top: 16px; justify-content: flex-end; }

.btn {
  font: inherit;
  font-size: 14px;
  padding: 8px 18px;
  border-radius: 7px;
  cursor: pointer;
  border: 1px solid var(--border);
}
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.ghost:hover { background: var(--surface-3); }
.btn.primary { background: var(--accent); color: #0d1320; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: 0.6; cursor: progress; }

.example-name { color: var(--muted); font-size: 13px; margin: 10px 0 0; text-align: right; }

/* ---------- сообщения ---------- */

.error {
  background: rgba(232, 138, 138, 0.1);
  border: 1px solid rgba(232, 138, 138, 0.45);
  color: #f2b3b3;
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 16px;
}

.notice {
  border-radius: 8px;
  padding: 10px 14px;
  margin: 0 0 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 14px;
}
.notice.warn { border-color: rgba(227, 198, 109, 0.45); background: rgba(227, 198, 109, 0.08); color: #ecd895; }
.notice.ok { border-color: rgba(143, 207, 154, 0.45); background: rgba(143, 207, 154, 0.08); color: #b5e2bd; }
.notice.bad { border-color: rgba(232, 138, 138, 0.45); background: rgba(232, 138, 138, 0.08); color: #f2b3b3; }
.notice.info { color: #c2cad6; }
.notice.big { font-size: 17px; font-weight: 600; }
.reason { color: #c2cad6; }

/* ---------- числовые результаты ---------- */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}
.stat {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 20px; font-weight: 600; font-family: var(--mono); }
.stat.center { border-color: rgba(143, 179, 255, 0.45); }
.stat.periphery { border-color: rgba(227, 198, 109, 0.45); }

table.matrix {
  border-collapse: separate;
  border-spacing: 0;
  font-family: var(--mono);
  font-size: 13px;
}
table.matrix th, table.matrix td {
  padding: 5px 9px;
  text-align: center;
  border-bottom: 1px solid var(--border);
  min-width: 34px;
}
table.matrix thead th { color: var(--muted); font-weight: 500; border-bottom-color: var(--surface-3); }
table.matrix tbody th { color: var(--muted); font-weight: 500; text-align: right; border-right: 1px solid var(--border); }
table.matrix td.zero { color: var(--faint); }
table.matrix td.inf { color: var(--warn); }
table.matrix td.neg { color: var(--bad); }
table.matrix td.two { color: var(--teal); }
table.matrix td.row-hl { background: var(--accent-soft); }
table.matrix td.left { text-align: left; }
table.matrix [data-edge] { cursor: default; transition: background 0.12s; }
table.matrix .hl { background: rgba(240, 210, 122, 0.16); color: var(--edge-hl); }
table.matrix.inv td, table.matrix.inv th { font-family: inherit; }

.edge-list, .reach-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 6px;
}
.edge-list li, .reach-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid transparent;
  font-size: 14px;
}
.reach-list li { cursor: pointer; }
.edge-list li.hl, .reach-list li.hl { border-color: var(--edge-hl); }
.edge-tag {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--surface-3);
  color: var(--muted);
  padding: 1px 6px;
  border-radius: 4px;
  white-space: nowrap;
}

.pairs { display: flex; flex-wrap: wrap; gap: 8px; }
.pair {
  font-family: var(--mono);
  font-size: 14px;
  padding: 4px 10px 4px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 4px solid var(--c);
  cursor: default;
}
.pair.hl { border-color: var(--c); }

/* ---------- граф ---------- */

.viz-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.viz-head h3 { margin: 0; }

svg.graph {
  display: block;
  width: 100%;
  max-width: 520px;
  height: auto;
  margin: 0 auto;
  user-select: none;
}

.edge .line { fill: none; stroke: var(--edge); stroke-width: 2; transition: stroke 0.12s; }
.edge .hit { fill: none; stroke: transparent; stroke-width: 14; }
.edge .head { fill: var(--edge); transition: fill 0.12s; }
.edge.hl .line { stroke: var(--edge-hl); stroke-width: 3; }
.edge.hl .head { fill: var(--edge-hl); }
.edge.dim .line { stroke: #39404c; }
.edge.dim .head { fill: #39404c; }

.edge-label {
  font: 600 12px var(--mono);
  fill: var(--muted);
  text-anchor: middle;
  dominant-baseline: central;
  paint-order: stroke;
  stroke: var(--surface);
  stroke-width: 5px;
  stroke-linejoin: round;
  pointer-events: none;
}
.edge-label.hl { fill: var(--edge-hl); }

.vertex .body { fill: var(--vertex); stroke: var(--vertex-stroke); stroke-width: 1.5; transition: fill 0.15s; }
.vertex .num {
  font: 600 13px var(--mono);
  fill: var(--text);
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}
.vertex.filled .body { stroke: rgba(0, 0, 0, 0.35); }
.vertex.filled .num { fill: #10141b; }
.vertex .halo, .vertex .ring { fill: none; stroke: none; }
.vertex.is-center .halo { fill: var(--halo); }
.vertex.is-periphery .ring { stroke: var(--ring); stroke-width: 2.5; stroke-dasharray: 5 3; }
.vertex.is-start .body { stroke: #ffffff; stroke-width: 2.5; }
.vertex.unreached .body { fill: var(--bg); stroke: var(--faint); stroke-dasharray: 3 3; }
.vertex.unreached .num { fill: var(--muted); }
.vertex.dim { opacity: 0.45; }
.vertex.focus .body { stroke: #ffffff; stroke-width: 3; }
.vertex.clickable { cursor: pointer; }

.two-graphs { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.two-graphs figure { margin: 0; }
.two-graphs figcaption { text-align: center; color: var(--muted); font-size: 13px; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin-top: 10px;
  font-size: 13px;
  color: #c2cad6;
}
.legend:empty { display: none; }
.legend-item { display: inline-flex; align-items: center; gap: 7px; }
.sw { display: inline-block; flex: 0 0 auto; }
.sw-dot { width: 13px; height: 13px; border-radius: 50%; }
.sw-dot-empty { width: 13px; height: 13px; border-radius: 50%; border: 1.5px dashed var(--faint); }
.sw-halo { width: 16px; height: 16px; border-radius: 50%; background: var(--halo); }
.sw-ring { width: 15px; height: 15px; border-radius: 50%; border: 2px dashed var(--ring); }
.sw-line { width: 20px; height: 2px; background: var(--edge); }
.sw-line-hl { width: 20px; height: 3px; background: var(--edge-hl); }
.sw-arrow { width: 0; height: 0; border-left: 11px solid var(--edge); border-top: 5px solid transparent; border-bottom: 5px solid transparent; margin-left: 8px; position: relative; }
.sw-arrow::before { content: ""; position: absolute; right: 11px; top: -1px; width: 10px; height: 2px; background: var(--edge); }
.sw-loop { width: 13px; height: 13px; border-radius: 50%; border: 2px solid var(--edge); }

/* ---------- адаптивность ---------- */

@media (max-width: 900px) {
  .cols { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 560px) {
  .wrap { padding: 0 16px; }
  h1 { font-size: 17px; padding: 14px 0 8px; }
  .tab { padding: 8px 10px 10px; font-size: 13px; }
  .card { padding: 14px 14px; }
  .two-inputs, .two-graphs { grid-template-columns: minmax(0, 1fr); }
  .segmented { margin-left: 0; }
  table.grid input { width: 30px; height: 28px; }
  .actions { justify-content: stretch; }
  .actions .btn { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
