:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #16181d;
  --muted: #6b7280;
  --faint: #9ca3af;
  --border: #e5e7eb;
  --hover: #f1f3f6;
  --primary: #2563eb;
  --primary-soft: #dbe6fd;
  --primary-text: #ffffff;
  --views: #9fbcf7;
  --visitors: #2563eb;
  --good: #15803d;
  --good-bg: #dcfce7;
  --bad: #b91c1c;
  --bad-bg: #fee2e2;
  --map-empty: #eceef2;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --panel: #171a21;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --faint: #6b7280;
    --border: #262a33;
    --hover: #1e222b;
    --primary: #5b8cff;
    --primary-soft: #1c2a4a;
    --views: #2c4372;
    --visitors: #5b8cff;
    --good: #4ade80;
    --good-bg: #14301f;
    --bad: #f87171;
    --bad-bg: #3a1717;
    --map-empty: #232731;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --text: #e8eaee;
  --muted: #9aa1ad;
  --faint: #6b7280;
  --border: #262a33;
  --hover: #1e222b;
  --primary: #5b8cff;
  --primary-soft: #1c2a4a;
  --views: #2c4372;
  --visitors: #5b8cff;
  --good: #4ade80;
  --good-bg: #14301f;
  --bad: #f87171;
  --bad-bg: #3a1717;
  --map-empty: #232731;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }
a:hover { color: var(--primary); }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 15px; }

.app { min-height: 100vh; display: flex; flex-direction: column; }

/* Header */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 56px;
  background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.brand svg { width: 22px; height: 22px; color: var(--primary); }
.nav { display: flex; gap: 4px; }
.nav a { padding: 6px 10px; border-radius: 8px; color: var(--muted); font-weight: 500; }
.nav a.active, .nav a:hover { color: var(--text); background: var(--hover); }
.topbar .spacer { flex: 1; }
.user-menu { display: flex; align-items: center; gap: 8px; color: var(--muted); }

.container { width: 100%; max-width: 1320px; margin: 0 auto; padding: 24px; }

/* Buttons and inputs */
.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel); cursor: pointer;
  font-weight: 500; white-space: nowrap;
}
.button:hover { background: var(--hover); color: var(--text); }
.button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.button.primary:hover { filter: brightness(1.08); }
.button.danger { color: var(--bad); }
.button.ghost { border-color: transparent; background: transparent; }
.button.icon { width: 34px; padding: 0; }
.button svg { width: 16px; height: 16px; }
.button:disabled { opacity: 0.5; cursor: default; }

.input, .select {
  height: 34px; padding: 0 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel); min-width: 0;
}
.input:focus, .select:focus, .button:focus-visible { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
label.field { display: grid; gap: 6px; font-weight: 500; }
label.field .hint { color: var(--muted); font-weight: 400; font-size: 13px; }

/* Panels */
.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px; min-width: 0;
}
.panel-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.two-one { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.grid.one-two { grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); }
@media (max-width: 960px) {
  .grid.two, .grid.two-one, .grid.one-two { grid-template-columns: minmax(0, 1fr); }
  .topbar { gap: 12px; padding: 0 12px; }
  .container { padding: 16px 12px; }
}

/* Website header */
.page-header { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-header h1 { display: flex; align-items: center; gap: 10px; }
.page-header .domain { color: var(--muted); font-size: 14px; font-weight: 400; }
.live {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px;
  background: var(--good-bg); color: var(--good); font-size: 13px; font-weight: 600;
}
.live::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; animation: pulse 2s infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs a, .tabs button {
  padding: 8px 12px; border: 0; background: none; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px; font-weight: 500; white-space: nowrap;
}
.tabs a.active, .tabs button.active { color: var(--text); border-bottom-color: var(--primary); }
.tabs a:hover, .tabs button:hover { color: var(--text); }
.subtabs { display: flex; gap: 4px; }
.subtabs button {
  border: 0; background: none; padding: 4px 8px; border-radius: 6px; cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 500;
}
.subtabs button.active { background: var(--hover); color: var(--text); }

/* Filter bar */
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }
.range-label { color: var(--muted); font-size: 13px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 6px 0 10px;
  border-radius: 999px; background: var(--primary-soft); color: var(--text); font-size: 13px;
}
.chip b { font-weight: 600; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 2px; display: flex; }
.chip button:hover { color: var(--text); }
.chip svg { width: 14px; height: 14px; }

/* Metrics bar */
.metrics { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }
@media (max-width: 960px) { .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.metric { padding: 16px 20px; }
.metric .label { color: var(--muted); font-weight: 500; font-size: 13px; }
.metric .value { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; margin: 2px 0 4px; }
.change { display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 12px; font-weight: 600; }
.change.up { background: var(--good-bg); color: var(--good); }
.change.down { background: var(--bad-bg); color: var(--bad); }
.change.flat { background: var(--hover); color: var(--muted); }

/* Charts */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .axis text { fill: var(--faint); font-size: 11px; }
.chart .gridline { stroke: var(--border); }
.chart .bar-hit { fill: transparent; cursor: default; }
.chart .bar-hit:hover { fill: var(--hover); opacity: 0.6; }
.legend { display: flex; gap: 16px; justify-content: center; margin-top: 10px; color: var(--muted); font-size: 13px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.tooltip {
  position: fixed; pointer-events: none; z-index: 50; padding: 8px 10px; border-radius: 8px;
  background: #111827; color: #f9fafb; font-size: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.2); white-space: nowrap;
}
.tooltip .title { font-weight: 600; margin-bottom: 4px; }
.tooltip i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; margin-right: 6px; }

/* Metric tables */
.table-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; padding: 0 0 6px; }
.rows { display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; }
.row {
  position: relative; display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 6px; cursor: pointer; min-height: 32px;
}
.row:hover { background: var(--hover); }
.row { min-width: 0; }
.row .bar { position: absolute; left: 8px; bottom: 2px; height: 3px; max-width: calc(100% - 16px); background: var(--visitors); opacity: .55; border-radius: 2px; z-index: 0; }
.row > * { position: relative; z-index: 1; }
.row .label { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.row .label .text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .label .flag { flex-shrink: 0; }
.row .count { font-weight: 600; font-variant-numeric: tabular-nums; }
.row .percent { color: var(--muted); width: 44px; text-align: right; font-size: 12px; font-variant-numeric: tabular-nums; }
.row .flag { font-size: 16px; line-height: 1; }
.row .muted, .muted { color: var(--muted); }
.more { margin-top: 10px; text-align: center; }
.empty { color: var(--faint); text-align: center; padding: 32px 0; }

/* Heatmap */
.heatmap { display: grid; grid-template-columns: 32px repeat(7, 1fr); gap: 3px; font-size: 11px; color: var(--faint); }
.heatmap .cell { aspect-ratio: 1.7; border-radius: 3px; background: var(--map-empty); }
.heatmap .day { text-align: center; }
.heatmap .hour { text-align: right; padding-right: 4px; line-height: 1; align-self: center; }

/* Map */
.world { width: 100%; height: auto; display: block; }
.world path { stroke: var(--panel); stroke-width: 0.5; transition: opacity .1s; }
.world path:hover { opacity: 0.75; }

/* Data tables */
table.data { width: 100%; border-collapse: collapse; }
table.data th { text-align: left; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); }
table.data td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data tr.link:hover td { background: var(--hover); cursor: pointer; }
.table-wrap { overflow-x: auto; }
.pagination { display: flex; align-items: center; justify-content: space-between; margin-top: 12px; color: var(--muted); }

/* Website list */
.site-card { display: grid; gap: 14px; }
.site-card .site-title { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.site-card .site-title h2 { font-size: 16px; }
.site-stats { display: flex; gap: 28px; }
.site-stats div b { display: block; font-size: 20px; }
.site-stats div span { color: var(--muted); font-size: 12px; }

/* Activity */
.activity { display: grid; gap: 0; }
.activity .item { display: flex; gap: 12px; padding: 9px 4px; border-bottom: 1px solid var(--border); align-items: center; }
.activity .item:last-child { border-bottom: 0; }
.activity .time { color: var(--muted); width: 70px; flex-shrink: 0; font-size: 12px; font-variant-numeric: tabular-nums; }
.activity .what { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 12px; font-weight: 600; background: var(--hover); color: var(--muted); }
.badge.event { background: #fef3c7; color: #92400e; }
.badge.session { background: var(--good-bg); color: var(--good); }
.badge.pageview { background: var(--primary-soft); color: var(--primary); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .badge.event { background: #3b2a0b; color: #fbbf24; } }
:root[data-theme="dark"] .badge.event { background: #3b2a0b; color: #fbbf24; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 20px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 500; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login form { width: 100%; max-width: 360px; display: grid; gap: 16px; }
.login .brand { justify-content: center; font-size: 22px; margin-bottom: 8px; }
.error { color: var(--bad); font-size: 13px; }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--hover); color: var(--muted); }

/* Popovers and dialogs */
.popover {
  position: absolute; z-index: 30; background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.12); padding: 12px; min-width: 240px;
}
.menu { display: grid; padding: 4px; min-width: 200px; }
.menu button { text-align: left; border: 0; background: none; padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover, .menu button.active { background: var(--hover); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }
.dialog-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; z-index: 40; padding: 16px; }
.dialog { width: 100%; max-width: 560px; max-height: 85vh; overflow: auto; background: var(--panel); border-radius: 12px; padding: 24px; display: grid; gap: 16px; }
.dialog.wide { max-width: 760px; }
.dialog .actions { display: flex; justify-content: flex-end; gap: 8px; }
.stack { display: grid; gap: 12px; }
.inline { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
code.snippet { display: block; padding: 12px; border-radius: 8px; background: var(--hover); font-size: 12px; white-space: pre-wrap; word-break: break-all; }
.checklist { display: grid; gap: 6px; max-height: 200px; overflow: auto; }
.checklist label { display: flex; gap: 8px; align-items: center; }
.loading { color: var(--faint); padding: 24px 0; text-align: center; }
.skeleton { background: linear-gradient(90deg, var(--hover), var(--border), var(--hover)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 6px; }
@keyframes shimmer { to { background-position: -200% 0; } }
