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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #222536;
  --border: #2e3148;
  --accent: #EF5B5B;
  --accent-hover: #f47c7c;
  --text: #e2e4f0;
  --text-muted: #8b8fa8;
  --green: #34d399;
  --red: #f87171;
  --yellow: #fbbf24;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

/* Light mode overrides */
.light {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface2: #eef0f7;
  --border: #dde1f0;
  --accent: #EF5B5B;
  --accent-hover: #d43a3a;
  --text: #3C3C58;
  --text-muted: #6b7080;
  --green: #10b981;
  --red: #ef4444;
  --yellow: #f59e0b;
}

html, body { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
}

/* ── Layout ─────────────────────────────────── */

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
}

header h1 { font-size: 16px; font-weight: 600; letter-spacing: -0.3px; }
header h1 i { color: var(--accent); margin-right: 6px; }
header h1 span { color: var(--accent); }

.header-logo {
  display: block;
  height: 32px;
  width: auto;
  border-radius: 6px;
}

.stat-pills {
  display: flex;
  gap: 8px;
  margin-left: auto;
  flex-wrap: wrap;
}

.pill {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.pill strong { color: var(--text); margin-right: 4px; }

.layout {
  display: flex;
  flex: 1;
}

/* ── Sidebar ─────────────────────────────────── */

.sidebar {
  width: 280px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

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

.sidebar-header h2 { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.8px; color: var(--text-muted); }

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.saved-query {
  border-radius: var(--radius);
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  transition: background 0.1s;
  margin-bottom: 2px;
}

.saved-query:hover { background: var(--surface2); }
.saved-query.active { background: var(--surface2); border: 1px solid var(--accent); }

.saved-query-icon {
  color: var(--accent);
  font-size: 14px;
  flex-shrink: 0;
  margin-top: 1px;
}

.saved-query-body { flex: 1; min-width: 0; }

.saved-query-text {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}

.saved-query-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.saved-query-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  opacity: 0;
  font-size: 14px;
  flex-shrink: 0;
}

.saved-query:hover .saved-query-delete { opacity: 1; }
.saved-query-delete:hover { color: var(--red); background: rgba(248,113,113,0.1); }

.sidebar-empty {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-muted);
  font-size: 13px;
}

/* ── Main Content ─────────────────────────────── */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 20px;
  gap: 16px;
}

/* ── Query Box ────────────────────────────────── */

.query-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.query-box label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.query-input-row {
  display: flex;
  gap: 8px;
}

.query-input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  padding: 10px 14px;
  outline: none;
  transition: border-color 0.15s;
}

.query-input:focus { border-color: var(--accent); }
.query-input::placeholder { color: var(--text-muted); }

.btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn:hover:not(:disabled) { background: var(--accent-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.btn-ghost:hover:not(:disabled) { background: var(--surface2); color: var(--text); }

.suggested-queries {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.suggest-chip {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color 0.1s, color 0.1s;
}

.suggest-chip:hover { border-color: var(--accent); color: var(--text); }

/* ── Results Area ─────────────────────────────── */

.results-area {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.result-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.result-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.result-header-left { flex: 1; min-width: 0; }

.result-title { font-size: 15px; font-weight: 600; margin-bottom: 4px; }

.result-answer {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
}

.result-actions { display: flex; gap: 6px; flex-shrink: 0; }

.result-body { padding: 0; overflow-x: auto; }

.result-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.result-table th {
  background: var(--surface2);
  padding: 8px 14px;
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.result-table td {
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

.result-table tr:last-child td { border-bottom: none; }
.result-table tr:hover td { background: var(--surface2); }

.result-table .num { font-variant-numeric: tabular-nums; text-align: right; }

.result-footer {
  padding: 8px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.result-count { font-size: 12px; color: var(--text-muted); }

.sql-toggle {
  font-size: 12px;
  color: var(--text-muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}

.sql-toggle:hover { color: var(--accent); }

.sql-block {
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-all;
  display: none;
}

.sql-block.visible { display: block; }

/* ── Empty / Loading States ───────────────────── */

.state-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-muted);
  padding: 40px;
}

.state-empty-icon { font-size: 40px; opacity: 0.4; }

.state-empty p { font-size: 14px; text-align: center; max-width: 320px; }

.loading-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px;
  color: var(--text-muted);
  font-size: 13px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* ── Notification Toast ───────────────────────── */

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
  font-size: 13px;
  color: var(--text);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  z-index: 100;
}

.toast.show { opacity: 1; transform: translateY(0); }

/* ── Email Section ────────────────────────────── */

.email-section {
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.email-section-toggle {
  width: 100%;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  transition: color 0.1s, background 0.1s;
}

.email-section-toggle:hover { color: var(--text); background: var(--surface2); }
.email-section-toggle i:first-child { margin-right: 6px; color: var(--accent); }

.email-panel {
  display: none;
  flex-direction: column;
  gap: 8px;
  padding: 8px 12px 12px;
}

.send-now-row {
  display: flex;
  gap: 6px;
}

.send-now-row .query-input {
  flex: 1;
  font-size: 12px;
  padding: 7px 10px;
}

.email-divider {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  padding-top: 8px;
  margin-top: 2px;
}

.email-panel.open { display: flex; }

.schedule-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.schedule-inactive { opacity: 0.5; }

.schedule-item-info { flex: 1; min-width: 0; }

.schedule-email {
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.schedule-email i { color: var(--accent); margin-right: 4px; }

.schedule-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.schedule-meta i { margin-right: 3px; }

.schedule-item-actions { display: flex; gap: 4px; flex-shrink: 0; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  font-size: 12px;
  transition: color 0.1s, background 0.1s;
}

.icon-btn:hover { color: var(--text); background: var(--border); }
.icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.icon-btn-danger:hover { color: var(--red); background: rgba(248,113,113,0.1); }

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

.email-form .query-input {
  font-size: 12px;
  padding: 7px 10px;
}

.email-form-actions { display: flex; gap: 6px; }

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

/* ── Scrollbar ────────────────────────────────── */

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Overview Stats Row ───────────────────────── */

.stats-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.stat-card {
  flex: 1;
  min-width: 130px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.stat-card-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-muted); margin-bottom: 4px; }
.stat-card-value { font-size: 24px; font-weight: 700; line-height: 1; }
.stat-card-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

.no-data { padding: 24px; color: var(--text-muted); font-size: 13px; text-align: center; }

/* ── View Tabs ────────────────────────────────── */

.view-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  margin-bottom: 16px;
}

.tab-btn {
  background: none;
  border: none;
  padding: 10px 18px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 0.15s, border-color 0.15s;
  display: flex;
  align-items: center;
  gap: 7px;
  font-family: var(--font);
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ── Dashboard ────────────────────────────────── */

.dashboard-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 16px;
  flex-shrink: 0;
}

#query-view, #dashboard-view {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

#dashboard-view {
  overflow-y: auto;
}

.widget-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  align-content: start;
}

.widget-empty {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 60px 24px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

.widget-empty i { font-size: 36px; opacity: 0.3; }

.widget-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.widget-header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

.widget-title {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.widget-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

.widget-type-badge {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
}

.widget-body { padding: 16px; flex: 1; min-height: 0; overflow: auto; }

.widget-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px;
  color: var(--text-muted);
  font-size: 13px;
}

.widget-error { padding: 8px; font-size: 13px; color: var(--text-muted); }

.widget-metric { text-align: center; padding: 12px 0; }
.widget-metric-value { font-size: 40px; font-weight: 700; color: var(--accent); line-height: 1; }
.widget-metric-label { font-size: 12px; color: var(--text-muted); margin-top: 6px; }

.widget-chart-wrap { position: relative; height: 220px; }

.widget-table-wrap { overflow-x: auto; }
.widget-table-wrap .result-table th,
.widget-table-wrap .result-table td { padding: 7px 12px; }

/* ── Type Button Group (widget modal) ─────────── */

.type-btn-group { display: flex; gap: 6px; }

.type-btn {
  flex: 1;
  padding: 8px 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 13px;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.type-btn:hover { color: var(--text); border-color: var(--text-muted); }
.type-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ── Login Page ───────────────────────────────── */

.login-body {
  align-items: center;
  justify-content: center;
}

.login-wrap {
  width: 100%;
  max-width: 380px;
  padding: 24px 16px;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 32px;
  text-align: center;
}

.login-logo {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-logo img {
  height: 56px;
  width: auto;
  border-radius: 8px;
}

.login-title {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 6px;
}

.login-title span { color: var(--accent); }

.login-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 28px;
}

.login-btns {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.login-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  padding: 11px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid var(--border);
  transition: background 0.15s, border-color 0.15s;
}

.login-btn-google {
  background: var(--surface2);
  color: var(--text);
}

.login-btn-google:hover { background: var(--surface2); border-color: var(--accent); }

.login-btn-github {
  background: var(--surface2);
  color: var(--text);
}

.login-btn-github:hover { background: var(--surface2); border-color: var(--accent); }

.login-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 13px;
  padding: 8px 0;
}

.login-error {
  margin-top: 16px;
  background: rgba(248,113,113,0.1);
  border: 1px solid rgba(248,113,113,0.3);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  color: var(--red);
  text-align: left;
}

/* ── Auth Overlay ─────────────────────────────── */

.auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

/* ── Site Selector ────────────────────────────── */

.site-selector-group {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: 16px;
}

.site-select {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  padding: 6px 10px;
  cursor: pointer;
  outline: none;
  max-width: 320px;
}

.site-select:focus { border-color: var(--accent); }

/* ── Modal ────────────────────────────────────── */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 16px;
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 400px;
}

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

.modal-header h2 {
  font-size: 15px;
  font-weight: 600;
}

.modal-body {
  padding: 20px;
}

.field-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

/* ── Realtime View ────────────────────────────────── */

#realtime-view {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.realtime-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.realtime-live {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
}

.live-dot {
  width: 8px;
  height: 8px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse-dot 1.5s ease-in-out infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.7); }
}

.realtime-count {
  font-size: 13px;
  color: var(--text-muted);
}

.realtime-feed {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.rt-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rt-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.rt-location { font-size: 14px; }

.rt-time {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.rt-meta {
  display: flex;
  gap: 14px;
  font-size: 12px;
  color: var(--text-muted);
  flex-wrap: wrap;
}

.rt-meta i { margin-right: 5px; }

.rt-page {
  font-size: 13px;
  font-family: var(--mono);
  color: var(--accent);
}

.rt-page i {
  margin-right: 6px;
  color: var(--text-muted);
  font-size: 11px;
}

.rt-journey {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 2px;
}

.rt-path-step {
  font-family: var(--mono);
  font-size: 11px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 7px;
  color: var(--text-muted);
}

.rt-arrow {
  font-size: 9px;
  color: var(--text-muted);
  opacity: 0.4;
}
