:root {
  --bg: #1f2329;
  --panel: #2a2f37;
  --panel-2: #323843;
  --border: #3b424e;
  --text: #e6e9ee;
  --muted: #9aa3b2;
  --accent: #4c8dff;
  --accent-weak: rgba(76, 141, 255, 0.18);
  --danger: #ff6b6b;
  --viewer-bg: #3a3f48;
  font-family: "Yu Gothic UI", "Meiryo UI", "Segoe UI", sans-serif;
  font-size: 13px;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
body { display: flex; flex-direction: column; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }

button {
  font: inherit;
  color: inherit;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  height: 32px;
  padding: 0 9px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  white-space: nowrap;
}
@media (hover: hover) { button:hover:not(:disabled) { background: var(--panel-2); } }
button:active:not(:disabled) { background: var(--border); }
button:disabled { opacity: 0.35; cursor: default; }
button.icon-only { padding: 0 7px; }
button.active { background: var(--accent-weak); border-color: var(--accent); color: #fff; }
button.primary { background: var(--accent); color: #fff; }
button.primary:hover { background: #3b7cf0; }
button svg {
  width: 18px; height: 18px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

/* ---- toolbar ---- */
#toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  min-height: 46px;
}
#toolbar .group { display: flex; align-items: center; gap: 2px; padding-right: 6px; margin-right: 3px; border-right: 1px solid var(--border); }
#toolbar .group:last-child, #toolbar .group.zoom { border-right: none; }
#toolbar .spacer { flex: 1; }
#zoomLabel { min-width: 50px; justify-content: center; font-variant-numeric: tabular-nums; }

.props { min-height: 32px; }
.prop { display: none; align-items: center; gap: 4px; height: 32px; padding: 0 4px; color: var(--muted); }
.prop.show { display: inline-flex; }
.prop input[type="color"] {
  width: 30px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 5px; background: none; cursor: pointer;
}
.prop input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
.prop input[type="color"]::-webkit-color-swatch { border: none; border-radius: 3px; }
.prop select, .prop input[type="number"] {
  font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 5px; height: 28px; padding: 0 6px;
}
.prop input[type="number"] { width: 64px; }
.prop input[type="checkbox"] { accent-color: var(--accent); }
.prop .unit { font-size: 12px; }

/* ---- layout ---- */
#main { flex: 1; display: flex; min-height: 0; }

#sidebar {
  width: 196px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}
.side-actions { display: flex; flex-wrap: wrap; gap: 2px; padding: 6px; border-bottom: 1px solid var(--border); }
.side-actions button.danger:hover:not(:disabled) { color: var(--danger); }

#thumbs { flex: 1; overflow-y: auto; padding: 10px 0 30px; outline: none; }
.thumb {
  position: relative;
  margin: 0 auto 12px;
  width: 150px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}
.thumb-box {
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.5);
  outline: 2px solid transparent;
  outline-offset: 3px;
  border-radius: 1px;
  overflow: hidden;
}
.thumb-box canvas { display: block; width: 100%; height: 100%; }
.thumb-box .overlay-wrap { position: absolute; inset: 0; pointer-events: none; }
.thumb.current .thumb-box { outline-color: var(--muted); }
.thumb.selected .thumb-box { outline-color: var(--accent); }
.thumb-num { font-size: 11px; color: var(--muted); }
/* 保存のときに画像にするページ（利用者が選んだもの）の印 */
.thumb.raster .thumb-num::after { content: '画像'; margin-left: 4px; padding: 0 4px; border-radius: 3px; background: var(--accent); color: #fff; font-size: 10px; }
.thumb.selected .thumb-num { color: #fff; }
.thumb.dragging { opacity: 0.4; }
.thumb.drop-before::before, .thumb.drop-after::after {
  content: ""; position: absolute; left: 8px; right: 8px; height: 3px; background: var(--accent); border-radius: 2px;
}
.thumb.drop-before::before { top: -8px; }
.thumb.drop-after::after { bottom: -8px; }

/* ---- viewer ---- */
#viewer { flex: 1; position: relative; overflow: auto; background: var(--viewer-bg); outline: none; }
#pages { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 24px 24px 60px; min-width: min-content; }
.page {
  position: relative;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0,0,0,0.45);
  flex: none;
}
.page > canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.page .overlay {
  position: absolute; left: 0; top: 0;
  transform-origin: 0 0;
}
.page .page-label {
  position: absolute; top: 100%; left: 0; right: 0; text-align: center; font-size: 11px; color: #c8ccd4; padding-top: 3px;
}

body:is([data-mode="text"], [data-mode="fill"]) .page .overlay { cursor: text; }
body[data-mode="whiteout"] .page .overlay,
body[data-mode="redact"] .page .overlay,
body[data-mode="highlight"] .page .overlay,
body[data-mode="rect"] .page .overlay,
body[data-mode="line"] .page .overlay,
body[data-mode="arrow"] .page .overlay,
body[data-mode="ink"] .page .overlay { cursor: crosshair; }
body:not([data-mode="select"]) .obj { pointer-events: none; }
/* 選択ツールでは、追加した項目以外の場所は下の文字の層（PDF の文字の選択）に通す */
body[data-mode="select"] .page .overlay { pointer-events: none; }
body[data-mode="select"] .page .overlay :is(.obj:not(.vector), .handle) { pointer-events: auto; }

/* PDF の文字の層（pdf.js の TextLayer。pdf_viewer.css から必要な分だけ） */
.page { --scale-round-x: 1px; --scale-round-y: 1px; }
.textLayer {
  position: absolute; inset: 0; overflow: clip; opacity: 1; line-height: 1;
  text-align: initial; letter-spacing: normal; word-spacing: normal;
  -webkit-text-size-adjust: none; text-size-adjust: none; forced-color-adjust: none;
  transform-origin: 0 0; caret-color: CanvasText; z-index: 0;
  -webkit-user-select: text; user-select: text; /* 画面全体は選択不可なので、行間の余白から選び始められるようにする */
  -webkit-touch-callout: default; /* iPhone で選んだ文字に「コピー」などのメニューを出す（画面全体では止めている） */
  --min-font-size: 1;
  --text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
  --min-font-size-inv: calc(1 / var(--min-font-size));
}
.textLayer :is(span, br) {
  color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%;
  -webkit-user-select: text; user-select: text;
}
.textLayer > :not(.markedContent), .textLayer .markedContent span:not(.markedContent) {
  z-index: 1;
  --font-height: 0;
  font-size: calc(var(--text-scale-factor) * var(--font-height));
  --scale-x: 1;
  --rotate: 0deg;
  transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
}
.textLayer .markedContent { display: contents; }
.textLayer span[role="img"] { -webkit-user-select: none; user-select: none; cursor: default; }
.textLayer ::selection { background: rgba(30, 110, 255, 0.3); color: transparent; }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent {
  display: block; position: absolute; inset: 100% 0 0; z-index: 0; cursor: default;
  -webkit-user-select: none; user-select: none;
}
.textLayer.selecting .endOfContent { top: 0; }
.textLayer[data-main-rotation="90"] { transform: rotate(90deg) translateY(-100%); }
.textLayer[data-main-rotation="180"] { transform: rotate(180deg) translate(-100%, -100%); }
.textLayer[data-main-rotation="270"] { transform: rotate(270deg) translateX(-100%); }

/* objects */
.obj { position: absolute; transform-origin: 0 0; }
body[data-mode="select"] .obj { cursor: move; }
.obj.text { white-space: pre; padding: 0; margin: 0; }
.obj.text[contenteditable] { outline: 1px dashed var(--accent); cursor: text; -webkit-user-select: text; user-select: text; min-width: 4px; caret-color: currentColor; }
/* 入力中の文字は、どのツールでも押せるようにする（ドラッグで一部を選ぶ・2回クリックで言葉を選ぶ・iPhone の長押しのメニュー） */
.obj.text[contenteditable] { pointer-events: auto; touch-action: auto; -webkit-touch-callout: default; }
.obj.image img { width: 100%; height: 100%; display: block; pointer-events: none; }
.obj.highlight { mix-blend-mode: multiply; }
/* 回転したページでは重ね合わせ（乗算）が効かないため、画面上だけ半透明で代用する */
.overlay.rotated .obj.highlight { opacity: 0.45 !important; }
.obj.vector { pointer-events: none; overflow: visible; }
.obj.vector .hit { pointer-events: stroke; stroke: transparent; fill: none; }
body:not([data-mode="select"]) .obj.vector .hit { pointer-events: none; }
.obj.selected { outline: 1.5px solid var(--accent); outline-offset: 2px; }
/* 下の文字を消した絵が出ている白塗り：白く塗らない（「選択」ツールの点線は出るので、押して選べる） */
.obj.whiteout.erased { background: transparent !important; }
/* 白塗りは白くて見えないので、「選択」「白塗り」ツールのときは点線で場所を見せる（画面だけ。保存・印刷には出ない） */
body:is([data-mode="select"], [data-mode="whiteout"]) .obj.whiteout:not(.selected) { outline: 1px dashed rgba(74, 144, 255, 0.7); outline-offset: -1px; }
.obj.vector.selected { outline: none; }
.obj.vector.selected .shape { filter: drop-shadow(0 0 1.5px var(--accent)) drop-shadow(0 0 1.5px var(--accent)); }

.handle {
  position: absolute; width: 10px; height: 10px; margin: -5px 0 0 -5px;
  background: #fff; border: 1.5px solid var(--accent); border-radius: 2px; z-index: 5;
}
.handle.se { cursor: nwse-resize; }
.handle.pt { border-radius: 50%; cursor: crosshair; }
/* 回転のつまみ：上辺の真ん中から伸びる棒の先の丸 */
.handle.rot { border-radius: 50%; cursor: grab; }
.handle.rot::after {
  content: ''; position: absolute; left: 50%; top: 100%; width: 1.5px; margin-left: -0.75px;
  height: calc(var(--stem, 26px) - 5px); background: var(--accent); pointer-events: none;
}

.draft { position: absolute; pointer-events: none; }
/* 複数選んだとき：全部を囲む枠（上の丸でまとめて回す） */
.group-box { position: absolute; border: 1px dashed var(--accent); pointer-events: none; z-index: 4; }

/* 右クリックのメニュー */
#ctxMenu {
  position: fixed; z-index: 200; min-width: 200px; padding: 4px; outline: none;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,0.45);
}
#ctxMenu[hidden] { display: none; }
#ctxMenu button { position: relative; display: flex; width: 100%; height: 30px; padding: 0 12px 0 28px; gap: 24px; border: 0; border-radius: 5px; text-align: left; }
#ctxMenu button > span { flex: 1; }
#ctxMenu button:is(:hover, :focus-visible):not(:disabled) { background: var(--accent); color: #fff; outline: none; }
#ctxMenu kbd { font: inherit; font-size: 12px; color: var(--muted); }
#ctxMenu button:is(:hover, :focus-visible):not(:disabled) kbd { color: inherit; }
#ctxMenu button[aria-checked="true"]::before { content: ''; position: absolute; left: 12px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: currentColor; }
#ctxMenu .sep { height: 1px; margin: 4px 8px; background: var(--border); }
#ctxMenu .head { padding: 6px 12px 2px 28px; font-size: 11px; color: var(--muted); }
@media (pointer: coarse) {
  #ctxMenu button { height: 40px; }
  #ctxMenu kbd { display: none; }
}
.draft.box { border: 1px dashed var(--accent); background: var(--accent-weak); }

/* ---- welcome ---- */
#welcome { position: absolute; inset: 0; display: flex; overflow-y: auto; padding: 12px 0; }
.welcome-card { margin: auto; }
.phone-only { display: none; }
body.has-doc #welcome { display: none; }
body:not(.has-doc) #sidebar .side-actions { visibility: hidden; }
.welcome-card {
  width: 460px; text-align: center; padding: 36px 40px 30px; border-radius: 14px;
  background: var(--panel); border: 1px solid var(--border); box-shadow: 0 10px 40px rgba(0,0,0,0.35);
}
.welcome-icon svg { width: 54px; height: 54px; fill: none; stroke: var(--accent); stroke-width: 1.4; stroke-linejoin: round; }
.welcome-card h1 { margin: 10px 0 8px; font-size: 22px; font-weight: 600; }
.welcome-card p { color: var(--muted); line-height: 1.7; margin: 0 0 20px; }
.welcome-actions { display: flex; justify-content: center; gap: 10px; }
.welcome-actions button { height: 38px; padding: 0 18px; border: 1px solid var(--border); }
.welcome-actions button.primary { border-color: var(--accent); }
.welcome-url, .welcome-links { display: none; } /* ブラウザ版だけ web.css で表示する */

#dropHint {
  position: absolute; inset: 10px; border: 3px dashed var(--accent); border-radius: 12px; background: rgba(76,141,255,0.08);
  display: none; align-items: center; justify-content: center; font-size: 20px; color: #fff; pointer-events: none; z-index: 20;
}
body.drag-over #dropHint { display: flex; }

/* ---- status ---- */
#status {
  display: flex; gap: 18px; align-items: center; height: 26px; padding: 0 12px;
  background: var(--panel); border-top: 1px solid var(--border); color: var(--muted); font-size: 12px;
}
#statusFile, #statusPage { flex: none; white-space: nowrap; }
#statusHint { margin-left: auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- overlays ---- */
#busy {
  position: fixed; inset: 0; display: none; align-items: center; justify-content: center; gap: 12px;
  background: rgba(15,17,20,0.55); z-index: 100; font-size: 14px;
}
body.busy #busy { display: flex; }
.spinner { width: 22px; height: 22px; border: 3px solid rgba(255,255,255,0.25); border-top-color: #fff; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#toast {
  position: fixed; left: 50%; bottom: 44px; transform: translateX(-50%) translateY(20px);
  background: #11141a; color: #fff; border: 1px solid var(--border); padding: 10px 16px; border-radius: 8px;
  opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; z-index: 110; max-width: 70vw;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { border-color: var(--danger); }

::-webkit-scrollbar { width: 11px; height: 11px; }
::-webkit-scrollbar-thumb { background: #59606c; border-radius: 6px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: #6c7482; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-corner { background: transparent; }

/* 幅が足りないパソコン画面では、開く・保存・印刷を絵だけにしてツールバーを1段に収める */
@media (min-width: 761px) and (max-width: 1520px) {
  #toolbar .group:first-child button > span { display: none; }
  #toolbar .group:first-child button { padding: 0 7px; }
}
/* さらに狭いときは、よく使う「選択」「文字」「記入」以外のツールも絵だけにする */
@media (min-width: 761px) and (max-width: 1400px) {
  #toolbar .group.tools :is([data-tool="whiteout"], [data-tool="redact"], [data-tool="highlight"], [data-cmd="insertImage"], [data-cmd="sign"]) > span { display: none; }
  #toolbar .group.tools :is([data-tool="whiteout"], [data-tool="redact"], [data-tool="highlight"], [data-cmd="insertImage"], [data-cmd="sign"]) { padding: 0 7px; }
}

/* ---- 指での操作（スマホ・タブレット） ----
   1本指：選択ツールと文字ツールではスクロール、それ以外のツールでは書き込み。2本指：拡大縮小と移動（app.js で処理） */
.mobile-only { display: none !important; }
button { touch-action: manipulation; }
#viewer { touch-action: pan-x pan-y; }
body:not([data-mode="select"]):not([data-mode="text"]):not([data-mode="fill"]) .page .overlay { touch-action: none; }
.obj, .handle { touch-action: none; }
@media (pointer: coarse) {
  .handle { width: 22px; height: 22px; margin: -11px 0 0 -11px; border-width: 2px; border-radius: 4px; }
  .handle.pt, .handle.rot { border-radius: 50%; }
  .handle.rot::after { height: calc(var(--stem, 40px) - 11px); width: 2px; margin-left: -1px; }
}

/* ---- スマホの画面（幅 760px 以下）：上に保存などのボタン、下にツール、ページ一覧は左から出す ---- */
@media (max-width: 760px) {
  :root { --bar-h: 52px; --safe-b: env(safe-area-inset-bottom, 0px); font-size: 14px; }
  .mobile-only { display: inline-flex !important; }
  button { height: 40px; }
  button.icon-only, #toolbar button { padding: 0 9px; }
  #toolbar {
    flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: 2px;
    padding: 4px 6px; padding-top: max(4px, env(safe-area-inset-top, 0px)); min-height: 0;
  }
  #toolbar::-webkit-scrollbar { display: none; }
  #toolbar .group { padding-right: 4px; margin-right: 2px; flex: none; }
  #toolbar button > span { display: none; }
  #toolbar .group.zoom [data-cmd="zoomIn"], #toolbar .group.zoom [data-cmd="zoomOut"], #zoomLabel { display: none; }

  /* ツールは画面の下（指が届きやすい位置）に横並び */
  #toolbar .group.tools {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    height: calc(var(--bar-h) + var(--safe-b)); padding: 0 6px var(--safe-b); margin: 0;
    background: var(--panel); border-top: 1px solid var(--border); border-right: none;
    overflow-x: auto; scrollbar-width: none; justify-content: space-between;
  }
  #toolbar .group.tools::-webkit-scrollbar { display: none; }
  #toolbar .group.tools button { height: 44px; min-width: 40px; justify-content: center; flex: none; }
  body:not(.has-doc) #toolbar .group.tools { display: none; }

  /* 色や太さなどは、ツールの上にもう1段 */
  #toolbar .group.props {
    position: fixed; left: 0; right: 0; bottom: calc(var(--bar-h) + var(--safe-b)); z-index: 25;
    display: none; height: 46px; padding: 0 8px; margin: 0; gap: 6px;
    background: var(--panel); border-top: 1px solid var(--border); border-right: none;
    overflow-x: auto; scrollbar-width: none;
  }
  #toolbar .group.props:has(.prop.show) { display: flex; }
  .prop select, .prop input[type="number"] { height: 34px; font-size: 16px; }
  .prop input[type="color"] { width: 36px; height: 32px; }
  /* 文字を選んだとき（色・書体・大きさ・コピー・貼り付け・削除）も1段に収まるように詰める */
  #propFont { max-width: 112px; }
  #propSize { width: 54px; }
  #toolbar .group.props button.icon-only { padding: 0 5px; flex: none; }

  #main { position: relative; padding-bottom: calc(var(--bar-h) + var(--safe-b)); }
  body:has(#props .prop.show) #main { padding-bottom: calc(var(--bar-h) + 46px + var(--safe-b)); }
  body:not(.has-doc) #main { padding-bottom: 0; }
  #status { display: none; }

  /* ページ一覧は左から引き出す */
  #sidebar {
    position: absolute; left: 0; top: 0; bottom: calc(var(--bar-h) + var(--safe-b)); z-index: 24;
    width: min(220px, 70vw); transform: translateX(-102%); transition: transform 0.18s ease-out;
    box-shadow: 4px 0 18px rgba(0,0,0,0.45);
  }
  body.side-open #sidebar { transform: none; }
  body:not(.has-doc) #sidebar { display: none; }
  .side-actions button { height: 40px; min-width: 40px; justify-content: center; }

  #pages { gap: 20px; padding: 10px 10px 40px; }
  .welcome-card { width: calc(100vw - 24px); padding: 20px 16px 18px; }
  .welcome-icon svg { width: 40px; height: 40px; }
  .welcome-card h1 { margin: 4px 0 6px; font-size: 20px; }
  .welcome-card p { margin-bottom: 16px; }
  .welcome-actions { flex-wrap: wrap; }
  /* スマホでは使えない説明（ドロップ・Ctrl+V・パソコンでの上書き）と機能の一覧は出さない */
  .desk-only { display: none !important; }
  .phone-only { display: revert; }
  #toast { bottom: calc(var(--bar-h) + 58px + var(--safe-b)); max-width: calc(100vw - 24px); }
}

/* 指で使う端末：PDF の文字はブラウザではなくアプリが選ぶ（iPhone の長押しの選択が透明な文字の層でうまく働かないため） */
body.touch-text .textLayer, body.touch-text .textLayer :is(span, br) {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.tsel { position: absolute; inset: 0; pointer-events: none; }
.tsel > div:first-child > div { position: absolute; background: rgba(30, 110, 255, 0.3); }
/* 範囲の両端のつまみ（始まりは上に丸、終わりは下に丸）。指で押しやすいよう、見た目より広く受ける */
.tsel-knob { position: absolute; width: 44px; margin-left: -22px; pointer-events: auto; touch-action: none; }
.tsel-knob[hidden] { display: none; }
.tsel-knob::before { content: ''; position: absolute; left: 21px; width: 2px; background: #1e6eff; }
.tsel-knob::after { content: ''; position: absolute; left: 15px; width: 14px; height: 14px; border-radius: 50%; background: #1e6eff; }
.tsel-knob.a::before { top: 14px; bottom: 0; }
.tsel-knob.a::after { top: 2px; }
.tsel-knob.b::before { top: 0; bottom: 14px; }
.tsel-knob.b::after { bottom: 2px; }

/* 記入：見つけた記入欄の点線（「記入」ツールのときだけ。書いた欄は消す）。追加した書き込みより下に重ねる */
/* ---- 一覧（1行 = 1枚）。rows.js。「記入」ツールの「一覧」を押したときだけ、PDF の下に出る ---- */
#rowsPanel { position: relative; flex: none; height: 230px; display: flex; flex-direction: column; background: var(--panel); border-top: 1px solid var(--border); -webkit-user-select: text; user-select: text; }
#rowsPanel[hidden] { display: none; }
.rows-grip { position: absolute; left: 0; right: 0; top: -3px; height: 7px; cursor: ns-resize; z-index: 3; }
.rows-bar { display: flex; align-items: center; gap: 4px; padding: 5px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; overflow-x: auto; scrollbar-width: none; }
.rows-bar::-webkit-scrollbar { display: none; }
.rows-bar b { margin-right: 4px; }
.rows-bar button { flex: none; height: 26px; }
.rows-note, .rows-count { color: var(--muted); font-size: 12px; margin-right: 8px; }
.rows-gap { flex: 1; }
.rows-sep { flex: none; width: 1px; height: 18px; margin: 0 4px; background: var(--border); }
.rows-wrap { flex: 1; min-height: 0; overflow: auto; }
.rows-wrap table { border-collapse: separate; border-spacing: 0; font-size: 12.5px; }
.rows-wrap :is(th, td) { height: 26px; padding: 0 6px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rows-wrap thead th { position: sticky; top: 0; z-index: 2; background: var(--panel-2); font-weight: 600; text-align: left; min-width: 110px; max-width: 170px; color: var(--muted); }
.rows-wrap thead th.mark { min-width: 44px; max-width: 90px; }
.rows-wrap tr > th:first-child { position: sticky; left: 0; z-index: 1; min-width: 40px; max-width: 40px; text-align: right; background: var(--panel-2); color: var(--muted); font-weight: 400; cursor: pointer; }
.rows-wrap thead th:first-child { z-index: 3; }
.rows-wrap td { min-width: 110px; max-width: 170px; cursor: pointer; }
.rows-wrap td.mark { min-width: 44px; max-width: 90px; text-align: center; }
.rows-wrap tr.cur > th:first-child { background: var(--accent); color: #fff; }
.rows-wrap tr.cur td { background: var(--accent-weak); padding: 0; cursor: text; }
.rows-wrap tr.cur td.mark { cursor: default; }
.rows-wrap td input[type="text"] { width: 100%; height: 25px; padding: 0 6px; border: 0; outline: 0; background: transparent; color: var(--text); font: inherit; }
.rows-wrap td input[type="text"]:focus { background: var(--bg); box-shadow: inset 0 0 0 1.5px var(--accent); }
.rows-wrap td.warn { box-shadow: inset 0 0 0 1.5px var(--danger); }
@media (max-width: 760px), (pointer: coarse) {
  /* スマホには出さない（表を扱える広さが無い） */
  #toolbar .prop[data-prop="rows"] { display: none !important; }
}

/* 記入欄を本物の入力欄としても重ねておく（ページの構造を読む AI・読み上げソフト用）。見えず、人の操作には当たらない。
   display や opacity で隠すと、AI や読み上げソフトからも見えなくなることがあるので、色を透明にして隠す */
.aform { position: absolute; left: 0; top: 0; pointer-events: none; }
.aform > * {
  position: absolute; box-sizing: border-box; margin: 0; padding: 0; border: 0; outline: 0; border-radius: 0;
  background: transparent; color: transparent; caret-color: transparent; font: 10px/1 sans-serif;
  appearance: none; -webkit-appearance: none; resize: none; overflow: hidden; pointer-events: none;
}
.aform > *::selection { background: transparent; color: transparent; }
/* AI 向けの案内など、画面には出さず、ページの文としてだけ置くもの */
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
/* ---- アプリの中で出す画面（署名・印鑑など）。ブラウザ版の確認の画面（web.css の .web-dialog）と同じ見た目 */
.app-dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 12px;
  padding: 22px 24px 18px; width: min(600px, calc(100vw - 24px)); max-height: calc(100vh - 24px); overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,0.45);
}
.app-dialog::backdrop { background: rgba(15,17,20,0.55); }
.app-dialog .msg { margin: 0 0 6px; font-size: 15px; font-weight: 600; }
.app-dialog .detail { margin: 0 0 12px; color: var(--muted); line-height: 1.6; font-size: 13px; }
.app-dialog .detail.warn { color: #e5484d; min-height: 1.2em; margin: 8px 0 0; }
.app-dialog .actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.app-dialog button { height: 34px; padding: 0 14px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel-2); color: inherit; font: inherit; cursor: pointer; }
.app-dialog button.primary { border-color: var(--accent); }
.sign-dialog .sign-head, .compress-dialog .sign-head { margin: 12px 0 6px; font-size: 13px; font-weight: 600; }
.sign-dialog .sign-saved { display: flex; flex-wrap: wrap; gap: 8px; max-height: 150px; overflow: auto; }
.sign-dialog .sign-cell { position: relative; }
.sign-dialog .sign-item { width: 132px; height: 64px; padding: 4px; background: #fff; border-color: var(--border); display: flex; align-items: center; justify-content: center; }
.sign-dialog .sign-item img { max-width: 100%; max-height: 100%; }
/* 消すボタン：枠の内側の右上に置く（外へはみ出すと、スクロールする入れ物の端で切れる）。×は文字ではなく線で描く（文字だと丸の中心からずれる） */
.sign-dialog .sign-del { position: absolute; top: 3px; right: 3px; box-sizing: border-box; width: 20px; height: 20px; min-width: 0; min-height: 0; padding: 0; border-radius: 50%; font-size: 0; line-height: 0; background: #fff; color: #444; border: 1px solid #b6bcc6; }
.sign-dialog .sign-del::before, .sign-dialog .sign-del::after { content: ''; position: absolute; left: 50%; top: 50%; width: 10px; height: 1.5px; margin: -0.75px 0 0 -5px; background: currentColor; border-radius: 1px; }
.sign-dialog .sign-del::before { transform: rotate(45deg); }
.sign-dialog .sign-del::after { transform: rotate(-45deg); }
.sign-dialog .sign-del:hover { background: #fde8e8; color: #c62828; border-color: #c62828; }
/* 圧縮の画面：大きさの棒、強さのスライダー、仕上がりの見本 */
.compress-dialog { width: min(560px, calc(100vw - 24px)); }
.compress-dialog .cmp-sizes { display: grid; gap: 8px; margin: 4px 0 14px; }
.compress-dialog .cmp-row { display: grid; grid-template-columns: minmax(64px, auto) 1fr minmax(76px, auto) 48px; align-items: center; gap: 10px; font-size: 13px; }
.compress-dialog .cmp-name { color: var(--muted); white-space: nowrap; }
.compress-dialog .cmp-bar { height: 14px; border-radius: 7px; background: rgba(127,127,127,0.18); overflow: hidden; }
.compress-dialog .cmp-bar i { display: block; height: 100%; border-radius: 7px; background: #8a93a0; transition: width 0.2s; }
.compress-dialog .cmp-bar i.after { background: var(--accent); }
.compress-dialog .cmp-val { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.compress-dialog .cmp-pct { color: #3fb950; font-variant-numeric: tabular-nums; white-space: nowrap; }
.compress-dialog.pending .cmp-row:last-child .cmp-val, .compress-dialog.pending .cmp-pct { opacity: 0.6; }
.compress-dialog .cmp-slider { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); }
.compress-dialog .cmp-slider input { width: 100%; height: 28px; accent-color: var(--accent); cursor: pointer; }
.compress-dialog .cmp-state { min-height: 1.6em; margin: 6px 0 0; }
.compress-dialog .cmp-preview { height: min(240px, 32vh); overflow: auto; border: 1px solid var(--border); border-radius: 6px; background: #5a6069; }
.compress-dialog .cmp-preview canvas { display: block; background: #fff; }
.compress-dialog.pending .cmp-preview canvas { opacity: 0.55; }
/* 署名を書く所：白い紙。指でなぞってもページが動かないようにする */
.sign-dialog .sign-pad { display: block; width: 100%; aspect-ratio: 1040 / 400; background: #fff; border: 1px dashed #8a93a0; border-radius: 6px; touch-action: none; cursor: crosshair; }
.sign-dialog .sign-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; }
.sign-dialog .sign-gap { flex: 1; }
.sign-dialog .sign-check { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--muted); }
.sign-dialog .sign-color { width: 28px; height: 28px; padding: 0; border-radius: 50%; background: var(--c); border: 2px solid var(--border); }
.sign-dialog .sign-color.active { outline: 2px solid var(--accent); outline-offset: 2px; }

/* 広告つきサイトの枠の中で動いているとき：言葉の切り替えはサイト側のページで行うので、ここでは出さない */
body.embed #langSel, body.embed #langSelWelcome { display: none; }
#langSelWelcome { margin: 10px auto 0; max-width: 220px; height: 34px; padding: 0 8px; border: 1px solid var(--border); border-radius: 6px; background: transparent; color: inherit; font: inherit; }
#langSelWelcome option { color: #111; background: #fff; }
#langSelWelcome[hidden] { display: none; }
/* 枠の中で端まで動かしたとき、外側のページまで一緒に動かさない */
html:has(body.embed), body.embed { overscroll-behavior: none; }

/* 英語のとき：言葉の置き換えが済むまで、画面を見せない（日本語が一瞬見えるのを防ぐ） */
html.lang-pending body { visibility: hidden; }
#langSel { flex: none; max-width: 150px; border: 1px solid var(--border); border-radius: 4px; background: var(--panel, transparent); color: inherit; font: inherit; padding: 1px 4px; cursor: pointer; }
#langSel option { color: #111; background: #fff; }
#aiGuideBtn { flex: none; white-space: nowrap; border: 0; background: none; padding: 0; color: inherit; font: inherit; cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
#aiGuideBtn:hover { color: var(--text, inherit); }

.fields { position: absolute; left: 0; top: 0; pointer-events: none; display: none; }
body[data-mode="fill"] .fields { display: block; }
.fields > div { position: absolute; box-sizing: border-box; border: 1.5px dashed rgba(30, 110, 255, 0.8); background: rgba(30, 110, 255, 0.07); border-radius: 2px; }
.fields > div:is(.check, .choice) { border-color: rgba(0, 150, 80, 0.9); background: rgba(0, 150, 80, 0.08); }
.fields > div.low { border-color: rgba(30, 110, 255, 0.45); background: rgba(30, 110, 255, 0.03); }
/* 職員が書く欄（「職員記入欄」など）：灰色で控えめに */
.fields > div.office { border-color: rgba(120, 120, 120, 0.45); background: rgba(120, 120, 120, 0.04); }
.fields > div.filled { display: none; }
/* 枠の番号（AI が「枠に番号を出す」を使ったとき）。書いた欄も薄く見せて、番号が分かるようにする */
.fields > div > .fnum {
  position: absolute; left: 0; top: 0; transform: translate(-35%, -55%); min-width: 16px; height: 16px; padding: 0 3px;
  border-radius: 8px; background: #1e6eff; color: #fff; font: 700 11px/16px "Segoe UI", sans-serif; text-align: center; pointer-events: none;
}
.fields > div:is(.check, .choice) > .fnum { background: #00965a; }
body.field-numbers .fields > div.filled { display: block; opacity: 0.5; }
/* 枠を直しているとき：枠を押せるようにし（追加した書き込みは押せないようにする）、書いた欄の枠も見せる */
body.field-editing .page .overlay { pointer-events: none; }
.fields.editing > div { pointer-events: auto; touch-action: none; cursor: move; }
.fields.editing > div.filled { display: block; opacity: 0.55; }
.fields.editing > div.sel { border-style: solid; border-width: 2px; border-color: #1e6eff; background: rgba(30, 110, 255, 0.14); z-index: 1; }
.fields > div > [data-fh] {
  position: absolute; display: flex; align-items: center; justify-content: center;
  font: 600 12px/1 "Yu Gothic UI", "Meiryo UI", sans-serif; color: #fff; background: #1e6eff; pointer-events: auto; touch-action: none;
}
/* つまみは枠の角の外側に斜めに置く（小さな枠でも重ならないように）：左上に種類、右上に消す、右下で大きさ */
.fields > div > [data-fh="type"] { right: 100%; bottom: 100%; margin: 0 0 1px; height: 20px; padding: 0 7px; border-radius: 10px; white-space: nowrap; cursor: pointer; }
.fields > div > [data-fh="del"] { left: 100%; bottom: 100%; margin: 0 0 -4px -4px; width: 22px; height: 22px; border-radius: 50%; background: #e53935; font-size: 16px; cursor: pointer; }
.fields > div > [data-fh="size"] { left: 100%; top: 100%; margin: -6px 0 0 -6px; width: 14px; height: 14px; border-radius: 3px; border: 2px solid #fff; cursor: nwse-resize; }
/* 枠の編集中：枠の中の見出し（見出しのない枠は赤で「見出しなし」）。選んだ枠は左下の札に出し、押すと直せる */
.fields > div > .flabel {
  position: absolute; left: 2px; right: 2px; top: 50%; transform: translateY(-50%); overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  font: normal 10px/1.2 "Yu Gothic UI", "Meiryo UI", sans-serif; color: #1e5ad8; pointer-events: none;
}
.fields > div > .flabel.none { color: #d32f2f; }
.fields > div.sel > .flabel { display: none; }
.fields > div > [data-fh="label"] {
  left: 0; top: 100%; margin: 10px 0 0; max-width: 260px; height: 20px; padding: 0 8px; border-radius: 10px; z-index: 1;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis; display: block; line-height: 20px; cursor: text;
}
#labelEditor {
  position: fixed; z-index: 210; display: flex; flex-direction: column; gap: 8px; width: 310px; padding: 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,0.45);
}
#labelEditor label { display: flex; align-items: center; gap: 8px; }
#labelEditor label > span { width: 4.5em; flex: none; color: var(--muted); font-size: 12px; }
#labelEditor input {
  flex: 1; min-width: 0; height: 30px; padding: 0 8px; font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 5px; outline: none; -webkit-user-select: text; user-select: text;
}
#labelEditor input:focus { border-color: var(--accent); }
#labelEditor .row { display: flex; align-items: center; gap: 6px; }
#labelEditor .row > span { flex: 1; }
#labelEditor p { margin: 0; font-size: 11px; color: var(--muted); }
/* 見出しを直しているあいだ：押せる言葉を囲む（足したときは少し濃く） */
.label-pick { position: absolute; z-index: 6; border-radius: 3px; background: rgba(255, 193, 7, 0.22); box-shadow: 0 0 0 1px rgba(255, 160, 0, 0.9); pointer-events: none; }
.label-pick.picked { background: rgba(255, 160, 0, 0.5); }
body.picking-label .page { cursor: pointer; }
/* 見出しを直しているあいだは、枠の中の見出しの字を薄くして、下の印刷された言葉を見やすくする */
body.picking-label .fields > div > .flabel { opacity: 0.25; }
/* 枠のつまみ（種類・×・大きさ・見出しの札）は枠の外にはみ出して、すぐ上・下の言葉を隠すので、見出しを直しているあいだは出さない */
body.picking-label .fields > div > [data-fh] { display: none; }
@media (pointer: coarse) {
  #labelEditor { left: 8px; right: 8px; top: 8px; width: auto; }
  #labelEditor input { height: 38px; font-size: 16px; }
  .fields > div > [data-fh="label"] { margin-top: 20px; height: 26px; line-height: 26px; font-size: 13px; border-radius: 13px; padding: 0 10px; }
}
@media (pointer: coarse) {
  .fields > div > [data-fh="del"] { width: 30px; height: 30px; margin: 0 0 -6px -6px; }
  .fields > div > [data-fh="size"] { width: 26px; height: 26px; margin: -8px 0 0 -8px; }
  .fields > div > [data-fh="type"] { height: 26px; padding: 0 10px; font-size: 13px; border-radius: 13px; }
}
@media (max-width: 760px) {
  /* ツールが10個になったので、スマホの下の帯に収まるよう少し詰める */
  #toolbar .group.tools button { min-width: 36px; padding: 0 6px; }
  /* 「枠を直す」「最初の枠に戻す」は、スマホでも文字を見せる */
  #toolbar .group.props button:is([data-prop="fieldEdit"], [data-prop="fieldReset"]) > span { display: inline; }
}

/* 検索：表示欄の右上（スマホでは上いっぱい）に出す欄と、ページの上の当たった所の色 */
#main { position: relative; }
#findBar {
  position: absolute; top: 8px; right: 22px; z-index: 30; display: none; align-items: center; gap: 2px;
  padding: 4px 4px 4px 6px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
body.finding.has-doc #findBar { display: flex; }
body.finding.has-doc #pages { padding-top: 64px; }
#findInput {
  width: 230px; height: 30px; padding: 0 8px; font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 5px; outline: none; -webkit-user-select: text; user-select: text;
}
#findInput:focus { border-color: var(--accent); }
#findBar.none #findInput { border-color: var(--danger); }
#findCount { min-width: 62px; padding: 0 4px; text-align: center; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.find-hl { position: absolute; inset: 0; pointer-events: none; }
.find-hl > div { position: absolute; background: rgba(255, 196, 0, 0.42); border-radius: 2px; }
.find-hl > div.cur { background: rgba(255, 110, 0, 0.5); box-shadow: 0 0 0 2px rgba(255, 110, 0, 0.95); }
@media (max-width: 760px) {
  #findBar { left: 6px; right: 6px; top: 6px; }
  body.finding.has-doc #pages { padding-top: 66px; }
  #findInput { flex: 1; min-width: 0; width: auto; height: 38px; font-size: 16px; } /* 16px 未満だと iPhone が勝手に拡大する */
  #findCount { min-width: 0; font-size: 13px; }
  #findBar button { height: 40px; }
}

/* PDF の文字を選んでいるときに出す「コピー」「すべて選択」 */
#copyBar {
  position: fixed; z-index: 60; display: none; border-radius: 999px; overflow: hidden;
  background: #1f2329; box-shadow: 0 4px 14px rgba(0,0,0,0.35);
}
#copyBar.show { display: flex; }
#copyBar button {
  padding: 10px 16px; border: 0; background: transparent; color: #fff; font-size: 15px; font-weight: 600; white-space: nowrap;
}
#copyBar button + button { border-left: 1px solid rgba(255,255,255,0.25); }
#copyBar button:active { background: rgba(255,255,255,0.15); }

/* ---- 印刷：印刷ボタンで作ったページの画像だけを、1ページずつ用紙に収めて印刷する ---- */
#printRoot { display: none; }
.print-note { display: none; }
/* パソコンの Chrome・Edge で印刷するPDF（ブラウザのPDF表示に読み込ませて、その印刷を使う） */
.print-frame { position: fixed; left: 0; top: 0; width: 10px; height: 10px; border: 0; opacity: 0; pointer-events: none; z-index: -1; }
@media print {
  @page { margin: 0; }
  /* 大きさは用紙に対する割合で決める（画面の大きさの単位 vw・vh は、Safari などで用紙の大きさにならない） */
  html, body { height: 100%; overflow: visible; background: #fff; }
  body { display: block; margin: 0; }
  body > * { display: none !important; }
  body.printing > #printRoot { display: block !important; height: 100%; }
  .print-page {
    width: 100%; height: 100%; box-sizing: border-box;
    display: flex; align-items: center; justify-content: center;
    overflow: hidden; break-after: page; break-inside: avoid;
  }
  .print-page:last-child { break-after: auto; }
  .print-page img { display: block; max-width: 100%; max-height: 100%; object-fit: contain; }
  /* ブラウザのメニューから直接印刷したとき（アプリの画面は印刷しない） */
  body:not(.printing) > .print-note { display: block !important; padding: 20mm; color: #000; font-size: 14pt; }
}

/* 調べもの用の表示（URL に ?debug を付けたときだけ）：PDF の文字の層を赤く見せ、状態を画面の上に出す */
body.debug-text .textLayer :is(span, br) { color: rgba(255, 0, 0, 0.6) !important; }
#debugPanel {
  position: fixed; left: 4px; right: 4px; bottom: calc(var(--bar-h, 0px) + var(--safe-b, 0px) + 6px); z-index: 200;
  padding: 6px 8px; border-radius: 6px; background: rgba(0, 0, 0, 0.82); color: #7dff7d; pointer-events: none;
}
#debugPanel pre {
  margin: 0; font: 11px/1.35 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-all;
  max-height: 36vh; overflow: hidden;
}
#debugPanel button {
  float: right; margin: 0 0 4px 8px; padding: 6px 10px; border: 0; border-radius: 6px; background: #2e7d32; color: #fff;
  font-size: 13px; font-weight: 600; pointer-events: auto;
}

/* 雛形の欄（ツールバーの「雛形」）と、はじめの画面の雛形の一覧 */
#tplPanel {
  position: fixed; z-index: 70; width: 320px; max-width: calc(100vw - 12px); max-height: 60vh; overflow: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 28px rgba(0,0,0,0.45); padding: 10px;
}
#tplPanel[hidden] { display: none; }
.tpl-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.tpl-head b { font-size: 14px; }
.tpl-head button { border: 1px solid var(--accent); color: #fff; background: var(--accent-weak); }
.tpl-item { display: flex; align-items: center; gap: 4px; }
.tpl-open { flex: 1; min-width: 0; justify-content: flex-start; height: 38px; }
.tpl-open span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tpl-del { color: var(--muted); font-size: 16px; }
.tpl-del:hover:not(:disabled) { color: var(--danger); }
.tpl-empty, .tpl-note { margin: 6px 2px; color: var(--muted); font-size: 12px; line-height: 1.6; }
.tpl-note { border-top: 1px solid var(--border); padding-top: 8px; }
.welcome-tpl { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 6px; margin: 14px 0 0; }
.welcome-tpl:empty { display: none; }
.welcome-tpl b { width: 100%; font-size: 12px; color: var(--muted); font-weight: 600; }
.welcome-tpl button { border: 1px solid var(--border); height: 34px; padding: 0 12px; }
@media (max-width: 760px) { #tplPanel { left: 6px !important; right: 6px; width: auto; } }
