/* ─── Reset & Base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100vh; overflow: hidden; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #0f172a; color: #e2e8f0; }

/* ─── Sidebar ─────────────────────────────────────────────────── */
.sidebar {
  position: fixed; top: 0; left: 0; z-index: 1000;
  width: 300px; height: 100vh;
  background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
  overflow-y: auto; padding: 16px;
  transition: transform 0.3s ease;
  border-right: 1px solid rgba(148,163,184,0.15);
}
.sidebar:not(.open) { transform: translateX(-300px); }
.sidebar h1 { font-size: 1.4rem; margin-bottom: 4px; }
.sidebar .caption { color: #94a3b8; font-size: 0.78rem; margin-bottom: 12px; }
.sidebar hr { border: none; border-top: 1px solid rgba(148,163,184,0.15); margin: 12px 0; }

.sidebar-section { margin-bottom: 14px; }
.sidebar-section label { display: block; color: #94a3b8; font-size: 0.78rem; margin-bottom: 4px; margin-top: 8px; }
.sidebar-section input[type="password"],
.sidebar-section input[type="text"],
.sidebar-section select {
  width: 100%; padding: 6px 10px; border-radius: 8px;
  border: 1px solid rgba(148,163,184,0.2); background: rgba(30,41,59,0.8);
  color: #e2e8f0; font-size: 0.82rem; outline: none;
}
.sidebar-section input:focus, .sidebar-section select:focus { border-color: #3b82f6; }
.sidebar-section summary {
  cursor: pointer; color: #e2e8f0; font-size: 0.9rem; font-weight: 600;
  padding: 6px 0; user-select: none;
}
.sidebar-section h4 { font-size: 0.9rem; margin-bottom: 8px; }

.overlay-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }
.overlay-grid label { display: flex; align-items: center; gap: 6px; font-size: 0.8rem; color: #e2e8f0; cursor: pointer; }
.overlay-grid input[type="checkbox"] { accent-color: #3b82f6; }

#question-buttons { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; max-height: 260px; overflow-y: auto; }
#question-buttons button {
  background: rgba(59,130,246,0.15); border: 1px solid rgba(59,130,246,0.3);
  color: #93c5fd; border-radius: 8px; font-size: 0.75rem; padding: 6px 8px;
  cursor: pointer; text-align: left; transition: all 0.2s; white-space: normal;
}
#question-buttons button:hover { background: rgba(59,130,246,0.3); border-color: #3b82f6; }

.btn-primary {
  background: #3b82f6; color: white; border: none; border-radius: 8px;
  padding: 8px 16px; cursor: pointer; font-size: 0.85rem; font-weight: 600;
  transition: background 0.2s;
}
.btn-primary:hover { background: #2563eb; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  background: rgba(59,130,246,0.15); border: 1px solid rgba(59,130,246,0.3);
  color: #93c5fd; border-radius: 8px; padding: 8px 16px; cursor: pointer;
  font-size: 0.78rem; width: 100%; transition: all 0.2s;
}
.btn-secondary:hover { background: rgba(59,130,246,0.3); border-color: #3b82f6; }

/* ─── Sidebar Toggle ──────────────────────────────────────────── */
.sidebar-toggle {
  position: fixed; top: 12px; left: 12px; z-index: 1001;
  background: rgba(15,23,42,0.9); backdrop-filter: blur(8px);
  border: 1px solid rgba(148,163,184,0.2); color: #e2e8f0;
  width: 36px; height: 36px; border-radius: 8px; cursor: pointer;
  font-size: 1.1rem; display: flex; align-items: center; justify-content: center;
  transition: left 0.3s ease;
}
.sidebar.open ~ .sidebar-toggle { left: 312px; }

/* ─── Main Content ────────────────────────────────────────────── */
.main-content {
  display: flex; height: 100vh; width: 100%;
  transition: margin-left 0.3s ease;
}
.main-content.sidebar-open { margin-left: 300px; width: calc(100% - 300px); }

.map-column { flex: 3; position: relative; height: 100vh; }
#map { width: 100%; height: 100%; z-index: 1; }

.polygon-controls {
  position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
  z-index: 500; background: rgba(15,23,42,0.92); backdrop-filter: blur(12px);
  border-radius: 12px; border: 1px solid rgba(148,163,184,0.15);
  padding: 10px 16px; display: flex; align-items: center; gap: 10px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
.polygon-controls span { color: #94a3b8; font-size: 0.82rem; }
.hidden { display: none !important; }

/* ─── Right Panel ─────────────────────────────────────────────── */
.right-panel {
  flex: 2; height: 100vh; display: flex; flex-direction: column;
  background: rgba(15,23,42,0.95); border-left: 1px solid rgba(148,163,184,0.1);
}

.tabs {
  display: flex; border-bottom: 1px solid rgba(148,163,184,0.15);
  flex-shrink: 0;
}
.tab {
  flex: 1; padding: 10px; background: transparent; border: none;
  color: #94a3b8; font-size: 0.85rem; cursor: pointer;
  border-bottom: 2px solid transparent; transition: all 0.2s;
}
.tab.active { color: #e2e8f0; border-bottom-color: #3b82f6; background: rgba(59,130,246,0.08); }
.tab:hover { color: #e2e8f0; }

.tab-content { display: none; flex: 1; overflow-y: auto; flex-direction: column; }
.tab-content.active { display: flex; }

/* ─── Chat ────────────────────────────────────────────────────── */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 12px;
  display: flex; flex-direction: column; gap: 10px;
}
.welcome-message {
  background: rgba(59,130,246,0.08); border-radius: 12px; padding: 16px;
  border: 1px solid rgba(59,130,246,0.15); font-size: 0.88rem; line-height: 1.5;
}
.welcome-message ul { margin: 8px 0; padding-left: 20px; }
.welcome-message li { margin: 4px 0; }

.chat-bubble {
  max-width: 90%; padding: 10px 14px; border-radius: 12px;
  font-size: 0.88rem; line-height: 1.5; word-wrap: break-word;
}
.chat-bubble.user {
  align-self: flex-end;
  background: #3b82f6; color: white; border-bottom-right-radius: 4px;
}
.chat-bubble.assistant {
  align-self: flex-start;
  background: rgba(30,41,59,0.9); color: #e2e8f0;
  border: 1px solid rgba(148,163,184,0.15); border-bottom-left-radius: 4px;
}
.chat-bubble.assistant p { margin-bottom: 8px; }
.chat-bubble.assistant p:last-child { margin-bottom: 0; }
.chat-bubble.assistant strong { color: #93c5fd; }
.chat-bubble.assistant ul, .chat-bubble.assistant ol { padding-left: 18px; margin-bottom: 8px; }
.chat-bubble.assistant li { margin-bottom: 4px; }
.chat-bubble.assistant code { background: rgba(0,0,0,0.3); padding: 1px 5px; border-radius: 4px; font-size: 0.82rem; }

.chat-bubble.loading {
  align-self: flex-start;
  background: rgba(30,41,59,0.9); color: #94a3b8;
  border: 1px solid rgba(148,163,184,0.15); border-bottom-left-radius: 4px;
}
.loading-dots span { animation: blink 1.4s infinite both; }
.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; } 40% { opacity: 1; } }

.chat-bubble.error {
  align-self: flex-start;
  background: rgba(239,68,68,0.15); color: #fca5a5;
  border: 1px solid rgba(239,68,68,0.3); border-bottom-left-radius: 4px;
}

.chat-input-container {
  display: flex; padding: 10px 12px; gap: 8px;
  border-top: 1px solid rgba(148,163,184,0.15); flex-shrink: 0;
}
#chat-input {
  flex: 1; padding: 10px 14px; border-radius: 10px;
  border: 1px solid rgba(148,163,184,0.2); background: rgba(30,41,59,0.8);
  color: #e2e8f0; font-size: 0.88rem; outline: none;
}
#chat-input:focus { border-color: #3b82f6; }
#chat-send { flex-shrink: 0; }

/* ─── Charts & Data Tabs ──────────────────────────────────────── */
#chart-container, #data-container { padding: 12px; }
.placeholder-text { color: #64748b; font-size: 0.88rem; text-align: center; padding: 40px 0; }

#data-container pre {
  background: rgba(30,41,59,0.8); border: 1px solid rgba(148,163,184,0.15);
  border-radius: 8px; padding: 12px; font-size: 0.75rem; color: #94a3b8;
  overflow-x: auto; margin-bottom: 10px; white-space: pre-wrap;
}
#data-container details {
  margin-bottom: 8px; background: rgba(30,41,59,0.5);
  border: 1px solid rgba(148,163,184,0.1); border-radius: 8px;
}
#data-container details summary {
  cursor: pointer; padding: 8px 12px; font-size: 0.82rem; color: #93c5fd;
}

/* ─── Leaflet Overrides ───────────────────────────────────────── */
.leaflet-control-layers { background: rgba(15,23,42,0.92) !important; color: #e2e8f0 !important; border: 1px solid rgba(148,163,184,0.2) !important; border-radius: 8px !important; backdrop-filter: blur(8px); }
.leaflet-control-layers label { color: #e2e8f0 !important; }
.leaflet-control-layers-separator { border-color: rgba(148,163,184,0.2) !important; }

/* ─── Scrollbar ───────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(148,163,184,0.3); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(148,163,184,0.5); }

/* ─── Responsive ──────────────────────────────────────────────── */
@media (max-width: 900px) {
  .sidebar { width: 260px; }
  .main-content.sidebar-open { margin-left: 260px; width: calc(100% - 260px); }
  .sidebar.open ~ .sidebar-toggle { left: 272px; }
  .main-content { flex-direction: column; }
  .map-column { flex: none; height: 50vh; }
  .right-panel { flex: none; height: 50vh; }
}
