:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #16181d;
  --muted: #6b7280;
  --line: #e4e7ec;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --ok: #16a34a;
  --err: #dc2626;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #171a21;
    --ink: #e8eaed;
    --muted: #9aa3b2;
    --line: #262a33;
    --accent: #3b82f6;
    --accent-ink: #0b1020;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* ── 顶栏 ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px clamp(12px, 3vw, 24px);
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; }

.logo { width: 22px; height: 22px; fill: var(--accent); }

.status { display: flex; align-items: center; gap: 8px; }

.pill {
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.pill.on { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--line)); }
.pill.muted { opacity: .75; }

.link {
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 6px;
}
.link:hover { color: var(--ink); }

/* ── 布局 ── */
.grid {
  display: grid;
  grid-template-columns: 1.05fr .95fr;
  gap: clamp(12px, 2vw, 20px);
  padding: clamp(12px, 3vw, 24px);
  align-items: start;
}

@media (max-width: 860px) {
  .grid { grid-template-columns: 1fr; }
}

.pane {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}

.pane-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
  flex-wrap: wrap;   /* 窄屏时按钮换行，别被挤没 */
}

h2 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: .02em; }

.hint { color: var(--muted); font-size: 12px; }
.hint.small, .small { font-size: 12px; }

.head-right { display: flex; align-items: center; gap: 10px; min-width: 0; }
.head-right .hint { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-right .link { flex: none; }
.head-right .link:disabled { opacity: .4; cursor: default; }
.head-right .link {
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 10px;
}
.head-right .link:hover { color: var(--err); border-color: color-mix(in srgb, var(--err) 40%, var(--line)); }

/* ── 二维码 ── */
.qr-wrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 300px;
  background: #fff;
  border-radius: 12px;
  border: 1px solid var(--line);
  padding: 14px;
}

.qr svg { display: block; width: min(64vw, 380px); height: auto; }

.empty { text-align: center; color: var(--muted); }
.empty p { margin: 4px 0; }

.meta {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.text {
  margin: 10px 0 0;
  padding: 10px 12px;
  max-height: 200px;
  overflow: auto;
  background: color-mix(in srgb, var(--ink) 4%, transparent);
  border-radius: 10px;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-all;
}

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 12px; }

/* ── 按钮 ── */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
  transition: transform .04s ease, background .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.file { position: relative; display: inline-flex; align-items: center; }

/* ── 扫描区 ── */
.scanner {
  position: relative;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 4 / 3;
  display: flex;
  align-items: center;
  justify-content: center;
}

#video { width: 100%; height: 100%; object-fit: cover; display: none; }
#video.live { display: block; }

.scan-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #9ca3af;
  font-size: 13px;
  text-align: center;
  padding: 16px;
}
.scan-hint.hidden { display: none; }

.input {
  width: 100%;
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  resize: vertical;
}
.input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); }

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

/* ── 历史 ── */
.history { list-style: none; margin: 14px 0 0; padding: 0; }
.history li {
  display: flex;
  gap: 8px;
  padding: 7px 0;
  border-top: 1px dashed var(--line);
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
}
.history li:hover { color: var(--ink); }
.history li .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history li .w { flex: none; }

/* ── 摄像头覆盖层 / 扫描线 ── */
.cam-cover {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 18px;
  background: #0b0f14;
  color: #cbd5e1;
}
.cam-cover[hidden] { display: none; }

.scan-line {
  position: absolute;
  left: 6%;
  right: 6%;
  height: 2px;
  top: 10%;
  background: linear-gradient(90deg, transparent, #22d3ee, transparent);
  box-shadow: 0 0 8px #22d3ee;
  animation: sweep 2.4s ease-in-out infinite;
  pointer-events: none;
}
.scan-line[hidden] { display: none; }

@keyframes sweep {
  0%   { top: 10%; }
  50%  { top: 88%; }
  100% { top: 10%; }
}

.fps {
  position: absolute;
  right: 10px;
  top: 10px;
  font-size: 11px;
  letter-spacing: .04em;
  color: #e5e7eb;
  background: rgba(0, 0, 0, .45);
  padding: 2px 7px;
  border-radius: 999px;
}
.fps[hidden] { display: none; }

/* ── 发送回执 / 手动输入 ── */
.lastsend {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
}
.lastsend[hidden] { display: none; }

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ok);
  flex: none;
}

.manual { margin-top: 12px; }
.manual summary {
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  padding: 4px 0;
}
.manual summary:hover { color: var(--ink); }


/* ── toast ── */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--panel);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  transition: opacity .2s ease;
  pointer-events: none;
  z-index: 60;
  max-width: 86vw;
}
.toast.show { opacity: .95; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .95em;
  background: color-mix(in srgb, var(--ink) 6%, transparent);
  padding: 1px 5px;
  border-radius: 5px;
}

/* ───────────────── 手机适配 ─────────────────
 *
 * 三个真实问题：
 *   1. 手机竖屏时摄像头 16:9，被 object-fit:cover 裁掉两边，
 *      而解码用的是**整幅**帧 —— 用户看到的比能识别的少，会产生"明明有码却没反应"。
 *      触摸设备改成 contain，所见即所识（多余部分黑边）。
 *   2. 横屏时两列挤在 400px 高里，按钮和二维码互相挤。
 *      单独给横屏一套更紧凑的栅格。
 *   3. 刘海/底部手势条。用 env(safe-area-inset-*) 让开，
 *      配合 viewport-fit=cover（HTML 里已开）。
 */

/* 触摸设备：按钮够大、画面不裁切 */
@media (pointer: coarse) {
  .btn { min-height: 44px; padding: 11px 16px; font-size: 15px; }
  .link { min-height: 40px; }
  #video { object-fit: contain; background: #000; }
  .qr svg { width: min(76vw, 44vh, 400px); }
  .scanner { aspect-ratio: 4 / 3; }
}

/* 手机竖屏：单列，二维码和摄像头各占半屏 */
@media (max-width: 860px) {
  .topbar {
    padding-top: max(10px, env(safe-area-inset-top));
    padding-left: max(10px, env(safe-area-inset-left));
    padding-right: max(10px, env(safe-area-inset-right));
  }
  .grid {
    grid-template-columns: 1fr;
    gap: 10px;
    padding:
      10px
      max(10px, env(safe-area-inset-right))
      max(16px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }
  .pane { padding: 12px; border-radius: 12px; }
  .qr-wrap { min-height: 0; padding: 10px; }
  .scanner { max-height: 46vh; }
  .history li { padding: 6px 0; }
}

/* 手机横屏：两列并排，砍掉占地方的次要信息 */
@media (orientation: landscape) and (max-height: 560px) {
  .grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 8px max(8px, env(safe-area-inset-right));
  }
  .topbar { padding-block: 6px; }
  .brand span { font-size: 14px; }
  .pane { padding: 10px; }
  .pane-head { margin-bottom: 6px; }
  .qr-wrap { min-height: 0; padding: 8px; }
  .qr svg { width: min(36vw, 58vh); }
  .scanner {
    aspect-ratio: auto;
    height: calc(100vh - 190px);
    height: calc(100dvh - 190px);
    min-height: 140px;
    max-height: none;
  }
  .history, .manual, .hint.small, .lastsend { display: none; }
}

/* 矮屏（折叠屏内屏、小窗口）：别让二维码把页面顶出去 */
@media (max-height: 640px) and (max-width: 860px) {
  .qr svg { width: min(64vw, 36vh, 320px); }
  .scanner { max-height: 40vh; }
}

/* ── 微信等 WebView 的提示条 ── */
.banner {
  margin: 10px max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  padding: 10px 14px;
  border-radius: 10px;
  background: #fef3c7;
  border: 1px solid #fcd34d;
  color: #78350f;
  font-size: 13px;
  line-height: 1.6;
}
.banner[hidden] { display: none; }
.banner b { color: #92400e; }

@media (prefers-color-scheme: dark) {
  .banner { background: #3a2a08; border-color: #7c5a12; color: #fde68a; }
  .banner b { color: #fbbf24; }
}