:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #cfd6e2;
  --text: #172033;
  --muted: #667085;
  --accent: #0b6bcb;
  --address: #0b6bcb;
  --input: #9a3412;
  --pon: #334155;
  --map-bg: hsl(78 86% 86%);
  --map-grid: rgba(76, 116, 32, .17);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.topbar {
  height: 60px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 64px 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 5;
}

h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
}

button,
input[type="search"] {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
}

button {
  padding: 7px 10px;
  cursor: pointer;
}

button:hover {
  border-color: #aeb8c7;
}

button:disabled {
  cursor: not-allowed;
  opacity: .46;
}

input[type="search"] {
  width: min(390px, 32vw);
  padding: 7px 9px;
}

.area-search-wrap {
  flex: 1 1 320px;
  max-width: 620px;
}

.area-search-wrap input[type="search"] {
  width: 100%;
}

.area-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

.area-actions button {
  min-width: 72px;
  font-weight: 700;
}

.area-actions button:not(:disabled) {
  border-color: #2563eb;
  background: #eff6ff;
  color: #1d4ed8;
}

.search-wrap {
  position: relative;
  display: grid;
}

.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  width: min(560px, 82vw);
  max-height: 340px;
  overflow: auto;
  display: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 12px 28px rgba(16, 24, 40, .16);
}

.search-results.open { display: block; }

.result-button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border: 0;
  border-bottom: 1px solid #edf0f4;
  border-radius: 0;
  padding: 9px 10px;
  text-align: left;
}

.result-button:hover,
.result-button.active {
  background: #f2f5f9;
}

.badge {
  flex: 0 0 auto;
  border-radius: 999px;
  padding: 1px 6px;
  background: #e0f2fe;
  color: #075985;
  font-size: 11px;
  font-weight: 700;
}

main {
  height: 100vh;
  padding-top: 60px;
  padding-bottom: 0;
}

.map {
  position: relative;
  height: 100%;
  overflow: auto;
  background:
    linear-gradient(var(--map-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--map-grid) 1px, transparent 1px),
    var(--map-bg);
  background-size: 28px 28px;
}

.edges {
  position: absolute;
  inset: 0;
  overflow: visible;
  pointer-events: auto;
  transform-origin: 0 0;
}

.edge-path {
  fill: none;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.edge-path.branch-hit {
  stroke-width: 5;
  filter: drop-shadow(0 0 4px rgba(37, 99, 235, .45));
}

.edge-label {
  pointer-events: none;
}

.edge-path.reverse {
  stroke-dasharray: 7 5;
}

.edge-label {
  font-size: 11px;
  fill: #475467;
  paint-order: stroke;
  stroke: #fff;
  stroke-width: 4px;
  stroke-linejoin: round;
}

.nodes {
  position: absolute;
  inset: 0;
  pointer-events: none;
  transform-origin: 0 0;
}

.node {
  position: absolute;
  min-width: 128px;
  max-width: 220px;
  min-height: 38px;
  display: grid;
  align-content: center;
  gap: 2px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 2px 6px rgba(16, 24, 40, .08);
  transform: translate(-50%, -50%);
  cursor: default;
  pointer-events: auto;
}

.map-spacer {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  pointer-events: none;
}

.node.pon {
  max-width: 360px;
  border-color: #64748b;
  background: #f8fafc;
}

.node.pon span {
  white-space: normal;
  line-height: 1.25;
}

.node.input {
  border-color: #fdba74;
  background: #fff7ed;
}

.node.first-input {
  border-color: #ef4444;
  background: #fef2f2;
}

.node.first-input span {
  color: #991b1b;
}

.node.blue-stop {
  border-color: #60a5fa;
  background: #eff6ff;
  cursor: pointer;
}

.node.selectable-input {
  cursor: pointer;
}

.node.branch-hit {
  border-color: #2563eb;
  box-shadow: 0 0 0 4px rgba(37, 99, 235, .24), 0 10px 28px rgba(37, 99, 235, .22);
}

.node.area-hit {
  border-color: #2563eb;
  background: #dbeafe;
  box-shadow: 0 0 0 4px rgba(37, 99, 235, .20), 0 8px 22px rgba(37, 99, 235, .20);
}

.node.area-hit span {
  color: #1d4ed8;
}

.node.area-duplicate::after {
  content: "";
  position: absolute;
  top: 50%;
  right: -14px;
  width: 10px;
  height: 10px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: #facc15;
  box-shadow: 0 0 0 1px rgba(161, 98, 7, .35), 0 4px 10px rgba(161, 98, 7, .24);
  transform: translateY(-50%);
}

.node.libre {
  min-width: 96px;
  border-style: dashed;
  border-color: #94a3b8;
  background: #f8fafc;
}

.node strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.node span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 11px;
}

.ellipsis {
  pointer-events: auto;
  cursor: pointer;
  touch-action: manipulation;
}

.ellipsis circle {
  fill: #fff;
  stroke: #94a3b8;
  stroke-width: 1.6;
}

.ellipsis text {
  font-size: 18px;
  font-weight: 800;
  fill: #475467;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 28px;
  background: rgba(15, 23, 42, .42);
  z-index: 20;
}

.modal-backdrop.open { display: flex; }

.modal {
  width: min(980px, 94vw);
  max-height: min(760px, 90vh);
  display: grid;
  grid-template-rows: auto 1fr;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 24px 70px rgba(15, 23, 42, .28);
}

.modal.area-modal {
  width: max-content;
  min-width: 320px;
  max-width: 96vw;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
}

.modal-header div {
  display: grid;
  gap: 2px;
}

.modal-header span {
  color: var(--muted);
  font-size: 12px;
}

.modal-body {
  overflow: auto;
  padding: 10px 12px 14px;
}

.empty {
  color: var(--muted);
  padding: 18px;
}

.area-table {
  width: auto;
  border-collapse: collapse;
  table-layout: auto;
  font-size: 12px;
}

.area-table th,
.area-table td {
  border: 1px solid #dfe4ec;
  padding: 4px 6px;
  text-align: left;
  vertical-align: middle;
}

.area-table th {
  background: #f8fafc;
  color: var(--muted);
  font-weight: 650;
  white-space: nowrap;
}

.area-table td.value-cell {
  white-space: nowrap;
  width: 1%;
  max-width: 360px;
  overflow: hidden;
  text-overflow: ellipsis;
  background: #fff;
  cursor: pointer;
}

.area-table td.pon-row-cell {
  background: #fee2e2;
  border-color: #ef4444;
}

.area-table td.in-out-group-cell {
  background: #fff1f2;
  border-color: #f87171;
}

.area-table td.pon-row-cell.in-out-group-cell {
  background: #fecaca;
}

.area-table tr.empty-row td.value-cell {
  color: #c1c7d0;
}

.area-click-info {
  margin-bottom: 8px;
  padding: 6px 8px;
  border: 1px solid #fecaca;
  border-radius: 6px;
  display: none;
  background: #fff7f7;
  color: #991b1b;
  font-size: 12px;
}

.area-click-info.open {
  display: block;
}

.chain-map {
  position: relative;
  min-width: 100%;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  background:
    linear-gradient(rgba(148, 163, 184, .14) 1px, transparent 1px),
    linear-gradient(90deg, rgba(148, 163, 184, .14) 1px, transparent 1px),
    #f8fafc;
  background-size: 24px 24px;
}

.chain-edges {
  position: absolute;
  inset: 0;
  overflow: visible;
}

.chain-edge {
  fill: none;
  stroke-width: 2.2;
  stroke-linecap: round;
}

.chain-node {
  position: absolute;
  width: 150px;
  min-height: 42px;
  display: grid;
  align-content: center;
  gap: 2px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 2px 6px rgba(16, 24, 40, .08);
  transform: translate(-50%, -50%);
}

.chain-node.input {
  border-color: #fdba74;
  background: #fff7ed;
}

.chain-node strong,
.chain-node span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chain-node strong {
  font-size: 13px;
}

.chain-node span {
  color: var(--muted);
  font-size: 11px;
}

.settings-button {
  position: fixed;
  top: 10px;
  right: 16px;
  z-index: 18;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 50%;
  box-shadow: 0 8px 24px rgba(16, 24, 40, .18);
}

.zoom-controls {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 14;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 12px 30px rgba(16, 24, 40, .18);
}

.stats-badge {
  position: fixed;
  left: 16px;
  bottom: 16px;
  z-index: 14;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .92);
  color: var(--muted);
  box-shadow: 0 10px 24px rgba(16, 24, 40, .14);
  font-size: 12px;
  font-weight: 700;
}

.zoom-controls button {
  min-width: 34px;
  height: 32px;
  padding: 0 8px;
}

.zoom-controls span {
  min-width: 48px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.settings-panel {
  position: fixed;
  top: 58px;
  right: 16px;
  z-index: 18;
  width: 280px;
  display: none;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 16px 40px rgba(16, 24, 40, .18);
}

.settings-panel.open {
  display: grid;
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.settings-head button,
.settings-panel > button {
  padding: 5px 8px;
}

.settings-panel label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

.settings-panel input[type="range"] {
  width: 100%;
}

.bg-preview {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--map-bg);
}

@media (max-width: 820px) {
  .topbar {
    height: 56px;
    padding: 8px 58px 8px 10px;
    gap: 8px;
  }

  h1 {
    font-size: 15px;
  }

  main {
    padding-top: 56px;
    padding-bottom: 0;
  }

  input[type="search"] {
    width: 100%;
    min-width: 0;
    font-size: 13px;
  }

  .area-search-wrap {
    min-width: 0;
  }

  .search-results {
    width: min(520px, calc(100vw - 20px));
    max-height: 42vh;
  }

  .settings-button {
    top: 8px;
    right: 10px;
    width: 40px;
    height: 40px;
  }

  .settings-panel {
    top: 56px;
    right: 8px;
    width: min(300px, calc(100vw - 16px));
  }

  .zoom-controls {
    right: 8px;
    bottom: 8px;
    gap: 4px;
    padding: 5px;
  }

  .zoom-controls button {
    min-width: 32px;
  }

  .stats-badge {
    left: 8px;
    bottom: 8px;
  }
}
