/* ==========================================================================
   CSS Custom Properties
   ========================================================================== */

:root {
  /* Page */
  --bg: #f0f2f5;
  --surface: #fff;
  --surface-hover: #fafafe;
  --surface-active: #f8f7ff;
  --surface-muted: #f9fafb;

  /* Text */
  --text: #1a1a2e;
  --text-heading: #1e1b4b;
  --text-secondary: #374151;
  --text-dim: #4b5563;
  --text-muted: #6b7280;
  --text-faint: #9ca3af;

  /* Primary (Indigo) */
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-light: #c7d2fe;
  --primary-muted: #a5b4fc;
  --primary-pale: #e0e7ff;
  --primary-bg: #eef2ff;

  /* Secondary (Teal) */
  --secondary: #14b8a6;
  --secondary-hover: #0d9488;
  --secondary-light: #99f6e4;
  --secondary-muted: #5eead4;
  --secondary-pale: #ccfbf1;
  --secondary-bg: #f0fdfa;

  /* Borders */
  --border: #e5e7eb;
  --border-light: #f3f4f6;
  --border-subtle: #f0f0f5;
  --border-input: #d1d5db;

  /* Danger / Error */
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --danger-light: #fecaca;
  --danger-muted: #fca5a5;

  /* Success */
  --success: #059669;
  --success-light: #34d399;

  /* Badges */
  --badge-blue-bg: #dbeafe;
  --badge-blue-text: #1d4ed8;
  --badge-amber-bg: #fef3c7;
  --badge-amber-text: #92400e;
  --badge-purple-bg: #f3e8ff;
  --badge-purple-text: #7c3aed;

  /* Mono font */
  --font-mono: 'SF Mono', Consolas, monospace;
}

/* ==========================================================================
   Reset & Base
   ========================================================================== */

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

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

/* ==========================================================================
   Navigation (shared by admin & logs)
   ========================================================================== */

.navbar { background: var(--text-heading); color: var(--surface); padding: 0 16px; position: sticky; top: 0; z-index: 40; }
.nav-inner { max-width: 1200px; margin: 0 auto; display: flex; align-items: center; height: 52px; gap: 8px; }
.nav-brand { font-size: 1.1rem; font-weight: 700; color: var(--surface); text-decoration: none; margin-right: 24px; letter-spacing: -0.02em; }
.nav-links { display: flex; align-items: center; gap: 4px; }
.nav-link { color: var(--primary-light); text-decoration: none; padding: 6px 14px; border-radius: 6px; font-size: 0.87rem; font-weight: 500; transition: background 0.15s, color 0.15s; }
.nav-link:hover { background: rgba(255,255,255,0.1); color: var(--surface); text-decoration: none; }
.nav-link.active { background: rgba(255,255,255,0.15); color: var(--surface); }
.nav-spacer { flex: 1; }
.nav-user { display: flex; align-items: center; gap: 10px; }
.nav-email { font-size: 0.8rem; color: var(--primary-light); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-logout { color: var(--danger-muted); text-decoration: none; font-size: 0.82rem; font-weight: 500; padding: 5px 12px; border-radius: 6px; transition: background 0.15s; }
.nav-logout:hover { background: rgba(239,68,68,0.15); color: var(--danger-light); text-decoration: none; }
.nav-toggle { display: none; background: none; border: none; color: var(--surface); font-size: 1.4rem; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.nav-toggle:hover { background: rgba(255,255,255,0.1); }

@media (max-width: 640px) {
  .nav-toggle { display: block; }
  .nav-links, .nav-user { display: none; flex-direction: column; align-items: stretch; width: 100%; }
  .nav-links.open, .nav-user.open { display: flex; }
  .nav-inner { flex-wrap: wrap; height: auto; padding: 10px 0; }
  .nav-link { padding: 10px 14px; }
  .nav-user { padding: 8px 0; gap: 8px; border-top: 1px solid rgba(255,255,255,0.1); }
  .nav-email { max-width: none; }
}

/* ==========================================================================
   Layout
   ========================================================================== */

.container { max-width: 1200px; margin: 0 auto; padding: 24px 16px; }
.container-sm { max-width: 800px; }

/* ==========================================================================
   Buttons
   ========================================================================== */

.btn { padding: 8px 16px; border: none; border-radius: 6px; font-size: 0.85rem; font-weight: 500; cursor: pointer; transition: all 0.15s; }
.btn-primary { background: var(--primary); color: var(--surface); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-danger:hover { background: var(--danger-light); }
.btn-sm { padding: 6px 12px; font-size: 0.8rem; }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--surface-muted); border-color: var(--border-input); }

/* ==========================================================================
   Common Components
   ========================================================================== */

/* Empty state */
.empty { text-align: center; padding: 48px 24px; color: var(--text-faint); }
.empty .icon { font-size: 2.5rem; margin-bottom: 8px; }

/* Loading */
.loading { text-align: center; padding: 40px; color: var(--text-faint); }

/* Toast */
.toast { position: fixed; bottom: 24px; right: 24px; padding: 12px 20px; border-radius: 8px; color: var(--surface); font-size: 0.9rem; opacity: 0; transform: translateY(12px); transition: all 0.3s; z-index: 100; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* Confirm dialog */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.3); display: flex; align-items: center; justify-content: center; z-index: 50; }
.dialog { background: var(--surface); border-radius: 12px; padding: 24px; max-width: 400px; width: 90%; box-shadow: 0 20px 60px rgba(0,0,0,0.15); }
.dialog h3 { margin-bottom: 8px; }
.dialog p { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 20px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ==========================================================================
   Login Page
   ========================================================================== */

body.page-login {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.card { background: var(--surface); border-radius: 12px; box-shadow: 0 2px 12px rgba(0,0,0,0.08); padding: 48px 40px; max-width: 400px; width: 100%; text-align: center; }
.logo { font-size: 28px; font-weight: 700; color: var(--text); margin-bottom: 8px; }
.subtitle { color: var(--text-muted); font-size: 14px; margin-bottom: 32px; }

.btn-google {
  display: inline-flex; align-items: center; gap: 12px;
  padding: 12px 32px; background: var(--surface); border: 1px solid #dadce0; border-radius: 8px;
  font-size: 15px; font-weight: 500; color: #3c4043; cursor: pointer; text-decoration: none;
  transition: background 0.15s, box-shadow 0.15s;
}
.btn-google:hover { background: var(--surface-muted); box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.btn-google svg { width: 20px; height: 20px; }

.login-error { background: var(--danger-bg); color: var(--danger); padding: 10px 16px; border-radius: 6px; font-size: 13px; margin-bottom: 24px; display: none; }

/* ==========================================================================
   Admin Page – Site Management
   ========================================================================== */

.admin-title { font-size: 1.5rem; font-weight: 700; margin-bottom: 24px; display: flex; align-items: center; gap: 10px; }
.admin-title .icon { font-size: 1.2rem; }

/* Add site form */
.add-form { display: flex; gap: 8px; margin-bottom: 24px; }
.add-form input { flex: 1; padding: 10px 14px; border: 1px solid var(--border-input); border-radius: 8px; font-size: 0.95rem; outline: none; transition: border-color 0.15s; }
.add-form input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(99,102,241,0.1); }
.add-form input::placeholder { color: var(--text-faint); }
.add-form .btn { padding: 10px 18px; border-radius: 8px; font-size: 0.9rem; }

/* Site list */
.sites-list { display: flex; flex-direction: column; gap: 6px; }
.site-item { border-radius: 10px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.05); transition: box-shadow 0.2s; }
.site-item.open { box-shadow: 0 3px 14px rgba(0,0,0,0.08); }

.site-row { background: var(--surface); padding: 14px 18px; cursor: pointer; transition: background 0.15s; display: flex; align-items: center; gap: 14px; }
.site-row:hover { background: var(--surface-hover); }
.site-item.open .site-row { background: var(--surface-active); }
.site-info { flex: 1; min-width: 0; }
.site-domain { font-size: 1rem; font-weight: 600; color: var(--text-heading); }
.site-meta { display: flex; align-items: center; gap: 8px; margin-top: 4px; flex-wrap: wrap; }
.site-date { font-size: 0.75rem; color: var(--text-faint); }
.site-pills { display: flex; gap: 4px; flex-wrap: wrap; }
.pill { display: inline-block; padding: 1px 7px; border-radius: 4px; font-size: 0.68rem; font-weight: 600; letter-spacing: 0.01em; }
.pill-on { background: var(--badge-blue-bg); color: var(--badge-blue-text); }
.pill-keys { background: var(--badge-purple-bg); color: var(--badge-purple-text); }
.site-logs-btn { flex-shrink: 0; font-size: 0.78rem; text-decoration: none; color: var(--secondary); border-color: var(--secondary-muted); }
.site-logs-btn:hover { background: var(--secondary-bg); border-color: var(--secondary); color: var(--secondary-hover); text-decoration: none; }
.site-chevron { color: var(--primary-light); font-size: 1.1rem; flex-shrink: 0; transition: transform 0.2s; }
.site-item.open .site-chevron { transform: rotate(90deg); color: var(--primary); }

/* Site detail dropdown */
.site-detail { display: none; background: var(--surface); border-top: 1px solid var(--border-subtle); padding: 18px 20px; }
.site-item.open .site-detail { display: block; }

.detail-sections { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.detail-section { min-width: 0; }
.detail-section-title { font-size: 0.72rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 8px; }

/* Settings toggles */
.setting-row { display: flex; align-items: center; justify-content: space-between; padding: 5px 0; }
.setting-name { font-size: 0.84rem; color: var(--text-secondary); }
.toggle { position: relative; width: 34px; height: 18px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .slider { position: absolute; inset: 0; background: var(--border-input); border-radius: 9px; cursor: pointer; transition: background 0.2s; }
.toggle .slider::before { content: ''; position: absolute; width: 14px; height: 14px; left: 2px; top: 2px; background: var(--surface); border-radius: 50%; transition: transform 0.2s; }
.toggle input:checked + .slider { background: var(--primary); }
.toggle input:checked + .slider::before { transform: translateX(16px); }

/* API keys */
.key-row { display: flex; align-items: center; gap: 8px; padding: 5px 0; font-size: 0.82rem; }
.key-value { font-family: var(--font-mono); font-size: 0.76rem; color: var(--text-dim); background: var(--border-light); padding: 2px 7px; border-radius: 4px; cursor: pointer; }
.key-value:hover { background: var(--border); }
.key-info { flex: 1; min-width: 0; }
.key-label { color: var(--text-faint); font-size: 0.73rem; }
.key-date { color: var(--border-input); font-size: 0.7rem; }
.key-del { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 0.82rem; padding: 2px 6px; border-radius: 4px; }
.key-del:hover { background: var(--danger-bg); }
.no-keys { font-size: 0.82rem; color: var(--text-faint); padding: 4px 0; }

/* Snippet */
.snippet-section { grid-column: 1 / -1; }
.snippet-code { background: var(--text-heading); color: var(--primary-pale); padding: 10px 14px; border-radius: 8px; font-family: var(--font-mono); font-size: 0.74rem; overflow-x: auto; cursor: pointer; position: relative; white-space: pre-wrap; word-break: break-all; }
.snippet-code:hover::after { content: 'Click to copy'; position: absolute; right: 10px; top: 8px; font-size: 0.66rem; color: var(--primary-muted); }
.snippet-code.copied::after { content: 'Copied!'; position: absolute; right: 10px; top: 8px; font-size: 0.66rem; color: var(--success-light); }

/* Detail footer */
.detail-footer { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: center; padding-top: 12px; border-top: 1px solid var(--border-light); margin-top: 4px; }

@media (max-width: 640px) {
  .site-row { padding: 12px 14px; gap: 10px; }
  .site-domain { font-size: 0.92rem; }
  .pill { font-size: 0.62rem; }
  .detail-sections { grid-template-columns: 1fr; }
  .site-detail { padding: 14px 14px; }
}

/* ==========================================================================
   Logs Page – Event Log
   ========================================================================== */

body.page-logs { font-size: 14px; }

/* Header */
.header { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.header h1 { font-size: 1.4rem; font-weight: 700; white-space: nowrap; }

/* Controls bar */
.controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 20px; background: var(--surface); padding: 14px 18px; border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.06); }
.controls label { font-size: 0.8rem; color: var(--text-muted); font-weight: 500; }
.controls select, .controls input[type="date"] {
  padding: 7px 10px; border: 1px solid var(--border-input); border-radius: 6px; font-size: 0.85rem;
  outline: none; background: var(--surface); color: var(--text);
}
.controls select:focus, .controls input[type="date"]:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(99,102,241,0.1); }
.control-group { display: flex; flex-direction: column; gap: 3px; }
.controls .spacer { flex: 1; }
.controls .stats-summary { font-size: 0.85rem; color: var(--text-muted); }
.controls .stats-summary strong { color: var(--text); }

/* Quick date buttons */
.quick-dates { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.quick-dates label { font-size: 0.8rem; color: var(--text-muted); font-weight: 500; margin-right: 4px; }
.btn-quick { padding: 5px 10px; border: 1px solid var(--border-input); border-radius: 6px; font-size: 0.78rem; font-weight: 500; cursor: pointer; background: var(--surface); color: var(--text-dim); transition: all 0.15s; white-space: nowrap; }
.btn-quick:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-bg); }
.btn-quick.active { background: var(--primary); color: var(--surface); border-color: var(--primary); }

/* Chart */
.chart-wrap { background: var(--surface); border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.06); padding: 18px; margin-bottom: 16px; }
.chart-header h3 { font-size: 0.85rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 14px; }
.chart { display: flex; align-items: flex-end; gap: 2px; height: 120px; }
.chart-bar-group { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.chart-bar { width: 100%; min-height: 2px; background: var(--primary); border-radius: 3px 3px 0 0; transition: background 0.15s; }
.chart-bar:hover { background: var(--primary-hover); }
.chart-value { font-size: 0.65rem; color: var(--text-muted); margin-bottom: 2px; }
.chart-label { font-size: 0.65rem; color: var(--text-faint); margin-top: 4px; white-space: nowrap; }

/* Table */
.table-wrap { background: var(--surface); border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.06); overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
thead { background: var(--surface-muted); position: sticky; top: 0; z-index: 2; }
th { text-align: left; padding: 10px 14px; font-size: 0.75rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 9px 14px; border-bottom: 1px solid var(--border-light); font-size: 0.83rem; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-muted); }

/* Type badges */
.badge { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 0.75rem; font-weight: 600; }
.badge-pageview { background: var(--badge-blue-bg); color: var(--badge-blue-text); }
.badge-event { background: var(--badge-amber-bg); color: var(--badge-amber-text); }

/* Metrics row */
.metrics-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.metric-card { background: var(--surface); border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.06); padding: 16px 18px; text-align: center; }
.metric-value { font-size: 1.5rem; font-weight: 700; color: var(--text-heading); }
.metric-label { font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-top: 2px; }

/* Aggregate grid */
.agg-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.agg-panel { background: var(--surface); border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.06); padding: 18px; }
.agg-title { font-size: 0.75rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 12px; }
.agg-row { position: relative; display: flex; align-items: center; padding: 7px 10px; border-radius: 4px; margin-bottom: 2px; }
.agg-bar { position: absolute; left: 0; top: 0; bottom: 0; background: var(--primary-bg); border-radius: 4px; z-index: 0; }
.agg-name { position: relative; z-index: 1; flex: 1; font-size: 0.83rem; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agg-count { position: relative; z-index: 1; font-size: 0.83rem; font-weight: 600; color: var(--text); margin-left: 8px; }
.agg-pct { position: relative; z-index: 1; font-size: 0.75rem; color: var(--text-muted); width: 40px; text-align: right; }
.agg-more { font-size: 0.78rem; color: var(--text-faint); text-align: center; padding: 8px 0 0; }
.agg-empty { font-size: 0.83rem; color: var(--text-faint); padding: 8px 0; }

/* Pagination */
.pagination { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; background: var(--surface); border-radius: 0 0 10px 10px; border-top: 1px solid var(--border-light); }
.pagination .info { font-size: 0.83rem; color: var(--text-muted); }
.pagination .buttons { display: flex; gap: 6px; }

@media (max-width: 768px) {
  .controls { flex-direction: column; align-items: stretch; }
  .control-group { flex-direction: row; align-items: center; gap: 8px; }
  .controls .spacer { display: none; }
  td { padding: 8px 10px; font-size: 0.78rem; }
  .metrics-row { grid-template-columns: repeat(2, 1fr); }
  .agg-grid { grid-template-columns: 1fr; }
}
