/* web_statistics · Layout — Reset + body 网格 + Topbar + Sidebar + Main + page-header
   加载顺序：tokens.css → layout.css → components.css → responsive.css
*/
/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
html { overflow-x: clip; }  /* 兜底：阻止 topbar 溢出撑大 body */
body {
  background:
    radial-gradient(1100px 540px at 90% -10%, rgba(99, 102, 241, 0.08), transparent 60%),
    radial-gradient(900px 480px at 0% 110%, rgba(99, 102, 241, 0.04), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs-md);     /* body baseline */
  line-height: 1.55;
  letter-spacing: -0.005em;
  font-feature-settings: var(--font-features);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas:
    "topbar  topbar"
    "sidebar main";
  min-height: 100vh;
  text-rendering: optimizeLegibility;
}

/* Headings inherit the family's tighter tracking — important for the new 24px page title */
h1, h2, h3, h4 {
  letter-spacing: -0.015em;
}

/* Scrollbar */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: var(--r-pill);
  border: 2px solid transparent;
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--border-strong); background-clip: padding-box; }

/* ====================================================================
   Topbar (品牌 / 上下文 / 状态)
   ==================================================================== */
.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 24px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  height: var(--topbar-h);
  position: sticky;
  top: 0;
  z-index: 50;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.topbar-brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding-right: 16px;
  border-right: 1px solid var(--border-subtle);
  height: 28px;
  margin-right: 4px;
  flex-shrink: 0;
}
.brand-mark {
  width: 22px;
  height: 22px;
  display: block;
  border-radius: 6px;
  object-fit: cover;
  /* drop-shadow 删除：让 indigo 渐变自己说话，不靠光晕点缀 */
}
.brand-text {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
}
.brand-name {
  background: var(--gradient-brand);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  font-size: 15px;                /* was 14 — more prominent */
  font-weight: 700;
  letter-spacing: -0.02em;
}
.topbar-context {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 auto;
  min-width: 0;
  position: relative;
}
.ctx-label {
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  color: var(--text-tertiary);
  letter-spacing: var(--tracking-body);
  /* sentence case：caps eyebrow 是 AI 通用仪表盘最显眼的指纹 */
  flex-shrink: 0;
}
.ctx-divider {
  width: 1px;
  height: 18px;
  background: var(--border-subtle);
  flex-shrink: 0;
  margin: 0 2px;
}

.topbar-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* Topbar select */
.topbar-context .select-wrap { width: 240px; }
.select-wrap { position: relative; }
.select-wrap select {
  width: 100%;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background: var(--surface-1);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 8px 32px 8px 13px;
  font-size: var(--fs-base);
  font-family: inherit;
  font-weight: var(--fw-medium);
  outline: none;
  cursor: pointer;
  transition:
    border-color 0.15s var(--ease),
    background 0.15s var(--ease),
    box-shadow 0.15s var(--ease);
}
.select-wrap select:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
}
.select-wrap select:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.select-caret {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 12px;
  height: 12px;
  color: var(--text-tertiary);
  pointer-events: none;
}

/* Segmented (range) */
.segmented {
  display: inline-flex;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 3px;
  gap: 2px;
}
.segmented button {
  background: transparent;
  color: var(--text-tertiary);
  border: none;
  border-radius: var(--r-xs);
  padding: 5px 13px;
  cursor: pointer;
  font-size: var(--fs-base);
  font-weight: var(--fw-medium);
  font-family: inherit;
  letter-spacing: var(--tracking-body);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  flex-shrink: 0;
  transition:
    color 0.15s var(--ease),
    background 0.15s var(--ease),
    transform 0.1s var(--ease);
}
.segmented button svg { width: 12px; height: 12px; }
.segmented button:hover { color: var(--text); }
.segmented button:active { transform: scale(0.97); }
.segmented button.active {
  background: var(--surface-3);
  color: var(--text);
  box-shadow:
    inset 0 0 0 1px var(--border),
    0 1px 2px var(--button-shadow);
}

/* Custom-range popover anchored under the segmented control */
.ctx-popover {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  width: 280px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 60;
  animation: ctx-popover-in 0.18s var(--ease-out);
}
.ctx-popover[hidden] { display: none; }
@keyframes ctx-popover-in {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Status pills in topbar */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  font-weight: var(--fw-medium);
  white-space: nowrap;
  cursor: default;
  transition: border-color 0.2s var(--ease), background 0.15s var(--ease), color 0.15s var(--ease);
}
.status-pill:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
  color: var(--text);
}
.status-pill b { color: var(--text); font-weight: var(--fw-bold); }
.status-pill .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 0 var(--success-soft);
  flex-shrink: 0;
}
#online-pill .dot {
  animation: pulse 1.8s var(--ease) infinite;
}
#health-pill[data-state="ok"] .dot { background: var(--success); }
#health-pill[data-state="fail"] .dot { background: var(--danger); }
#health-pill[data-state="fail"] { color: var(--danger); }
#health-pill[data-state="unknown"] .dot { background: var(--text-tertiary); animation: none; }
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 var(--success-ring); }
  70%  { box-shadow: 0 0 0 7px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

/* Icon-only button */
.icon-btn {
  width: 32px;                     /* was 30 */
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r);
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease), color 0.15s var(--ease);
}
.icon-btn:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
  color: var(--text);
}
.icon-btn svg { width: 14px; height: 14px; }

/* Theme toggle：2 SVG 按 data-mode 切换可见（dark / light） */
.theme-toggle .theme-icon { display: none; }
html[data-mode="dark"]  .theme-toggle .theme-icon-dark  { display: block; }
html[data-mode="light"] .theme-toggle .theme-icon-light { display: block; }
/* 兜底：未设置 mode（理论上防闪烁 inline script 会设） → 默认 dark */
html:not([data-mode]) .theme-toggle .theme-icon-dark { display: block; }

/* User menu popover */
.user-menu { position: relative; }
.user-menu-pop {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 200px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  z-index: 60;
  animation: ctx-popover-in 0.18s var(--ease-out);
}
.user-menu-pop[hidden] { display: none; }
.user-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  background: transparent;
  border: none;
  border-radius: var(--r-sm);
  color: var(--text-secondary);
  font-size: var(--fs-base);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s var(--ease), color 0.12s var(--ease);
}
.user-menu-item:hover { background: var(--surface-hover); color: var(--text); }
.user-menu-item svg { width: 14px; height: 14px; flex-shrink: 0; color: var(--text-tertiary); }
.user-menu-item:hover svg { color: var(--text-secondary); }
.user-menu-item.user-menu-danger { color: var(--danger); }
.user-menu-item.user-menu-danger svg { color: var(--danger); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: var(--r);
  padding: 8px 14px;
  font-size: var(--fs-base);
  font-weight: var(--fw-semibold);
  font-family: inherit;
  letter-spacing: var(--tracking-body);
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
  transition:
    background 0.15s var(--ease),
    border-color 0.15s var(--ease),
    color 0.15s var(--ease),
    transform 0.1s var(--ease),
    box-shadow 0.15s var(--ease);
  user-select: none;
  -webkit-user-select: none;
}
.btn:active { transform: scale(0.985); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn-icon { width: 13px; height: 13px; flex-shrink: 0; }

.btn-primary {
  background: var(--gradient-brand);
  color: #fff;
  border-color: var(--on-accent-border);
  box-shadow:
    inset 0 1px 0 var(--highlight-line),
    0 1px 2px var(--button-shadow);
}
.btn-primary:hover {
  filter: brightness(1.08);
  box-shadow:
    inset 0 1px 0 var(--highlight-line),
    0 4px 14px var(--ring-medium),
    0 1px 2px var(--button-shadow);
}

.btn-accent {
  background: var(--accent-soft);
  color: var(--accent-bright);
  border-color: var(--ring-medium);
}
.btn-accent:hover {
  background: var(--ring-medium);
  border-color: var(--ring-strong);
  color: var(--accent-bright);
}

.btn-ghost {
  background: var(--ghost-tint);
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--ghost-tint-hover); border-color: var(--border-strong); }

.btn-sm { padding: 5px 11px; font-size: var(--fs-sm); min-height: 28px; }

.btn-icon-only {
  background: transparent;
  border: none;
  color: var(--text-tertiary);
  font-size: 18px;
  line-height: 1;
  width: 24px;
  height: 24px;
  border-radius: var(--r-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s var(--ease), color 0.15s var(--ease);
}
.btn-icon-only:hover { background: var(--ghost-tint-hover); color: var(--text); }

/* ====================================================================
   Sidebar (嵌入代码 + 元数据)
   ==================================================================== */
.sidebar {
  grid-area: sidebar;
  background: var(--bg-elevated);   /* dark 默认，与 topbar 同色但内部 textarea/section 已有 surface 阶梯 */
  border-right: 1px solid var(--border);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 28px;
  min-height: calc(100vh - var(--topbar-h));
}

.sidebar-section { display: flex; flex-direction: column; gap: 12px; }
.sidebar-section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0 2px;
}
.section-title {
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text);
  letter-spacing: var(--tracking-body);
  /* sentence case */
}
.section-hint {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.sidebar textarea {
  width: 100%;
  background: var(--surface-1);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 11px 13px;
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  line-height: 1.65;
  resize: vertical;
  outline: none;
  min-height: 180px;                /* was 92 — 默认完整显示嵌入代码，留 8 行余量 */
  transition: border-color 0.15s var(--ease), background 0.15s var(--ease);
}
.sidebar textarea:hover { background: var(--surface-2); border-color: var(--border-strong); }
/* .sidebar textarea:focus 由下方全局规则(行 ~1189)覆盖,避免重复 */

.sidebar .btn-primary { width: 100%; min-height: 34px; }

.sidebar-meta {
  padding-top: 24px;
  border-top: 1px solid var(--border-subtle);
}
.meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 2px;
  font-size: var(--fs-base);
  border-bottom: 1px dashed var(--border-subtle);
}
.meta-row:last-child { border-bottom: none; }
.meta-row-label {
  color: var(--text-tertiary);
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  letter-spacing: var(--tracking-body);
}
.meta-row-value {
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  font-weight: var(--fw-medium);
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.meta-fresh[data-fresh="stale"] { color: var(--text-muted); }
.fresh-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 6px var(--success-ring);
}
.meta-fresh[data-fresh="stale"] .fresh-dot { background: var(--text-muted); box-shadow: none; }

/* ====================================================================
   Main
   ==================================================================== */
.main {
  grid-area: main;
  padding: 28px 40px 64px;          /* was 20 32 56 — more breathing room */
  display: flex;
  flex-direction: column;
  gap: 20px;                        /* was 16 */
  min-width: 0;
  max-width: var(--content-max);
  width: 100%;
}

/* Page header (slim, no eyebrow stripe) */
.page-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  margin: 0 0 4px;                  /* tiny bottom breath before KPIs */
  flex-wrap: wrap;
}
/* 标题 + 副标题改为上下堆叠：标题清晰、副标题一行短描述 + 紧凑 site id badge，
   避免长 site id 把整行挤爆或与相邻列视觉重叠。 */
.page-header-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}
.page-title {
  margin: 0;
  font-size: var(--fs-2xl);         /* 22px；KPI 36px 才是真正主角 */
  font-weight: var(--fw-bold);
  letter-spacing: var(--tracking-tight);
  color: var(--text);
  line-height: 1.2;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
.page-subtitle {
  margin: 0;
  color: var(--text-secondary);
  font-size: var(--fs-base);
  font-variant-numeric: tabular-nums;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  max-width: 100%;
}
.page-subtitle .site-id-pill {
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  padding: 2px 8px;
  border-radius: var(--r-xs);
  background: var(--surface-2);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 260px;
}

/* Global error */
.global-error {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid var(--danger-ring);
  border-radius: var(--r-md);
  font-size: var(--fs-base);
}
.global-error[hidden] { display: none; }
.global-error #global-error-msg { flex: 1; }

/* Light theme sidebar：让侧栏比 topbar（--bg-elevated=#ffffff）暗一档（--surface-2=#f5f5f7），
   视觉上 topbar 浮起 / sidebar 凹陷 / textarea（--surface-1=#ffffff）再浮起 → 三级阶梯。
   Dark 主题保持原状（topbar 与 sidebar 同 --bg-elevated 已天然有层次，因 main body 比 bg 更暗）。 */
[data-mode="light"] .sidebar { background: var(--surface-2); }

