:root {
  --bg: #0b1220;
  --bg-panel: #121a2b;
  --grid-line: #1c2740;
  --text: #e8edf7;
  --text-dim: #8b98b5;
  --border: #263353;

  --c-source: #f5a623;
  --c-storage: #34d399;
  --c-distribution: #38bdf8;
  --c-bridge: #c084fc;
  --c-load: #60a5fa;

  --line-color: #3b4a72;
}

* { box-sizing: border-box; }

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

#app-root {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 18, 32, 0.85);
  backdrop-filter: blur(6px);
  flex-shrink: 0;
  z-index: 10;
}

.topbar h1 {
  font-size: 1.3rem;
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.bolt { color: var(--c-source); }

.view-toggle {
  display: flex;
  gap: 8px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
}

.toggle-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}

.toggle-btn:hover { color: var(--text); }

.toggle-btn.active {
  background: var(--c-distribution);
  color: #04121f;
}

main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.view { display: none; flex: 1; min-height: 0; flex-direction: column; }
.view.active { display: flex; }

#view-plan.active { padding: 28px; overflow: auto; }

/* ---------------- Modal (add device / add cable forms) ---------------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
  z-index: 40;
  padding: 20px;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-box {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 22px;
  width: min(420px, 100%);
  max-height: 90vh;
  overflow-y: auto;
}

.modal-box h2 {
  font-size: 1.05rem;
  margin: 0 0 14px;
}

.form-group {
  margin-bottom: 14px;
}

.form-group label {
  display: block;
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group select,
.form-group textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.85rem;
  font-family: inherit;
  box-sizing: border-box;
  resize: vertical;
}

.form-group.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
}

.form-group.checkbox label { margin-bottom: 0; }

.form-row {
  display: flex;
  gap: 10px;
}
.form-row .form-group { flex: 1; }

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

.btn-primary, .btn-secondary, .btn-danger {
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 0.85rem;
  cursor: pointer;
  border: 1px solid var(--border);
}

.btn-primary {
  background: var(--c-distribution);
  border-color: var(--c-distribution);
  color: #04121f;
  font-weight: 600;
}

.btn-secondary {
  background: transparent;
  color: var(--text-dim);
}

.btn-danger {
  background: transparent;
  border-color: #7f1d1d;
  color: #f87171;
  margin-top: 12px;
}

.btn-danger:hover { background: rgba(248,113,113,0.1); }

.file-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

.file-row .file-name {
  flex: 1;
  font-size: 0.82rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-small {
  padding: 5px 10px;
  font-size: 0.75rem;
  margin-top: 0;
  white-space: nowrap;
}

/* Sélecteur de fichier natif masqué visuellement mais toujours fonctionnel
   (le <label for="..."> associé déclenche le sélecteur de fichier). */
.file-input-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.form-group label.file-trigger {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: auto;
  color: var(--text);
  margin-bottom: 0;
  cursor: pointer;
}

.form-group input[type="color"] {
  width: 100%;
  height: 38px;
  padding: 3px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}

.photo-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.photo-thumb-wrap {
  position: relative;
  width: 76px;
  height: 76px;
}

.photo-thumb {
  width: 76px;
  height: 76px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  display: block;
}

.photo-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: #7f1d1d;
  color: #fff;
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.connect-hint {
  font-size: 0.8rem;
  color: var(--c-distribution);
  margin-bottom: 4px;
}

/* ---------------- Toolbar ---------------- */

.canvas-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  flex-shrink: 0;
  padding: 14px 20px;
}

.toolbar-hint {
  color: var(--text-dim);
  font-size: 0.8rem;
  margin-right: auto;
}

.canvas-status {
  display: none;
  color: #fbbf24;
  font-size: 0.8rem;
  font-weight: 600;
  width: 100%;
  order: 10;
}

.canvas-status.active {
  display: block;
}

.toolbar-btn {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.82rem;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.toolbar-btn:hover {
  border-color: #3d4d78;
  background: #16213a;
}

.export-box {
  width: 100%;
  min-height: 220px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  resize: vertical;
}

/* ---------------- Wiring view ---------------- */

/* Fenêtre plein écran, sans scroll natif : le pan/zoom est géré à la main via
   une transformation CSS appliquée à #wiring-world (voir .wiring-world). */
.wiring-canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  width: 100%;
  overflow: hidden;
  background: var(--bg-panel);
  cursor: grab;
  touch-action: none;
}

.wiring-canvas-wrap.panning { cursor: grabbing; }

/* Grand plan de travail virtuel, déplacé/zoomé via transform. Toutes les
   coordonnées des appareils/câbles vivent dans cet espace "monde", en px. */
.wiring-world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.wiring-svg {
  position: absolute;
  top: -4000px;
  left: -4000px;
  display: block;
}

.cable-line {
  transition: stroke 0.12s ease;
}

.cable-line-hit {
  stroke: transparent;
  cursor: pointer;
}

.cable-line-hit:hover + .cable-line {
  stroke: #f8fafc;
}

.wiring-nodes {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  overflow: visible;
  pointer-events: none;
}

.device-node {
  position: absolute;
  pointer-events: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  cursor: grab;
  touch-action: none;
  transition: box-shadow 0.12s ease, border-color 0.12s ease;
}

.device-node:hover {
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
  border-color: #3d4d78;
}

.device-node.dragging {
  cursor: grabbing;
  z-index: 5;
  box-shadow: 0 10px 28px rgba(0,0,0,0.5);
  border-color: var(--c-distribution);
}

.device-node.connect-source {
  border-color: #fbbf24;
  box-shadow: 0 0 0 3px rgba(251,191,36,0.25);
}

.device-node.selected {
  border-color: var(--c-distribution);
  box-shadow: 0 0 0 3px rgba(56,189,248,0.3);
}

.resize-handle {
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: var(--bg);
  border: 2px solid var(--c-distribution);
  cursor: nwse-resize;
  touch-action: none;
  pointer-events: auto;
  z-index: 4;
}

.device-node.virtual .resize-handle { display: none; }

.cable-line-hit.selected + .cable-line {
  stroke: var(--c-distribution);
  filter: drop-shadow(0 0 3px rgba(56,189,248,0.8));
}

.connect-anchor-marker {
  position: absolute;
  width: 12px;
  height: 12px;
  margin-left: -6px;
  margin-top: -6px;
  border-radius: 50%;
  background: #fbbf24;
  box-shadow: 0 0 0 4px rgba(251, 191, 36, 0.3);
  pointer-events: none;
  z-index: 6;
}

.waypoint-handle {
  position: absolute;
  pointer-events: auto;
  width: 14px;
  height: 14px;
  margin-left: -7px;
  margin-top: -7px;
  background: var(--bg);
  border: 2px solid #f8fafc;
  border-radius: 3px;
  transform: rotate(45deg);
  cursor: grab;
  touch-action: none;
}

.waypoint-handle:hover { background: var(--c-distribution); }
.waypoint-handle.dragging { cursor: grabbing; background: var(--c-distribution); z-index: 6; }

/* Coude automatique (routage horizontal/vertical), pas encore un vrai point d'angle. */
.waypoint-handle.auto {
  border-style: dashed;
  border-color: var(--text-dim);
  opacity: 0.65;
}

#wiring-connect-toggle.active,
#wiring-waypoint-toggle.active {
  background: var(--c-distribution);
  color: #04121f;
  border-color: var(--c-distribution);
}

.device-node .device-icon {
  font-size: 1.3rem;
  flex-shrink: 0;
}

.device-node .device-visual {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.device-node .device-image {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 6px;
  display: block;
}

.device-node .device-name {
  font-weight: 600;
  font-size: 0.82rem;
  line-height: 1.2;
}

.device-node.image-only {
  padding: 6px;
  justify-content: center;
}

.device-node.image-only .device-visual {
  width: 100%;
  height: 100%;
}

.device-image-preview {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.device-node.virtual {
  border-radius: 50%;
  justify-content: center;
  border-style: dashed;
  background: var(--bg-panel);
}

.device-node.virtual .device-icon {
  font-size: 1.1rem;
}

/* ---------------- Plan view ---------------- */

.van-plan-wrap {
  display: flex;
  justify-content: center;
}

.van-plan {
  position: relative;
  width: 100%;
  max-width: 900px;
  aspect-ratio: 16 / 9;
}

.van-outline {
  position: absolute;
  inset: 0;
  background: var(--bg-panel);
  border: 2px solid var(--border);
  border-radius: 60px 20px 20px 60px;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.02);
}

.van-cab {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 14%;
  border-right: 2px dashed var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  writing-mode: vertical-rl;
  color: var(--text-dim);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  border-radius: 60px 0 0 60px;
}

.hotspot {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  cursor: grab;
  touch-action: none;
  padding: 0;
  color: var(--text);
}

.hotspot.dragging {
  cursor: grabbing;
  z-index: 5;
}

.hotspot.dragging .hotspot-dot {
  transform: scale(1.25);
  box-shadow: 0 0 0 4px rgba(255,255,255,0.15);
}

.hotspot .hotspot-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.6);
  box-shadow: 0 0 0 4px rgba(255,255,255,0.06);
  transition: transform 0.12s ease;
}

.hotspot:hover .hotspot-dot { transform: scale(1.25); }

.hotspot.cat-source .hotspot-dot { background: var(--c-source); }
.hotspot.cat-storage .hotspot-dot { background: var(--c-storage); }
.hotspot.cat-distribution .hotspot-dot { background: var(--c-distribution); }
.hotspot.cat-bridge .hotspot-dot { background: var(--c-bridge); }
.hotspot.cat-load .hotspot-dot { background: var(--c-load); }

.hotspot-label {
  font-size: 0.68rem;
  background: rgba(11, 18, 32, 0.85);
  padding: 2px 6px;
  border-radius: 6px;
  white-space: nowrap;
  border: 1px solid var(--border);
}

.plan-note {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.8rem;
  margin-top: 16px;
}

/* ---------------- Detail panel ---------------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 20;
}

.overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.detail-panel {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: min(460px, 92vw);
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 30;
  padding: 24px;
  overflow-y: auto;
}

.detail-panel.active { transform: translateX(0); }

.detail-close {
  position: absolute;
  top: 18px;
  right: 18px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 0.9rem;
}

.detail-close:hover { color: var(--text); border-color: #3d4d78; }

.detail-title {
  font-size: 1.15rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0 6px;
}

.detail-summary {
  color: var(--text-dim);
  font-size: 0.88rem;
  margin-bottom: 20px;
  line-height: 1.4;
}

.component-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 10px;
}

.component-name {
  font-weight: 600;
  font-size: 0.9rem;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.component-qty {
  color: var(--c-distribution);
  font-weight: 700;
  font-size: 0.82rem;
  white-space: nowrap;
}

.component-model {
  color: var(--text-dim);
  font-size: 0.8rem;
  margin-top: 2px;
}

.component-note {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 6px;
  font-style: italic;
}

@media (max-width: 900px) {
  .schema-container { flex-direction: column; gap: 24px; }
  .col-bridge { justify-content: flex-start; }
}

/* ---------------- Login screen ---------------- */

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: min(340px, 100%);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px 28px;
  text-align: center;
}

.login-icon {
  font-size: 2rem;
  margin-bottom: 10px;
}

.login-title {
  font-size: 1.1rem;
  margin: 0 0 8px;
}

.login-hint {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin: 0 0 18px;
}

.login-pin-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 1.3rem;
  letter-spacing: 0.3em;
  text-align: center;
}

.login-error {
  color: #f87171;
  font-size: 0.82rem;
  min-height: 1.2em;
  margin: 10px 0 0;
}

.login-submit {
  width: 100%;
  margin-top: 14px;
}
