:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-alt: #f0f2f5;
  --border: #dfe3e8;
  --text: #1b1f24;
  --text-muted: #5b6472;
  --accent: #c8102e;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --ok: #1f7a4d;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px rgba(16, 24, 40, 0.05);
  /* 走势线故意压低存在感：形状交给它，数值交给上面的大字，末点才用主色点题。
     取值卡在对纯白 3:1 以上（实测 3.63:1），再浅就看不清了。 */
  --spark-line: #7d8798;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f24;
    --surface-alt: #23272e;
    --border: #333941;
    --text: #e8eaed;
    --text-muted: #9aa3b0;
    --accent: #ff5a6e;
    --accent-text: #1a0d10;
    --danger: #ff8080;
    --ok: #57c98a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --spark-line: #79838f;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar__brand {
  font-weight: 650;
  letter-spacing: 0.01em;
}

.topbar__user {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar__name {
  color: var(--text-muted);
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main {
  flex: 1;
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

.view__title {
  font-size: 20px;
  margin: 4px 0 16px;
}

.view__subtitle {
  margin: -12px 0 0;
  color: var(--text-muted);
  font-size: 13px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}

.card--narrow {
  max-width: 400px;
  margin: 8vh auto 0;
}

.card__title {
  font-size: 17px;
  margin: 0 0 6px;
}

.card__hint {
  color: var(--text-muted);
  font-size: 14px;
  margin: 0 0 16px;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form--grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

@media (min-width: 640px) {
  .form--grid {
    grid-template-columns: 1fr 1fr;
  }
  .form__actions {
    grid-column: 1 / -1;
  }
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field__label {
  font-size: 13px;
  color: var(--text-muted);
}

.field__input {
  width: 100%;
  padding: 11px 12px;
  font-size: 16px; /* 16px 可避免 iOS Safari 聚焦时自动放大页面 */
  color: var(--text);
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-family: inherit;
}

.field__input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 18px;
  font-size: 15px;
  font-family: inherit;
  font-weight: 550;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.button:disabled {
  opacity: 0.55;
  cursor: progress;
}

.button--primary {
  background: var(--accent);
  color: var(--accent-text);
}

.button--ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.button--small {
  min-height: 34px;
  padding: 0 12px;
  font-size: 13px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.stat__value {
  font-size: 24px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.stat__label {
  font-size: 13px;
  color: var(--text-muted);
}

.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table {
  width: 100%;
  min-width: 560px;
  border-collapse: collapse;
  font-size: 14px;
}

.table th,
.table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.table th {
  color: var(--text-muted);
  font-weight: 550;
  font-size: 13px;
}

.table tbody tr:last-child td {
  border-bottom: none;
}

.table-actions {
  display: flex;
  /* 每行现在有六个操作，不换行会把这一列撑到 500px 以上并顶出横向滚动 */
  flex-wrap: wrap;
  gap: 7px;
  min-width: 220px;
}

.secret,
.paircode {
  margin-top: 16px;
  padding: 14px;
  background: var(--surface-alt);
  border: 1px dashed var(--border);
  border-radius: 10px;
}

.secret__value,
.paircode__value {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 22px;
  letter-spacing: 0.12em;
  word-break: break-all;
  user-select: all;
}

.paircode__value {
  font-size: 32px;
  font-weight: 650;
}

.secret__note,
.paircode__note {
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

.kv {
  margin: 0 0 16px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 16px;
  font-size: 14px;
}

.kv dt {
  color: var(--text-muted);
}

.kv dd {
  margin: 0;
  word-break: break-all;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: min(92vw, 460px);
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 14px;
  z-index: 50;
}

.toast--error {
  border-color: var(--danger);
  color: var(--danger);
}

.toast--ok {
  border-color: var(--ok);
  color: var(--ok);
}

/* ---------- 实时看板 ---------- */

.live-entry,
.realtime-heading,
.panel-heading,
.connection {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.live-entry .card__hint {
  margin-bottom: 0;
}

.realtime-heading {
  align-items: end;
  margin: 2px 0 16px;
}

.realtime-heading__title {
  margin-top: 12px;
}

.session-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 13px;
}

.session-picker span {
  white-space: nowrap;
}

.session-picker .field__input {
  min-width: 190px;
  padding: 8px 10px;
  font-size: 14px;
}

.connection {
  justify-content: flex-start;
  flex-wrap: wrap;
  min-height: 44px;
  margin-bottom: 14px;
  padding: 9px 13px;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
}

.connection strong {
  color: var(--text);
}

.connection__dot {
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #9aa3b0;
  box-shadow: 0 0 0 4px color-mix(in srgb, #9aa3b0 16%, transparent);
}

.connection[data-state="live"] .connection__dot {
  background: var(--ok);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 16%, transparent);
}

.connection[data-state="delayed"] .connection__dot,
.connection[data-state="connecting"] .connection__dot {
  background: #b7791f;
  box-shadow: 0 0 0 4px color-mix(in srgb, #b7791f 16%, transparent);
}

.connection[data-state="offline"] .connection__dot {
  background: var(--danger);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--danger) 16%, transparent);
}

.realtime-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.metric-card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 12px 14px 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.metric-card--primary {
  border-top: 3px solid var(--accent);
  padding-top: 10px;
}

.metric-card__label,
.metric-card__sub {
  display: block;
  color: var(--text-muted);
  font-size: 12px;
}

.metric-card__label {
  font-weight: 600;
  letter-spacing: 0.01em;
}

.metric-card__value {
  display: block;
  margin: 1px 0 0;
  overflow: hidden;
  color: var(--text);
  font-size: 26px;
  /* 数值每几秒刷新一次，等宽数字避免位数变化时整块跟着抖 */
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.metric-card__sub {
  margin-top: 2px;
}

.metric-card__value--small {
  font-size: 19px;
  line-height: 1.55;
}

/* 单条序列、无坐标轴的走势小图：只交代形状，绝对值由上面的大数字负责。
   贴着卡片底部，让带图和不带图的卡片上半部分仍然对齐。 */
.sparkline {
  display: block;
  width: 100%;
  height: 34px;
  margin-top: auto;
  overflow: visible;
}

/* 间距只能加在上一个元素身上，不能用 .sparkline 自己的 padding：
   SVG 视口是内容盒，而 drawSparkline 用 getBoundingClientRect 量的是边框盒，
   两者不等时 preserveAspectRatio 会把整张图等比缩小再居中，左右各空一块。 */
.metric-card__sub:has(+ .sparkline) {
  margin-bottom: 8px;
}

.sparkline__line {
  fill: none;
  stroke: var(--spark-line);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sparkline__end {
  fill: var(--accent);
}

.realtime-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

.realtime-panel {
  margin-bottom: 0;
}

.panel-heading {
  align-items: flex-start;
  margin-bottom: 12px;
}

.panel-heading .card__hint {
  margin-bottom: 0;
}

.status-pill {
  flex: 0 0 auto;
  padding: 4px 9px;
  color: var(--text-muted);
  background: var(--surface-alt);
  border-radius: 999px;
  font-size: 12px;
}

.status-pill[data-state="live"] {
  color: var(--ok);
}

.health-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  margin: 0;
  overflow: hidden;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 9px;
}

.health-list div {
  padding: 10px 11px;
  background: var(--surface-alt);
}

.health-list dt {
  color: var(--text-muted);
  font-size: 12px;
}

.health-list dd {
  margin: 2px 0 0;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.ai-placeholder {
  background:
    linear-gradient(135deg, color-mix(in srgb, var(--accent) 7%, var(--surface)), var(--surface));
}

.ai-placeholder__eyebrow {
  margin-bottom: 8px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.08em;
}

.timeline-panel {
  margin-top: 14px;
}

.timeline-count {
  flex: 0 0 auto;
  color: var(--text-muted);
  font-size: 12px;
}

.timeline-empty {
  padding: 28px 12px;
  color: var(--text-muted);
  text-align: center;
}

.timeline-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0 0;
}

.filter-chip {
  padding: 5px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 12px;
  line-height: 1.4;
  cursor: pointer;
}

.filter-chip:hover {
  border-color: var(--text-muted);
}

.filter-chip[aria-pressed="true"] {
  border-color: currentColor;
  background: var(--accent-soft, rgba(37, 99, 168, 0.1));
  color: var(--accent, #2563a8);
  font-weight: 600;
}

.filter-chip:focus-visible {
  outline: 2px solid var(--accent, #2563a8);
  outline-offset: 2px;
}

.timeline-hidden {
  margin: 8px 0 0;
  color: var(--text-muted);
  font-size: 12px;
}

/* 事件流单独滚动，避免几百条把整页撑长、把行动建议挤出首屏 */
.timeline-scroll {
  max-height: 60vh;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.timeline {
  display: flex;
  flex-direction: column;
  gap: 0;
  margin: 0;
  padding: 0;
  list-style: none;
}

.timeline__item {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 12px;
  padding: 12px 0;
  border-top: 1px solid var(--border);
}

.timeline__time {
  color: var(--text-muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.timeline__topline {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px;
  margin-bottom: 3px;
}

.timeline__badge {
  padding: 2px 7px;
  color: var(--text-muted);
  background: var(--surface-alt);
  border-radius: 999px;
  font-size: 11px;
}

.timeline__badge[data-type="comment"] { color: #2563a8; }
.timeline__badge[data-type="gift"] { color: #b45309; }
.timeline__badge[data-type="transcript_final"] { color: #7c3aed; }
.timeline__badge[data-type="follow"],
.timeline__badge[data-type="share"] { color: var(--ok); }

.timeline__viewer {
  color: var(--text-muted);
  font-size: 12px;
}

.timeline__content {
  margin: 0;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.timeline__meta {
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 11px;
}

@media (min-width: 720px) {
  .realtime-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .realtime-layout {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 560px) {
  .live-entry,
  .realtime-heading {
    align-items: stretch;
    flex-direction: column;
  }

  .session-picker {
    align-items: stretch;
    flex-direction: column;
  }

  .session-picker .field__input {
    width: 100%;
  }

  .realtime-stats {
    grid-template-columns: 1fr 1fr;
  }

  .metric-card {
    padding: 12px;
  }

  .metric-card__value {
    font-size: 22px;
  }

  .timeline__item {
    grid-template-columns: 58px minmax(0, 1fr);
    gap: 8px;
  }

  .timeline-scroll {
    max-height: 55vh;
  }
}

[hidden] {
  display: none !important;
}

/* ---------- AI 行动建议 ---------- */

.ai-panel .panel-heading {
  align-items: flex-start;
}

.suggestions {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.suggestion {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--surface-alt);
  border-left: 3px solid var(--accent);
}

.suggestion__kind {
  align-self: start;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  white-space: nowrap;
}

.suggestion__action {
  font-size: 15px;
  line-height: 1.5;
}

.suggestion__why {
  grid-column: 2;
  font-size: 13px;
  color: var(--text-muted);
}

.ai-empty {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.ai-section {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.ai-section__title {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.ai-list {
  margin: 0;
  padding-left: 18px;
  font-size: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ai-sentiment {
  margin: 0 0 8px;
  font-size: 14px;
}

/* ---------- 导航卡片 ---------- */

.nav-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.nav-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-start;
  text-align: left;
  padding: 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  min-height: 64px;
}

.nav-card strong {
  font-size: 15px;
}

.nav-card span {
  font-size: 13px;
  color: var(--text-muted);
}

/* ---------- 报告 ---------- */

.notice {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  font-size: 14px;
  margin-bottom: 16px;
}

.notice--error {
  border-color: var(--danger);
  color: var(--danger);
}

.report-summary {
  margin: 0 0 16px;
  font-size: 15px;
  line-height: 1.7;
}

.peak-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

.peak-card {
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--surface-alt);
  border-left: 3px solid var(--ok);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.peak-card--low {
  border-left-color: var(--text-muted);
}

.peak-card__stat {
  font-size: 13px;
  color: var(--text-muted);
}

.peak-card__why {
  margin: 4px 0 0;
  font-size: 13px;
  line-height: 1.6;
}

/* ---------- 观众画像 ---------- */

.viewers-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

@media (min-width: 900px) {
  .viewers-layout {
    grid-template-columns: minmax(260px, 340px) 1fr;
    align-items: start;
  }
}

.viewer-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 60vh;
  overflow-y: auto;
}

.viewer-item {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 8px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}

.viewer-item strong {
  flex: 1 1 100%;
  font-size: 14px;
}

.viewer-item__meta {
  font-size: 12px;
  color: var(--text-muted);
}

.tag {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.tag--accent {
  border-color: var(--accent);
  color: var(--accent);
}

.profile-block {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.profile-block__head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
}

.profile-block__label {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--text-muted);
}

.profile-block__confidence {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: auto;
}

.evidence-list {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.evidence-list li {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
  font-size: 13px;
  padding-left: 10px;
  border-left: 2px solid var(--border);
}

.evidence-list__time {
  color: var(--text-muted);
  font-size: 12px;
}
