/* ==========================================================================
   CSS Variables & Tokens
   ========================================================================== */
:root {
  --bg-dark: #0f172a;
  --bg-panel: rgba(15, 23, 42, 0.7);
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --secondary: #334155;
  --secondary-hover: #475569;

  /* Status Colors */
  --status-offline: #1e293b;
  --status-run: #10b981;
  --status-off: #ef4444;
  --status-overload: #eab308;
  --status-warning: #f97316;
  --status-active-ring: #06b6d4;

  /* Glassmorphism */
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
  --glass-blur: blur(12px);

  /* Fonts */
  --font-main: 'Inter', sans-serif;
  --font-heading: 'Outfit', sans-serif;
}

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

body {
  font-family: var(--font-main);
  background-color: var(--bg-dark);
  color: var(--text-primary);
  overflow: hidden;
  /* Full screen, no scroll by default */
}

/* ==========================================================================
   Layout
   ========================================================================== */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

/* Header */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background: var(--bg-panel);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border-bottom: 1px solid var(--glass-border);
  z-index: 10;
}

.header-title h1 {
  font-family: var(--font-heading);
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: 0.5px;
}

.header-title span {
  font-weight: 400;
  color: var(--text-secondary);
}

.header-actions {
  display: flex;
  gap: 1rem;
}

/* Buttons */
.btn {
  font-family: var(--font-main);
  padding: 0.5rem 1.25rem;
  border-radius: 0.5rem;
  border: none;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0.9rem;
}

.btn-primary {
  background-color: var(--primary);
  color: white;
  box-shadow: 0 4px 14px 0 rgba(59, 130, 246, 0.39);
}

.btn-primary:hover {
  background-color: var(--primary-hover);
  transform: translateY(-1px);
}

.btn-secondary {
  background-color: var(--secondary);
  color: white;
}

.btn-secondary:hover {
  background-color: var(--secondary-hover);
}

.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  cursor: pointer;
  transition: color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

/* ==========================================================================
   Map Area
   ========================================================================== */
.map-container {
  position: relative;
  flex: 1;
  overflow: hidden;
  background-color: #000;
  cursor: default;
}

.map-container.add-mode {
  cursor: crosshair;
}

.map-bg {
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* or contain based on preference */
  opacity: 0.85;
}

.pins-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  /* Let map handle clicks except for pins */
}

/* Pins */
.map-pin {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: auto;
  cursor: pointer;
  border: 2px solid white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  z-index: 5;
}

.map-pin:hover {
  transform: translate(-50%, -50%) scale(1.2);
}

/* Pin Colors */
.map-pin.offline {
  background-color: var(--status-offline);
}

.map-pin.pump-on {
  background-color: var(--status-run);
}

.map-pin.pump-off {
  background-color: var(--status-off);
}

.map-pin.overload {
  background-color: var(--status-overload);
}

.map-pin.warning {
  background-color: var(--status-warning);
}

/* Active Pin Pulse */
.map-pin.active {
  width: 34px;
  height: 34px;
  border: 3px solid #ffffff;
  box-shadow: 0 0 0 3px rgba(6, 182, 212, 0.9), 0 0 20px rgba(6, 182, 212, 0.8);
  animation: pulse-ring 1.5s infinite cubic-bezier(0.215, 0.61, 0.355, 1);
  z-index: 6;
}

@keyframes pulse-ring {
  0%   { box-shadow: 0 0 0 3px rgba(6, 182, 212, 0.9), 0 0 20px rgba(6, 182, 212, 0.8); }
  50%  { box-shadow: 0 0 0 12px rgba(6, 182, 212, 0.2), 0 0 30px rgba(6, 182, 212, 0.5); }
  100% { box-shadow: 0 0 0 3px rgba(6, 182, 212, 0.9), 0 0 20px rgba(6, 182, 212, 0.8); }
}

/* ==========================================================================
   Legend
   ========================================================================== */
.legend-panel {
  position: absolute;
  top: 100px;
  left: 20px;
  background: var(--bg-panel);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: 1px solid var(--glass-border);
  border-radius: 0.75rem;
  padding: 1rem;
  box-shadow: var(--glass-shadow);
  z-index: 10;
  width: 220px;
  transition: height 0.3s ease;
}

.legend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.legend-header h3 {
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
}

.legend-content {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  transition: opacity 0.3s, max-height 0.3s;
  overflow: hidden;
}

.legend-content.collapsed {
  opacity: 0;
  max-height: 0;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.color-box {
  width: 14px;
  height: 14px;
  border-radius: 3px;
}

.color-box.offline {
  background-color: var(--status-offline);
}

.color-box.pump-on {
  background-color: var(--status-run);
}

.color-box.pump-off {
  background-color: var(--status-off);
}

.color-box.overload {
  background-color: var(--status-overload);
}

.color-box.warning {
  background-color: var(--status-warning);
}

/* ==========================================================================
   Draggable Control Popup
   ========================================================================== */
.control-popup {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 380px;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 1rem;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
  z-index: 20;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: opacity 0.2s, transform 0.2s;
}

.control-popup.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(-10px);
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.05);
  border-bottom: 1px solid var(--glass-border);
  cursor: grab;
}

.popup-header:active {
  cursor: grabbing;
}

.popup-title {
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 1.1rem;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.device-selector {
  background: var(--bg-dark);
  color: var(--text-primary);
  border: 1px solid var(--glass-border);
  border-radius: 0.25rem;
  padding: 0.25rem 0.5rem;
  font-family: var(--font-main);
  font-size: 0.85rem;
  outline: none;
  cursor: pointer;
}

.device-selector option.delete-option {
  color: #ef4444;
  font-weight: bold;
}

.popup-body {
  display: flex;
  padding: 1rem;
  gap: 1rem;
  border-bottom: 1px solid var(--glass-border);
}

.panel {
  flex: 1;
  background: rgba(255, 255, 255, 0.02);
  border-radius: 0.5rem;
  padding: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.panel h4 {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 1rem;
  letter-spacing: 0.05em;
}

/* Indicators */
.indicators {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.indicator {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #333;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.5);
  transition: background 0.3s, box-shadow 0.3s;
}

.indicator.green.active {
  background: #10b981;
  box-shadow: 0 0 10px #10b981;
}

.indicator.red.active {
  background: #ef4444;
  box-shadow: 0 0 10px #ef4444;
}

.indicator.yellow.active {
  background: #eab308;
  box-shadow: 0 0 10px #eab308;
}

/* Control Buttons */
.control-buttons {
  display: flex;
  gap: 0.5rem;
}

.btn-action {
  flex: 1;
  padding: 0.75rem 0;
  font-weight: 700;
  font-size: 0.85rem;
  text-align: center;
}

.btn-action.start {
  background-color: #047857;
  color: white;
}

.btn-action.start:active {
  background-color: #065f46;
  transform: scale(0.95);
}

.btn-action.stop {
  background-color: #b91c1c;
  color: white;
}

.btn-action.stop:active {
  background-color: #991b1b;
  transform: scale(0.95);
}

/* Data Panel */
.data-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.data-value {
  font-family: var(--font-heading);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
  text-shadow: 0 0 10px rgba(59, 130, 246, 0.3);
}

/* Footer Stats */
.popup-footer {
  padding: 1rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  font-size: 0.8rem;
  background: rgba(0, 0, 0, 0.2);
}

.stat {
  display: flex;
  justify-content: space-between;
}

.stat .label {
  color: var(--text-secondary);
}

.stat .val {
  font-weight: 600;
  color: var(--text-primary);
}

/* ==========================================================================
   Modals
   ========================================================================== */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 50;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: opacity 0.3s;
}

.modal-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-content {
  background: var(--bg-dark);
  padding: 2rem;
  border-radius: 1rem;
  border: 1px solid var(--glass-border);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
  width: 100%;
  max-width: 400px;
}

.modal-content h2 {
  font-family: var(--font-heading);
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border-radius: 0.5rem;
  border: 1px solid var(--secondary);
  background: rgba(0, 0, 0, 0.2);
  color: var(--text-primary);
  font-family: var(--font-main);
  outline: none;
  transition: border-color 0.2s;
}

.form-group input:focus {
  border-color: var(--primary);
}

.mt-3 {
  margin-top: 1.5rem;
  width: 100%;
}