/* 布局与组件 —— 移动端优先（Mobile First）
 *
 * 断点：
 *   基础样式        手机竖屏（≥320px），单列、大点击目标、底部固定操作条
 *   ≥ 600px         手机横屏 / 小平板：结果卡片双列
 *   ≥ 900px         平板 / 桌面：左画布右面板双栏，操作条回到行内
 *   ≥ 1200px        大屏：加宽容器
 *
 * 交互目标：任何设备上都是「看一屏 → 做一件事 → 点下一步」，不需要横向滚动页面本身。
 */

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--font);
  /* 手机 15px，大屏最多 16px */
  font-size: clamp(15px, 0.9rem + 0.15vw, 16px);
  line-height: 1.7;
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { line-height: 1.35; margin: 0 0 .5em; font-weight: 650; }
h1 { font-size: clamp(1.15rem, 1rem + 1vw, 1.4rem); }
h2 { font-size: clamp(1.02rem, .95rem + .4vw, 1.14rem); }
h3 { font-size: .95rem; }
p { margin: 0 0 .8em; }
code, .mono { font-family: var(--mono); font-size: .9em; }
a { color: var(--accent); }
b, strong { font-weight: 650; }

/* ============================== 骨架 ============================== */

.app {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 14px calc(24px + env(safe-area-inset-bottom));
}
@media (min-width: 900px) {
  .app { padding: 18px 20px 32px; }
}
@media (min-width: 1200px) {
  .app { max-width: 1280px; }
}

/* --- 顶栏：手机上吸顶，节省纵向空间 --- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 10px;
  margin: -12px -14px 12px;
  padding: 10px 14px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; white-space: nowrap; }
.topbar .sub { color: var(--fg-muted); font-size: .82rem; }
.topbar .spacer { flex: 1 1 auto; }
/* 副标题在窄屏上藏起来，标题本身已经说明了用途 */
@media (max-width: 519px) { .topbar .sub { display: none; } }
@media (min-width: 900px) {
  .topbar { position: static; margin: 0 0 16px; padding: 0 0 12px; backdrop-filter: none; background: none; }
}


/* --- 面板 --- */
.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
@media (min-width: 900px) { .panel { padding: 18px; margin-bottom: 16px; } }
.panel > :last-child { margin-bottom: 0; }
.panel-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.panel-head h2 { margin: 0; flex: 1 1 auto; }

/* --- 输入区：≥900px 时左右并排（左边填写、右边取景） --- */
.input-grid { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 900px) {
  .input-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); gap: 20px; align-items: start; }
}

.hidden { display: none !important; }

/* ============================== 控件 ============================== */

button, .btn {
  font: inherit; font-weight: 550;
  /* 触摸目标不小于 44px */
  min-height: 44px; padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-panel); color: var(--fg);
  cursor: pointer;
  transition: background .12s, border-color .12s, transform .06s;
  touch-action: manipulation;
}
button:active:not(:disabled) { transform: scale(.985); }
button:hover:not(:disabled) { background: var(--bg-sunken); }
button:disabled { opacity: .42; cursor: not-allowed; }
button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
button.primary {
  background: var(--accent); color: var(--accent-fg); border-color: var(--accent);
}
button.primary:hover:not(:disabled) { filter: brightness(1.08); }
button.ghost { border-color: transparent; background: transparent; color: var(--fg-muted); }
button.ghost:hover:not(:disabled) { background: var(--bg-sunken); color: var(--fg); }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }


label { display: block; font-size: .86rem; color: var(--fg-muted); margin-bottom: 5px; }
input[type="text"], input[type="number"], select {
  font: inherit; width: 100%; min-height: 44px;
  padding: 8px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-panel); color: var(--fg);
}
input[type="range"] {
  width: 100%; height: 32px; margin: 2px 0 0;
  accent-color: var(--accent); touch-action: pan-y;
}
.field { margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field-row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.field-row.tight { grid-template-columns: 1fr 1fr; gap: 10px; }

fieldset {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 12px; margin: 0 0 12px;
}
legend { font-size: .82rem; color: var(--fg-muted); padding: 0 5px; font-weight: 600; }
.radio-row { display: flex; flex-direction: column; gap: 2px; }
.radio-row label {
  display: flex; gap: 10px; align-items: flex-start;
  margin: 0; padding: 8px 4px; min-height: 44px;
  color: var(--fg); cursor: pointer; border-radius: var(--radius-sm);
}
.radio-row label:hover { background: var(--bg-sunken); }
.radio-row input { margin-top: 5px; flex: none; width: 18px; height: 18px; accent-color: var(--accent); }

.hint { font-size: .8rem; color: var(--fg-faint); margin: 6px 0 0; line-height: 1.55; }

/* ============================== 画布 ============================== */

.stage {
  position: relative;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  line-height: 0;
}
.stage video, .stage canvas { display: block; width: 100%; height: auto; }
.stage canvas.overlay {
  position: absolute; inset: 0;
  touch-action: none;          /* 拖动关键点时不要触发页面滚动 */
  cursor: crosshair;
}

/* 横向可滚容器：右侧渐隐提示还有内容 */
.scroll-x {
  overflow-x: auto; overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius-sm);
  background:
    linear-gradient(to right, var(--bg-panel) 30%, transparent) left / 24px 100% no-repeat,
    linear-gradient(to left, var(--bg-panel) 30%, transparent) right / 24px 100% no-repeat;
  background-attachment: local, local;
}
.scroll-hint { font-size: .76rem; color: var(--fg-faint); margin: 4px 0 0; }
@media (min-width: 900px) { .scroll-hint { display: none; } }

.shot-strip { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.shot-thumb {
  position: relative; width: 76px; border-radius: var(--radius-sm);
  border: 2px solid var(--border); overflow: hidden; line-height: 0;
  cursor: pointer; transition: border-color .12s;
}
.shot-thumb:hover { border-color: var(--border-strong); }
.shot-thumb.sel { border-color: var(--accent); }
.shot-thumb:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.shot-thumb img { width: 100%; display: block; }
.shot-thumb .tag {
  position: absolute; top: 2px; left: 3px; font-size: .68rem; line-height: 1.6;
  padding: 0 5px; border-radius: 4px; background: rgba(0,0,0,.62); color: #fff;
}

/* ============================== 提示条 ============================== */

.note {
  padding: 10px 12px; border-radius: var(--radius-sm);
  border-left: 3px solid var(--border-strong);
  background: var(--bg-sunken); font-size: .87rem; line-height: 1.6;
  margin-bottom: 10px;
}
.note.ok     { border-color: var(--ok);     background: var(--ok-soft); }
.note.warn   { border-color: var(--warn);   background: var(--warn-soft); }
.note.danger { border-color: var(--danger); background: var(--danger-soft); }
.note.info   { border-color: var(--info);   background: var(--info-soft); }
.note > :last-child { margin-bottom: 0; }
.note.compact { padding: 8px 12px; margin: 0 0 12px; }

/* 可折叠的提示条（长说明默认展开，但用户可以收起） */
details.note > summary {
  cursor: pointer; list-style: none; padding: 0; color: inherit;
  font-size: .87rem; margin: 0;
}
details.note > summary::-webkit-details-marker { display: none; }
details.note > summary::after { content: ' ▾'; opacity: .5; }
details.note[open] > summary::after { content: ' ▴'; }
details.note[open] > summary { margin-bottom: 8px; }

.pill {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: .72rem; font-weight: 600; vertical-align: 2px; white-space: nowrap;
}
.pill.high   { background: var(--ok-soft);     color: var(--ok); }
.pill.medium { background: var(--warn-soft);   color: var(--warn); }
.pill.low    { background: var(--danger-soft); color: var(--danger); }
.pill.src    { background: var(--bg-sunken);   color: var(--fg-faint); font-weight: 500; }

/* ============================== 结果 ============================== */

.result-grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .result-grid { grid-template-columns: 1fr 1fr; } }

.result-card {
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; background: var(--bg-panel);
}
.result-card h3 {
  margin: 0 0 6px; color: var(--fg-muted);
  font-size: .82rem; font-weight: 600;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.result-big {
  font-size: clamp(1.6rem, 1.2rem + 2vw, 2rem);
  font-weight: 700; line-height: 1.12; letter-spacing: -.015em;
}
.result-big small { font-size: .5em; font-weight: 500; color: var(--fg-muted); margin-left: 6px; }
.result-sub { font-size: .83rem; color: var(--fg-muted); margin-top: 5px; line-height: 1.55; }

/* --- 弦长推荐带 --- */
.band { margin: 12px 0 4px; }
.band-track {
  position: relative; height: 36px;
  background: var(--bg-sunken); border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.band-fill { position: absolute; top: 0; bottom: 0; background: var(--accent-soft); }
.band-fill.tight { background: var(--ok-soft); }
.band-center { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--accent); }
.band-tick { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--border-strong); }
.band-tick.in { background: var(--accent); }
.band-labels { position: relative; height: 18px; font-size: .7rem; color: var(--fg-faint); }
.band-labels span { position: absolute; transform: translateX(-50%); white-space: nowrap; }

/* --- 表格：窄屏可横向滚动，不撑破页面 --- */
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--fg-muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; font-family: var(--mono); white-space: nowrap; }
tr.hi td { background: var(--accent-soft); }
tbody tr:last-child td { border-bottom: 0; }

details { margin-bottom: 10px; }
details > summary {
  cursor: pointer; color: var(--fg-muted); font-size: .87rem;
  padding: 9px 0; min-height: 44px; display: flex; align-items: center;
}
details > summary:hover { color: var(--fg); }

/* ============================== 系数面板 ============================== */

.param-group {
  margin: 0 0 8px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); overflow: hidden;
}
.param-group-head {
  display: flex; align-items: center; gap: 8px;
  padding: 11px 12px; min-height: 48px;
  background: var(--bg-sunken); color: var(--fg);
  font-size: .89rem; font-weight: 600; cursor: pointer;
  list-style: none;
}
.param-group-head::-webkit-details-marker { display: none; }
.param-group-head::after { content: '▾'; margin-left: auto; opacity: .5; }
.param-group[open] .param-group-head::after { content: '▴'; }
.param-count {
  font-size: .72rem; font-weight: 500; color: var(--fg-faint);
  background: var(--bg-panel); border-radius: 999px; padding: 1px 8px;
}

.param { padding: 12px; border-top: 1px solid var(--border); }
.param-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 2px; }
.param-head .name { font-weight: 600; font-size: .87rem; flex: 1 1 140px; }
.param-head .val {
  font-family: var(--mono); font-size: .85rem; font-weight: 600;
  color: var(--accent); background: var(--accent-soft);
  padding: 1px 8px; border-radius: 5px;
}
.param-range {
  display: flex; justify-content: space-between;
  font-size: .7rem; color: var(--fg-faint); font-family: var(--mono);
  margin-top: -4px;
}
.param .note-sm {
  font-size: .79rem; color: var(--fg-faint); margin: 7px 0 0; line-height: 1.6;
}
.derived-hint {
  font-size: .79rem; color: var(--info); margin: 4px 0 0;
  padding: 5px 9px; background: var(--info-soft); border-radius: 5px;
}

/* 尊重「减少动态效果」的系统设置 */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

/* ============================== 零散组件 ============================== */

/* 「上传照片」按钮：label 伪装成 button，尺寸与真按钮一致 */
.file-btn {
  margin: 0; display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 9px 16px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  cursor: pointer; font-weight: 550;
}
.file-btn:hover { background: var(--bg-sunken); }

/* 屏幕 DPI 标定用的矩形（宽度由 JS 按滑块设置） */
.dpi-card {
  height: 54px; margin-top: 8px;
  border: 2px solid var(--accent); border-radius: 6px;
  background: var(--accent-soft);
}

.calib-field { margin-top: 14px; max-width: 460px; }

/* 拍摄要点列表 */
.howto { padding-left: 1.25em; margin: 0 0 12px; }
.howto li { margin-bottom: 6px; }

button.danger { color: var(--danger); }

/* 判据选择器 */
.crit-row { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
.crit-btn {
  display: flex; flex-direction: column; gap: 2px; align-items: center;
  min-height: 56px; padding: 8px 10px; line-height: 1.3;
}
.crit-btn b { font-size: 1rem; }
.crit-btn span { font-size: .74rem; color: var(--fg-muted); font-weight: 400; }
.crit-btn.on {
  background: var(--accent); color: var(--accent-fg); border-color: var(--accent);
}
.crit-btn.on span { color: var(--accent-fg); opacity: .85; }

/* 可达性表：行可点击 */
table.reach tr.pick { cursor: pointer; }
table.reach tr.pick:hover td { background: var(--bg-sunken); }
table.reach tr.pick:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
table.reach tr.sel td { background: var(--accent-soft); }
table.reach tr.sel:hover td { background: var(--accent-soft); }
.tag-rec { color: var(--ok); font-size: .74rem; }
.tag-sel { color: var(--accent); font-size: .74rem; }

.result-big-sm { font-size: 1.15rem !important; }

/* 结果页：无级滑块 */
.slider-row { margin: 14px 0 0; }
.slider-row label {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: .86rem; color: var(--fg-muted); margin-bottom: 2px;
}
.slider-row output {
  font-family: var(--mono); font-weight: 600; font-size: .95rem;
  color: var(--accent);
}

/* 判据的按弦说明：把「3→7」翻译成手上的实际动作 */
.fingering {
  margin: 6px 0 10px; padding: 7px 10px;
  font-size: .84rem; line-height: 1.5;
  color: var(--fg-muted); background: var(--bg-sunken);
  border-radius: 6px; border-left: 3px solid var(--accent);
}
.fingering b { color: var(--fg); font-weight: 600; }
.fingering-hint {
  margin: 10px 0 0; font-size: .82rem; line-height: 1.55; color: var(--fg-muted);
}
.fingering-hint b { color: var(--fg); font-weight: 600; }

/* 结果卡的键值行 */
.kv {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: .84rem; color: var(--fg-muted); padding: 2px 0;
}
.kv b { font-family: var(--mono); color: var(--fg); font-weight: 600; }
.high-fg { color: var(--ok) !important; }
.medium-fg { color: var(--warn) !important; }
.low-fg { color: var(--danger) !important; }

/* 「最接近的真实档位」标注 */
.near {
  margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border);
  font-size: .8rem; color: var(--fg-muted); font-family: var(--mono);
}

details.panel > summary { padding: 0; min-height: 0; font-weight: 600; color: var(--fg); }
details.panel[open] > summary { margin-bottom: 12px; }

/* 底部操作 */
.footer-actions { margin-top: 4px; }

/* 结果面板首次出现时的淡入，避免"突然多出一块" */
#resultPanel:not(.hidden) { animation: fadeIn .18s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { #resultPanel { animation: none !important; } }

/* 关键点工具条：跟在缩略图下面，紧凑一行 */
.lm-tools { margin-top: 8px; align-items: center; }
.lm-tools .hint { margin: 0; }
.lm-tools .spacer { flex: 1 1 auto; }
.lm-tools button { min-height: 38px; padding: 6px 12px; font-size: .85rem; }

/* 合并后的画面：底层照片 + 上层关键点，二者尺寸一致 */
#camStage #lmBase { width: 100%; height: auto; display: block; }
#camStage.hidden-layer #lmBase, #camStage.hidden-layer #lmOverlay { display: none; }
