:root {
  --bg: #ffffff;
  --fg: #111111;
  --muted: #666666;
  --panel-bg: #ffffff;
  --border: #e2e2e2;
  --line: #c2c2c2;
  --line-hover: #8a8a8a;
  --contrast: #000000;
  --contrast-fg: #ffffff;
  --tile-fg: #111111;
  --shadow: 0 2px 10px rgba(0,0,0,0.08);
  --dot: #d9d9d9;
}

* { box-sizing: border-box; }

html {
  color-scheme: light;
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  overflow: hidden;
}

.hidden { display: none !important; }

.screen { width: 100vw; height: 100vh; }

/* Login */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-box {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 280px;
  text-align: center;
}
.login-box h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 8px;
}
.login-box input {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-bg);
  color: var(--fg);
  font-size: 15px;
}
.login-box button {
  padding: 12px;
  border-radius: 10px;
  border: none;
  background: var(--contrast);
  color: var(--contrast-fg);
  font-size: 15px;
  cursor: pointer;
}
.error { color: #e05252; font-size: 13px; margin: 0; }

/* Canvas */
#canvas-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1.3px, transparent 1.3px);
  background-size: 18px 18px;
  background-position: 0 0;
}
/* Both layers are anchored at the origin and sized 0x0 — the canvas is
   conceptually infinite, so tiles and connections are free to sit at any
   coordinate (including negative ones). overflow:visible stops the SVG
   viewport from clipping strokes that leave its box. */
/* #canvas and #lines-layer both create a stacking context (they're
   transformed), so .tile's own z-index can't lift it above the lines —
   the layers themselves have to be ordered. Tiles must win, otherwise the
   connections' wide invisible hit-strokes swallow clicks on the tiles. */
#canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  overflow: visible;
  z-index: 2;
  transform-origin: 0 0;
}
#lines-layer {
  position: absolute;
  top: 0; left: 0;
  width: 1px;
  height: 1px;
  overflow: visible;
  pointer-events: none;
  z-index: 1;
  transform-origin: 0 0;
}
/* Notes sit behind the connections and tiles, so a large coloured note can
   be used as a backdrop to group companies. */
#notes-layer {
  position: absolute;
  top: 0; left: 0;
  width: 0;
  height: 0;
  overflow: visible;
  pointer-events: none;
  z-index: 0;
  transform-origin: 0 0;
}

.note {
  position: absolute;
  pointer-events: auto;
  border-radius: 12px;
  padding: 12px 14px;
  cursor: grab;
  overflow: visible;
  display: flex;
}
.note:active { cursor: grabbing; }

.note-text {
  flex: 1;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  color: #1a1a1a;
  overflow: hidden;
  user-select: none;
}
.note-text.placeholder {
  color: var(--muted);
  opacity: 0.7;
}
.note-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  resize: none;
  font: inherit;
  font-size: 14px;
  line-height: 1.45;
  color: #1a1a1a;
  padding: 0;
  font-family: inherit;
}

.note-filled {
  border: 1px solid rgba(15, 15, 15, 0.07);
  box-shadow: 0 1px 2px rgba(15, 15, 15, 0.04), 0 4px 12px rgba(15, 15, 15, 0.06);
}
.note-plain {
  background: transparent;
  border: 1px dashed transparent;
}
.note-plain:hover {
  border-color: var(--border);
}

/* The toolbar is a single shared overlay pinned above every layer, so it
   stays clickable even when the note itself sits behind a tile. */
.note-toolbar {
  position: fixed;
  z-index: 15;
  display: flex;
  gap: 4px;
}
.note-toolbar button {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel-bg);
  color: var(--muted);
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}
.note-toolbar button:hover { color: var(--fg); }
.note-toolbar button.confirm {
  background: #fdecec;
  border-color: #e05252;
  color: #e05252;
}
.note-resize {
  position: absolute;
  right: 2px;
  bottom: 2px;
  width: 14px;
  height: 14px;
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity 0.12s ease;
}
.note:hover .note-resize { opacity: 1; }
.note-resize::after {
  content: "";
  position: absolute;
  right: 1px;
  bottom: 1px;
  width: 7px;
  height: 7px;
  border-right: 2px solid rgba(0,0,0,0.28);
  border-bottom: 2px solid rgba(0,0,0,0.28);
  border-radius: 0 0 2px 0;
}

/* Colour popover for a note */
.note-popover {
  position: fixed;
  z-index: 40;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  box-shadow: var(--shadow);
  width: 250px;
}
.note-plain-option {
  width: 100%;
  margin-bottom: 8px;
  padding: 7px 9px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
}
.note-plain-option:hover { background: #f0f0f0; }
#lines-layer .conn-line {
  fill: none;
  stroke: var(--line);
  stroke-width: 1.75;
  stroke-linecap: round;
  transition: stroke 0.12s ease;
}
/* The marker lives in <defs>, so it can't inherit hover state via a selector.
   context-stroke makes it follow the stroke of whichever path references it. */
#lines-layer .conn-arrow-head {
  fill: var(--line);
  fill: context-stroke;
}
#lines-layer .conn:hover .conn-line {
  stroke: var(--line-hover);
}
#lines-layer .conn-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 18;
  pointer-events: stroke;
  cursor: pointer;
}
#lines-layer .pending {
  stroke: var(--line-hover);
  stroke-dasharray: 5 4;
}
#lines-layer .conn-delete {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.1s ease;
}
#lines-layer .conn-delete.visible {
  opacity: 1;
  pointer-events: auto;
}
#lines-layer .conn-delete circle {
  fill: var(--panel-bg);
  stroke: var(--border);
  cursor: pointer;
}
#lines-layer .conn-delete circle:hover {
  fill: #fdecec;
  stroke: #e05252;
}
#lines-layer .conn-delete text {
  font-size: 11px;
  pointer-events: none;
}

.tile {
  position: absolute;
  min-width: 140px;
  min-height: 72px;
  cursor: grab;
  z-index: 2;
  color: var(--fg);
  user-select: none;
  overflow: visible;
}
.tile:active { cursor: grabbing; }

.tile-surface {
  position: absolute;
  inset: 0;
  border-radius: 14px;
  box-shadow: 0 1px 2px rgba(15, 15, 15, 0.04), 0 6px 16px rgba(15, 15, 15, 0.07);
  border: 1px solid rgba(15, 15, 15, 0.06);
  overflow: hidden;
  transition: box-shadow 0.15s ease;
}
.tile:hover .tile-surface {
  box-shadow: 0 2px 4px rgba(15, 15, 15, 0.06), 0 10px 24px rgba(15, 15, 15, 0.1);
}

.tile-body {
  padding: 14px 16px 12px;
}

.tile .tile-top {
  display: flex;
  align-items: center;
  gap: 6px;
}
.tile .tile-flag {
  font-size: 15px;
  line-height: 1;
}
.tile .tile-country {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--accent);
  opacity: 0.85;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tile .tile-name {
  font-weight: 600;
  font-size: 15px;
  margin-top: 8px;
  line-height: 1.3;
  word-break: break-word;
  color: #1a1a1a;
}

/* Rubber-band selection rectangle. Lives inside #canvas so it shares the
   pan/zoom transform and can be positioned in plain canvas coordinates. */
#marquee {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  border: 1px solid var(--contrast);
  background: rgba(0, 0, 0, 0.06);
  border-radius: 2px;
}

.tile.selected .tile-surface {
  box-shadow: 0 0 0 2px var(--contrast), 0 6px 16px rgba(15, 15, 15, 0.1);
}

.tile.link-source .tile-surface { box-shadow: 0 0 0 2px var(--contrast); }
.tile.link-target .tile-surface { box-shadow: 0 0 0 2px var(--accent); }

.tile .port,
.tile .resize-handle {
  opacity: 0;
  transition: opacity 0.12s ease, transform 0.12s ease, background 0.12s ease;
}
.tile:hover .port,
.tile:hover .resize-handle,
.tile .port.port-connected {
  opacity: 1;
}

.tile .port {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--port-color, var(--contrast));
  border: 2px solid var(--bg);
  cursor: crosshair;
  z-index: 3;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}
.tile .port:hover {
  transform: scale(1.25);
}

/* A port that already has a connection stays visible and gets a solid
   accent-colored core, so it's obvious at a glance which dots are docked. */
.tile .port.port-connected {
  background: var(--accent);
  border-color: var(--bg);
}

/* The port currently under the cursor while dragging a connection —
   enlarged and filled solid so docking onto it is unambiguous. */
.tile .port.port-target {
  background: var(--contrast);
  box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.12);
}
.tile .port-top.port-target,
.tile .port-bottom.port-target {
  transform: translateX(-50%) scale(1.6);
}
.tile .port-left.port-target,
.tile .port-right.port-target {
  transform: translateY(-50%) scale(1.6);
}
.tile .port-top {
  top: -7px;
  left: 50%;
  transform: translateX(-50%);
}
.tile .port-top:hover { transform: translateX(-50%) scale(1.25); }
.tile .port-bottom {
  bottom: -7px;
  left: 50%;
  transform: translateX(-50%);
}
.tile .port-bottom:hover { transform: translateX(-50%) scale(1.25); }
.tile .port-left {
  left: -7px;
  top: 50%;
  transform: translateY(-50%);
}
.tile .port-left:hover { transform: translateY(-50%) scale(1.25); }
.tile .port-right {
  right: -7px;
  top: 50%;
  transform: translateY(-50%);
}
.tile .port-right:hover { transform: translateY(-50%) scale(1.25); }

.tile .resize-handle {
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 14px;
  height: 14px;
  cursor: nwse-resize;
  z-index: 3;
}
.tile .resize-handle::after {
  content: "";
  position: absolute;
  right: 1px;
  bottom: 1px;
  width: 7px;
  height: 7px;
  border-right: 2px solid rgba(0,0,0,0.28);
  border-bottom: 2px solid rgba(0,0,0,0.28);
  border-radius: 0 0 2px 0;
}

/* Brand */
#brand {
  position: fixed;
  top: 20px;
  left: 24px;
  display: flex;
  align-items: center;
  gap: 9px;
  z-index: 10;
  pointer-events: none;
  user-select: none;
}
.brand-mark {
  width: 20px;
  height: 20px;
  border-radius: 6px;
  background: var(--contrast);
  position: relative;
  flex-shrink: 0;
}
.brand-mark::before {
  content: "";
  position: absolute;
  top: 6px;
  left: 6px;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: var(--bg);
}
.brand-name {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--fg);
}

/* Add buttons */
#add-btn {
  position: fixed;
  right: 28px;
  bottom: 28px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: none;
  background: var(--contrast);
  color: var(--contrast-fg);
  font-size: 28px;
  line-height: 54px;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  z-index: 10;
}
/* Sits directly above the company button: smaller and translucent so the
   two are clearly distinguishable at a glance. */
#add-note-btn {
  position: fixed;
  right: 37px;
  bottom: 92px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--contrast);
  opacity: 0.38;
  color: var(--contrast-fg);
  font-size: 19px;
  line-height: 36px;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow);
  z-index: 10;
  transition: opacity 0.12s ease;
}
#add-note-btn:hover { opacity: 0.65; }

/* Detail panel */
.panel {
  position: fixed;
  top: 24px;
  right: 24px;
  width: 300px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  box-shadow: var(--shadow);
  z-index: 20;
}
.panel .detail-flag { font-size: 30px; }
.panel h2 { margin: 6px 0 2px; font-size: 18px; }
.panel .muted { color: var(--muted); font-size: 13px; margin: 2px 0; }
.panel #detail-info { font-size: 14px; margin-top: 10px; white-space: pre-wrap; }
.icon-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  cursor: pointer;
}
.panel-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 16px;
}
.link-btn {
  border: none;
  background: none;
  color: var(--fg);
  font-size: 13px;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}
.danger-link {
  border: none;
  background: none;
  color: #e05252;
  font-size: 13px;
  cursor: pointer;
  padding: 0;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
}
.modal-box {
  width: 340px;
  background: var(--panel-bg);
  border-radius: 14px;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: var(--shadow);
}
.modal-box h2 { margin: 0 0 4px; font-size: 18px; }
.modal-box label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}
.modal-box input, .modal-box select, .modal-box textarea {
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-size: 14px;
  font-family: inherit;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}
.modal-actions button {
  padding: 9px 16px;
  border-radius: 8px;
  border: none;
  font-size: 14px;
  cursor: pointer;
}
.modal-actions button[type=submit] {
  background: var(--contrast);
  color: var(--contrast-fg);
}
.modal-actions .secondary {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
}

/* Color swatch picker */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field-label {
  font-size: 13px;
  color: var(--muted);
}
.field-hint {
  color: var(--muted);
  opacity: 0.75;
}
.swatch-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 6px;
}
.swatch {
  aspect-ratio: 1;
  border-radius: 6px;
  border: 1px solid rgba(0, 0, 0, 0.08);
  cursor: pointer;
  padding: 0;
  position: relative;
}
.swatch:hover {
  transform: scale(1.12);
}
.swatch.selected {
  box-shadow: 0 0 0 2px var(--panel-bg), 0 0 0 4px var(--contrast);
}
.swatch-grid.disabled {
  opacity: 0.4;
  pointer-events: none;
}

/* Custom country select */
.country-select {
  position: relative;
}
.country-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
}
.country-trigger-placeholder { color: var(--muted); }
.country-trigger.field-error { border-color: #e05252; }
.country-trigger-chevron {
  color: var(--muted);
  font-size: 11px;
  margin-left: 8px;
}
.country-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 40;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.country-search {
  margin: 8px;
  padding: 8px 10px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  font-size: 13px;
  font-family: inherit;
}
.country-options {
  max-height: 220px;
  overflow-y: auto;
  padding: 4px;
}
.country-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 7px;
  font-size: 13px;
  cursor: pointer;
}
.country-option:hover,
.country-option.active {
  background: #f0f0f0;
}
.country-option .flag { font-size: 15px; }
.country-options .no-results {
  padding: 10px;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}
