/* ============================================================
   EdgeTwin Viewer — Global Styles & Configuration Panel
   ============================================================ */

/* --- Design Tokens --- */
:root {
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

  /* Dark palette */
  --bg-body:        #0b0f1a;
  --bg-panel:       #111827;
  --bg-input:       #1e293b;
  --bg-input-focus: #263246;
  --border-subtle:  #1e293b;
  --border-input:   #334155;
  --border-focus:   #6366f1;

  --text-primary:   #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted:     #64748b;

  --accent:         #6366f1;
  --accent-glow:    rgba(99, 102, 241, 0.25);

  /* Panel dimensions */
  --panel-width: 340px;

  /* Transitions */
  --transition-panel: 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-fast:  0.18s ease;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-family);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-body);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- Three.js Container --- */
#viewerContainer {
  position: fixed;
  inset: 0;
  z-index: 0;
}

/* --- Floating Settings Button --- */
#openConfigBtn {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  background: rgba(17, 24, 39, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text-secondary);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    color var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    transform var(--transition-fast);
}

#openConfigBtn:hover {
  background: rgba(17, 24, 39, 0.92);
  color: var(--text-primary);
  border-color: var(--accent);
  box-shadow: 0 0 14px var(--accent-glow);
  transform: rotate(30deg);
}

#openConfigBtn:active {
  transform: rotate(30deg) scale(0.94);
}

/* Hide the button when the panel is open */
body.panel-open #openConfigBtn,
body.global-panel-open #openConfigBtn {
  opacity: 0;
  pointer-events: none;
}

/* --- Floating Share Button --- */
#shareBtn {
  position: fixed;
  top: 16px;
  right: 70px;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  background: rgba(17, 24, 39, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text-secondary);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    color var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    transform var(--transition-fast);
}

#shareBtn:hover {
  background: rgba(17, 24, 39, 0.92);
  color: var(--text-primary);
  border-color: var(--accent);
  box-shadow: 0 0 14px var(--accent-glow);
  transform: scale(1.05);
}

#shareBtn:active {
  transform: scale(0.94);
}

body.panel-open #shareBtn,
body.global-panel-open #shareBtn {
  opacity: 0;
  pointer-events: none;
}

/* --- Floating Global Settings Button --- */
#openGlobalConfigBtn {
  position: fixed;
  top: 16px;
  right: 124px;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  background: rgba(17, 24, 39, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text-secondary);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    color var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    transform var(--transition-fast);
}

#openGlobalConfigBtn:hover {
  background: rgba(17, 24, 39, 0.92);
  color: var(--text-primary);
  border-color: var(--accent);
  box-shadow: 0 0 14px var(--accent-glow);
  transform: scale(1.05);
}

#openGlobalConfigBtn:active {
  transform: scale(0.94);
}

body.panel-open #openGlobalConfigBtn,
body.global-panel-open #openGlobalConfigBtn {
  opacity: 0;
  pointer-events: none;
}

/* --- Floating Agency Link Button --- */
#agencyBtn {
  position: fixed;
  top: 16px;
  right: 178px;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  background: rgba(17, 24, 39, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text-secondary);
  cursor: pointer;
  text-decoration: none;
  transition:
    background var(--transition-fast),
    color var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast),
    transform var(--transition-fast);
}

#agencyBtn:hover {
  background: rgba(17, 24, 39, 0.92);
  color: var(--text-primary);
  border-color: var(--accent);
  box-shadow: 0 0 14px var(--accent-glow);
  transform: scale(1.05);
}

#agencyBtn:active {
  transform: scale(0.94);
}

body.panel-open #agencyBtn,
body.global-panel-open #agencyBtn {
  opacity: 0;
  pointer-events: none;
}

/* --- Panel Overlay Backdrop --- */
#panelOverlay {
  position: fixed;
  inset: 0;
  z-index: 199;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-panel);
}

body.panel-open #panelOverlay,
body.global-panel-open #panelOverlay {
  opacity: 1;
  pointer-events: auto;
}

/* --- Configuration Panel (Right Drawer) --- */
#configPanel,
#globalConfigPanel {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 200;
  width: var(--panel-width);
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border-left: 1px solid var(--border-subtle);
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.5);
  transform: translateX(100%);
  transition: transform var(--transition-panel);
  overflow: hidden;
}

body.panel-open #configPanel,
body.global-panel-open #globalConfigPanel {
  transform: translateX(0);
}

/* --- Panel Header --- */
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.panel-header h2 {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text-primary);
}

/* --- Panel Header Actions --- */
.panel-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.panel-header-actions button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    color var(--transition-fast),
    box-shadow var(--transition-fast);
}

.panel-header-actions button:hover {
  background: var(--bg-input);
  color: var(--text-primary);
}

#saveConfigBtn:hover,
#loadConfigBtn:hover {
  color: var(--accent);
  box-shadow: 0 0 8px var(--accent-glow);
}

/* --- Toast Notification --- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  z-index: 500;
  padding: 12px 24px;
  border-radius: 10px;
  background: rgba(17, 24, 39, 0.92);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}

.toast.toast-success {
  border-color: #22c55e;
  box-shadow: 0 0 16px rgba(34, 197, 94, 0.2), 0 8px 32px rgba(0, 0, 0, 0.5);
}

.toast.toast-error {
  border-color: #ef4444;
  box-shadow: 0 0 16px rgba(239, 68, 68, 0.2), 0 8px 32px rgba(0, 0, 0, 0.5);
}

/* --- Panel Body --- */
.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

/* Scrollbar styling */
.panel-body::-webkit-scrollbar {
  width: 5px;
}
.panel-body::-webkit-scrollbar-track {
  background: transparent;
}
.panel-body::-webkit-scrollbar-thumb {
  background: var(--border-input);
  border-radius: 10px;
}

/* --- Empty State --- */
.panel-empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
  padding: 40px 20px 20px;
  color: var(--text-muted);
  font-size: 13px;
}

/* --- Sensor List (inside empty state) --- */
.sensor-list {
  width: 100%;
  margin-top: 8px;
}

.sensor-list-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.sensor-button-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sensor-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-input);
  border-radius: 8px;
  background: var(--bg-input);
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast),
    box-shadow var(--transition-fast);
}

.sensor-btn:hover {
  background: var(--bg-input-focus);
  border-color: var(--accent);
  box-shadow: 0 0 10px var(--accent-glow);
}

.sensor-btn:active {
  transform: scale(0.98);
}

.sensor-btn-icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  color: var(--accent);
  opacity: 0.8;
}

.sensor-btn-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sensor-btn-type {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.apply-global-btn {
  margin-top: 10px;
  justify-content: center;
  background: rgba(239, 68, 68, 0.1);
  border-color: rgba(239, 68, 68, 0.3);
  color: #ef4444;
}

.apply-global-btn:hover {
  background: rgba(239, 68, 68, 0.15);
  border-color: #ef4444;
  box-shadow: 0 0 10px rgba(239, 68, 68, 0.2);
}

/* --- Config Form --- */
.config-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

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

.form-group label {
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.form-group input[type="text"],
.form-group input[type="number"] {
  width: 100%;
  padding: 10px 12px;
  font-family: var(--font-family);
  font-size: 14px;
  color: var(--text-primary);
  background: var(--bg-input);
  border: 1px solid var(--border-input);
  border-radius: 8px;
  outline: none;
  transition:
    border-color var(--transition-fast),
    background var(--transition-fast),
    box-shadow var(--transition-fast);
}

.form-group input[type="text"]:focus,
.form-group input[type="number"]:focus {
  border-color: var(--border-focus);
  background: var(--bg-input-focus);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.form-group input[readonly] {
  color: var(--text-muted);
  cursor: default;
  opacity: 0.7;
}

/* Side-by-side row */
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

/* --- Color Picker --- */
.color-picker-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
}

.color-picker-wrapper input[type="color"] {
  -webkit-appearance: none;
  appearance: none;
  width: 40px;
  height: 40px;
  border: 2px solid var(--border-input);
  border-radius: 8px;
  cursor: pointer;
  background: none;
  padding: 2px;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.color-picker-wrapper input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

.color-picker-wrapper input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 5px;
}

.color-picker-wrapper input[type="color"]:hover,
.color-picker-wrapper input[type="color"]:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.color-hex {
  font-size: 12px;
  font-family: 'Courier New', Courier, monospace;
  color: var(--text-muted);
  user-select: all;
}

/* --- Form Hint --- */
.form-hint {
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  font-style: italic;
}

/* --- Model Loading Overlay --- */
#modelOverlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(11, 15, 26, 0.88);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: opacity 0.5s ease, visibility 0.5s ease;
}

#modelOverlay.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.overlay-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  text-align: center;
}

.overlay-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

.overlay-hint {
  font-size: 13px;
  color: var(--text-muted);
  max-width: 280px;
}

/* Spinner */
.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-subtle);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* --- Canvas --- */
#viewerContainer canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* --- Sensor Label (CSS2DObject floating above mesh) --- */
/* Neutral state: always visible, subtle glass appearance */
.sensor-label {
  padding: 6px 14px;
  font-family: var(--font-family);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  background: rgba(17, 24, 39, 0.78);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  transform: translateY(-12px);
  transition:
    background 0.4s ease,
    border-color 0.4s ease,
    color 0.4s ease,
    box-shadow 0.4s ease;
}

/* Small downward caret anchoring the label to the mesh */
.sensor-label::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid rgba(17, 24, 39, 0.78);
  transition: border-top-color 0.4s ease;
}

/* Warning state: red alert appearance with pulse */
.sensor-label.warning {
  color: #fff;
  background: rgba(220, 38, 38, 0.85);
  border-color: rgba(255, 100, 100, 0.5);
  box-shadow:
    0 0 12px rgba(239, 68, 68, 0.4),
    0 4px 16px rgba(0, 0, 0, 0.4);
  animation: sensor-label-pulse 2s ease-in-out infinite;
}

.sensor-label.warning::after {
  border-top-color: rgba(220, 38, 38, 0.85);
}

@keyframes sensor-label-pulse {
  0%, 100% {
    box-shadow:
      0 0 12px rgba(239, 68, 68, 0.4),
      0 4px 16px rgba(0, 0, 0, 0.4);
  }
  50% {
    box-shadow:
      0 0 24px rgba(239, 68, 68, 0.7),
      0 0 48px rgba(239, 68, 68, 0.25),
      0 4px 16px rgba(0, 0, 0, 0.4);
  }
}

/* --- DVR / Live Scrubbing Timeline --- */
#timelineBar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 90;
  padding: 10px 16px;
  pointer-events: none; /* let clicks fall through except on children */
}

.timeline-inner {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 720px;
  margin: 0 auto;
  padding: 8px 16px;
  background: rgba(17, 24, 39, 0.78);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  pointer-events: auto;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.timeline-inner:hover {
  border-color: rgba(99, 102, 241, 0.3);
}

/* Mode badge */
.timeline-badge {
  flex-shrink: 0;
  font-family: var(--font-family);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 3px 10px;
  border-radius: 6px;
  white-space: nowrap;
  user-select: none;
  transition: background 0.25s ease, color 0.25s ease, box-shadow 0.25s ease;
}

.timeline-badge.live {
  color: #34d399;
  background: rgba(52, 211, 153, 0.12);
  box-shadow: 0 0 8px rgba(52, 211, 153, 0.2);
}

.timeline-badge.scrubbing {
  color: #fbbf24;
  background: rgba(251, 191, 36, 0.12);
  box-shadow: 0 0 8px rgba(251, 191, 36, 0.2);
}

/* Scrubbing-mode border accent on the bar */
#timelineBar.scrubbing .timeline-inner {
  border-color: rgba(251, 191, 36, 0.3);
  box-shadow: 0 0 16px rgba(251, 191, 36, 0.08), 0 4px 24px rgba(0, 0, 0, 0.4);
}

/* Timestamp label */
.timeline-timestamp {
  flex-shrink: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 12px;
  color: var(--text-muted);
  min-width: 64px;
  text-align: right;
  user-select: none;
}

/* Range slider */
#timelineSlider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: 3px;
  background: var(--bg-input);
  outline: none;
  cursor: pointer;
  transition: background 0.2s ease;
}

/* Track (WebKit) */
#timelineSlider::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(
    90deg,
    rgba(99, 102, 241, 0.35) 0%,
    var(--bg-input) 100%
  );
}

/* Thumb (WebKit) */
#timelineSlider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid rgba(255, 255, 255, 0.15);
  margin-top: -5px;
  box-shadow: 0 0 8px var(--accent-glow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

#timelineSlider::-webkit-slider-thumb:hover {
  transform: scale(1.25);
  box-shadow: 0 0 14px var(--accent-glow);
}

/* Thumb (Firefox) */
#timelineSlider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid rgba(255, 255, 255, 0.15);
  box-shadow: 0 0 8px var(--accent-glow);
  cursor: pointer;
}

/* Track (Firefox) */
#timelineSlider::-moz-range-track {
  height: 6px;
  border-radius: 3px;
  background: var(--bg-input);
}

/* Hide when panel is open to avoid z-index conflict */
body.panel-open #timelineBar {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

/* --- Share Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 1;
  visibility: visible;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

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

.modal-content {
  background: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 24px;
  max-width: 420px;
  width: 90%;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
  color: var(--text-primary);
  text-align: center;
}

.modal-content h3 {
  margin-bottom: 16px;
  font-size: 18px;
  color: var(--text-primary);
}

.modal-content p {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 16px;
  line-height: 1.5;
}

.modal-content a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}

.modal-content a:hover {
  text-decoration: underline;
}

.modal-close-btn {
  margin-top: 8px;
  padding: 10px 24px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-input);
  border-radius: 8px;
  cursor: pointer;
  font-family: var(--font-family);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.2s, border-color 0.2s, box-shadow 0.2s;
}

.modal-close-btn:hover {
  background: var(--bg-input-focus);
  border-color: var(--accent);
  box-shadow: 0 0 8px var(--accent-glow);
}

/* --- Responsive --- */
@media (max-width: 480px) {
  :root {
    --panel-width: 100%;
  }
}
