:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1f2329;
  --muted: #8a9099;
  --line: #e5e7eb;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --ok: #16a34a;
  --bad: #dc2626;
  --radius: 10px;
  --page-gutter: clamp(12px, 3vw, 24px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  min-width: 0;
  overflow-wrap: anywhere;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
}

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px 16px;
  flex-wrap: wrap;
  padding: max(14px, env(safe-area-inset-top)) max(var(--page-gutter), env(safe-area-inset-right)) 14px max(var(--page-gutter), env(safe-area-inset-left));
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; max-width: 100%; }
.brand > div { min-width: 0; }
.logo { font-size: 26px; flex-shrink: 0; }
.topbar h1 { margin: 0; font-size: 17px; }
.sub { margin: 0; color: var(--muted); font-size: 12px; }
.topbar-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; min-width: 0; max-width: 100%; }
#current-user { min-width: 0; overflow-wrap: anywhere; }

/* ---------- 布局 ---------- */
.boards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr));
  gap: 16px;
  padding: 20px max(var(--page-gutter), env(safe-area-inset-right)) max(60px, env(safe-area-inset-bottom)) max(var(--page-gutter), env(safe-area-inset-left));
  max-width: 1400px;
  margin: 0 auto;
}

.card {
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}

/* ---------- 卡片头 ---------- */
.card-head { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 8px 12px; }
.card-head h2 { margin: 0; font-size: 16px; display: flex; align-items: center; flex-wrap: wrap; gap: 6px; min-width: 0; max-width: 100%; }
.chip { max-width: 100%; overflow-wrap: anywhere; padding: 2px 8px; border-radius: 999px; font-size: 13px; }
.chip.key { background: #eef2ff; color: #3730a3; }
.chip.type { background: #ecfdf5; color: #065f46; }
.chip-sep { color: var(--muted); }
.meta { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }

/* ---------- 字段 ---------- */
.fields { min-width: 0; margin-top: 12px; display: grid; gap: 10px; }
.field { min-width: 0; border-top: 1px dashed var(--line); padding-top: 10px; }
.field-head { display: flex; align-items: center; gap: 4px 8px; flex-wrap: wrap; margin-bottom: 4px; }
.field-label { min-width: 0; max-width: 100%; overflow-wrap: anywhere; font-size: 13px; font-weight: 600; }
.field-key { min-width: 0; max-width: 100%; overflow-wrap: anywhere; font-size: 11px; color: var(--muted); background: #f3f4f6; padding: 1px 6px; border-radius: 4px; }
.help { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }

.input {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
textarea.input { resize: vertical; }
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
.remark-input { margin-top: 8px; }

.switch { min-height: 44px; display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.switch input { flex-shrink: 0; width: 38px; height: 20px; appearance: none; background: #d1d5db; border-radius: 999px; position: relative; transition: background .15s; cursor: pointer; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked { background: var(--ok); }
.switch input:checked::after { transform: translateX(18px); }
.switch-text { font-size: 13px; color: var(--muted); }

/* ---------- 按钮 ---------- */
.btn {
  min-height: 40px;
  min-width: 0;
  max-width: 100%;
  touch-action: manipulation;  border: 1px solid transparent;
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--text); }
.btn.ghost:hover { border-color: #cbd5e1; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: 32px;
  min-height: 32px;
  touch-action: manipulation;  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 4px;
  border-radius: 4px;
}
.icon-btn:hover { background: #f3f4f6; color: var(--text); }
.field-head .icon-btn { margin-left: auto; }
.field-head .icon-btn + .icon-btn { margin-left: 0; }

/* ---------- 新增配置项 / 原始JSON ---------- */
.adder { min-width: 0; display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.adder .input:first-child { flex: 1 1 180px; }
.adder select.input { flex: 0 0 110px; width: 110px; }

.raw { min-width: 0; max-width: 100%; margin-top: 12px; }
.raw summary { min-height: 44px; padding: 10px 0; cursor: pointer; color: var(--muted); font-size: 12px; }
.raw pre { max-width: 100%; overscroll-behavior-x: contain; background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; padding: 10px; font-size: 12px; overflow: auto; max-height: 240px; }

.actions { min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 14px; }
.status { min-width: 0; overflow-wrap: anywhere; font-size: 12px; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.bad { color: var(--bad); }

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

.jd-panel { max-width: 1400px; margin: 0 auto 60px; padding: 0 var(--page-gutter); }
.jd-products { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 360px), 1fr)); align-items: start; gap: 16px; margin-top: 16px; }
.jd-product h3 { font-size: 15px; margin: 0; }
.jd-price { color: #dc2626; font-size: 26px; font-weight: 650; margin: 12px 0; }
.jd-price .help { font-weight: normal; }
.jd-product .field { display: grid; gap: 6px; }
.jd-product input[type="checkbox"] { width: 24px; height: 24px; accent-color: var(--primary); }
.jd-total { margin-top: 16px; }
.jd-total-card { border-color: #93c5fd; }
.jd-history { border-top: 1px solid var(--line); margin-top: 18px; padding-top: 16px; }
.jd-history h4 { margin: 0; font-size: 14px; }
.jd-history-list { list-style: none; margin: 12px 0 0; padding: 0; }
.jd-history-row { padding: 12px 0; border-bottom: 1px solid var(--line); }
.jd-history-row:first-child { padding-top: 0; }
.jd-history-row p { margin: 2px 0 0; }
.jd-history-prices { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 12px; margin-top: 4px; }
.jd-history-prices strong { font-size: 18px; font-variant-numeric: tabular-nums; }
.jd-history-change { font-size: 12px; color: #64748b; background: #f1f5f9; border-radius: 5px; padding: 2px 7px; }
.jd-history-change.down { color: #15803d; background: #f0fdf4; }
.jd-history-change.up { color: #b91c1c; background: #fef2f2; }
.jd-chart-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 120px), 1fr)); gap: 8px; margin: 14px 0; }
.jd-chart-metric { min-width: 0; border: 1px solid #e2e8f0; border-radius: 8px; padding: 10px; background: #f8fafc; }
.jd-chart-metric > span { display: block; color: #64748b; font-size: 12px; }
.jd-chart-metric strong { display: block; font-size: 16px; margin-top: 3px; font-variant-numeric: tabular-nums; }
.jd-chart-metric:last-child strong { font-size: 14px; }
.jd-price-chart .down { color: #15803d; }
.jd-price-chart .up { color: #b91c1c; }
.jd-chart-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; color: #64748b; font-size: 12px; margin-bottom: 8px; }
.jd-chart-frame { display: grid; grid-template-columns: 64px minmax(0, 1fr); }
.jd-chart-axis { position: relative; font-size: 11px; color: #64748b; font-variant-numeric: tabular-nums; }
.jd-chart-axis span { position: absolute; right: 8px; transform: translateY(-50%); }
.jd-chart-plot { position: relative; height: 220px; touch-action: pan-y; }
.jd-chart-plot svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.jd-chart-grid { stroke: #e2e8f0; stroke-width: 1; vector-effect: non-scaling-stroke; stroke-dasharray: 4 4; }
.jd-chart-area { fill: #eff6ff; opacity: .6; }
.jd-chart-line { stroke-width: 2.5; vector-effect: non-scaling-stroke; stroke-linecap: round; }
.jd-chart-line.down { stroke: #16a34a; }
.jd-chart-line.up { stroke: #dc2626; }
.jd-chart-line.flat { stroke: #2563eb; }
.jd-chart-guide { stroke: #94a3b8; stroke-dasharray: 3 4; stroke-width: 1; vector-effect: non-scaling-stroke; }
.jd-chart-point { position: absolute; transform: translate(-50%, -50%); width: 20px; height: 28px; padding: 0; border: 0; background: transparent; cursor: pointer; border-radius: 8px; }
.jd-chart-point::after { content: ''; position: absolute; width: 8px; height: 8px; left: 6px; top: 10px; border: 2px solid #2563eb; background: #fff; border-radius: 50%; }
.jd-chart-point[aria-pressed="true"]::after { background: #2563eb; box-shadow: 0 0 0 4px #dbeafe; }
.jd-chart-point.no-selection::after { border-color: #94a3b8; background: #e2e8f0; border-radius: 1px; }
.jd-chart-point:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }
.jd-chart-dates { display: flex; justify-content: space-between; gap: 12px; margin: 0 0 12px 64px; color: #64748b; font-size: 11px; }
.jd-chart-dates span:last-child { text-align: right; }
.jd-chart-detail { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 10px; min-height: 76px; padding: 10px 12px; border: 1px solid #bfdbfe; border-radius: 8px; background: #f8fbff; }
.jd-chart-detail time { flex-basis: 100%; color: #475569; }
.jd-chart-detail strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.jd-price-chart > .help { margin-top: 10px; }
.jd-history-details { margin-top: 14px; border-top: 1px solid var(--line); }
.jd-history-details summary { cursor: pointer; min-height: 44px; padding: 12px 0; font-size: 13px; color: #475569; }
.jd-history > .actions { align-items: center; gap: 8px; }
.jd-history > .actions .btn { width: auto; }
@media (min-width: 900px) {
  .jd-total-card .fields { grid-template-columns: repeat(4, minmax(0, 1fr)); align-items: start; gap: 16px; }
}

/* ---------- 提示条 ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  width: max-content;
  max-width: calc(100% - 2 * var(--page-gutter));
  overflow-wrap: anywhere;
  text-align: center;
  transform: translateX(-50%);
  padding: 10px 18px;
  border-radius: 14px;
  color: #fff;
  font-size: 13px;
  box-shadow: 0 6px 18px rgba(16, 24, 40, .18);
  z-index: 50;
}
.toast.good { background: #111827; }
.toast.bad { background: var(--bad); }

/* 登录前隐藏配置区域，避免display规则覆盖hidden。 */
[hidden] { display: none !important; }
.login-panel { width: calc(100% - 2 * var(--page-gutter)); max-width: 420px; margin: clamp(20px, 5vw, 40px) auto; }
.login-panel h1 { margin-top: 0; font-size: clamp(20px, 5vw, 26px); }
.login-panel h2 { margin-top: 0; }
.login-panel label { display: block; margin-top: 12px; }
.login-panel .input { width: 100%; margin: 6px 0 12px; }
.config-actions { max-width: 1400px; margin: 20px auto 0; padding: 0 max(var(--page-gutter), env(safe-area-inset-right)) 0 max(var(--page-gutter), env(safe-area-inset-left)); display: flex; flex-wrap: wrap; gap: 12px; }

.auth-card { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
.auth-tabs { display: flex; gap: 8px; margin-bottom: 20px; }
.auth-tabs .btn { flex: 1; }
.auth-card form > .btn { width: 100%; margin-top: 8px; }
.auth-card .status { min-height: 20px; }
#entry-retry, #entry-login { display: inline-flex; align-items: center; justify-content: center; margin: 8px 8px 0 0; text-decoration: none; }

/* 平板及手机采用单列内容，控件保持易点按的尺寸。 */
@media (max-width: 768px) {
  .boards { grid-template-columns: minmax(0, 1fr); gap: 12px; padding-top: 12px; }
  .topbar { position: static; }
  .topbar-actions { width: 100%; }
  #current-user { flex: 1 1 100%; }
  .topbar-actions .btn { flex: 1 1 120px; }
  .card { padding: 14px; }
  .input { min-height: 44px; font-size: 16px; }
  .btn { min-height: 44px; font-size: 14px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .field-head { align-items: center; }
  .adder { display: grid; grid-template-columns: minmax(0, 1fr); }
  .adder select.input { width: 100%; }
  .adder .btn { width: 100%; }
  .actions { align-items: stretch; }
  .actions .btn { width: 100%; }
  .actions .status { flex-basis: 100%; }
  .config-actions { margin-top: 12px; gap: 8px; }
  .config-actions .btn { flex: 1 1 140px; }
  .auth-tabs { gap: 8px; }
  .empty { padding: 24px var(--page-gutter); }
}

@media (max-width: 360px) {
  .brand { gap: 8px; }
  .topbar h1 { font-size: 16px; }
  .config-actions .btn { flex-basis: 100%; }
  #entry-retry, #entry-login { display: flex; width: 100%; margin-right: 0; }
}

/* 平板横屏等宽屏触控设备也使用易点按的控件，避免输入时页面放大。 */
@media (pointer: coarse) {
  .input { min-height: 44px; font-size: 16px; }
  .btn { min-height: 44px; font-size: 14px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
}
