/* AFU Body Scale — instrument-panel aesthetic.
   Palette: cool porcelain ground, deep petrol ink, single teal accent. */

:root {
  color-scheme: light;
  --bg: #f1f5f4;
  --surface: #ffffff;
  --surface-2: #eaf1f0;
  --surface-3: #dde9e8;
  --ink: #13272c;
  --ink-2: #3f5a60;
  --muted: #698085;
  --line: #d3e0df;
  --accent: #0e7c7b;
  --accent-strong: #0a5d5c;
  --on-accent: #ffffff;
  --danger: #a34d46;
  --shadow: 0 1px 2px rgba(19, 39, 44, 0.06), 0 8px 24px rgba(19, 39, 44, 0.06);
  --radius: 16px;
  --radius-small: 10px;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  --font-num: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d181b;
  --surface: #132226;
  --surface-2: #1a2d31;
  --surface-3: #223a3f;
  --ink: #e4edec;
  --ink-2: #b7cccb;
  --muted: #7e989c;
  --line: #24393e;
  --accent: #3bb4b1;
  --accent-strong: #58c8c5;
  --on-accent: #04292a;
  --danger: #d98880;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.25);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    color-scheme: dark;
    --bg: #0d181b;
    --surface: #132226;
    --surface-2: #1a2d31;
    --surface-3: #223a3f;
    --ink: #e4edec;
    --ink-2: #b7cccb;
    --muted: #7e989c;
    --line: #24393e;
    --accent: #3bb4b1;
    --accent-strong: #58c8c5;
    --on-accent: #04292a;
    --danger: #d98880;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.25);
  }
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  overflow: hidden;
  white-space: nowrap;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Accessible skip link */
.skip-link {
  position: absolute;
  left: 12px;
  top: -48px;
  z-index: 50;
  background: var(--accent);
  color: var(--on-accent);
  padding: 10px 16px;
  border-radius: 8px;
  text-decoration: none;
  transition: top 0.15s ease;
}
.skip-link:focus { top: 12px; }

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
}
.app-header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand svg { flex: none; }
.brand-name { font-size: 17px; font-weight: 650; letter-spacing: 0.01em; margin: 0; }
.brand-tagline {
  font-size: 11.5px;
  color: var(--muted);
  margin: 0;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-actions { margin-left: auto; display: flex; gap: 10px; align-items: center; }
.lang-select, .btn-icon {
  font: inherit;
  color: var(--ink-2);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  min-height: 40px;
}
.lang-select { padding: 4px 12px; }
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  padding: 0;
  cursor: pointer;
}
.btn-icon svg { display: block; }
.lang-select:focus-visible, .btn-icon:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Layout */
main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 20px 8px;
  display: grid;
  gap: 16px;
  /* minmax(0,1fr)：防止长内容（历史表格）把隐式轨道撑出视口 */
  grid-template-columns: minmax(0, 1fr);
}
@media (min-width: 960px) {
  main {
    grid-template-columns: minmax(0, 7fr) minmax(0, 5fr);
    grid-template-areas:
      "measure profile"
      "result profile"
      "share share"
      "history history"
      "help help";
  }
  .card-measure { grid-area: measure; }
  .card-result { grid-area: result; }
  .card-profile { grid-area: profile; align-self: start; }
  .card-share { grid-area: share; }
  .card-history { grid-area: history; }
  .card-help { grid-area: help; }
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  min-width: 0;
}
.card > h2 {
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  color: var(--muted);
}
.card-sub {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13.5px;
}

/* Stage rail: the protocol's real progression */
.stage-rail {
  list-style: none;
  display: flex;
  padding: 0;
  margin: 0 0 18px;
  counter-reset: stage;
}
.stage-rail li {
  flex: 1;
  position: relative;
  text-align: center;
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: 0.03em;
}
.stage-rail li::before {
  content: "";
  position: absolute;
  top: 5px;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--line);
}
.stage-rail li:first-child::before { left: 50%; }
.stage-rail li:last-child::before { right: 50%; }
.stage-rail li .dot {
  position: relative;
  z-index: 1;
  display: block;
  width: 12px;
  height: 12px;
  margin: 0 auto 6px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--line);
}
.stage-rail li.done { color: var(--accent); }
.stage-rail li.done .dot { background: var(--accent); border-color: var(--accent); }
.stage-rail li.done::before { background: var(--accent); }
.stage-rail li.now { color: var(--ink); font-weight: 650; }
.stage-rail li.now .dot {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 22%, transparent);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent); }
  50% { box-shadow: 0 0 0 7px color-mix(in srgb, var(--accent) 28%, transparent); }
}

/* Instrument readout */
.readout-panel {
  background: var(--surface-2);
  border-radius: var(--radius-small);
  padding: 18px 16px 12px;
  text-align: center;
  border: 1px solid var(--line);
}
.readout-label {
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.readout {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: clamp(52px, 13vw, 84px);
  font-weight: 600;
  line-height: 1.05;
  letter-spacing: -0.01em;
  margin: 6px 0 2px;
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 8px;
}
.readout .num { color: var(--ink); }
.readout .unit {
  font-size: 0.28em;
  color: var(--muted);
  font-family: var(--font-body);
  font-weight: 500;
}
.readout-meta {
  display: flex;
  justify-content: center;
  gap: 18px;
  font-size: 12.5px;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  min-height: 1.6em;
}
.readout-meta strong { color: var(--ink); font-weight: 600; }
.status-line {
  margin: 12px 0 0;
  font-size: 13.5px;
  color: var(--ink-2);
  min-height: 1.5em;
}
.measure-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  padding: 10px 18px;
  border-radius: 999px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-strong); }
.btn-secondary {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.btn[disabled] { opacity: 0.45; pointer-events: none; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Metrics */
.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(128px, 1fr));
  gap: 10px;
}
.metric {
  background: var(--surface-2);
  border-radius: var(--radius-small);
  padding: 12px 14px;
}
.metric-label {
  font-size: 11px;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  color: var(--muted);
}
.metric-value {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: 22px;
  font-weight: 600;
  margin-top: 3px;
}
.metric-unit { font-size: 11px; color: var(--muted); font-weight: 400; font-family: var(--font-body); margin-left: 4px; white-space: nowrap; }
.result-note { margin: 14px 0 0; font-size: 12.5px; color: var(--muted); }

/* Profile form */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.field input, .field select {
  font: inherit;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 46px;
}
.field input:focus-visible, .field select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.radio-row { display: flex; gap: 10px; }
.radio-pill {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  background: var(--surface-2);
}
.radio-pill:has(input:checked) { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.radio-pill input { accent-color: var(--accent); }

/* Share */
.share-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.share-fallback { margin-top: 12px; font-size: 13px; color: var(--ink-2); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

/* History */
.history-tools { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.table-wrap { overflow-x: auto; max-width: 100%; border-radius: var(--radius-small); border: 1px solid var(--line); -webkit-overflow-scrolling: touch; }
table.records { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.records th, table.records td {
  padding: 10px 12px;
  text-align: right;
  font-size: 14px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
table.records th:first-child, table.records td:first-child { text-align: left; }
table.records th:last-child, table.records td:last-child { text-align: center; }
table.records th:nth-last-child(2), table.records td:nth-last-child(2) { text-align: center; }
/* 窄屏隐藏次要列（阻抗/去脂体重），分享与删除操作保留 */
@media (max-width: 640px) {
  table.records th:nth-child(3), table.records td:nth-child(3),
  table.records th:nth-child(6), table.records td:nth-child(6) { display: none; }
  table.records th, table.records td { padding: 8px 9px; font-size: 13px; }
}
table.records thead th {
  background: var(--surface-2);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  position: sticky;
  top: 0;
}
table.records tbody tr:last-child td { border-bottom: none; }
table.records tbody tr:hover { background: var(--surface-2); }
.sparkline-wrap { margin-top: 16px; }
.sparkline-wrap svg { width: 100%; height: 64px; display: block; overflow: visible; }
.empty-note {
  color: var(--muted);
  font-size: 13.5px;
  background: var(--surface-2);
  border: 1px dashed var(--line);
  border-radius: var(--radius-small);
  padding: 16px;
  text-align: center;
}
.delete-cell .btn-icon, .share-cell .btn-icon { border-radius: 8px; }

/* Help */
.help details {
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  padding: 0;
  margin-bottom: 10px;
  background: var(--surface-2);
}
.help summary {
  cursor: pointer;
  padding: 12px 16px;
  font-weight: 600;
  font-size: 14px;
  color: var(--ink);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.help summary::-webkit-details-marker { display: none; }
.help summary::before {
  content: "";
  width: 0;
  height: 0;
  border-left: 5px solid var(--accent);
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  transition: transform 0.15s ease;
}
.help details[open] summary::before { transform: rotate(90deg); }
.help details > p, .help details > ol, .help details > ul {
  margin: 0;
  padding: 0 16px 14px;
  font-size: 13.5px;
  color: var(--ink-2);
}
.help details p + p { margin-top: 8px; }
.help a { color: var(--accent); }

/* Debug log */
details.debug {
  margin-top: 14px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  padding: 0;
}
details.debug summary {
  cursor: pointer;
  padding: 10px 14px;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
details.debug summary::-webkit-details-marker { display: none; }
details.debug summary::before {
  content: "";
  width: 0; height: 0;
  border-left: 5px solid var(--accent);
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  transition: transform 0.15s ease;
}
details.debug[open] summary::before { transform: rotate(90deg); }
details.debug summary + * { margin-top: 0; }
.debug-log {
  font-family: var(--font-num);
  font-size: 11.5px;
  color: var(--ink-2);
  background: var(--surface-3);
  border-radius: var(--radius-small);
  padding: 10px 12px;
  max-height: 180px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
  margin: 10px 0 0;
}
details.debug .btn-secondary { margin: 10px 14px 14px; padding: 6px 14px; min-height: 36px; font-size: 13px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 12px);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 40;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

footer {
  max-width: 1100px;
  margin: 0 auto;
  padding: 8px 20px 32px;
  color: var(--muted);
  font-size: 12.5px;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.001s !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001s !important;
  }
}

@media print {
  .app-header, .measure-actions, .share-row, .history-tools, .debug-log, .toast { display: none !important; }
  .card { box-shadow: none; }
}
