:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --bg-canvas: #090d16;
  --border-color: #334155;
  --border-highlight: #38bdf8;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent: #0284c7;
  --accent-hover: #0369a1;
  --accent-active: #0ea5e9;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --panel-width: 340px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-canvas);
  color: var(--text-main);
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
}

/* Header Navbar */
.navbar {
  height: 52px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 50;
  flex-shrink: 0;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: -0.02em;
}

.brand-badge {
  background: linear-gradient(135deg, #0284c7, #38bdf8);
  color: #fff;
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 600;
}

.nav-center {
  display: flex;
  align-items: center;
  gap: 12px;
}

.map-selector {
  background: var(--bg-secondary);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.875rem;
  outline: none;
  cursor: pointer;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 500;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background: var(--bg-secondary);
  color: var(--text-main);
  cursor: pointer;
  transition: all 0.15s ease;
  user-select: none;
}

.btn:hover {
  background: var(--bg-tertiary);
  border-color: #475569;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-success {
  background: #059669;
  border-color: #059669;
  color: #fff;
}

.btn-success:hover {
  background: #047857;
}

.btn-danger {
  background: #dc2626;
  border-color: #dc2626;
  color: #fff;
}

.btn-danger:hover {
  background: #b91c1c;
}

.btn.active {
  background: var(--accent);
  border-color: var(--accent-active);
  color: #fff;
}

.btn-sm {
  padding: 4px 8px;
  font-size: 0.75rem;
}

/* Main Workspace */
.workspace {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
}

/* Floating HUD Toolbar in Editor Mode */
.editor-toolbar {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30, 41, 59, 0.95);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
  z-index: 40;
}

.toolbar-sep {
  width: 1px;
  height: 24px;
  background: var(--border-color);
  margin: 0 4px;
}

/* Kiosk Live HUD */
.live-hud {
  position: absolute;
  top: 16px;
  left: 16px;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.85rem;
  z-index: 30;
  box-shadow: 0 4px 15px rgba(0,0,0,0.3);
}

.status-indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 8px var(--success);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(0.9); }
  100% { opacity: 1; transform: scale(1); }
}

/* Canvas Area */
.canvas-container {
  flex: 1;
  height: 100%;
  position: relative;
  overflow: hidden;
  background-color: var(--bg-canvas);
  background-image: radial-gradient(rgba(255, 255, 255, 0.08) 1px, transparent 1px);
  background-size: 20px 20px;
  cursor: grab;
}

.canvas-container.panning {
  cursor: grabbing;
}

.canvas-container.connecting {
  cursor: crosshair;
}

svg#map-svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* SVG Nodes & Links Styling */
.svg-node {
  cursor: move;
  user-select: none;
}

.svg-node.selected rect.node-card {
  stroke: #38bdf8 !important;
  stroke-width: 2.5px !important;
  stroke-dasharray: 4;
}

/* Multi-selection marquee and group bounding box */
.selection-marquee {
  fill: rgba(56, 189, 248, 0.15);
  stroke: #38bdf8;
  stroke-width: 1.5;
  stroke-dasharray: 4 2;
  pointer-events: none;
}

.group-bounding-box {
  fill: rgba(56, 189, 248, 0.05);
  stroke: #38bdf8;
  stroke-width: 1.5;
  stroke-dasharray: 5 4;
  pointer-events: none;
}

.group-bounding-badge-wrap {
  cursor: move;
  user-select: none;
}

.svg-link {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* Hitbox is inactive for mouse cursor on desktop to avoid triggering in empty space */
.svg-link .link-hitbox {
  pointer-events: none;
}

/* Hitbox is active only for touch screens (fingers) */
@media (pointer: coarse), (max-width: 768px) {
  .svg-link .link-hitbox {
    pointer-events: stroke;
  }
}

.svg-link:hover .link-path {
  filter: brightness(1.25);
  stroke-width: 2px;
}

.svg-link.selected .link-path {
  filter: drop-shadow(0 0 6px #38bdf8);
}

.link-handle {
  cursor: pointer;
  fill: #38bdf8;
  stroke: #fff;
  stroke-width: 2;
  transition: r 0.15s ease;
}

.link-handle:hover {
  r: 7;
  fill: #ef4444;
}

/* Properties Sidebar Drawer */
.sidebar {
  width: var(--panel-width);
  background: var(--bg-secondary);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: 45;
  transition: transform 0.2s ease, width 0.2s ease;
  overflow-y: auto;
}

.sidebar.hidden {
  display: none;
}

.sidebar-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-header h3 {
  font-size: 0.95rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.sidebar-content {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.form-control {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.15s ease;
}

.form-control:focus {
  border-color: var(--border-highlight);
}

.form-row {
  display: flex;
  gap: 8px;
}

.form-row .form-group {
  flex: 1;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-backdrop.hidden {
  display: none;
}

.modal-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  width: 90%;
  max-width: 650px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 {
  font-size: 1.1rem;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-primary);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

/* Legend Box */
.weathermap-legend {
  position: absolute;
  bottom: 20px;
  left: 20px;
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.75rem;
  z-index: 30;
  backdrop-filter: blur(6px);
}

.legend-title {
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-muted);
}

.legend-bars {
  display: flex;
  border-radius: 4px;
  overflow: hidden;
  height: 12px;
  width: 220px;
}

.legend-segment {
  flex: 1;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 0.7rem;
}

/* Tooltip & Link Graph Popover */
.link-graph-popover {
  position: fixed;
  z-index: 99999;
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid #334155;
  border-radius: 10px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(56, 189, 248, 0.2);
  backdrop-filter: blur(12px);
  padding: 12px;
  width: 604px;
  max-width: calc(100vw - 32px);
  box-sizing: border-box;
  display: none;
  pointer-events: auto;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.link-graph-popover.visible {
  display: block;
  opacity: 1;
}

.popover-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  border-bottom: 1px solid #334155;
  padding-bottom: 8px;
  gap: 16px;
}

.popover-title-group {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 0.9rem;
  color: #f8fafc;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 340px;
}

.popover-tabs {
  display: flex;
  gap: 3px;
  background: #1e293b;
  padding: 2px;
  border-radius: 6px;
  border: 1px solid #334155;
}

.popover-tab {
  background: transparent;
  border: none;
  color: #94a3b8;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.popover-tab:hover {
  color: #f8fafc;
}

.popover-tab.active {
  background: #0284c7;
  color: #ffffff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.popover-stats {
  display: flex;
  gap: 14px;
  font-size: 0.78rem;
  margin-bottom: 8px;
  align-items: center;
  background: rgba(30, 41, 59, 0.5);
  padding: 6px 10px;
  border-radius: 6px;
}

.stat-badge {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #94a3b8;
}

.stat-badge .stat-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}
.stat-badge.in .stat-dot { background: #10b981; }
.stat-badge.out .stat-dot { background: #38bdf8; }

.stat-badge .stat-val {
  font-weight: 700;
  color: #f8fafc;
}

.popover-graph-wrap {
  position: relative;
  width: 100%;
  height: 228px;
  min-height: 228px;
  background: #090d16;
  border: 1px solid #1e293b;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.popover-graph-img {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 577px;
  max-height: 226px;
  object-fit: contain;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
  transition: opacity 0.15s ease;
}

/* Modern Minimalist Loading Indicator */
.graph-progress-bar {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 2.5px;
  background: transparent;
  overflow: hidden;
  z-index: 10;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.graph-progress-bar.active {
  opacity: 1;
}

.graph-progress-bar::after {
  content: '';
  position: absolute;
  top: 0;
  left: -50%;
  width: 50%;
  height: 100%;
  background: linear-gradient(90deg, transparent, #38bdf8, #818cf8, transparent);
  border-radius: 2px;
  animation: progress-indeterminate 1.2s infinite ease-in-out;
}

@keyframes progress-indeterminate {
  0% { left: -50%; width: 30%; }
  50% { left: 25%; width: 60%; }
  100% { left: 100%; width: 30%; }
}

.graph-spinner-wrap {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(4px);
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid rgba(56, 189, 248, 0.2);
  z-index: 5;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.graph-spinner-wrap.visible {
  opacity: 1;
}

.graph-spinner {
  width: 24px;
  height: 24px;
  animation: spinner-rotate 1s linear infinite;
}

.graph-spinner .spinner-track {
  stroke: rgba(56, 189, 248, 0.15);
}

.graph-spinner .spinner-head {
  stroke: #38bdf8;
  stroke-linecap: round;
  animation: spinner-dash 1.4s ease-in-out infinite;
}

.graph-spinner-wrap .spinner-text {
  color: #94a3b8;
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

@keyframes spinner-rotate {
  100% { transform: rotate(360deg); }
}

@keyframes spinner-dash {
  0% { stroke-dashoffset: 65; }
  50% { stroke-dashoffset: 15; }
  100% { stroke-dashoffset: 65; }
}

/* Node Resize Handles (Edit Mode) */
.node-resize-handle {
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.12s ease, fill 0.12s ease;
}

.node-resize-handle:hover {
  transform: scale(1.35);
  fill: #38bdf8 !important;
  stroke: #ffffff !important;
}

.popover-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.popover-close-btn {
  background: transparent;
  border: 1px solid #334155;
  color: #94a3b8;
  width: 26px;
  height: 26px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  transition: all 0.15s ease;
  user-select: none;
}

.popover-close-btn:hover,
.popover-close-btn:active {
  background: #ef4444;
  border-color: #ef4444;
  color: #ffffff;
}

/* Mobile & Small Screen Adaptations */
@media (max-width: 768px) {
  .navbar {
    height: auto;
    min-height: 52px;
    padding: 8px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .nav-brand {
    font-size: 1rem;
  }

  .nav-center {
    order: 2;
    width: 100%;
    justify-content: space-between;
  }

  .map-selector {
    max-width: 100%;
    flex: 1;
  }

  .nav-actions {
    order: 1;
    margin-left: auto;
    gap: 6px;
  }

  .nav-actions #btn-fullscreen,
  .nav-actions #btn-import-export {
    display: none;
  }

  .live-hud {
    top: 105px;
    left: 10px;
    padding: 6px 10px;
    font-size: 0.75rem;
  }

  .weathermap-legend {
    bottom: 8px;
    right: 8px;
    transform: scale(0.85);
    transform-origin: bottom right;
    opacity: 0.85;
  }
}

@media (max-width: 680px) {
  .link-graph-popover,
  .link-graph-popover.mobile-modal {
    position: fixed !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    bottom: 12px !important;
    top: auto !important;
    width: calc(100vw - 16px) !important;
    max-width: 540px !important;
    max-height: 85vh;
    overflow-y: auto;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.9), 0 0 0 1px rgba(56, 189, 248, 0.4);
    padding: 10px;
  }

  .popover-header {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .popover-header .popover-title-group {
    max-width: 100%;
    justify-content: space-between;
  }

  .popover-header-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
  }

  .popover-tabs {
    flex: 1;
    display: flex;
    justify-content: center;
  }

  .popover-tab {
    flex: 1;
    text-align: center;
    padding: 6px 4px;
    font-size: 0.75rem;
  }

  .popover-close-btn {
    width: 32px;
    height: 32px;
    font-size: 1rem;
    margin-left: 8px;
  }

  .popover-stats {
    flex-wrap: wrap;
    gap: 8px;
    font-size: 0.75rem;
    padding: 6px 8px;
  }

  .popover-stats .stat-badge.max {
    margin-left: 0 !important;
    width: 100%;
  }

  .popover-graph-wrap {
    height: auto;
    min-height: 140px;
    aspect-ratio: 577 / 226;
  }

  .popover-graph-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }

  .weathermap-legend {
    display: none; /* Hide legend on mobile to keep map clean and prevent collision */
  }
}

