* { box-sizing: border-box; }
body { margin: 0; font-family: -apple-system, "Segoe UI", Arial, sans-serif; background: #eceff1; color: #263238; overflow: hidden; user-select: none; }
#app { display: grid; grid-template-rows: 44px 1fr; grid-template-columns: 280px 1fr 320px; grid-template-areas: "tb tb tb" "pl cv pr"; height: 100vh; }
#toolbar { grid-area: tb; background: #1f4e79; color: #fff; display: flex; align-items: center; padding: 0 12px; gap: 6px; }
#toolbar h1 { font-size: 15px; font-weight: 700; margin: 0 12px 0 0; letter-spacing: 0.2px; }
#toolbar h1 .dev-by { display: inline-block; margin-left: 14px; padding-left: 14px; border-left: 1px solid rgba(255,255,255,0.25); font-size: 11px; font-weight: 400; color: rgba(255,255,255,0.6); }
#toolbar h1 .author-link { color: #ffc107; text-decoration: none; font-weight: 600; margin-left: 2px; }
#toolbar h1 .author-link:hover { color: #ffd54f; text-decoration: underline; }
#toolbar .sep { width: 1px; height: 22px; background: rgba(255,255,255,0.3); margin: 0 4px; }
#toolbar button { background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.25); color: #fff; padding: 6px 10px; border-radius: 4px; cursor: pointer; font-size: 12px; }
#toolbar button:hover:not(:disabled) { background: rgba(255,255,255,0.22); }
#toolbar button:disabled { opacity: 0.4; cursor: not-allowed; }
#toolbar button.primary { background: #4caf50; border-color: #4caf50; }
#toolbar button.primary:hover { background: #43a047; }
#toolbar button.accent { background: #ff9800; border-color: #ff9800; }
#toolbar button.accent:hover { background: #fb8c00; }
#toolbar .mode-btn.active { background: #ffc107; color: #000; border-color: #ffc107; }
#toolbar #sim-btn.active { background: #4caf50; color: #fff; border-color: #4caf50; }
#toolbar button.tool-on { background: #ffc107; color: #000; border-color: #ffc107; }
#toolbar #sim-btn.active:hover:not(:disabled) { background: #43a047; }
#toolbar .info { margin-left: auto; font-size: 11px; opacity: 0.85; max-width: 350px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#palette { grid-area: pl; background: #fff; border-right: 1px solid #cfd8dc; overflow-y: auto; }
#palette h2 { font-size: 11px; padding: 8px 12px 4px; margin: 0; color: #607d8b; text-transform: uppercase; letter-spacing: 0.6px; }
.palette-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px 8px; }
.palette-item { background: #f5f5f5; border: 1.5px solid transparent; border-radius: 4px; padding: 6px 4px; text-align: center; cursor: pointer; font-size: 9px; line-height: 1.2; }
.palette-item:hover { background: #e3f2fd; border-color: #1f4e79; }
.palette-item.selected { background: #ffe082; border-color: #ff9800; }
.palette-item svg { display: block; margin: 0 auto 3px; }
#palette .group { padding: 4px 0 8px; border-bottom: 1px dashed #e0e0e0; }
#bg-controls { padding: 8px 12px; border-top: 1px solid #cfd8dc; background: #fafafa; }
#bg-controls label { font-size: 11px; display: block; margin-bottom: 4px; color: #455a64; }
#bg-controls input[type=file] { width: 100%; font-size: 11px; }
#bg-controls input[type=range] { width: 100%; }
#bg-controls .row { display: flex; gap: 6px; align-items: center; margin: 4px 0; font-size: 11px; }

#svc-list { padding: 4px 8px; }
.svc-row { display: grid; grid-template-columns: 24px 1fr 24px 22px; gap: 4px; align-items: center; padding: 3px 0; border-bottom: 1px dotted #eceff1; cursor: pointer; }
.svc-row:hover { background: #eef4fb; }
.svc-row.selected { background: #fff3e0; border-radius: 3px; padding-left: 4px; padding-right: 4px; }
.svc-row input[type=color] { width: 24px; height: 24px; padding: 0; border: 1px solid #ccc; border-radius: 3px; cursor: pointer; background: none; }
.svc-row input[type=text] { width: 100%; padding: 3px 6px; font-size: 11px; border: 1px solid transparent; border-radius: 3px; background: transparent; }
.svc-row input[type=text]:hover, .svc-row input[type=text]:focus { background: #fff; border-color: #cfd8dc; outline: none; }
.svc-row button { padding: 0; height: 22px; font-size: 11px; background: #fff; border: 1px solid #cfd8dc; border-radius: 3px; cursor: pointer; color: #455a64; }
.svc-row button:hover { background: #f5f5f5; }
.svc-row .svc-btn-pick { color: #1f4e79; }
.svc-row.selected .svc-btn-pick { background: #ff9800; color: #fff; border-color: #ff9800; }
.svc-row .svc-btn-del { color: #c62828; }
.svc-row .svc-btn-del:hover { background: #ffebee; }

#canvas-wrap { grid-area: cv; background: #fafafa; position: relative; overflow: hidden; }
#canvas { display: block; width: 100%; height: 100%; cursor: default; }
#canvas.mode-add { cursor: crosshair; }
#canvas.mode-pipe { cursor: crosshair; }
#canvas.dragging { cursor: grabbing; }
#hud { position: absolute; left: 10px; bottom: 10px; background: rgba(0,0,0,0.7); color: #fff; padding: 6px 10px; border-radius: 4px; font-size: 11px; pointer-events: none; }
#hud b { color: #ffc107; }

#props { grid-area: pr; background: #fff; border-left: 1px solid #cfd8dc; overflow-y: auto; padding: 12px; }
#props h2 { font-size: 13px; margin: 0 0 8px 0; color: #1f4e79; padding-bottom: 6px; border-bottom: 2px solid #1f4e79; }
#props-tabs { display: flex; gap: 0; margin: -12px -12px 12px -12px; border-bottom: 2px solid #1f4e79; }
#props-tabs .tab-btn { flex: 1; background: #eceff1; border: none; padding: 10px 8px; font-size: 12px; font-weight: 600; color: #607d8b; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -2px; }
#props-tabs .tab-btn:hover { background: #cfd8dc; }
#props-tabs .tab-btn.active { background: #fff; color: #1f4e79; border-bottom-color: #fff; }
.pending-list { display: flex; flex-direction: column; gap: 3px; }
.pending-item { display: grid; grid-template-columns: 1fr auto; gap: 6px; align-items: center; padding: 6px 8px; background: #f5f5f5; border: 1px solid #eceff1; border-radius: 3px; cursor: pointer; font-size: 11px; line-height: 1.3; }
.pending-item:hover { background: #fff3e0; border-color: #ff9800; }
.pending-item.placing { background: #ff9800; color: #fff; border-color: #f57c00; }
.pending-item .pi-tag { font-weight: 600; }
.pending-item .pi-desc { color: #607d8b; font-size: 10px; }
.pending-item .pi-type { font-size: 9px; color: #1f4e79; background: #e3f2fd; padding: 1px 6px; border-radius: 8px; text-align: center; white-space: nowrap; }
#canvas.mode-placing { cursor: crosshair; }
.field { margin-bottom: 8px; }
.field label { display: block; font-size: 11px; color: #455a64; font-weight: bold; margin-bottom: 3px; }
.field input, .field select, .field textarea { width: 100%; padding: 5px 8px; border: 1px solid #cfd8dc; border-radius: 3px; font-size: 12px; font-family: inherit; }
.field textarea { resize: vertical; min-height: 50px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.empty-prop { text-align: center; color: #90a4ae; padding: 30px 10px; font-size: 12px; }
#delete-btn { background: #e53935; color: #fff; border: none; padding: 8px 12px; border-radius: 4px; cursor: pointer; font-size: 12px; width: 100%; margin-top: 12px; }
#delete-btn:hover { background: #c62828; }

.component { cursor: move; }
.component.selected .body { stroke: #ff5722 !important; stroke-width: 2.5px !important; }

/* Estado en operación normal de la válvula (convención petrolera):
   rojo = abierta (flujo activo, "caliente"),
   verde = cerrada (sin flujo, "seguro"),
   amarillo = disponible (control automático / en modo auto),
   blanco/gris claro = no aplica (sin estado asignado). */
.component.state-A .body { fill: #ef5350 !important; }
.component.state-C .body { fill: #66bb6a !important; }
.component.state-D .body { fill: #ffc107 !important; }
.component.state-NA .body { fill: #eceff1 !important; }
.pipe { fill: none; cursor: pointer; stroke-linecap: round; stroke-linejoin: round; }
.pipe-halo { fill: none; pointer-events: none; stroke-linecap: round; stroke-linejoin: round; }
.pipe.selected { stroke-dasharray: 6 3; }
.pipe-label rect { pointer-events: none; }
.pipe-draft { fill: none; pointer-events: none; }
.pipe-seg { pointer-events: stroke; }
.pipe-handle { cursor: move; }
.pipe-handle:hover { stroke: #ff5722; stroke-width: 3; }
.pipe-handle.endpoint { cursor: grab; }
.detected-line { cursor: pointer; }
.detected-line:hover { stroke: #ff5722 !important; stroke-width: 3 !important; opacity: 1 !important; }
.grid line { stroke: #d4dadd; stroke-width: 0.6; }
.grid line.major { stroke: #aab8bf; stroke-width: 0.8; }
.align-guide { stroke: #e91e63; stroke-width: 1; stroke-dasharray: 6 4; pointer-events: none; }

#status-bar { position: absolute; right: 10px; bottom: 10px; background: rgba(255,255,255,0.95); padding: 6px 10px; border-radius: 4px; font-size: 11px; border: 1px solid #cfd8dc; }

/* Modal */
.modal { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 100; align-items: center; justify-content: center; }
.modal-box { background: #fff; max-width: 720px; width: 90%; max-height: 80vh; border-radius: 8px; padding: 20px; display: flex; flex-direction: column; }
.modal-box h2 { margin: 0 0 12px 0; color: #1f4e79; font-size: 16px; }
.modal-box .preview-area { flex: 1; overflow-y: auto; border: 1px solid #cfd8dc; border-radius: 4px; padding: 12px; margin: 12px 0; min-height: 200px; max-height: 50vh; background: #fafafa; }
.modal-box .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.modal-box .actions button { padding: 8px 14px; border-radius: 4px; border: 1px solid #cfd8dc; background: #fff; cursor: pointer; font-size: 12px; font-weight: 600; }
.modal-box .actions button:hover:not(:disabled) { background: #f5f5f5; }
.modal-box .actions button:disabled { opacity: 0.5; cursor: not-allowed; }
.modal-box .actions button.primary { background: #4caf50; color: #fff; border-color: #4caf50; }
.modal-box .actions button.primary:hover:not(:disabled) { background: #43a047; }
.modal-box .actions button.warning { background: #ff9800; color: #fff; border-color: #ff9800; }
.modal-box .actions button.warning:hover:not(:disabled) { background: #fb8c00; }
.modal-box .help-text { font-size: 11.5px; color: #607d8b; line-height: 1.5; }
.modal-box .help-text code { background: #eceff1; padding: 1px 5px; border-radius: 2px; font-size: 11px; }