/* ============================================================
   CSS Custom Properties — theme tokens
   ============================================================ */
:root {
  --bg:          #f4f6f9;
  --bg-card:     #ffffff;
  --bg-sidebar:  #1e2a3a;
  --fg:          #1a1a2e;
  --fg-muted:    #6b7280;
  --fg-sidebar:  #c9d1dc;
  --accent:      #3b82f6;
  --accent-dark: #2563eb;
  --danger:      #ef4444;
  --success:     #22c55e;
  --border:      #e5e7eb;
  --radius:      8px;
  --shadow:      0 1px 4px rgba(0,0,0,0.08);
  --font:        -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --sidebar-w:   220px;
}

body.theme-dark {
  --bg:          #0f172a;
  --bg-card:     #1e293b;
  --bg-sidebar:  #0a0f1a;
  --fg:          #e2e8f0;
  --fg-muted:    #94a3b8;
  --fg-sidebar:  #94a3b8;
  --border:      #334155;
  --shadow:      0 1px 4px rgba(0,0,0,0.4);
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--fg);
  font-size: 14px;
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; font-weight: 600; }
h2 { font-size: 1.1rem; font-weight: 600; margin-bottom: 12px; }

code {
  font-family: 'SF Mono', 'Consolas', monospace;
  font-size: 0.85em;
  background: var(--border);
  padding: 1px 5px;
  border-radius: 3px;
}

/* ============================================================
   Layout
   ============================================================ */
#app {
  display: flex;
  min-height: 100vh;
}

#sidebar {
  width: var(--sidebar-w);
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
}

#main {
  margin-left: var(--sidebar-w);
  flex: 1;
  padding: 32px;
  max-width: 1200px;
}

/* ============================================================
   Sidebar
   ============================================================ */
#sidebar-header {
  padding: 20px 16px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.logo {
  display: block;
  font-size: 1.3rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: 1px;
}

.logo-sub {
  font-size: 0.7rem;
  color: var(--fg-sidebar);
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

#nav-links {
  list-style: none;
  padding: 12px 0;
  flex: 1;
}

#nav-links li { margin: 2px 8px; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius);
  color: var(--fg-sidebar);
  font-size: 0.9rem;
  transition: background 0.15s;
}

.nav-link:hover { background: rgba(255,255,255,0.07); text-decoration: none; color: #fff; }
.nav-link.active { background: var(--accent); color: #fff; }

.nav-icon { width: 18px; text-align: center; }

#sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid rgba(255,255,255,0.08);
}

#theme-toggle {
  background: none;
  border: none;
  color: var(--fg-sidebar);
  cursor: pointer;
  font-size: 1.2rem;
  padding: 4px;
}

/* ============================================================
   Views
   ============================================================ */
.view { display: none; }
.view.active { display: block; }

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

/* ============================================================
   Cards
   ============================================================ */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card-danger { border-color: var(--danger); }
.card-list { display: flex; flex-direction: column; gap: 10px; }

/* ============================================================
   Forms
   ============================================================ */
.form-row {
  margin-bottom: 16px;
}

.form-row label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.form-row input[type="text"],
.form-row input[type="date"],
.form-row select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  font-size: 0.95rem;
  transition: border-color 0.15s;
}

.form-row input:focus,
.form-row select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}

.form-row-inline {
  display: flex;
  gap: 16px;
}
.form-row-inline > div { flex: 1; }

.radio-group {
  display: flex;
  gap: 20px;
  align-items: center;
}
.radio-group label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--fg);
  text-transform: none;
  letter-spacing: 0;
  font-weight: normal;
  cursor: pointer;
}

.form-actions {
  display: flex;
  gap: 10px;
  padding-top: 8px;
}

.hint {
  color: var(--fg-muted);
  font-size: 0.85rem;
  margin-bottom: 12px;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--fg);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--bg); border-color: #aaa; }

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

.btn-secondary { background: var(--bg-card); }

.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: #dc2626; }

.btn-sm { padding: 5px 10px; font-size: 0.8rem; }

/* ============================================================
   Status / Progress
   ============================================================ */
.status-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
  display: inline-block;
}

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

.progress-bar {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

#progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.4s;
  animation: indeterminate 1.5s linear infinite;
}

@keyframes indeterminate {
  0%   { width: 0%; margin-left: 0%; }
  50%  { width: 60%; margin-left: 20%; }
  100% { width: 0%; margin-left: 100%; }
}

/* ============================================================
   Results toolbar & table
   ============================================================ */
.results-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.results-actions { display: flex; gap: 8px; }

#results-meta { font-size: 0.85rem; color: var(--fg-muted); }

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-card);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

thead th {
  background: var(--bg);
  padding: 10px 14px;
  text-align: left;
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--fg-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}
thead th:hover { color: var(--fg); }

tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: rgba(59,130,246,0.04); }

tbody td {
  padding: 9px 14px;
  color: var(--fg);
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================================
   Charts
   ============================================================ */
.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(460px, 1fr));
  gap: 16px;
}

.chart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.chart-card h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.chart-canvas { width: 100%; height: 280px; }

/* ============================================================
   Empty state
   ============================================================ */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--fg-muted);
}
.empty-state p { margin-bottom: 16px; font-size: 1rem; }

/* ============================================================
   Toast notification
   ============================================================ */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--fg);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
  z-index: 9999;
  max-width: 360px;
  animation: slideUp 0.2s ease;
}
.toast.success { background: var(--success); color: #fff; }
.toast.error   { background: var(--danger);  color: #fff; }

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

/* ============================================================
   Connection status indicator
   ============================================================ */
#connection-status {
  margin-left: 12px;
  font-size: 0.85rem;
  font-weight: 500;
}
#connection-status.ok     { color: var(--success); }
#connection-status.error  { color: var(--danger); }

/* ============================================================
   Advanced Options panel (Phase 2)
   ============================================================ */

details#advanced-options {
  margin-bottom: 16px;
}

.advanced-summary {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  cursor: pointer;
  padding: 8px 0;
  user-select: none;
  list-style: none;
}
.advanced-summary::-webkit-details-marker { display: none; }
.advanced-summary::before {
  content: '\25B6';
  display: inline-block;
  margin-right: 6px;
  transition: transform 0.15s;
  font-size: 0.65rem;
  vertical-align: middle;
}
details#advanced-options[open] .advanced-summary::before {
  transform: rotate(90deg);
}

.adv-section {
  padding: 12px 0;
  border-top: 1px solid var(--border);
}

.adv-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.adv-section-title {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.adv-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}

.adv-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  font-size: 0.85rem;
  min-width: 120px;
  flex-shrink: 0;
}
.adv-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}

.adv-input {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  font-size: 0.85rem;
  flex: 1;
  min-width: 100px;
}
.adv-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}

.calc-fields {
  display: flex;
  gap: 6px;
  flex: 1;
}

.btn-remove {
  flex-shrink: 0;
  color: var(--danger);
  border-color: transparent;
  background: transparent;
  padding: 4px 8px;
}
.btn-remove:hover {
  background: rgba(239,68,68,0.08);
  border-color: var(--danger);
}

/* ============================================================
   Chart Config Modal (Phase 3)
   ============================================================ */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  width: min(560px, calc(100vw - 48px));
  max-height: calc(100vh - 80px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.modal-header h2 {
  margin-bottom: 0;
  font-size: 1rem;
}

.modal-close-btn {
  color: var(--fg-muted);
  border-color: transparent;
  background: transparent;
}
.modal-close-btn:hover {
  background: rgba(239, 68, 68, 0.08);
  border-color: var(--danger);
  color: var(--danger);
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chart-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.chart-card-header h3 {
  margin-bottom: 0;
}

.chart-card-meta {
  font-size: 0.75rem;
  color: var(--fg-muted);
  margin-top: 8px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.chart-empty-msg {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 280px;
  color: var(--fg-muted);
  font-size: 0.9rem;
  font-style: italic;
}

/* ============================================================
   Column visibility dropdown (Phase 4)
   ============================================================ */
.col-visibility-wrap {
  position: relative;
}

.col-visibility-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  padding: 8px;
  z-index: 100;
  min-width: 160px;
  max-height: 260px;
  overflow-y: auto;
}

.col-check-label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  cursor: pointer;
  font-size: 0.85rem;
  border-radius: 4px;
  white-space: nowrap;
}
.col-check-label:hover {
  background: var(--bg);
}

/* ============================================================
   Column metadata selector (entity attribute discovery)
   ============================================================ */
.col-meta-list {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 8px;
  max-height: 160px;
  overflow-y: auto;
  margin-top: 6px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}

.col-meta-label {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 5px;
  cursor: pointer;
  font-size: 0.82rem;
  border-radius: 4px;
  white-space: nowrap;
}

.col-meta-label:hover {
  background: var(--bg-card);
}

.col-meta-actions {
  margin-top: 6px;
  display: flex;
  gap: 6px;
}

#col-selector-status {
  word-break: break-word;
  white-space: pre-wrap;
}
