@font-face {
  font-family: 'PdfFont';
  src: url('../fonts/BIZUDGothic-Regular-subset.ttf') format('truetype');
  font-display: swap;
}

:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --ink: #17202b;
  --muted: #5b6878;
  --line: #d6dde6;
  --brand: #1f3a5f;
  --accent: #1d64c8;
  --accent-weak: #e7f0fc;
  --ok: #1d7a4c;
  --ok-weak: #e1f3e8;
  --warn: #9a5200;
  --warn-weak: #fff0d6;
  --danger: #c0352b;
  --radius: 12px;
  --thumb-w: 120px;
  --thumb-h: 90px;
  --font: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Noto Sans CJK JP", "Yu Gothic UI", sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
body.no-scroll { overflow: hidden; }
button { font: inherit; color: inherit; }
.boot { padding: 40px 16px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pre { white-space: pre-line; margin: 0; }
.ico { display: inline-flex; line-height: 0; }

/* ---------- ボタン ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--ink);
  font-weight: 700; white-space: nowrap; cursor: pointer;
}
.btn:active { filter: brightness(.94); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-outline { background: transparent; border-color: #e0675e; color: #ff8a80; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn.ghost.light { color: #fff; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn:disabled { opacity: .4; cursor: default; filter: none; }
.icon-btn {
  display: inline-grid; place-items: center; flex: 0 0 auto;
  width: 44px; height: 44px; border: 0; border-radius: 10px;
  background: transparent; color: inherit; cursor: pointer;
}
.icon-btn:active { background: rgba(255, 255, 255, .16); }
.icon-btn.dark { color: var(--muted); }
.icon-btn.dark:active { background: #e8ecf1; }

/* ---------- 上部バー ---------- */
.appbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 6px;
  min-height: 56px; padding: 6px 8px;
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--brand); color: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .18);
}
.appbar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; padding-left: 6px; }
.appbar h1 { margin: 0; font-size: 17px; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.appbar-sub { font-size: 12px; opacity: .8; }
.btn.save { background: #fff; border-color: #fff; color: var(--brand); min-width: 72px; }

/* ---------- 一覧 ---------- */
.list-main { max-width: 760px; margin: 0 auto; padding: 12px 12px calc(24px + env(safe-area-inset-bottom)); }
.list-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.list-actions .btn.primary { flex: 1; }
.ptable { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.prow {
  display: grid; grid-template-columns: minmax(0, 1fr) 82px 82px 40px; align-items: center; gap: 6px;
  min-height: 62px; padding: 4px 4px 4px 12px; border-top: 1px solid var(--line); cursor: pointer;
}
.prow:active { background: #f4f7fa; }
.prow.head { min-height: 34px; border-top: 0; background: #f6f8fa; font-size: 12px; color: var(--muted); cursor: default; }
.pname { font-weight: 700; overflow-wrap: anywhere; }
.dt { font-size: 12px; line-height: 1.35; color: var(--muted); font-variant-numeric: tabular-nums; }
.empty { padding: 48px 16px; text-align: center; color: var(--muted); }

/* ---------- プロジェクト（項目と写真） ---------- */
.items { padding: 4px 0 calc(32px + env(safe-area-inset-bottom)); }
.item-section {
  position: relative; margin: 8px; overflow: hidden;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  transition: background-color .15s, border-color .15s;
}
.item-head { display: flex; align-items: center; gap: 8px; padding: 8px 8px 2px 10px; }
.item-no { font-size: 12px; font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; }
.item-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.item-name { font-weight: 700; overflow-wrap: anywhere; }
.item-rule { font-size: 11px; color: var(--muted); }
.item-count {
  padding: 2px 10px; border-radius: 999px; background: #edf0f4;
  font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap;
}
.item-count[data-state="ok"] { background: var(--ok-weak); color: var(--ok); }
.item-count[data-state="warn"] { background: var(--warn-weak); color: var(--warn); }
.btn.capture { min-height: 42px; padding: 0 12px; }

.strip {
  display: flex; gap: 8px; align-items: center;
  min-height: calc(var(--thumb-h) + 18px); padding: 6px 10px 12px;
  overflow-x: auto; overflow-y: hidden; scrollbar-width: thin;
}
.strip-empty { color: var(--muted); font-size: 13px; padding-left: 2px; }
.strip:has(.thumb) .strip-empty { display: none; }
.thumb {
  position: relative; flex: 0 0 auto; width: var(--thumb-w); height: var(--thumb-h);
  border-radius: 8px; background: #d9dee5 center / cover no-repeat;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .08);
  -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; cursor: pointer;
}
.thumb-no {
  position: absolute; left: 4px; top: 4px; min-width: 28px; padding: 1px 6px; border-radius: 6px;
  background: rgba(23, 32, 43, .8); color: #fff; font-size: 12px; font-weight: 700; text-align: center;
  font-variant-numeric: tabular-nums;
}
.thumb-flag {
  display: none; position: absolute; right: 4px; bottom: 4px; padding: 1px 6px; border-radius: 6px;
  background: var(--warn); color: #fff; font-size: 11px; font-weight: 700;
}
.thumb.portrait .thumb-flag { display: block; }

/* ドラッグ中 */
body.dragging, body.dragging * { -webkit-user-select: none !important; user-select: none !important; cursor: grabbing; }
.thumb.drag-placeholder { opacity: .35; outline: 2px dashed var(--accent); outline-offset: -2px; }
.drag-ghost {
  position: fixed !important; left: 0; top: 0; z-index: 1000; margin: 0;
  pointer-events: none; opacity: .95; will-change: transform;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .35);
}
.item-section.drag-current { border-color: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); }
.item-section.drop-pending { border-color: var(--accent); background: var(--accent-weak); }
.item-section.drop-pending::after {
  content: ''; position: absolute; left: 0; bottom: 0; width: 100%; height: 4px;
  background: var(--accent); transform-origin: left center;
  animation: dwell var(--dwell, 500ms) linear forwards;
}
@keyframes dwell { from { transform: scaleX(0); } to { transform: scaleX(1); } }

/* ---------- ダイアログ ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 200; display: grid; place-items: center; padding: 12px;
  background: rgba(10, 16, 24, .5);
}
.modal {
  display: flex; flex-direction: column; width: min(520px, 100%); max-height: calc(100dvh - 24px);
  overflow: hidden; background: var(--surface); border-radius: 16px; box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
.modal-title { margin: 0; padding: 16px 18px 6px; font-size: 17px; }
.modal-body { padding: 4px 18px 14px; overflow: auto; }
.modal-body p { margin: 6px 0; }
@media (max-width: 420px) { .modal-overlay { padding: 8px; } .modal-title { padding: 14px 14px 6px; } .modal-body { padding: 4px 14px 12px; } }
@media (max-width: 380px) { .alert-text { font-size: 13px; } }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 12px 12px; border-top: 1px solid var(--line); }
.modal-actions .btn { flex: 1 1 auto; }

.field-label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.text-input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  font: inherit; font-size: 16px; color: var(--ink);
}
.text-input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.field-hint { min-height: 1.2em; font-size: 12px; color: var(--muted); }
.field-error { min-height: 1.2em; font-size: 13px; color: var(--danger); }

/* アラート */
.alert-list { list-style: none; margin: 8px 0 0; padding: 0; max-height: 46vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
.alert-list li + li { border-top: 1px solid var(--line); }
.alert-list label { display: flex; align-items: center; gap: 8px; min-height: 48px; padding: 8px 10px; cursor: pointer; }
.alert-list input { flex: 0 0 auto; width: 22px; height: 22px; accent-color: var(--accent); }
.alert-text { font-size: 13.5px; line-height: 1.4; }
.alert-counter { margin: 8px 0 0; font-size: 13px; color: var(--muted); text-align: right; }
.missing-list { max-height: 30vh; overflow: auto; font-size: 12px; padding-left: 18px; }

/* 進行状況 */
.progress-text { margin: 4px 0; padding: 0 18px; font-size: 14px; }
.progress-bar { height: 8px; margin: 8px 18px 20px; overflow: hidden; border-radius: 99px; background: #e6eaf0; }
.progress-fill { width: 0; height: 100%; background: var(--accent); transition: width .15s; }
.busy-overlay { position: fixed; inset: 0; z-index: 250; display: grid; place-items: center; background: rgba(10, 16, 24, .35); }
.busy { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-radius: 12px; background: var(--surface); font-weight: 700; box-shadow: 0 10px 30px rgba(0, 0, 0, .25); }
.spinner { width: 20px; height: 20px; border: 3px solid #d5dde8; border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* 設定 */
.settings h3 { margin: 14px 0 6px; font-size: 14px; }
.settings h3:first-child { margin-top: 4px; }
.settings .radio { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.settings .radio input { width: 20px; height: 20px; accent-color: var(--accent); }
.settings small { color: var(--muted); }
.settings-row { display: flex; align-items: center; gap: 10px; }
.folder-name { flex: 1; min-width: 0; font-weight: 700; overflow-wrap: anywhere; }

/* ---------- プレビュー ---------- */
.preview { position: fixed; inset: 0; z-index: 150; display: flex; flex-direction: column; background: #4a5059; }
.preview-bar {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top)); background: var(--brand); color: #fff;
}
.preview-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.preview-title strong, .preview-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preview-title span { font-size: 12px; opacity: .85; }
.preview-zoom {
  position: absolute; right: 14px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 2;
  min-width: 64px; height: 44px; padding: 0 16px; border: 0; border-radius: 999px;
  background: rgba(23, 32, 43, .88); color: #fff; font-weight: 700; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .35);
}
.preview-scroll { --zoom: 1; flex: 1; overflow: auto; padding: 12px; container-type: inline-size; }
.preview-note { max-width: 820px; margin: 0 auto 10px; color: #e8ebef; font-size: 13px; }
.pv-sheet { margin: 0 auto 18px; width: calc(var(--zoom) * min(100cqi, 820px)); }
.pv-label { margin: 0 0 4px; color: #e8ebef; font-size: 12px; }
.pv-page {
  position: relative; width: 100%; aspect-ratio: 210 / 297; background: #fff;
  font-size: calc(var(--zoom) * min(100cqi, 820px) / 210); /* 1em = 1mm */
  box-shadow: 0 4px 16px rgba(0, 0, 0, .35);
}
.pv-photo { position: absolute; padding: 0; border: 0; background: #f2f2f2; cursor: zoom-in; }
.pv-photo img { display: block; width: 100%; height: 100%; object-fit: contain; }
.pv-line { position: absolute; height: 0; border-top: max(1px, .14em) solid #000; transform: translateY(-50%); }
.pv-line.thick { border-top-width: max(2.5px, .42em); }
.pv-cell {
  position: absolute; display: flex; align-items: center; justify-content: center;
  overflow: hidden; white-space: nowrap; color: #000; font-family: 'PdfFont', var(--font);
}
.pv-cell.name span { font-size: 3.88em; } /* 11pt */
.pv-cell.item span { font-size: 3.53em; } /* 10pt */

/* ---------- 全面表示 ---------- */
.viewer { position: fixed; inset: 0; z-index: 300; display: flex; flex-direction: column; background: #000; color: #fff; }
.viewer-bar {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top)); background: rgba(20, 24, 30, .92);
}
.viewer-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.viewer-title span { font-size: 12px; opacity: .8; }
.viewer-stage { position: relative; flex: 1; overflow: auto; }
.viewer-stage img { position: absolute; inset: 0; margin: auto; max-width: 100%; max-height: 100%; cursor: zoom-in; }
.viewer-stage.zoomed img { position: static; max-width: none; max-height: none; margin: 0; cursor: zoom-out; }
.viewer-hint { margin: 0; padding: 4px; text-align: center; font-size: 11px; opacity: .6; }
.viewer-actions {
  display: flex; gap: 8px; padding: 10px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: rgba(20, 24, 30, .92);
}
.viewer-actions .btn { flex: 1; }

/* ---------- トースト ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 400;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 10px;
  background: #17202b; color: #fff; font-size: 14px;
  opacity: 0; transform: translate(-50%, 24px); transition: opacity .2s, transform .2s; pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.ok { background: var(--ok); }
.toast.warn { background: var(--warn); }
.toast.error { background: var(--danger); }
