*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f0f2f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Toolbar ── */
#toolbar {
  background: #0f172a;
  color: #e2e8f0;
  /* Height expands to cover the top safe area (notch) on iOS */
  height: calc(52px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0,0,0,.35);
  z-index: 100;
  position: relative;
}

#toolbar-brand {
  font-size: 15px;
  font-weight: 700;
  color: #f1f5f9;
  margin-right: 8px;
  white-space: nowrap;
  letter-spacing: -0.3px;
}

/* Secondary group: inline on desktop, dropdown on mobile */
#toolbar-secondary {
  display: flex;
  align-items: center;
  gap: 6px;
}

#btn-overflow {
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  letter-spacing: 1px;
  line-height: 1;
}

.sep {
  width: 1px;
  height: 22px;
  background: rgba(255,255,255,.15);
  margin: 0 4px;
}

.btn {
  padding: 5px 12px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background .15s, color .15s;
  white-space: nowrap;
}

.btn-ghost {
  background: rgba(255,255,255,.07);
  color: #cbd5e1;
}
.btn-ghost:hover { background: rgba(255,255,255,.14); color: #f1f5f9; }

.btn-primary {
  background: #4f46e5;
  color: #fff;
}
.btn-primary:hover { background: #4338ca; }

#dirty-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #f59e0b;
  margin-left: 2px;
  display: none;
  flex-shrink: 0;
}
#dirty-dot.on { display: block; }

/* ── Canvas area ── */
#canvas-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: default;
  touch-action: none;
}
#canvas.is-panning  { cursor: grabbing; }
#canvas.is-connecting { cursor: crosshair; }

/* ── SVG elements ── */
.node-body {
  fill: #ffffff;
  stroke: #d1d5db;
  stroke-width: 1.5;
}
.node.selected .node-body {
  stroke: #4f46e5;
  stroke-width: 2;
}

.node-label {
  font-size: 14px;
  font-weight: 600;
  fill: #111827;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
}
.node-label.placeholder { fill: #d1d5db; }

.port {
  fill: #fff;
  stroke: #9ca3af;
  stroke-width: 1.5;
  cursor: crosshair;
  opacity: 0;
  transition: opacity .15s;
}
.node:hover .port { opacity: 1; }
.port:hover { fill: #4f46e5; stroke: #4f46e5; }

.edge {
  fill: none;
  stroke: #94a3b8;
  stroke-width: 2;
  pointer-events: none;
}

#temp-edge {
  fill: none;
  stroke: #4f46e5;
  stroke-width: 2;
  stroke-dasharray: 6 3;
  pointer-events: none;
  visibility: hidden;
}

/* ── Sidebar ── */
#sidebar {
  position: absolute;
  top: 0; right: 0;
  width: 272px;
  height: 100%;
  background: #fff;
  box-shadow: -4px 0 24px rgba(0,0,0,.1);
  transform: translateX(100%);
  transition: transform .22s ease;
  z-index: 50;
  display: flex;
  flex-direction: column;
}
#sidebar.open { transform: translateX(0); }

#sidebar-head {
  padding: 18px 18px 14px;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#sidebar-head h2 { font-size: 15px; font-weight: 600; color: #111827; }

#btn-close-sidebar {
  background: none; border: none;
  cursor: pointer; color: #9ca3af;
  font-size: 22px; line-height: 1;
  padding: 0 2px;
}
#btn-close-sidebar:hover { color: #374151; }

#sidebar-body {
  padding: 18px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; font-weight: 500; color: #374151; }
.field input,
.field select {
  padding: 7px 11px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  color: #111827;
  outline: none;
  background: #fff;
}
.field input:focus,
.field select:focus { border-color: #4f46e5; box-shadow: 0 0 0 2px rgba(79,70,229,.15); }
.field select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%239ca3af' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; padding-right: 30px; cursor: pointer; }

#sidebar-foot {
  padding: 14px 18px;
  border-top: 1px solid #e5e7eb;
  /* Grows with safe-area-inset-bottom so Delete isn't hidden behind home bar */
  padding-bottom: max(14px, env(safe-area-inset-bottom));
}

#btn-delete {
  width: 100%;
  padding: 8px;
  background: #fef2f2;
  color: #dc2626;
  border: 1px solid #fecaca;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}
#btn-delete:hover { background: #fee2e2; }

/* ── Relationship pickers ── */
.rel-picker {
  position: relative;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 6px 8px;
  background: #fff;
  cursor: text;
}
.rel-picker:focus-within {
  border-color: #4f46e5;
  box-shadow: 0 0 0 2px rgba(79,70,229,.15);
}
.rel-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 4px; }
.rel-chips:empty { margin-bottom: 0; }
.rel-chip {
  display: flex;
  align-items: center;
  gap: 3px;
  background: #eef2ff;
  color: #4338ca;
  border-radius: 4px;
  padding: 2px 6px 2px 8px;
  font-size: 12px;
  font-weight: 500;
}
.rel-chip-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: #818cf8;
  font-size: 15px;
  line-height: 1;
  padding: 0 0 0 2px;
}
.rel-chip-remove:hover { color: #4338ca; }
.rel-search {
  border: none;
  outline: none;
  width: 100%;
  font-size: 13px;
  padding: 2px 0;
  background: transparent;
  color: #111827;
}
.rel-search::placeholder { color: #9ca3af; }
.rel-dropdown {
  position: absolute;
  left: -1px; right: -1px;
  top: calc(100% + 4px);
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  list-style: none;
  padding: 4px 0;
  z-index: 300;
  display: none;
  max-height: 150px;
  overflow-y: auto;
}
.rel-dropdown.open { display: block; }
.rel-dropdown li { padding: 7px 12px; font-size: 13px; cursor: pointer; color: #374151; }
.rel-dropdown li:hover { background: #eef2ff; color: #4338ca; }
.rel-dropdown li.no-results { color: #9ca3af; cursor: default; font-style: italic; }
.rel-dropdown li.no-results:hover { background: none; }

/* ── Backdrop (visible on mobile when sidebar is open) ── */
#sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 49;
  cursor: pointer;
}
#sidebar-backdrop.open { display: block; }

/* ── HUD ── */
#zoom-hud {
  position: absolute;
  bottom: max(14px, calc(env(safe-area-inset-bottom) + 8px));
  left: max(14px, calc(env(safe-area-inset-left) + 8px));
  background: rgba(15,23,42,.65);
  color: #e2e8f0;
  font-size: 11px;
  font-weight: 500;
  padding: 4px 10px;
  border-radius: 20px;
  pointer-events: none;
}

/* ── Empty state ── */
#empty-state {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  text-align: center;
  pointer-events: none;
  user-select: none;
  max-width: 90vw;
  padding: 0 16px;
}
#empty-state h2 { font-size: 20px; font-weight: 500; color: #9ca3af; margin-bottom: 6px; }
#empty-state p  { font-size: 14px; color: #c4c9d4; }

/* ─────────────────────────────────────────────
   TABLET  (≤ 768px) — touch target sizes
───────────────────────────────────────────── */
@media (max-width: 768px) {
  /* Minimum 44px touch targets on all interactive elements */
  .btn {
    min-height: 44px;
    padding: 0 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  #btn-close-sidebar {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
  }

  .rel-chip-remove {
    min-width: 32px;
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
  }

  .rel-dropdown li {
    padding: 12px;
  }

  /* Prevent iOS Safari auto-zoom on input focus (requires font-size >= 16px) */
  .field input,
  .field select,
  .rel-search {
    font-size: 16px;
  }

  .field label {
    font-size: 13px;
  }
}

/* ─────────────────────────────────────────────
   PHONE  (≤ 600px) — bottom-sheet sidebar,
   collapsed toolbar overflow menu
───────────────────────────────────────────── */
@media (max-width: 600px) {
  /* ── Toolbar overflow menu ── */
  #toolbar-secondary {
    /* Hide from the toolbar bar; becomes a dropdown panel */
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    background: #0f172a;
    border: 1px solid rgba(255,255,255,.12);
    border-top: none;
    border-radius: 0 0 10px 10px;
    padding: 8px 6px;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    min-width: 160px;
    box-shadow: 0 8px 24px rgba(0,0,0,.5);
    z-index: 200;
  }

  #toolbar-secondary.open {
    display: flex;
  }

  /* Horizontal sep becomes a horizontal rule in the vertical list */
  #toolbar-secondary .sep {
    width: auto;
    height: 1px;
    margin: 4px 6px;
  }

  /* Buttons in the dropdown run full-width, left-aligned */
  #toolbar-secondary .btn {
    width: 100%;
    justify-content: flex-start;
    padding: 0 12px;
    border-radius: 6px;
  }

  /* Show overflow ⋯ button, hide the desktop separator before + Add Person */
  #btn-overflow { display: inline-flex; }
  #sep-primary  { display: none; }

  /* ── Sidebar as bottom sheet ── */
  #sidebar {
    width: 100%;
    height: 72vh;
    top: auto;
    bottom: 0;
    right: 0;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 32px rgba(0,0,0,.2);
    /* Slide up from bottom instead of left */
    transform: translateY(100%);
  }

  #sidebar.open {
    transform: translateY(0);
  }

  /* Visual drag-handle indicator */
  #sidebar-head {
    position: relative;
    padding-top: 22px;
  }

  #sidebar-head::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
  }

  /* Sidebar footer safe area on iPhone home bar */
  #sidebar-foot {
    padding-bottom: max(18px, env(safe-area-inset-bottom));
  }

  /* Backdrop: show it on mobile */
  #sidebar-backdrop {
    display: none;
  }
  #sidebar-backdrop.open {
    display: block;
  }

  /* Compact empty-state text */
  #empty-state h2 { font-size: 17px; }
  #empty-state p  { font-size: 13px; }
}

/* ─────────────────────────────────────────────
   LANDSCAPE PHONES  — compact sidebar
───────────────────────────────────────────── */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  #sidebar {
    width: 300px;
    height: 100%;
    top: 0;
    bottom: auto;
    border-radius: 0;
    transform: translateX(100%);
  }
  #sidebar.open { transform: translateX(0); }

  /* Tighter vertical spacing in landscape sidebar */
  #sidebar-body { gap: 10px; padding: 12px; }
  #sidebar-head { padding: 12px 14px 10px; }
  #sidebar-foot { padding: 10px 14px; }
}
