@import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,300;0,9..40,500;0,9..40,700;1,9..40,400&family=Space+Mono:wght@400;700&display=swap');

:root {
  --bg: #0a0c10;
  --surface: #12151c;
  --surface2: #1a1e28;
  --border: #252a36;
  --text: #e8eaf0;
  --text-dim: #8892a4;
  --accent: #f0c040;
  --accent2: #e87040;
  --accent3: #40b8f0;
  --accent4: #60d0a0;
  --red: #f05060;
  --radius: 12px;
}

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

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ── Header ─────────────────────────────────── */
.app-header {
  padding: 16px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(240,192,64,0.04) 0%, transparent 100%);
  position: sticky; top: 0; z-index: 100;
}

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

.logo-icon {
  width: 38px; height: 38px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}

.logo h1 {
  font-family: 'Space Mono', monospace;
  font-size: 17px; font-weight: 700; letter-spacing: -0.5px;
}
.logo h1 span { color: var(--accent); }

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

.nav-tabs { display: flex; gap: 4px; }

.nav-tab {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  padding: 7px 16px;
  border-radius: 8px;
  font-family: 'DM Sans', sans-serif;
  font-size: 13px; font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.nav-tab:hover { color: var(--text); background: var(--surface); }
.nav-tab.active {
  background: var(--surface);
  color: var(--accent);
  border-color: var(--border);
}

.ai-badge {
  display: flex; align-items: center; gap: 6px;
  background: rgba(240,192,64,0.1);
  border: 1px solid rgba(240,192,64,0.25);
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 11px; font-weight: 500; color: var(--accent);
}

.ai-badge .dot {
  width: 6px; height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 2s infinite;
}

@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.3} }

/* ── Layout ─────────────────────────────────── */
.main-layout {
  display: grid;
  grid-template-columns: 1fr 360px;
  height: calc(100vh - 65px);
}

.content-area {
  overflow-y: auto;
  padding: 20px 24px;
}

.content-area::-webkit-scrollbar { width: 5px; }
.content-area::-webkit-scrollbar-track { background: transparent; }
.content-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Page views ─────────────────────────────── */
.page { display: none; }
.page.active { display: block; }

/* ── KPI Cards ──────────────────────────────── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px; margin-bottom: 20px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  position: relative; overflow: hidden;
  transition: border-color 0.3s;
}
.kpi-card:hover { border-color: var(--accent); }

.kpi-card::before {
  content: ''; position: absolute;
  top: 0; left: 0; width: 100%; height: 3px;
}
.kpi-card:nth-child(1)::before { background: var(--accent); }
.kpi-card:nth-child(2)::before { background: var(--accent2); }
.kpi-card:nth-child(3)::before { background: var(--accent3); }
.kpi-card:nth-child(4)::before { background: var(--accent4); }

.kpi-label {
  font-size: 10px; text-transform: uppercase;
  letter-spacing: 1px; color: var(--text-dim); margin-bottom: 6px;
}
.kpi-value {
  font-family: 'Space Mono', monospace;
  font-size: 24px; font-weight: 700; line-height: 1;
}
.kpi-sub { font-size: 11px; color: var(--text-dim); margin-top: 5px; }

/* ── Filters ────────────────────────────────── */
.filter-bar {
  display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap;
}

.filter-select {
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); padding: 7px 12px; border-radius: 8px;
  font-family: 'DM Sans', sans-serif; font-size: 12px;
  cursor: pointer; transition: border-color 0.2s;
}
.filter-select:focus { outline: none; border-color: var(--accent); }
.filter-select option { background: var(--surface); }

/* ── Sections ───────────────────────────────── */
.section-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden; margin-bottom: 20px;
}

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

.section-title {
  font-size: 13px; font-weight: 700;
  display: flex; align-items: center; gap: 8px;
}
.section-title .icon { font-size: 15px; }
.section-subtitle { font-size: 11px; color: var(--text-dim); }

/* ── Map ────────────────────────────────────── */
#map { height: 460px; background: var(--bg); }

.map-legend {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 10px 18px; border-top: 1px solid var(--border);
  background: rgba(0,0,0,0.2);
}

.legend-item {
  display: flex; align-items: center; gap: 4px;
  font-size: 10px; color: var(--text-dim);
  padding: 2px 7px; border-radius: 4px;
}
.legend-dot { width: 7px; height: 7px; border-radius: 50%; }

/* ── Charts ─────────────────────────────────── */
.charts-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 14px; margin-bottom: 20px;
}

.chart-body { padding: 14px; height: 300px; position: relative; }
.chart-body canvas { max-height: 100%; }

/* ── Table ──────────────────────────────────── */
.table-scroll { overflow-x: auto; max-height: 340px; overflow-y: auto; }

.data-table { width: 100%; border-collapse: collapse; font-size: 12px; }

.data-table th {
  background: var(--surface2); padding: 9px 12px;
  text-align: left; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.5px; font-size: 10px; color: var(--text-dim);
  position: sticky; top: 0; z-index: 1; cursor: pointer;
}
.data-table th:hover { color: var(--accent); }

.data-table td {
  padding: 7px 12px; border-bottom: 1px solid var(--border);
}
.data-table tr:hover td { background: rgba(240,192,64,0.03); }

.data-table .num {
  font-family: 'Space Mono', monospace;
  text-align: right; font-size: 11px;
}

/* ── Upload page ────────────────────────────── */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 48px 32px;
  text-align: center;
  transition: all 0.3s;
  cursor: pointer;
  background: var(--surface);
}
.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--accent);
  background: rgba(240,192,64,0.04);
}

.upload-zone .icon { font-size: 48px; margin-bottom: 16px; display: block; }
.upload-zone h3 { font-size: 16px; margin-bottom: 8px; }
.upload-zone p { font-size: 13px; color: var(--text-dim); margin-bottom: 16px; }

.upload-btn {
  background: var(--accent); color: var(--bg);
  border: none; border-radius: 8px;
  padding: 10px 28px; font-family: 'DM Sans', sans-serif;
  font-size: 14px; font-weight: 700; cursor: pointer;
  transition: transform 0.15s;
}
.upload-btn:hover { transform: scale(1.03); }
.upload-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.col-spec {
  background: var(--surface2); border-radius: 8px;
  padding: 16px 20px; margin-top: 20px; text-align: left;
}
.col-spec h4 { font-size: 13px; margin-bottom: 10px; color: var(--accent); }
.col-spec .cols {
  display: flex; flex-wrap: wrap; gap: 6px;
}
.col-tag {
  background: var(--bg); border: 1px solid var(--border);
  padding: 4px 10px; border-radius: 6px; font-size: 11px;
  font-family: 'Space Mono', monospace; color: var(--accent3);
}

.upload-progress {
  display: none; margin-top: 20px;
}
.upload-progress.active { display: block; }

.progress-bar {
  width: 100%; height: 6px; background: var(--surface2);
  border-radius: 3px; overflow: hidden; margin: 12px 0;
}
.progress-fill {
  height: 100%; background: var(--accent);
  border-radius: 3px; transition: width 0.3s;
  width: 0%;
}

.upload-result {
  background: var(--surface2); border-radius: 8px;
  padding: 16px 20px; margin-top: 16px; text-align: left;
}
.upload-result.success { border-left: 3px solid var(--accent4); }
.upload-result.error { border-left: 3px solid var(--red); }

/* ── History table ──────────────────────────── */
.batch-history { margin-top: 24px; }

.status-badge {
  padding: 3px 8px; border-radius: 4px; font-size: 10px;
  font-weight: 600; text-transform: uppercase;
}
.status-badge.completed { background: rgba(96,208,160,0.15); color: var(--accent4); }
.status-badge.error { background: rgba(240,80,96,0.15); color: var(--red); }
.status-badge.processing { background: rgba(64,184,240,0.15); color: var(--accent3); }

.btn-delete {
  background: rgba(240,80,96,0.1); border: 1px solid rgba(240,80,96,0.3);
  color: var(--red); border-radius: 6px; padding: 4px 10px;
  font-size: 11px; cursor: pointer; transition: all 0.2s;
}
.btn-delete:hover { background: rgba(240,80,96,0.2); }

/* ── AI Panel ───────────────────────────────── */
.ai-panel {
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  background: var(--surface);
}

.ai-panel-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.ai-panel-header h2 { font-size: 13px; font-weight: 700; }

.ai-chat-area {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 10px;
}

.ai-msg {
  padding: 10px 14px; border-radius: 10px;
  font-size: 12px; line-height: 1.6;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity:0; transform:translateY(8px); }
  to { opacity:1; transform:translateY(0); }
}

.ai-msg.bot {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-bottom-left-radius: 3px;
}
.ai-msg.user {
  background: rgba(240,192,64,0.1);
  border: 1px solid rgba(240,192,64,0.2);
  border-bottom-right-radius: 3px;
  align-self: flex-end; max-width: 90%;
}
.ai-msg strong { color: var(--accent); }
.ai-msg .hl { color: var(--accent2); font-weight: 700; }

.ai-input-area {
  padding: 12px; border-top: 1px solid var(--border);
  display: flex; gap: 8px;
}
.ai-input {
  flex: 1; background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 12px; color: var(--text);
  font-family: 'DM Sans', sans-serif; font-size: 12px;
}
.ai-input:focus { outline: none; border-color: var(--accent); }

.ai-send {
  background: var(--accent); color: var(--bg); border: none;
  border-radius: 8px; width: 38px; height: 38px; cursor: pointer;
  font-size: 15px; font-weight: 700; transition: transform 0.15s;
}
.ai-send:hover { transform: scale(1.05); }

.ai-suggestions {
  display: flex; flex-wrap: wrap; gap: 5px;
  padding: 0 12px 10px;
}
.ai-suggestion {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 10px; font-size: 10px;
  color: var(--text-dim); cursor: pointer; transition: all 0.2s;
}
.ai-suggestion:hover { border-color: var(--accent); color: var(--accent); }

.typing-indicator { display: flex; gap: 4px; padding: 10px 14px; }
.typing-indicator span {
  width: 5px; height: 5px; background: var(--text-dim);
  border-radius: 50%; animation: bounce 1.4s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce { 0%,60%,100%{transform:translateY(0)} 30%{transform:translateY(-5px)} }

/* ── Loading ────────────────────────────────── */
.loading-overlay {
  position: fixed; top: 0; left: 0;
  width: 100%; height: 100%;
  background: var(--bg); display: flex;
  align-items: center; justify-content: center;
  z-index: 9999; flex-direction: column; gap: 16px;
}
.loading-overlay.hidden { display: none; }
.loading-spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Responsive ─────────────────────────────── */
@media (max-width: 1200px) {
  .main-layout { grid-template-columns: 1fr; }
  .ai-panel { display: none; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-grid { grid-template-columns: 1fr; }
}

/* ── Leaflet dark ───────────────────────────── */
.leaflet-container { background: var(--bg) !important; }
.leaflet-control-zoom a {
  background: var(--surface) !important; color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-popup-content-wrapper {
  background: var(--surface) !important; color: var(--text) !important;
  border-radius: 10px !important; box-shadow: 0 8px 32px rgba(0,0,0,0.5) !important;
}
.leaflet-popup-tip { background: var(--surface) !important; }
.leaflet-popup-content { font-family: 'DM Sans', sans-serif !important; font-size: 12px !important; }
