:root {
  --bg: #0f1720;
  --bg-card: #182230;
  --bg-card-active: #1f2c3d;
  --text: #e6edf3;
  --text-dim: #8b98a5;
  --text-faint: #5b6673;
  --border: #26313f;
  --up: #ff4d4f;      /* 涨：红 */
  --down: #16c784;    /* 跌：绿 */
  --flat: #8b98a5;    /* 平：中性 */
  --accent: #f0b90b;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* 浅色（白天）主题 */
body.light {
  --bg: #f4f6fa;
  --bg-card: #ffffff;
  --bg-card-active: #eef2f7;
  --text: #1b2430;
  --text-dim: #5b6673;
  --text-faint: #98a2af;
  --border: #e3e8ef;
  --up: #e5484d;
  --down: #12a150;
  --flat: #8b98a5;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  background: #0a0f16; /* 桌面端居中后两侧的底色 */
  transition: background .2s;
}
html.light-bg { background: #dfe4ec; }
body {
  margin: 0 auto;               /* 居中，避免桌面端过宽导致图表巨大/只显示一半 */
  padding: 0;
  max-width: 480px;             /* 以移动端宽度为上限 */
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  box-shadow: 0 0 40px rgba(0, 0, 0, .5);
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: linear-gradient(180deg, #131c27 0%, #0f1720 100%);
  padding: calc(12px + var(--safe-top)) 16px 12px;
  border-bottom: 1px solid var(--border);
}
body.light .app-header {
  background: linear-gradient(180deg, #ffffff 0%, #f4f6fa 100%);
}
.header-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.app-title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 1px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.app-logo {
  display: block;
  border-radius: 7px;
  flex-shrink: 0;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.refresh-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  transition: transform .2s;
}
.refresh-btn:active { transform: scale(.92); }
.refresh-btn.is-spinning svg { animation: spin .8s linear infinite; }
.refresh-btn:disabled { opacity: .5; cursor: not-allowed; }
@keyframes spin { to { transform: rotate(360deg); } }

.header-sub {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-dim);
}
.update-time { white-space: nowrap; }

/* 主题切换：纯图标滑动开关 */
.theme-toggle {
  position: relative;
  width: 52px;
  height: 30px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-card);
  cursor: pointer;
  padding: 0;
}
.theme-knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: #1a1a1a;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .25s cubic-bezier(.4, 0, .2, 1);
}
body.light .theme-knob { transform: translateX(22px); }
.theme-knob .icon-sun { display: none; }
body.light .theme-knob .icon-sun { display: block; }
body.light .theme-knob .icon-moon { display: none; }

/* ---------- Unit toggle（收窄，位于刷新按钮左侧） ---------- */
.unit-toggle {
  position: relative;
  display: flex;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
}
.unit-slider {
  position: absolute;
  top: 3px;
  left: 3px;
  width: calc((100% - 6px) / 3);
  height: calc(100% - 6px);
  background: var(--accent);
  border-radius: 999px;
  transition: transform .25s cubic-bezier(.4, 0, .2, 1);
}
.unit-option {
  position: relative;
  z-index: 1;
  min-width: 34px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 9px;
  cursor: pointer;
  transition: color .2s;
  white-space: nowrap;
}
.unit-option.is-active { color: #1a1a1a; }

/* ---------- Status bar ---------- */
.status-bar {
  margin: 10px 16px 0;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.status-bar[hidden] { display: none !important; }   /* 修复：避免被 display:flex 覆盖导致一直显示 */
.status-bar.is-loading { background: #1a2632; color: var(--text-dim); }
.status-bar.is-error   { background: #2a1a1c; color: #ff8f8f; }
.status-bar .retry-link {
  color: var(--accent);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

/* ---------- Metal list ---------- */
.metal-list { padding: 8px 12px 0; }

.metal-row {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  overflow: hidden;
  transition: background .2s;
}
.metal-row.is-expanded { background: var(--bg-card-active); }

.row-head {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  cursor: pointer;
  gap: 12px;
}
.cat-info { flex: 1; min-width: 0; }
.cat-name {
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}
.spec-count {
  font-size: 11px;
  color: var(--text-faint);
  background: rgba(255,255,255,.06);
  padding: 1px 6px;
  border-radius: 6px;
}
.tts-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(240, 185, 11, .14);
  color: var(--accent);
  cursor: pointer;
  vertical-align: middle;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
}
.tts-btn:active { transform: scale(.88); background: rgba(240, 185, 11, .3); }
.spec-name .tts-btn { width: 20px; height: 20px; }
.cat-price-main {
  text-align: right;
  min-width: 0;
}
.price-value {
  font-size: 18px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.price-unit { font-size: 12px; color: var(--text-dim); margin-left: 2px; }
.price-na { color: var(--text-faint); font-size: 14px; font-weight: 500; }

.change-badge {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 12px;
  font-weight: 600;
  margin-top: 3px;
  font-variant-numeric: tabular-nums;
}
.change-badge.up   { color: var(--up); }
.change-badge.down { color: var(--down); }
.change-badge.flat { color: var(--flat); }
.change-badge .arrow { font-size: 11px; }
.change-none { font-size: 12px; color: var(--text-faint); margin-top: 3px; }

.expand-caret {
  color: var(--text-faint);
  transition: transform .25s;
  flex-shrink: 0;
}
.metal-row.is-expanded .expand-caret { transform: rotate(180deg); }

/* ---------- Spec sub-rows ---------- */
.spec-list {
  border-top: 1px dashed var(--border);
  padding: 4px 16px 8px;
}
.spec-item {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 7px 0;
  font-size: 14px;
}
.spec-name { color: var(--text-dim); }
.spec-price { font-variant-numeric: tabular-nums; font-weight: 600; }

/* ---------- Trend chart (expandable) ---------- */
.trend-panel {
  max-height: 0;
  overflow: hidden;
  transition: max-height .3s ease;
}
.metal-row.is-expanded .trend-panel { max-height: 360px; }
.trend-inner {
  padding: 4px 12px 16px;
  border-top: 1px solid var(--border);
}
.trend-title {
  font-size: 12px;
  color: var(--text-dim);
  margin: 10px 4px 6px;
  display: flex;
  justify-content: space-between;
}
.trend-chart { width: 100%; height: 150px; display: block; }
.trend-empty {
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
  padding: 28px 0;
}
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; }
.chart-area { fill: rgba(240,185,11,.10); stroke: none; }
.chart-dot  { fill: var(--accent); }
.chart-gap  { stroke: var(--text-faint); stroke-dasharray: 3 3; stroke-width: 1; }
.chart-axis-label { fill: var(--text-faint); font-size: 10px; }

/* 走势图十字准星与浮窗（触摸/鼠标滑动查看某日价格） */
.trend-chart-wrap { position: relative; touch-action: none; }
.chart-cross { stroke: var(--text-dim); stroke-width: 1; stroke-dasharray: 3 3; vector-effect: non-scaling-stroke; visibility: hidden; }
.chart-cross-dot { fill: var(--accent); stroke: #fff; stroke-width: 1.5; visibility: hidden; }
body.light .chart-cross-dot { stroke: #1b2430; }
.chart-tip {
  position: absolute;
  top: -2px;
  transform: translateX(-50%);
  display: none;
  padding: 4px 8px;
  background: rgba(0, 0, 0, .82);
  color: #fff;
  font-size: 11px;
  white-space: nowrap;
  border-radius: 6px;
  pointer-events: none;
  z-index: 2;
}
body.light .chart-tip { background: rgba(27, 36, 48, .9); }
.chart-tip b { color: var(--accent); font-weight: 600; margin-right: 4px; }
.chart-tip span { color: #b9c2cd; }

/* ---------- Skeleton ---------- */
.skeleton-wrap { padding: 8px 12px; }
.skeleton-row {
  height: 68px;
  border-radius: var(--radius);
  margin-bottom: 10px;
  background: linear-gradient(90deg, #182230 25%, #1f2c3d 50%, #182230 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ---------- Footer ---------- */
.app-footer {
  text-align: center;
  color: var(--text-faint);
  font-size: 11px;
  padding: 16px 16px calc(20px + var(--safe-bottom));
}

/* ---------- Responsive guard (320–480) ---------- */
@media (max-width: 360px) {
  .app-title { font-size: 18px; }
  .price-value { font-size: 16px; }
  .unit-option { font-size: 12px; }
}
