.hidden { display: none !important; }
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f4f0; --bg2: #eceae4; --bg3: #e2dfd7;
  --surface: #ffffff; --border: #d8d5cc;
  --text: #1a1916; --text2: #5a5750; --text3: #8c8880;
  --accent: #c84b31; --positive: #16a34a; --negative: #dc2626; --warn: #d97706;
  --chart1: #c84b31; --chart2: #2d5fa6; --chart3: #1a6b4a; --chart4: #7c3d9e;
  --radius: 6px; --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.07), 0 2px 8px rgba(0,0,0,.05);
  --shadow-lg: 0 4px 20px rgba(0,0,0,.09);
  --mono: 'DM Mono', monospace;
  --sans: 'Syne', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111110; --bg2: #1a1917; --bg3: #232220;
    --surface: #1e1d1b; --border: #2e2c28;
    --text: #f0ede6; --text2: #a09d96; --text3: #6b6860;
    --accent: #e8604a;
    --shadow: 0 1px 3px rgba(0,0,0,.3);
    --shadow-lg: 0 4px 20px rgba(0,0,0,.4);
  }
}

html { scroll-behavior: smooth; }
body { font-family: var(--sans); background: var(--bg); color: var(--text); min-height: 100vh; font-size: 14px; line-height: 1.5; }

/* ── Gate screens ── */
.gate { display: flex; align-items: center; justify-content: center; height: 100vh; background: var(--bg); }
.gate-box { text-align: center; max-width: 340px; padding: 40px; }
.gate-icon { font-size: 40px; margin-bottom: 16px; }
.gate-title { font-size: 22px; font-weight: 800; margin-bottom: 8px; }
.gate-sub { font-size: 13px; color: var(--text2); margin-bottom: 20px; line-height: 1.6; }
.gate-code { font-family: var(--mono); font-size: 11px; background: var(--bg2); padding: 10px 14px; border-radius: var(--radius); color: var(--text3); word-break: break-all; }

/* ── Layout ── */
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }

/* ── Sidebar ── */
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.sidebar-logo { padding: 24px 20px 20px; border-bottom: 1px solid var(--border); }
.sidebar-logo .wordmark { font-size: 18px; font-weight: 800; letter-spacing: -0.5px; }
.sidebar-logo .wordmark span { color: var(--accent); }
.sidebar-logo .sub { font-family: var(--mono); font-size: 10px; color: var(--text3); margin-top: 2px; letter-spacing: .5px; }
.app-selector { padding: 12px; border-bottom: 1px solid var(--border); }
.app-selector select { width: 100%; padding: 8px 10px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--bg2); color: var(--text); font-family: var(--mono); font-size: 12px; cursor: pointer; }
.sidebar-section { padding: 16px 12px 8px; }
.sidebar-section-label { font-family: var(--mono); font-size: 9px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--text3); padding: 0 8px; margin-bottom: 6px; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--radius); cursor: pointer; color: var(--text2); font-size: 13px; font-weight: 600; border: none; background: none; width: 100%; text-align: left; transition: all 150ms; }
.nav-item:hover { background: var(--bg2); color: var(--text); }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item svg { width: 16px; flex-shrink: 0; opacity: .7; }
.nav-item.active svg { opacity: 1; }

/* ── Main ── */
.main { flex: 1; overflow-y: auto; }
.topbar { position: sticky; top: 0; z-index: 10; background: var(--bg); border-bottom: 1px solid var(--border); padding: 14px 28px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.topbar-title { font-size: 16px; font-weight: 800; letter-spacing: -0.3px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.range-tabs { display: flex; gap: 4px; }
.range-tab { padding: 5px 12px; border-radius: 100px; border: 1px solid var(--border); background: var(--surface); color: var(--text2); font-family: var(--mono); font-size: 11px; cursor: pointer; transition: all 150ms; }
.range-tab:hover { border-color: var(--accent); color: var(--accent); }
.range-tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.refresh-btn { display: flex; align-items: center; gap: 6px; padding: 6px 14px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--text2); font-family: var(--mono); font-size: 11px; cursor: pointer; transition: all 150ms; }
.refresh-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ── Pages ── */
.page { display: none; padding: 28px; }
.page.active { display: block; }

/* ── KPI ── */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; margin-bottom: 28px; }
.kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 18px 20px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.kpi-card::before { content: ''; position: absolute; top: 0; left: 0; right: 0; height: 3px; background: var(--kpi-color, var(--accent)); }
.kpi-label { font-family: var(--mono); font-size: 10px; letter-spacing: 1px; text-transform: uppercase; color: var(--text3); margin-bottom: 8px; }
.kpi-value { font-size: 28px; font-weight: 800; letter-spacing: -1px; line-height: 1; }
.kpi-icon { position: absolute; top: 16px; right: 16px; opacity: .12; font-size: 28px; }

/* ── Charts ── */
.charts-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(400px, 1fr)); gap: 14px; margin-bottom: 14px; }
.chart-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 20px; box-shadow: var(--shadow); }
.chart-card.full { grid-column: 1 / -1; }
.chart-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.chart-title { font-size: 13px; font-weight: 700; }
.chart-badge { font-family: var(--mono); font-size: 10px; padding: 3px 8px; border-radius: 100px; background: var(--bg2); color: var(--text3); }
.chart-wrap { position: relative; height: 200px; }
.chart-wrap.tall { height: 260px; }

/* ── Tables ── */
.table-wrap { overflow-x: auto; border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { font-family: var(--mono); font-size: 10px; letter-spacing: .8px; text-transform: uppercase; color: var(--text3); text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 9px 12px; border-bottom: 1px solid var(--border); color: var(--text2); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 100px; font-family: var(--mono); font-size: 10px; font-weight: 500; }
.badge-pageview   { background: rgba(45,95,166,.12);  color: var(--chart2); }
.badge-event      { background: rgba(200,75,49,.12);  color: var(--chart1); }
.badge-error      { background: rgba(220,38,38,.12);  color: var(--negative); }
.badge-signup     { background: rgba(22,163,74,.12);  color: var(--positive); }
.badge-revenue    { background: rgba(217,119,6,.12);  color: var(--warn); }
.badge-performance{ background: rgba(124,61,158,.12); color: var(--chart4); }

/* ── States ── */
.loading-overlay { display: flex; align-items: center; justify-content: center; height: 160px; color: var(--text3); font-family: var(--mono); font-size: 12px; gap: 8px; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty-state { text-align: center; padding: 40px; color: var(--text3); font-family: var(--mono); font-size: 12px; }
.error-banner { background: rgba(220,38,38,.08); border: 1px solid rgba(220,38,38,.2); border-radius: var(--radius); padding: 10px 14px; font-family: var(--mono); font-size: 11px; color: var(--negative); margin: 16px 28px 0; display: none; }
.error-banner.visible { display: block; }

/* ── Config ── */
.section-title { font-size: 13px; font-weight: 700; color: var(--text2); margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.config-form { max-width: 560px; display: flex; flex-direction: column; gap: 16px; }
.form-group label { display: block; font-family: var(--mono); font-size: 11px; color: var(--text2); margin-bottom: 6px; letter-spacing: .5px; }
.form-group input { width: 100%; padding: 9px 12px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--text); font-family: var(--mono); font-size: 12px; }
.form-group input:focus { outline: none; border-color: var(--accent); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.btn { display: inline-flex; align-items: center; gap: 8px; padding: 9px 18px; border-radius: var(--radius); border: none; font-family: var(--sans); font-size: 13px; font-weight: 700; cursor: pointer; transition: all 150ms; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); }
.app-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 24px; }
.app-row { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.app-row-name { font-weight: 700; font-size: 13px; }
.app-row-domain { font-family: var(--mono); font-size: 11px; color: var(--text2); }
.app-row-id { font-family: var(--mono); font-size: 10px; color: var(--text3); }
.snippet-label { font-family: var(--mono); font-size: 9px; letter-spacing: 1px; text-transform: uppercase; color: var(--text3); margin-bottom: 6px; }
.snippet-box { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; font-family: var(--mono); font-size: 11px; color: var(--text2); overflow-x: auto; white-space: pre; line-height: 1.6; position: relative; }
.copy-btn { position: absolute; top: 8px; right: 8px; padding: 4px 10px; font-family: var(--mono); font-size: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer; color: var(--text2); }
.copy-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ── Responsive ── */
@media (max-width: 768px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: relative; height: auto; }
  .page { padding: 16px; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-grid { grid-template-columns: 1fr; }
}
