/* yd-analytics frontend common styles.
   Sits on top of Pico.css. Keep rules minimal and layout-only.

   Theming:
   - Light by default (Pico's defaults)
   - Dark via :root[data-theme="dark"]
   - Pico's own colors flip via [data-theme="dark"]; we override our own
     variables here for the yd-* utilities.
*/

:root {
  --yd-radius: 6px;
  --yd-border: #d0d7de;
  --yd-muted: #6e7781;
  --yd-bg-soft: #f6f8fa;
  --yd-card-bg: #fff;
  --yd-fg: #1f2328;
  --yd-good: #2da44e;
  --yd-warn: #bf8700;
  --yd-bad:  #cf222e;
  --yd-accent: #1f6feb;
  --yd-heatmap-low: #fde2e2;
  --yd-heatmap-high: #1f6feb;
  --yd-nav-bg: #fff;
  --yd-table-hover: #fafbfc;
}

:root[data-theme="dark"] {
  --yd-border: #30363d;
  --yd-muted: #8b949e;
  --yd-bg-soft: #161b22;
  --yd-card-bg: #0d1117;
  --yd-fg: #e6edf3;
  --yd-good: #3fb950;
  --yd-warn: #d29922;
  --yd-bad:  #f85149;
  --yd-accent: #58a6ff;
  --yd-heatmap-low: #161b22;
  --yd-heatmap-high: #1f6feb;
  --yd-nav-bg: #161b22;
  --yd-table-hover: #1c2128;
}

body {
  padding-top: 56px;
  color: var(--yd-fg);
  background: var(--yd-bg-soft);
}

/* ----- nav ----- */
.yd-nav {
  position: fixed; top: 0; left: 0; right: 0; height: 48px;
  display: flex; align-items: center; gap: 16px;
  padding: 0 24px;
  background: var(--yd-nav-bg); border-bottom: 1px solid var(--yd-border);
  z-index: 100;
  font-size: 14px;
}
.yd-nav a {
  color: var(--yd-accent); text-decoration: none; padding: 4px 8px;
  border-radius: 4px;
}
.yd-nav a:hover { background: var(--yd-bg-soft); }
.yd-nav-right {
  margin-left: auto; display: flex; align-items: center; gap: 12px;
}
.yd-nav-user { color: var(--yd-muted); }

/* ----- buttons ----- */
.yd-btn {
  display: inline-block;
  padding: 6px 14px;
  border: 1px solid var(--yd-border);
  background: var(--yd-card-bg);
  color: var(--yd-fg);
  border-radius: var(--yd-radius);
  cursor: pointer;
  font-size: 14px;
  line-height: 1.4;
  text-decoration: none;
}
.yd-btn:hover { background: var(--yd-bg-soft); }
.yd-btn-sm { padding: 3px 10px; font-size: 12px; }
.yd-btn-primary { background: var(--yd-accent); color: #fff; border-color: var(--yd-accent); }
.yd-btn-primary:hover { filter: brightness(0.9); }
.yd-btn-danger { background: var(--yd-bad); color: #fff; border-color: var(--yd-bad); }
.yd-btn-danger:hover { filter: brightness(0.9); }
.yd-btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ----- cards ----- */
.yd-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin: 12px 0 24px;
}
.yd-card {
  background: var(--yd-card-bg);
  border: 1px solid var(--yd-border);
  border-radius: var(--yd-radius);
  padding: 14px 16px;
}
.yd-card-label { color: var(--yd-muted); font-size: 12px; margin-bottom: 4px; }
.yd-card-value { font-size: 24px; font-weight: 600; color: var(--yd-fg); }
.yd-card-sub   { color: var(--yd-muted); font-size: 12px; margin-top: 2px; }

/* ----- filters bar ----- */
.yd-filters {
  display: flex; flex-wrap: wrap; align-items: end; gap: 12px;
  background: var(--yd-card-bg); border: 1px solid var(--yd-border);
  border-radius: var(--yd-radius);
  padding: 12px 14px; margin-bottom: 16px;
}
.yd-filters label { display: flex; flex-direction: column; font-size: 12px; color: var(--yd-muted); gap: 2px; }
.yd-filters input, .yd-filters select {
  padding: 4px 8px; border: 1px solid var(--yd-border);
  border-radius: 4px; font-size: 14px; min-width: 120px;
  background: var(--yd-card-bg); color: var(--yd-fg);
}
.yd-filters .yd-actions { display: flex; gap: 8px; align-items: flex-end; }

/* ----- sections ----- */
.yd-section { background: var(--yd-card-bg); border: 1px solid var(--yd-border); border-radius: var(--yd-radius); padding: 16px; margin-bottom: 16px; }
.yd-section h2 { margin: 0 0 12px; font-size: 16px; }
.yd-section h3 { margin: 0 0 8px; font-size: 14px; color: var(--yd-muted); }

/* ----- tables ----- */
.yd-table { width: 100%; border-collapse: collapse; font-size: 13px; color: var(--yd-fg); }
.yd-table th, .yd-table td {
  border-bottom: 1px solid var(--yd-border);
  padding: 6px 8px;
  text-align: right;
}
.yd-table th:first-child, .yd-table td:first-child { text-align: left; }
.yd-table thead th { background: var(--yd-bg-soft); color: var(--yd-muted); font-weight: 600; }
.yd-table tr:hover td { background: var(--yd-table-hover); }
.yd-table .num { font-variant-numeric: tabular-nums; }
/* 需要左对齐的列（日期等） */
.yd-table .yd-left { text-align: left; }
/* 「域名」列里的「合并」徽章 */
.yd-table td .yd-badge { margin-left: 6px; vertical-align: middle; }

/* 广告数据列：默认隐藏，只在「合并」模式（表上加 .yd-with-ad）显示。
   广告数据没有 app 维度，只有跨 app 合并时才有意义。 */
.yd-table .ad-col { display: none; }
.yd-table.yd-with-ad .ad-col { display: table-cell; }
.yd-table .ad-col { white-space: nowrap; }
.yd-table thead .ad-group { text-align: center; }
/* 「点击差」偏差超阈值时高亮（黄底加粗） */
.yd-table td.yd-diff-warn {
  background: rgba(191, 135, 0, 0.14);
  color: var(--yd-warn);
  font-weight: 700;
}
.yd-table tr:hover td.yd-diff-warn { background: rgba(191, 135, 0, 0.22); }

/* ----- retention heatmap ----- */
.yd-heat-table { border-collapse: separate; border-spacing: 2px; font-size: 12px; }
.yd-heat-table th, .yd-heat-table td { padding: 0; text-align: center; }
.yd-heat-table th { color: var(--yd-muted); font-weight: 600; padding: 4px 6px; }
.yd-heat-cohort { text-align: left !important; padding: 4px 8px !important; color: var(--yd-fg); }
.yd-heat-cell {
  display: inline-block; min-width: 48px; padding: 4px 6px;
  border-radius: 4px; color: #fff; font-variant-numeric: tabular-nums;
}
.yd-heat-cell-empty { color: var(--yd-muted); }

/* ----- login ----- */
.yd-login-card {
  max-width: 360px; margin: 80px auto; padding: 24px;
  background: var(--yd-card-bg); border: 1px solid var(--yd-border);
  border-radius: 8px;
}
.yd-login-card h1 { margin: 0 0 16px; font-size: 20px; text-align: center; }
.yd-login-card label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--yd-muted); }
.yd-login-card input { width: 100%; box-sizing: border-box; padding: 8px 10px; border: 1px solid var(--yd-border); border-radius: 4px; font-size: 14px; background: var(--yd-card-bg); color: var(--yd-fg); }
.yd-login-card .yd-btn { width: 100%; margin-top: 8px; }
.yd-login-error { color: var(--yd-bad); font-size: 13px; min-height: 18px; margin-top: 8px; }

/* ----- lists ----- */
.yd-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.yd-list-item {
  display: block; padding: 12px 14px; background: var(--yd-card-bg);
  border: 1px solid var(--yd-border); border-radius: var(--yd-radius);
  text-decoration: none; color: var(--yd-fg);
}
.yd-list-item:hover { border-color: var(--yd-accent); }
.yd-list-item .name { font-weight: 600; margin-bottom: 2px; }
.yd-list-item .sub { color: var(--yd-muted); font-size: 12px; }

/* ----- tabs ----- */
.yd-tabs { display: flex; gap: 8px; border-bottom: 1px solid var(--yd-border); margin-bottom: 12px; overflow-x: auto; }
.yd-tab {
  padding: 8px 14px; cursor: pointer; color: var(--yd-muted);
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.yd-tab.active { color: var(--yd-accent); border-bottom-color: var(--yd-accent); }
.yd-tab-panel { display: none; }
.yd-tab-panel.active { display: block; }

/* ----- misc ----- */
.yd-row { display: flex; align-items: center; gap: 8px; }
.yd-muted { color: var(--yd-muted); }
.yd-error { color: var(--yd-bad); }
.yd-nowrap { white-space: nowrap; }
canvas { max-width: 100%; }
.yd-chart-wrap { position: relative; height: 280px; }

/* ----- wide layout (system config page) ----- */
.yd-wide { max-width: 1680px; }

/* ----- code / JSON editor ----- */
.yd-code-area {
  width: 100%;
  box-sizing: border-box;
  min-height: 52vh;
  height: calc(100vh - 320px);
  max-height: 88vh;
  padding: 12px 14px;
  border: 1px solid var(--yd-border);
  border-radius: var(--yd-radius);
  background: var(--yd-card-bg);
  color: var(--yd-fg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  line-height: 1.55;
  resize: vertical;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}
.yd-code-area:focus { outline: 2px solid var(--yd-accent); outline-offset: -1px; }
.yd-code-area::placeholder { color: var(--yd-muted); opacity: 0.7; }

/* editor toolbar (above the big textarea) */
.yd-editor-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; flex-wrap: wrap; margin-bottom: 8px;
}
.yd-editor-head h2 { margin: 0; }
.yd-editor-bar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap; margin-top: 10px;
}

/* ----- modal ----- */
.yd-modal {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.yd-modal-mask { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }
.yd-modal-panel {
  position: relative; display: flex; flex-direction: column;
  width: min(1000px, 96vw); max-height: 88vh;
  background: var(--yd-card-bg); border: 1px solid var(--yd-border);
  border-radius: 8px; box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}
.yd-modal-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--yd-border);
}
.yd-modal-head h3 { margin: 0; font-size: 15px; }
.yd-modal-body { padding: 14px 16px; overflow: auto; }
.yd-modal-foot {
  padding: 10px 16px; border-top: 1px solid var(--yd-border);
  display: flex; justify-content: flex-end; gap: 8px;
}

/* ----- 合并配置弹窗（广告统计页） ----- */
.yd-merge-item {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 12px; margin-bottom: 10px;
  border: 1px solid var(--yd-border); border-radius: var(--yd-radius);
  background: var(--yd-bg-soft);
}
.yd-merge-item:last-child { margin-bottom: 0; }
.yd-merge-item .name { font-weight: 600; color: var(--yd-fg); }
.yd-merge-item .sub { color: var(--yd-muted); font-size: 12px; margin-top: 2px; }

.yd-merge-group {
  padding: 10px 12px; margin-bottom: 12px;
  border: 1px solid var(--yd-border); border-radius: var(--yd-radius);
}
.yd-merge-group input[type="text"] {
  flex: 1; min-width: 0; padding: 4px 8px; font-size: 14px;
  border: 1px solid var(--yd-border); border-radius: 4px;
  background: var(--yd-card-bg); color: var(--yd-fg);
}
.yd-merge-domains {
  display: flex; flex-wrap: wrap; gap: 6px 16px;
  max-height: 220px; overflow: auto;
}
.yd-merge-domain {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--yd-fg); cursor: pointer;
}
.yd-merge-domain input { margin: 0; }

/* ----- 表单弹层（数据源管理页等） ----- */
.yd-form-row {
  display: flex; flex-direction: column; gap: 4px;
  margin-bottom: 10px;
}
.yd-form-label {
  font-size: 12px; color: var(--yd-muted);
}
.yd-form-row input, .yd-form-row textarea {
  width: 100%; box-sizing: border-box;
  padding: 6px 10px; font-size: 14px;
  border: 1px solid var(--yd-border); border-radius: 4px;
  background: var(--yd-card-bg); color: var(--yd-fg);
}

/* ----- status badge (已应用 / 未应用) ----- */
.yd-status {
  display: inline-block; padding: 2px 11px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap; line-height: 1.6;
}
.yd-status-applied {
  background: var(--yd-good); color: #fff;
  box-shadow: 0 0 0 3px rgba(45, 164, 78, 0.18);
}
.yd-status-not-applied {
  background: transparent; color: var(--yd-muted);
  border: 1px solid var(--yd-border);
}
/* 已应用的整行高亮 */
.yd-table tr.yd-row-applied td { background: rgba(45, 164, 78, 0.08); }
.yd-table tr.yd-row-applied:hover td { background: rgba(45, 164, 78, 0.14); }
.yd-table tr.yd-row-applied td:first-child { box-shadow: inset 3px 0 0 var(--yd-good); }

/* ----- busy mask (长耗时操作期间锁住页面) ----- */
.yd-busy {
  position: fixed; inset: 0; z-index: 2000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.5);
}
.yd-busy-card {
  min-width: 300px; max-width: 90vw; padding: 26px 34px; text-align: center;
  background: var(--yd-card-bg); border: 1px solid var(--yd-border);
  border-radius: 10px; box-shadow: 0 16px 40px rgba(0, 0, 0, 0.3);
}
.yd-spinner {
  width: 34px; height: 34px; margin: 0 auto 14px;
  border: 3px solid var(--yd-border); border-top-color: var(--yd-accent);
  border-radius: 50%; animation: yd-spin 0.8s linear infinite;
}
@keyframes yd-spin { to { transform: rotate(360deg); } }
.yd-busy-title { font-size: 15px; font-weight: 600; color: var(--yd-fg); }
.yd-busy-sub { margin-top: 6px; font-size: 13px; color: var(--yd-muted); }

/* ----- execution result / records ----- */
.yd-exec-meta {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin-bottom: 10px; font-size: 13px; color: var(--yd-muted);
}
.yd-badge {
  display: inline-block; padding: 1px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 600; color: #fff; white-space: nowrap;
}
.yd-badge-ok { background: var(--yd-good); }
.yd-badge-bad { background: var(--yd-bad); }
.yd-badge-warn { background: var(--yd-warn); }
.yd-badge-info { background: var(--yd-accent); }
.yd-exec-output {
  margin: 0; padding: 12px 14px;
  background: var(--yd-bg-soft); border: 1px solid var(--yd-border);
  border-radius: var(--yd-radius);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 12.5px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-word;
  max-height: 56vh; overflow: auto;
}
.yd-exec-item {
  border: 1px solid var(--yd-border); border-radius: var(--yd-radius);
  padding: 10px 12px; margin-bottom: 10px;
}
.yd-exec-item:last-child { margin-bottom: 0; }

/* ----- skeletons ----- */
.yd-skel {
  background: linear-gradient(90deg,
    var(--yd-bg-soft) 0%, var(--yd-border) 50%, var(--yd-bg-soft) 100%);
  background-size: 200% 100%;
  animation: yd-skel 1.2s linear infinite;
  border-radius: 4px;
  color: transparent;
  display: inline-block;
  min-width: 60px;
}
@keyframes yd-skel { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ----- theme toggle (in nav) ----- */
.yd-theme-toggle {
  background: transparent; border: 1px solid var(--yd-border);
  border-radius: 4px; padding: 2px 8px; cursor: pointer; color: var(--yd-fg);
  font-size: 14px; line-height: 1.4;
}
.yd-theme-toggle:hover { background: var(--yd-bg-soft); }

/* ============================================================
   Mobile / responsive (≤ 720px)
   ============================================================ */
@media (max-width: 720px) {
  body { padding-top: 48px; }

  .yd-nav { padding: 0 12px; gap: 8px; font-size: 13px; }
  .yd-nav a { padding: 2px 4px; }
  .yd-nav-right { gap: 6px; }
  .yd-nav-user { display: none; }

  main.container { padding: 0 12px; }
  h1 { font-size: 20px; }

  /* cards stack into a single column */
  .yd-cards { grid-template-columns: 1fr; }

  /* filters: each control takes full width on its own line */
  .yd-filters { flex-direction: column; align-items: stretch; }
  .yd-filters label { width: 100%; }
  .yd-filters input, .yd-filters select { width: 100%; box-sizing: border-box; min-width: 0; }
  .yd-filters .yd-actions { width: 100%; }
  .yd-filters .yd-actions .yd-btn { flex: 1; }

  /* tables: keep horizontal scroll inside a wrapper rather than the whole page */
  .yd-section > div[style*="overflow-x"] { margin: 0 -16px; padding: 0 16px; }

  /* nav becomes scrollable horizontally if there are many items */
  .yd-nav { overflow-x: auto; }
  .yd-nav a { white-space: nowrap; }

  .yd-card { padding: 10px 12px; }
  .yd-card-value { font-size: 20px; }
}
