:root {
  --text: #111;
  --muted: #5f6368;
  --bg: #f7f7f7;
  --surface: #fff;
  --soft: #fbfbfb;
  --border: #d9d9d9;
  --success: #1f7a3f;
  --danger: #b3261e;
  --radius: 10px;
  --shadow: 0 12px 28px rgba(0, 0, 0, .06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

button, select, input {
  font: inherit;
  color: var(--text);
}

.masthead {
  padding: 28px clamp(18px, 4vw, 56px);
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: flex-end;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.eyebrow, .section-index {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(30px, 4vw, 48px);
  font-weight: 700;
  letter-spacing: -.03em;
  line-height: 1.05;
}

h1 i {
  font-style: normal;
  color: var(--text);
}

.connection-block {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: max-content;
}

.connection-block strong, .connection-block small { display: block; }
.connection-block strong { font-size: 14px; }
.connection-block small { font-size: 13px; color: var(--muted); }

.connection-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 4px rgba(179, 38, 30, .12);
}

.connection-dot.online {
  background: var(--success);
  box-shadow: 0 0 0 4px rgba(31, 122, 63, .12);
}

main { padding: 0 clamp(18px, 4vw, 56px) 48px; }

.controls {
  display: flex;
  align-items: end;
  gap: 12px;
  flex-wrap: wrap;
  padding: 20px 0;
}

.field {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

.field select, .field input[type=number] {
  display: block;
  margin-top: 6px;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  min-width: 120px;
}

.input-field select { min-width: 230px; }
.max-field input { width: 110px; }

.button {
  height: 42px;
  padding: 0 18px;
  border: 1px solid #111;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 15px;
  font-weight: 700;
  transition: background .15s ease, transform .15s ease;
}

.button:hover { background: #efefef; }
.button:active { transform: translateY(1px); }
.button.primary { background: #111; color: #fff; }
.button.primary:hover { background: #333; }
.button.quiet { border-color: var(--border); color: var(--muted); }

.control-divider {
  width: 1px;
  height: 34px;
  background: var(--border);
  margin: 0 4px;
}

.speed-field {
  display: grid;
  grid-template-columns: 140px 44px;
  gap: 4px 10px;
  align-items: center;
  margin-left: auto;
}

.speed-field input { accent-color: #111; }
.speed-field output { font-size: 14px; color: var(--muted); }

.metrics-panel {
  margin-bottom: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.metrics-panel summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 22px;
  cursor: pointer;
  user-select: none;
}

.metrics-panel summary::-webkit-details-marker { display: none; }

.metrics-panel summary:hover { background: #f4f4f4; }
.metrics-panel summary:focus-visible { outline: 3px solid #111; outline-offset: -3px; }

.metrics-panel summary b {
  display: block;
  color: var(--text);
  font-size: 18px;
  font-weight: 800;
}

.metrics-panel summary small {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 14px;
}

.metrics-panel summary i {
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 50%;
  position: relative;
  flex: 0 0 auto;
  background: var(--surface);
}

.metrics-panel summary i::before,
.metrics-panel summary i::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 14px;
  height: 2px;
  background: #111;
  transform: translate(-50%, -50%);
}

.metrics-panel summary i::after {
  transform: translate(-50%, -50%) rotate(90deg);
  transition: transform .15s ease;
}

.metrics-panel[open] summary {
  border-bottom: 1px solid var(--border);
}

.metrics-panel[open] summary i::after {
  transform: translate(-50%, -50%) rotate(0deg);
}

.metrics {
  display: grid;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
  gap: 14px;
  padding: 18px 22px 22px;
}

.metrics article {
  min-height: 112px;
  padding: 18px;
  background: var(--soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.metrics span, .metrics small { display: block; }

.metrics span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
}

.metrics strong {
  display: block;
  margin: 6px 0 2px;
  font-size: 34px;
  line-height: 1.1;
  font-weight: 800;
  color: var(--text);
}

.metrics small { font-size: 14px; color: var(--muted); }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(300px, .8fr);
  gap: 22px;
  align-items: start;
}

.field-panel, .event-panel, .groups-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.field-panel { padding: 22px; }
.event-panel { padding: 22px; min-width: 0; }

.section-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 800;
  letter-spacing: -.02em;
}

.run-state {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  background: var(--soft);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .05em;
}

.run-state.running {
  background: #111;
  border-color: #111;
  color: #fff;
}

.visualization {
  position: relative;
  height: 580px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--soft);
}

#fieldSvg {
  display: block;
  width: 100%;
  height: 100%;
  color: #e1e1e1;
}

.grid { opacity: .55; }
.group-radius { fill: none; stroke-width: 2; stroke-dasharray: 8 7; opacity: .5; }
.member-link { stroke-width: 1.4; opacity: .25; }
.node-core { stroke: #fff; stroke-width: 4; }
.node-dead { fill: #b7b7b7; stroke: #777; stroke-width: 2; }
.dead-cross { stroke: #fff; stroke-width: 3; stroke-linecap: round; }
.node-label { fill: #111; font-size: 14px; font-weight: 800; text-anchor: middle; pointer-events: none; }
.node-energy { fill: #555; font-size: 12px; text-anchor: middle; pointer-events: none; }
.leader-ring { fill: none; stroke: #111; stroke-width: 2.5; }
.leader-crown { fill: #111; font-size: 12px; text-anchor: middle; }
.candidate-score { fill: #111; font-size: 12px; font-weight: 800; text-anchor: middle; }
.message-line { fill: none; stroke-width: 2; stroke-dasharray: 5 5; opacity: .65; }
.message-dot { stroke: #fff; stroke-width: 2; }
.message-label { font-size: 11px; font-weight: 800; text-anchor: middle; paint-order: stroke; stroke: #fff; stroke-width: 4; }

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  text-align: center;
  pointer-events: none;
}

.empty-state.hidden { display: none; }
.empty-state b { font-size: 22px; font-weight: 800; }
.empty-state span { display: block; margin-top: 6px; color: var(--muted); font-size: 15px; }

.legend {
  position: absolute;
  left: 14px;
  bottom: 12px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  padding: 9px 12px;
  background: rgba(255, 255, 255, .92);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 700;
  color: #111;
}

.legend span { display: flex; align-items: center; gap: 6px; }
.legend-node { width: 10px; height: 10px; border-radius: 50%; background: #111; }
.legend-node.leader { box-shadow: 0 0 0 2px #111; }
.legend-node.dead { background: #aaa; }
.legend-radius { width: 18px; height: 10px; border: 1px dashed #111; border-radius: 50%; }

.event-log {
  height: 580px;
  overflow: auto;
  list-style: none;
  padding: 0;
  margin: 0;
  border-top: 1px solid var(--border);
}

.event-log li {
  position: relative;
  padding: 14px 4px 14px 44px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  line-height: 1.45;
  color: #111;
}

.event-log li::before {
  content: attr(data-step);
  position: absolute;
  left: 0;
  top: 15px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.event-log li.election, .event-log li.regroup, .event-log li.death { color: #111; }
.event-log li.death { font-weight: 800; }
.event-log li.empty-log { padding-left: 0; color: var(--muted); }
.event-log li.empty-log::before { display: none; }

.groups-section { margin-top: 22px; padding: 22px; }
.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 15px;
}

th {
  padding: 12px 14px;
  border-bottom: 1px solid #111;
  color: #111;
  font-size: 13px;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: .05em;
}

td { padding: 14px; border-bottom: 1px solid var(--border); }
td:first-child, td:nth-child(2) { font-weight: 800; }

.member-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.member-pills i {
  padding: 4px 9px;
  background: #eee;
  border-radius: 999px;
  font-style: normal;
  font-size: 13px;
  color: #111;
}

.table-empty {
  color: var(--muted);
  text-align: center;
  font-weight: 400 !important;
  font-size: 15px !important;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  max-width: 360px;
  padding: 14px 18px;
  background: #111;
  color: #fff;
  border-radius: var(--radius);
  font-size: 15px;
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: .2s ease;
}

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

@media (max-width: 1100px) {
  .metrics { grid-template-columns: repeat(3, 1fr); }
  .workspace { grid-template-columns: 1fr; }
  .event-log { height: 340px; }
  .speed-field { margin-left: 0; }
  .visualization { height: 520px; }
}

@media (max-width: 680px) {
  body { font-size: 15px; }
  .masthead { align-items: flex-start; flex-direction: column; }
  .controls { align-items: stretch; }
  .button { flex: 1; }
  .control-divider { display: none; }
  .input-field, .input-field select, .field select, .field input[type=number] { width: 100%; }
  .speed-field { grid-template-columns: 1fr 44px; width: 100%; }
  .metrics-panel summary { padding: 16px; }
  .metrics { grid-template-columns: 1fr; padding: 16px; }
  .visualization { height: 430px; }
  .field-panel, .event-panel, .groups-section { padding: 16px; }
  .section-heading { align-items: flex-start; flex-direction: column; }
  .legend { right: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
  }
}
