/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #222240;
  --text: #e8e8f0;
  --text-muted: #8888aa;
  --accent: #4fc3f7;
  --accent-dim: rgba(79, 195, 247, 0.15);
  --danger: #ef5350;
  --success: #66bb6a;
  --radius: 10px;
  --header-h: 52px;
  --floor-sel-h: 44px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.hidden { display: none !important; }

/* ── Auth Screen ── */
.screen { display: none; height: 100%; }
.screen.active { display: flex; }

#auth-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f0f1a 0%, #1a1a3e 100%);
}

.auth-container {
  text-align: center;
  padding: 32px;
  max-width: 320px;
  width: 100%;
}

.auth-icon { font-size: 64px; margin-bottom: 16px; }

.auth-container h1 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.auth-container p {
  color: var(--text-muted);
  margin-bottom: 24px;
  font-size: 14px;
}

#auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#auth-form input {
  padding: 14px 16px;
  border: 1px solid #333;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

#auth-form input:focus { border-color: var(--accent); }

#auth-form button {
  padding: 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #000;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

#auth-form button:active { opacity: 0.8; }

.error {
  color: var(--danger);
  margin-top: 12px;
  font-size: 14px;
}

/* ── App Screen ── */
#app-screen {
  flex-direction: column;
}

/* ── Header ── */
#app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid #2a2a4a;
  flex-shrink: 0;
  z-index: 100;
}

#app-header h1 {
  font-size: 18px;
  font-weight: 600;
}

.header-right { display: flex; gap: 4px; }

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.icon-btn:hover, .icon-btn:active { background: var(--surface2); }
.icon-btn.active { background: var(--accent-dim); color: var(--accent); }

/* ── Floor Selector ── */
#floor-selector {
  height: var(--floor-sel-h);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--surface);
  flex-shrink: 0;
}

.floor-btn {
  flex: 1;
  max-width: 180px;
  padding: 8px 16px;
  border: 1px solid #444;
  border-radius: 20px;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.floor-btn.active {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
  font-weight: 600;
}

/* ── Filter Bar ── */
#filter-bar {
  background: var(--surface2);
  padding: 8px 12px;
  flex-shrink: 0;
  border-bottom: 1px solid #333;
}

.filter-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-content label {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
}

#filter-bar select, #edit-bar select {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid #444;
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  max-width: 250px;
}

.text-btn {
  padding: 6px 12px;
  border: 1px solid #555;
  border-radius: 6px;
  background: transparent;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.text-btn.danger { color: var(--danger); border-color: var(--danger); }

/* ── Edit Bar ── */
#edit-bar {
  background: var(--surface2);
  padding: 8px 12px;
  flex-shrink: 0;
  border-bottom: 2px solid var(--accent);
}

.edit-bar-content {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.edit-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  margin-right: 4px;
}

.edit-instructions {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
}

/* ── Floor Plan ── */
#floorplan-container {
  flex: 1;
  overflow: hidden;
  position: relative;
  touch-action: none;
}

#floorplan-wrapper {
  position: relative;
  display: inline-block;
  min-width: 100%;
  transform-origin: 0 0;
  will-change: transform;
}

#floorplan-img {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
  user-select: none;
}

#icons-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 15;
  overflow: hidden;
}

/* ── Electrical Icons ── */
.e-icon {
  position: absolute;
  width: 28px;
  height: 28px;
  margin-left: -14px;
  margin-top: -14px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  cursor: pointer;
  pointer-events: auto;
  transition: transform 0.15s, box-shadow 0.15s;
  z-index: 10;
  background: var(--surface);
  border: 2px solid var(--accent);
  color: var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}

.e-icon:active { transform: scale(1.3); }
.e-icon.filtered-out { opacity: 0.12; pointer-events: none; }
.e-icon.dimmed { opacity: 0.3; transition: opacity 0.3s ease; }
.e-icon.highlight { 
  animation: pulse 1s ease-in-out infinite;
  z-index: 20;
  box-shadow: 0 0 12px var(--accent);
}

.e-icon.dragging {
  transform: scale(1.4);
  z-index: 50;
  box-shadow: 0 0 16px var(--accent);
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 4px var(--accent); }
  50% { box-shadow: 0 0 16px var(--accent), 0 0 24px rgba(79,195,247,0.3); }
}

/* Icon type colors */
.e-icon[data-type="Light"] { background: #fff3e0; border-color: #ffb300; color: #ff8f00; }
.e-icon[data-type="Outlet"] { background: #e3f2fd; border-color: #42a5f5; color: #1565c0; }
.e-icon[data-type="Switch"] { background: #e8eaf6; border-color: #7e57c2; color: #4527a0; }
.e-icon[data-type="Outlet GFCI"] { background: #e0f2f1; border-color: #26a69a; color: #00695c; }
.e-icon[data-type="220 Outlet"] { background: #fce4ec; border-color: #ef5350; color: #c62828; }
.e-icon[data-type="Fan"] { background: #f3e5f5; border-color: #ab47bc; color: #6a1b9a; }
.e-icon[data-type="Hardline"] { background: #efebe9; border-color: #8d6e63; color: #4e342e; }

/* ── Panels & Overlays ── */
.panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  max-height: 50vh;
  overflow-y: auto;
  z-index: 200;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.5);
  animation: slideUp 0.25s ease-out;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid #333;
  position: sticky;
  top: 0;
  background: var(--surface);
}

.panel-header h2 { font-size: 18px; }

.panel-close {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: var(--surface2);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.panel-body { padding: 16px; }

/* ── Legend ── */
.legend-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid #2a2a3a;
}

.legend-item:last-child { border-bottom: none; }

.legend-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.legend-text {
  font-size: 15px;
  font-weight: 500;
}

.legend-count {
  margin-left: auto;
  font-size: 13px;
  color: var(--text-muted);
  background: var(--surface2);
  padding: 2px 8px;
  border-radius: 10px;
}

/* ── Detail Bottom Sheet ── */
.bottom-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 250;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.5);
  transition: max-height 0.3s ease, transform 0.2s ease;
  max-height: var(--sheet-compact-h, 140px);
  overflow: hidden;
}

.bottom-sheet.expanded {
  max-height: 75vh;
  overflow-y: auto;
}

.sheet-drag-handle {
  display: flex;
  justify-content: center;
  padding: 12px 0 6px;
  cursor: grab;
  touch-action: none;
}

.sheet-drag-handle span {
  width: 40px;
  height: 5px;
  background: #666;
  border-radius: 3px;
}

.sheet-compact {
  display: flex;
  align-items: flex-start;
  padding: 0 16px 12px;
  gap: 12px;
}

.sheet-compact-info {
  flex: 1;
  min-width: 0;
}

.sheet-compact-info h3 {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 4px;
}

.sheet-meta {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.sheet-meta .detail-label {
  color: var(--text-muted);
}

.sheet-close {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: var(--surface2);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
}

.sheet-expanded {
  padding: 8px 16px 24px;
  border-top: 1px solid #2a2a3a;
}

.bottom-sheet:not(.expanded) .sheet-expanded {
  display: none;
}

/* ── Overlays (edit popups) ── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 300;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.15s;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.detail-panel {
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  animation: slideUp 0.25s ease-out;
}

.small-panel { max-height: 60vh; }

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid #333;
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

.detail-header h2 { font-size: 18px; }

.detail-body { padding: 16px; }

.detail-label { color: var(--text-muted); }

.detail-panel-image h3 {
  font-size: 16px;
  margin-bottom: 10px;
  color: var(--text-muted);
}

#panel-image-container {
  position: relative;
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
}

#panel-img {
  width: 100%;
  display: block;
}

#panel-highlight {
  position: absolute;
  border: 3px solid var(--accent);
  background: rgba(79, 195, 247, 0.2);
  border-radius: 4px;
  pointer-events: none;
  transition: all 0.3s ease;
  box-shadow: 0 0 12px rgba(79, 195, 247, 0.5);
}

#other-panel-notice {
  padding: 12px;
  background: var(--surface2);
  border-radius: 8px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 10px;
}

/* ── Edit Item Actions ── */
.edit-item-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.action-btn {
  padding: 14px 20px;
  border: 1px solid #444;
  border-radius: var(--radius);
  background: var(--surface2);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
  text-align: center;
  transition: background 0.15s;
}

.action-btn:active { background: #333; }
.action-btn.primary { background: var(--accent); color: #000; border-color: var(--accent); font-weight: 600; }
.action-btn.danger { color: var(--danger); border-color: var(--danger); }

/* ── Add Type Form ── */
.form-group {
  margin-bottom: 16px;
}

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

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #444;
  border-radius: 6px;
  background: var(--surface2);
  color: var(--text);
  font-size: 16px;
}

.icon-picker {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
}

.icon-option {
  width: 44px;
  height: 44px;
  border: 2px solid #444;
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s;
}

.icon-option.selected { border-color: var(--accent); background: var(--accent-dim); }

.full-width-select {
  width: 100%;
  padding: 12px;
  border: 1px solid #444;
  border-radius: 6px;
  background: var(--surface2);
  color: var(--text);
  font-size: 16px;
}

/* ── Desktop adjustments ── */
@media (min-width: 768px) {
  .e-icon {
    width: 32px;
    height: 32px;
    margin-left: -16px;
    margin-top: -16px;
    font-size: 16px;
  }

  .detail-panel {
    border-radius: 16px;
    margin-bottom: 20px;
  }

  .overlay {
    align-items: center;
  }

  .bottom-sheet {
    max-width: 420px;
    left: auto;
    right: 16px;
    border-radius: 16px 16px 0 0;
  }
}

/* ── Panel Highlight Editor ── */
.panel-editor-panel {
  max-height: 95vh;
  max-width: 600px;
}

.panel-editor-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.panel-editor-toolbar select {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid #444;
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}

.panel-editor-info {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
  min-height: 18px;
}

#panel-editor-container {
  position: relative;
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
  cursor: crosshair;
  touch-action: none;
  user-select: none;
}

#panel-editor-img {
  width: 100%;
  display: block;
  pointer-events: none;
}

#panel-editor-rects {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.pe-ghost-rect {
  position: absolute;
  border: 2px dashed var(--text-muted);
  background: rgba(136, 136, 170, 0.15);
  border-radius: 3px;
}

#panel-editor-active-rect {
  position: absolute;
  border: 3px solid var(--accent);
  background: rgba(79, 195, 247, 0.25);
  border-radius: 4px;
  box-shadow: 0 0 12px rgba(79, 195, 247, 0.5);
  cursor: move;
  touch-action: none;
}

.pe-handle {
  position: absolute;
  width: 16px;
  height: 16px;
  background: var(--accent);
  border: 2px solid #fff;
  border-radius: 50%;
  touch-action: none;
}

.pe-handle-nw { top: -8px; left: -8px; cursor: nw-resize; }
.pe-handle-ne { top: -8px; right: -8px; cursor: ne-resize; }
.pe-handle-sw { bottom: -8px; left: -8px; cursor: sw-resize; }
.pe-handle-se { bottom: -8px; right: -8px; cursor: se-resize; }

.panel-editor-coords {
  margin-top: 8px;
  font-size: 12px;
  font-family: monospace;
  color: var(--text-muted);
  min-height: 18px;
}

/* ── Switch Connection Lines ── */
.lines-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
  overflow: hidden;
}

.switch-line {
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: 6 4;
  fill: none;
}

.switch-line.visible {
  animation: dashFlow 1s linear infinite;
}

@keyframes dashFlow {
  to { stroke-dashoffset: -20; }
}

.e-icon.controlled-highlight {
  animation: controlledPulse 0.8s ease-in-out infinite;
  z-index: 20;
  box-shadow: 0 0 14px var(--success);
  border-color: var(--success) !important;
}

@keyframes controlledPulse {
  0%, 100% { box-shadow: 0 0 4px var(--success); transform: scale(1); }
  50% { box-shadow: 0 0 18px var(--success), 0 0 28px rgba(102,187,106,0.3); transform: scale(1.15); }
}

/* ── Controls List in Detail Popup ── */
.detail-row-header {
  border-bottom: none;
  padding-bottom: 4px;
}

.controls-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 4px 0 8px 0;
}

.controls-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--surface2);
  border-radius: 6px;
  font-size: 14px;
}

.controls-list-item .cl-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  flex-shrink: 0;
  background: var(--surface);
  border: 2px solid var(--accent);
  color: var(--accent);
}

.controls-list-empty {
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
  padding: 4px 0;
}

/* ── Linking Mode ── */
.e-icon.link-candidate {
  cursor: pointer;
  animation: linkBounce 0.6s ease-in-out infinite alternate;
}

.e-icon.link-selected {
  border-color: var(--success) !important;
  box-shadow: 0 0 10px var(--success);
}

.e-icon.link-source {
  border-color: #ffb300 !important;
  box-shadow: 0 0 14px #ffb300;
  transform: scale(1.2);
  z-index: 30;
}

@keyframes linkBounce {
  from { transform: scale(1); }
  to { transform: scale(1.1); }
}
