:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22c55e;
  --accent-2: #16a34a;
  --warn: #f59e0b;
  --err: #f87171;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

body { min-height: 100dvh; padding-bottom: env(safe-area-inset-bottom); }

[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; gap: .6rem;
  padding: calc(env(safe-area-inset-top) + .7rem) 1rem .7rem;
  background: #0b1222; border-bottom: 1px solid #1f2a3d;
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 1.05rem; margin: 0; letter-spacing: .02em; }
.badge { font-size: .7rem; padding: .1rem .5rem; border-radius: 99px; background: #134e2a; color: #86efac; }

main { max-width: 640px; margin: 0 auto; padding: 1rem; }

.lead { color: var(--muted); text-align: center; margin: 1rem 0 1.5rem; }
.note { color: var(--muted); font-size: .85rem; }

.home-buttons { display: grid; gap: 1rem; }

.big-btn {
  display: flex; align-items: center; justify-content: center; gap: .8rem;
  min-height: 56px; padding: .9rem 1.2rem;
  border: 1px solid #334155; border-radius: var(--radius);
  background: var(--panel); color: var(--text);
  font-size: 1.05rem; font-weight: 600; cursor: pointer;
  touch-action: manipulation;
}
.big-btn:active { transform: scale(.98); }
.big-btn:disabled { opacity: .45; }
.big-btn.primary { background: var(--accent-2); border-color: var(--accent); color: #fff; }
.big-btn.wide { width: 100%; }
.home-buttons .big-btn { justify-content: flex-start; min-height: 96px; text-align: left; }
.home-buttons .icon { font-size: 2rem; }
.home-buttons small { display: block; font-weight: 400; font-size: .85rem; opacity: .85; }

.back-btn { margin-top: 1.2rem; background: none; border: none; color: var(--muted); font-size: 1rem; padding: .6rem 0; }

.howto { margin-top: 1.6rem; background: var(--panel); border-radius: var(--radius); padding: .8rem 1rem; }
.howto summary { cursor: pointer; font-weight: 600; }
.howto ol { padding-left: 1.2rem; }

.card { background: var(--panel); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }

.file-btn {
  display: flex; align-items: center; justify-content: center;
  min-height: 72px; border: 2px dashed #475569; border-radius: var(--radius);
  font-size: 1.1rem; font-weight: 600; cursor: pointer;
}
.file-btn input { display: none; }
.file-info { margin-top: .8rem; word-break: break-all; }
.file-info .thumb { max-width: 100%; max-height: 180px; border-radius: 8px; display: block; margin-top: .5rem; }

.options { display: grid; gap: .9rem; }
.options label { display: grid; gap: .35rem; font-size: .92rem; color: var(--muted); }
.options label.check { display: flex; align-items: center; gap: .6rem; color: var(--text); }
.options input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); }
select, input[type=range] { width: 100%; }
select {
  font-size: 1rem; padding: .6rem; border-radius: 10px;
  background: var(--panel-2); color: var(--text); border: 1px solid #334155;
}
input[type=range] { accent-color: var(--accent); height: 28px; }
output { color: var(--text); font-weight: 700; }

.summary { background: #0b2a1a; border: 1px solid #14532d; border-radius: var(--radius); padding: .8rem 1rem; margin-bottom: 1rem; font-size: .92rem; }
.summary b { color: #86efac; }

/* ---- scanner ---- */
.scanner {
  position: relative; width: 100%; aspect-ratio: 1 / 1;
  background: #000; border-radius: var(--radius); overflow: hidden; margin-bottom: 1rem;
}
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-placeholder {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: .5rem; padding: 1rem; text-align: center;
  background: radial-gradient(circle at center, #1e293b, #020617);
}
.guide { position: absolute; inset: 8%; pointer-events: none; transition: filter .15s; }
.guide i { position: absolute; width: 18%; height: 18%; border: 4px solid rgba(255,255,255,.8); }
.guide i:nth-child(1) { top: 0; left: 0; border-right: 0; border-bottom: 0; border-top-left-radius: 12px; }
.guide i:nth-child(2) { top: 0; right: 0; border-left: 0; border-bottom: 0; border-top-right-radius: 12px; }
.guide i:nth-child(3) { bottom: 0; left: 0; border-right: 0; border-top: 0; border-bottom-left-radius: 12px; }
.guide i:nth-child(4) { bottom: 0; right: 0; border-left: 0; border-top: 0; border-bottom-right-radius: 12px; }
.guide.hit i { border-color: var(--accent); }
.guide.miss i { border-color: var(--warn); }

.recv-status .status-row { display: flex; justify-content: space-between; gap: .5rem; align-items: baseline; }
.recv-status .status-row.small { font-size: .8rem; color: var(--muted); margin-top: .3rem; }
#recv-status { font-weight: 600; }
.count { font-variant-numeric: tabular-nums; font-weight: 700; }
#recv-file { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.progress { height: 10px; background: #0b1222; border-radius: 99px; overflow: hidden; margin-top: .5rem; }
.progress > div { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent-2), var(--accent)); transition: width .15s; }
.progress.thin { height: 6px; margin: .4rem 1rem 0; }

.recv-controls { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .8rem; }
.chip {
  min-height: 44px; padding: .4rem .9rem; border-radius: 99px;
  border: 1px solid #334155; background: var(--panel-2); color: var(--text);
  font-size: .95rem; cursor: pointer; touch-action: manipulation;
}
.chip.on { background: #854d0e; border-color: var(--warn); }

.result h2 { margin: 0 0 .5rem; font-size: 1.2rem; }
.result-meta { font-size: .9rem; color: var(--muted); word-break: break-all; margin-bottom: .8rem; }
.result-meta b { color: var(--text); font-size: 1rem; }
.preview img { max-width: 100%; max-height: 50vh; border-radius: 8px; display: block; margin: 0 auto .8rem; background: #fff; }
.preview pre {
  max-height: 40vh; overflow: auto; background: #0b1222; padding: .8rem; border-radius: 8px;
  font-size: .8rem; white-space: pre-wrap; word-break: break-all; margin: 0 0 .8rem;
}
.result-buttons { display: grid; gap: .6rem; }

/* ---- player ---- */
.player {
  position: fixed; inset: 0; z-index: 20; background: #fff; color: #111;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.player-head { display: flex; justify-content: space-between; align-items: center; padding: .5rem .8rem; gap: .5rem; }
.player-head span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; }
.player .chip { background: #f1f5f9; color: #111; border-color: #cbd5e1; }
.qr-wrap { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; padding: .5rem; }
#qr-canvas { image-rendering: pixelated; image-rendering: crisp-edges; max-width: 100%; max-height: 100%; }
.player-counter { text-align: center; font-size: 1.6rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.player .progress { background: #e2e8f0; }
.player-controls { display: flex; justify-content: center; gap: .6rem; padding: .7rem; }
.player-controls .chip { min-width: 56px; font-size: 1.2rem; }
.player-foot { text-align: center; color: #64748b; font-size: .8rem; padding-bottom: .5rem; }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 1.2rem); transform: translateX(-50%);
  background: #111827; color: #fff; padding: .7rem 1.1rem; border-radius: 12px; z-index: 50;
  max-width: 90vw; box-shadow: 0 8px 30px rgba(0,0,0,.4); font-size: .92rem;
}
.toast.err { background: #7f1d1d; }

@media (min-width: 720px) {
  .home-buttons { grid-template-columns: 1fr 1fr; }
}
