:root {
  --bg: #e9edf3;
  --panel: #ffffff;
  --text: #17202e;
  --muted: #5b6577;
  --line: #d4dae4;
  --accent: #1f3a8a;
  --accent-text: #ffffff;
  --chip: #eef2fb;
  --danger: #b42318;
  --ink-hint: rgba(31, 58, 138, 0.12);
  --shadow: 0 6px 24px rgba(10, 20, 40, 0.18);
  --toolbar-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161d; --panel: #1c222c; --text: #e7ebf2; --muted: #9aa4b5; --line: #2e3644;
    --accent: #5b7fe0; --chip: #252d3b; --danger: #f97066; --shadow: 0 6px 24px rgba(0,0,0,.5);
  }
}
:root[data-theme="dark"] {
  --bg: #12161d; --panel: #1c222c; --text: #e7ebf2; --muted: #9aa4b5; --line: #2e3644;
  --accent: #5b7fe0; --chip: #252d3b; --danger: #f97066; --shadow: 0 6px 24px rgba(0,0,0,.5);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, "Sarabun", "Noto Sans Thai", "Leelawadee UI", sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.top {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.fname { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 6px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; white-space: nowrap;
}
.btn.icon { width: 40px; padding: 0; font-size: 20px; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.big { min-height: 48px; font-size: 17px; }
.btn:active { transform: scale(.97); }

/* ---------- viewer ---------- */
#viewer {
  flex: 1; overflow: auto; position: relative;
  touch-action: pan-x pan-y;
  -webkit-user-select: text; user-select: text;
}
.pages { padding: 8px; display: flex; flex-direction: column; align-items: center; gap: 12px; width: max-content; min-width: 100%; transform-origin: 0 0; }
.page { position: relative; background: #fff; box-shadow: 0 1px 6px rgba(0,0,0,.25); flex: none; }
.page.blank { outline: 2px dashed #9db0e0; }
.page canvas.pdf { position: absolute; inset: 0; }
.page-label { position: absolute; top: -2px; left: 4px; transform: translateY(-100%); font-size: 11px; color: var(--muted); user-select: none; }
.page canvas.ink { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.page .ovl { position: absolute; inset: 0; z-index: 4; pointer-events: none; }

.textLayer { position: absolute; inset: 0; overflow: clip; opacity: 1; line-height: 1; text-size-adjust: none; -webkit-text-size-adjust: none; forced-color-adjust: none; transform-origin: 0 0; z-index: 2; }
.textLayer :is(span, br) { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(0, 90, 255, .3); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: 0; cursor: default; user-select: none; }
.textLayer.selecting .endOfContent { top: 0; }

/* ชิ้นงานลากวาง */
.item { position: absolute; pointer-events: auto; touch-action: none; user-select: none; -webkit-user-select: none; }
.item canvas { display: block; }
.item::after { content: ""; position: absolute; inset: -3px; border: 1.5px dashed rgba(31,58,138,.55); border-radius: 4px; pointer-events: none; }
.item.dragging::after { border-style: solid; background: var(--ink-hint); }
.item .grip { position: absolute; right: -14px; bottom: -14px; width: 28px; height: 28px; border-radius: 50%; background: var(--accent); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.item .grip-e { right: -12px; top: 50%; bottom: auto; width: 22px; height: 44px; margin-top: -22px; border-radius: 11px; }
.item .grip-s { bottom: -12px; left: 50%; right: auto; width: 44px; height: 22px; margin-left: -22px; border-radius: 11px; }
#viewer:not([data-mode="move"]) .item .grip-e, #viewer:not([data-mode="move"]) .item .grip-s { display: none; }
.item .del { position: absolute; right: -14px; top: -14px; width: 28px; height: 28px; border-radius: 50%; background: #fff; color: #b42318; border: 1px solid #ccc; font-size: 14px; line-height: 1; padding: 0; box-shadow: 0 1px 4px rgba(0,0,0,.3); }

/* โหมดปากกา/ยางลบ/OCR: เลเยอร์ลายมือรับ input, ชิ้นงานไม่ดักการเขียน */
#viewer:not([data-mode="move"]) { -webkit-user-select: none; user-select: none; }
#viewer:not([data-mode="move"]) .page canvas.ink { pointer-events: auto; touch-action: none; }
#viewer:not([data-mode="move"]) .item { pointer-events: none; }
#viewer:not([data-mode="move"]) .item::after, #viewer:not([data-mode="move"]) .item .grip, #viewer:not([data-mode="move"]) .item .grip-e { right: -12px; top: 50%; bottom: auto; width: 22px; height: 44px; margin-top: -22px; border-radius: 11px; }
.item .grip-s { bottom: -12px; left: 50%; right: auto; width: 44px; height: 22px; margin-left: -22px; border-radius: 11px; }
#viewer:not([data-mode="move"]) .item .grip-e, #viewer:not([data-mode="move"]) .item .grip-s { display: none; }
.item .del { display: none; }
#viewer[data-mode="pen"] .page canvas.ink { cursor: crosshair; }
#viewer[data-mode="ocr"] .page canvas.ink { cursor: cell; }
.ocr-box { position: absolute; border: 2px solid #e8590c; background: rgba(232, 89, 12, .12); }

/* empty state */
.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; }
.empty-card { background: var(--panel); border-radius: 16px; padding: 20px; max-width: 460px; box-shadow: var(--shadow); }
.empty-card h1 { margin: 0 0 8px; font-size: 22px; color: var(--accent); }
.empty-card ol { padding-left: 20px; margin: 8px 0; }
.empty-card li { margin: 4px 0; }
.muted { color: var(--muted); font-size: 14px; }
.resume { margin-top: 12px; padding: 10px; border-radius: 10px; background: var(--chip); display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.resume > div { flex: 1 1 100%; }

/* ---------- toolbar ---------- */
.tools {
  display: flex; overflow-x: auto; gap: 2px;
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 4px 4px; padding-bottom: max(4px, env(safe-area-inset-bottom));
  scrollbar-width: none;
}
.tools::-webkit-scrollbar { display: none; }
.tool {
  flex: 1 0 auto; min-width: 62px; display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 6px 4px; border: 0; border-radius: 10px; background: transparent; font-size: 12px; cursor: pointer;
}
.tool span { font-size: 22px; line-height: 1.1; }
.tool[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }

/* ---------- selection bar ---------- */
.selbar {
  position: fixed; left: 8px; right: 8px; bottom: calc(var(--toolbar-h) + 8px + env(safe-area-inset-bottom));
  background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); padding: 8px; z-index: 30;
}
.selbar-text { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 6px; }
.selbar-btns { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.selbar-btns span { font-size: 13px; color: var(--muted); }
.selbar-btns button { min-height: 38px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--accent); background: var(--chip); color: var(--text); }

/* ---------- sheets ---------- */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  max-height: 58vh; display: flex; flex-direction: column;
  background: var(--panel); border-radius: 16px 16px 0 0; box-shadow: var(--shadow);
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.sheet-body { overflow-y: auto; padding: 8px 14px 16px; overscroll-behavior: contain; }
.sheet h3 { margin: 18px 0 4px; font-size: 16px; color: var(--accent); }
.lbl { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
input:not([type]), input[type="number"], select, textarea {
  width: 100%; min-height: 42px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg);
}
textarea { resize: vertical; }
input[type="color"] { width: 100%; height: 42px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 2px; }
input[type="range"] { width: 100%; }
.check { display: flex; align-items: center; gap: 8px; margin: 12px 0 4px; }
.check input { width: 22px; height: 22px; }
.row { display: flex; gap: 8px; margin-top: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row select { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { min-height: 38px; padding: 4px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--chip); cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.chips.small .chip { min-height: 32px; font-size: 14px; }
.hint { font-size: 13px; background: var(--chip); padding: 8px 10px; border-radius: 10px; color: var(--muted); }
hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0 6px; }
.sig-preview { min-height: 70px; border: 1px dashed var(--line); border-radius: 10px; display: grid; place-items: center; background: #fff; }
.sig-preview img { max-height: 90px; max-width: 100%; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: grid; place-items: center; padding: 12px; }
.modal-card { background: var(--panel); border-radius: 16px; padding: 14px; width: min(560px, 100%); display: flex; flex-direction: column; gap: 8px; }
#sigPad { width: 100%; aspect-ratio: 3 / 1; background: #fff; border: 1px solid var(--line); border-radius: 10px; touch-action: none; }

.toast {
  position: fixed; left: 50%; top: calc(64px + env(safe-area-inset-top)); transform: translateX(-50%);
  background: #17202e; color: #fff; padding: 8px 14px; border-radius: 999px; z-index: 50; font-size: 14px; max-width: 90vw; text-align: center;
}
[hidden] { display: none !important; }
.row.tight { margin-top: 0; }
.row.tight input { flex: 1; }
.page-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.page-row { display: flex; align-items: center; gap: 10px; padding: 8px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); }
.page-row canvas { flex: none; border: 1px solid var(--line); border-radius: 4px; cursor: pointer; }
.page-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.page-info .muted { font-size: 13px; }
.page-btns { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; max-width: 96px; }
.page-btns .btn:disabled { opacity: .35; }
.size-row { display: flex; align-items: center; gap: 6px; margin-top: 8px; }
.size-row input[type="range"] { flex: 1; }
.size-row .btn.icon { width: 36px; min-height: 36px; font-size: 18px; }
.urg-size-val { min-width: 44px; text-align: right; }
.blank-box { margin-top: 12px; padding: 10px 12px; border-radius: 12px; border: 1px dashed var(--accent); background: var(--chip); }
.blank-box .row { margin-top: 8px; }
