:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --text: #111827;
  --muted: #4b5563;
  --line: #d1d5db;
  --primary: #1d4ed8;
  --primary-text: #ffffff;
  --danger: #b91c1c;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --error-bg: #fee2e2;
  --ok-bg: #dcfce7;
  --ok-text: #166534;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --text: #f1f5f9;
    --muted: #cbd5e1;
    --line: #475569;
    --primary: #3b82f6;
    --danger: #f87171;
    --warn-bg: #422006;
    --warn-text: #fde68a;
    --error-bg: #450a0a;
    --ok-bg: #052e16;
    --ok-text: #86efac;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 18px;
  line-height: 1.6;
}
main { max-width: 680px; margin: 0 auto; padding: 12px 16px calc(24px + var(--safe-bottom)); }
h2 { font-size: 1.1rem; margin: 0 0 8px; }
h3 { font-size: 1rem; margin: 12px 0 4px; }
a { color: var(--primary); }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  min-height: 52px; padding: env(safe-area-inset-top, 0px) 12px 0;
  background: var(--primary); color: var(--primary-text);
}
.topbar-title { font-weight: 700; font-size: 1.05rem; }
.back { background: none; border: 0; color: inherit; font-size: 1rem; padding: 10px 8px; min-height: 44px; }

.banner { padding: 10px 16px; font-size: 0.95rem; }
.banner-offline { background: var(--warn-bg); color: var(--warn-text); position: sticky; top: 52px; z-index: 19; }

.stack { display: flex; flex-direction: column; gap: 12px; margin: 12px 0; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row .input { flex: 1; }

.btn {
  appearance: none; border: 2px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: var(--radius); padding: 10px 16px; min-height: 48px; font-size: 1rem; font-weight: 600;
  cursor: pointer; touch-action: manipulation;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-ghost { background: transparent; }
.btn-big { min-height: 60px; font-size: 1.1rem; width: 100%; }
.btn-huge { min-height: 84px; font-size: 1.35rem; width: 100%; }
.btn-small { min-height: 44px; padding: 8px 12px; font-size: 0.95rem; }
.btn-ghost-dark { background: transparent; color: #fff; border-color: #fff8; min-height: 52px; }

.input {
  width: 100%; font: inherit; font-size: 1rem; color: var(--text); background: var(--surface);
  border: 2px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 48px;
}
textarea.input { resize: vertical; }
.input:focus, .btn:focus-visible { outline: 3px solid #93c5fd; outline-offset: 1px; }

.field { display: block; margin: 12px 0; }
.label { display: block; font-size: 0.92rem; color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.value { font-weight: 600; }
.hint, .note { font-size: 0.92rem; color: var(--muted); }
.note { background: var(--surface); border-left: 4px solid var(--line); padding: 8px 12px; border-radius: 6px; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 12px 0; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-weight: 700; }
.alert { padding: 12px 14px; border-radius: 10px; margin: 10px 0; }
.alert-error { background: var(--error-bg); }
.alert-warn { background: var(--warn-bg); color: var(--warn-text); }
.alert-ok { background: var(--ok-bg); color: var(--ok-text); }
.restore { background: var(--warn-bg); color: var(--warn-text); padding: 12px; border-radius: var(--radius); }

.status-list { list-style: none; padding: 0; margin: 8px 0; font-size: 0.95rem; }
.status-list li { padding: 4px 0; }
.tips { background: var(--surface); border-radius: var(--radius); padding: 10px 14px; margin-top: 12px; }
.tips summary { font-weight: 700; min-height: 44px; display: flex; align-items: center; cursor: pointer; }

.sticky-actions {
  position: sticky; bottom: 0; z-index: 10;
  background: var(--bg); padding: 10px 0 calc(10px + var(--safe-bottom));
  display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--line); margin-top: 16px;
}
.save-summary { font-size: 0.95rem; color: var(--muted); text-align: center; }

/* カメラ */
.screen-camera { position: fixed; inset: 0; z-index: 50; background: #000; display: flex; flex-direction: column; }
.screen-camera[hidden] { display: none; }
.camera-view { position: relative; flex: 1; overflow: hidden; }
.camera-view video { width: 100%; height: 100%; object-fit: contain; background: #000; }
.camera-guide {
  position: absolute; left: 12px; right: 12px; top: calc(12px + env(safe-area-inset-top, 0px));
  color: #fff; background: #0009; border-radius: 10px; padding: 8px 12px; font-size: 0.95rem; text-align: center;
}
.camera-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px calc(20px + var(--safe-bottom)); background: #000;
}
.camera-bar-spacer { width: 88px; }
.shutter {
  width: 84px; height: 84px; border-radius: 50%; border: 6px solid #fff; background: #ffffffcc;
  touch-action: manipulation;
}
.shutter:active { background: #fff; transform: scale(0.95); }
.shutter:disabled { opacity: 0.5; }

.photo-preview { width: 100%; max-height: 60vh; object-fit: contain; background: #000; border-radius: var(--radius); }

/* 結果 */
.progress { background: var(--surface); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; border: 1px solid var(--line); }
.progress-bar { height: 10px; background: var(--line); border-radius: 5px; overflow: hidden; margin-top: 6px; }
#progress-fill { height: 100%; width: 0; background: var(--primary); transition: width 0.3s; }
.canvas-wrap { width: 100%; }
.canvas-wrap canvas { display: block; border-radius: 8px; max-width: 100%; }
.region-tools { margin-top: 8px; }
.region-tools .row { margin: 6px 0; }

.card { background: var(--surface); border: 2px solid var(--line); border-radius: var(--radius); margin: 14px 0; overflow: hidden; }
.card.is-confirmed { border-color: var(--primary); }
.card.is-saved { border-color: #16a34a; }
.card.is-excluded { opacity: 0.75; }
.card-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.card-no {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 36px; height: 36px; border-radius: 18px; background: var(--primary); color: #fff; font-weight: 700;
}
.card-state { flex: 1; font-size: 0.92rem; font-weight: 600; }
.card-body, .excluded-body { padding: 12px; }
.excluded-body { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.crop-box { background: #0001; border-radius: 8px; text-align: center; }
.crop { max-width: 100%; max-height: 260px; object-fit: contain; display: block; margin: 0 auto; }
.crop-missing { padding: 16px; color: var(--muted); font-size: 0.9rem; }
.card-msg:not(:empty) { margin-top: 8px; }
.fields { border: 0; padding: 0; margin: 0; min-width: 0; }
.badge { display: inline-block; font-size: 0.8rem; padding: 0 8px; border-radius: 10px; margin-left: 4px; }
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }
.warnings { list-style: none; padding: 0; margin: 8px 0; }
.warnings li { background: var(--warn-bg); color: var(--warn-text); padding: 6px 10px; border-radius: 8px; margin: 4px 0; font-size: 0.92rem; }
.confirm {
  display: flex; gap: 12px; align-items: center; padding: 12px; margin: 10px 0;
  border: 2px dashed var(--line); border-radius: 10px; font-weight: 700; min-height: 56px;
}
.confirm input { width: 28px; height: 28px; flex: none; }
.role-choice { background: var(--warn-bg); color: var(--warn-text); padding: 10px; border-radius: 10px; margin-top: 8px; }
.choices { display: flex; flex-direction: column; gap: 8px; }
.choice { text-align: left; white-space: pre-wrap; font-weight: 500; color: var(--text); }
.others { margin-top: 8px; font-size: 0.92rem; }
.others summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; color: var(--muted); }
.others li { white-space: pre-wrap; }
.raw { margin-top: 8px; font-size: 0.92rem; }
.raw summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; color: var(--muted); }
.raw pre { white-space: pre-wrap; word-break: break-all; background: var(--bg); padding: 8px; border-radius: 8px; user-select: text; -webkit-user-select: text; font-family: inherit; }

.result-counts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 10px 0; }
.count { background: var(--surface); border-radius: var(--radius); padding: 14px; text-align: center; border: 2px solid var(--line); }
.count strong { display: block; font-size: 2rem; }
.count.ok { border-color: #16a34a; }
.count.ng { border-color: var(--danger); }
.result-list { padding-left: 0; list-style: none; }
.result-list li { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; margin: 6px 0; }
.result-list .reason { color: var(--danger); font-size: 0.92rem; }

.dialog { border: 0; border-radius: var(--radius); padding: 16px; max-width: min(560px, 94vw); background: var(--surface); color: var(--text); }
.dialog::backdrop { background: #0008; }
.consent-list dt { font-weight: 700; margin-top: 8px; }
.consent-list dd { margin: 2px 0 0 0; font-size: 0.95rem; }

#settings-message:not(:empty) { position: sticky; bottom: 12px; }
