@font-face { font-family: 'Sarabun'; src: url('../fonts/Sarabun-Regular.ttf') format('truetype'); font-weight: 400; font-display: block; }
@font-face { font-family: 'Sarabun'; src: url('../fonts/Sarabun-Bold.ttf') format('truetype'); font-weight: 700; font-display: block; }

:root {
  --bg: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f6f8fb;
  --text: #182033;
  --muted: #667089;
  --line: #dde2ea;
  --accent: #2557d6;
  --accent-soft: #e6edfd;
  --accent-text: #ffffff;
  --danger: #c93636;
  --ok: #18875a;
  --field: rgba(37, 87, 214, .10);
  --field-line: rgba(37, 87, 214, .75);
  --field-fill: rgba(255, 214, 10, .16);
  --guide: #e5367a;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .08);
  --radius: 10px;
  --topbar-h: 56px;
  --z: 1;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151c; --surface: #1b1f29; --surface-2: #222734; --text: #e7eaf1; --muted: #98a1b5;
    --line: #2f3545; --accent: #6b93ff; --accent-soft: #24304f; --accent-text: #0d1220;
    --field: rgba(107, 147, 255, .14); --field-line: rgba(107, 147, 255, .9);
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'Sarabun', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px; color: var(--text); background: var(--bg);
  overflow: hidden; -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
svg.i { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- top bar ---------- */
.topbar {
  height: var(--topbar-h); display: flex; align-items: center; gap: 8px; padding: 0 12px;
  background: var(--surface); border-bottom: 1px solid var(--line); position: relative; z-index: 20;
}
.brand { font-size: 17px; white-space: nowrap; color: var(--muted); }
.brand b { color: var(--accent); }
.doc-name {
  min-width: 80px; max-width: 280px; flex: 0 1 240px; border: 1px solid transparent; background: transparent;
  border-radius: 8px; padding: 6px 8px; font-weight: 700; text-overflow: ellipsis;
}
.doc-name:hover, .doc-name:focus { border-color: var(--line); background: var(--surface-2); outline: none; }
.save-state { font-size: 13px; color: var(--muted); white-space: nowrap; }
.spacer { flex: 1; }
.grp { display: flex; align-items: center; gap: 2px; }
.icon-btn {
  width: 38px; height: 38px; display: inline-grid; place-items: center; border: 0; border-radius: 9px;
  background: transparent; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:disabled { opacity: .35; cursor: default; background: transparent; }
.icon-btn.on { background: var(--accent-soft); color: var(--accent); }
.zoom-label { border: 0; background: transparent; min-width: 54px; font-variant-numeric: tabular-nums; border-radius: 8px; padding: 6px 4px; }
.zoom-label:hover { background: var(--surface-2); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 38px; padding: 0 14px;
  border-radius: 9px; border: 1px solid var(--line); background: var(--surface); white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 700; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.sm { height: 32px; padding: 0 10px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.block { width: 100%; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.seg button { display: inline-flex; align-items: center; gap: 6px; border: 0; background: transparent; padding: 5px 12px; border-radius: 7px; color: var(--muted); white-space: nowrap; }
.seg button.on { background: var(--surface); color: var(--accent); font-weight: 700; box-shadow: var(--shadow); }
.seg.small button { padding: 3px 8px; font-size: 14px; }

body.no-doc .doc-only { display: none !important; }
body:not(.no-doc) .home { display: none; }

/* ---------- workspace ---------- */
.workspace { position: relative; display: flex; height: calc(100% - var(--topbar-h)); }
.toolbar {
  width: 76px; flex: none; background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: stretch; gap: 2px; padding: 8px 6px; overflow-y: auto;
}
body.mode-fill .toolbar { display: none; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 2px; border: 0; background: transparent;
  border-radius: 10px; padding: 8px 2px 6px; font-size: 12px; color: var(--muted); line-height: 1.2;
}
.tool:hover { background: var(--surface-2); color: var(--text); }
.tool.on { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.tool svg.i { width: 22px; height: 22px; }
.tool-sep { height: 1px; background: var(--line); margin: 6px 4px; }

.viewer { flex: 1; overflow: auto; position: relative; overscroll-behavior: contain; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 22px; padding: 22px 22px 120px; width: max-content; min-width: 100%; }
.page {
  position: relative; background: #fff; box-shadow: var(--shadow);
  width: calc(var(--pw) * var(--z) * 1px); height: calc(var(--ph) * var(--z) * 1px);
}
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.page-num { position: absolute; top: 100%; left: 0; right: 0; text-align: center; font-size: 12px; color: var(--muted); padding-top: 3px; }
.overlay { position: absolute; inset: 0; touch-action: pan-x pan-y; }
body.mode-design:not(.tool-select) .overlay { cursor: crosshair; }
body.tool-table .overlay { touch-action: none; }
.lines-layer { position: absolute; inset: 0; pointer-events: none; }
.lines-layer i { position: absolute; background: rgba(229, 54, 122, .55); }

/* ---------- fields ---------- */
.fld {
  position: absolute;
  left: calc(var(--x) * var(--z) * 1px); top: calc(var(--y) * var(--z) * 1px);
  width: calc(var(--w) * var(--z) * 1px); height: calc(var(--h) * var(--z) * 1px);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.fld > svg.ft, .fld > img { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.fld > img { object-fit: contain; }
.fld text { font-family: 'Sarabun'; white-space: pre; }
body.mode-design .fld { background: var(--field); outline: 1px solid var(--field-line); outline-offset: -1px; cursor: move; }
body.mode-design .fld.locked { cursor: default; outline-style: dashed; }
body.mode-design .fld.sel { outline: 2px solid var(--accent); background: rgba(37, 87, 214, .18); z-index: 2; }
.fld-name {
  position: absolute; left: 0; bottom: 100%; transform: translateY(-1px); font-size: 11px; line-height: 1; padding: 2px 4px;
  background: var(--accent); color: #fff; border-radius: 4px 4px 4px 0; white-space: nowrap; pointer-events: none; display: none;
}
body.mode-design .fld:hover .fld-name, body.mode-design .fld.sel .fld-name { display: block; }
body.show-names.mode-design .fld .fld-name { display: block; opacity: .85; }
body.mode-fill .fld { cursor: text; touch-action: manipulation; border-radius: 2px; }
body.mode-fill .fld.t-checkbox, body.mode-fill .fld.t-signature { cursor: pointer; }
body.mode-fill .fld.empty { background: var(--field-fill); box-shadow: inset 0 -1.5px 0 rgba(200, 150, 0, .55); }
body.mode-fill .fld.req.empty { box-shadow: inset 0 -1.5px 0 rgba(201, 54, 54, .7); }
body.mode-fill .fld:hover { outline: 1.5px solid var(--field-line); }
body.mode-fill .fld.flash { animation: flash 1s ease-out; }
@keyframes flash { 0%, 40% { outline: 3px solid var(--accent); outline-offset: 2px; } 100% { outline: 0 solid transparent; } }
.fld .ph { fill: rgba(37, 87, 214, .55); }
.h {
  position: absolute; width: 12px; height: 12px; background: #fff; border: 2px solid var(--accent); border-radius: 3px;
  transform: translate(-50%, -50%); z-index: 3;
}
.h::after { content: ''; position: absolute; inset: -10px; }
.h[data-h="nw"] { left: 0; top: 0; cursor: nwse-resize; } .h[data-h="n"] { left: 50%; top: 0; cursor: ns-resize; }
.h[data-h="ne"] { left: 100%; top: 0; cursor: nesw-resize; } .h[data-h="e"] { left: 100%; top: 50%; cursor: ew-resize; }
.h[data-h="se"] { left: 100%; top: 100%; cursor: nwse-resize; } .h[data-h="s"] { left: 50%; top: 100%; cursor: ns-resize; }
.h[data-h="sw"] { left: 0; top: 100%; cursor: nesw-resize; } .h[data-h="w"] { left: 0; top: 50%; cursor: ew-resize; }

.ghost { position: absolute; border: 2px dashed var(--accent); background: rgba(37, 87, 214, .08); pointer-events: none; }
.ghost.cell { border: 1px solid var(--accent); border-style: solid; background: rgba(37, 87, 214, .12); }
.marquee { position: absolute; border: 1px solid var(--accent); background: rgba(37, 87, 214, .1); pointer-events: none; }
.guide { position: absolute; background: var(--guide); pointer-events: none; z-index: 5; }
.guide.v { width: 1px; top: 0; bottom: 0; } .guide.h { height: 1px; left: 0; right: 0; }

.fill-editor {
  position: absolute; z-index: 10; border: 2px solid var(--accent); border-radius: 3px; background: rgba(255,255,255,.96);
  font-family: 'Sarabun'; color: #000; padding: 0 2px; margin: 0; outline: none; resize: none; line-height: 1.3;
}
.fill-editor.date { padding: 0 4px; }

/* ---------- side panel ---------- */
.panel {
  width: 320px; flex: none; background: var(--surface); border-left: 1px solid var(--line); overflow-y: auto;
  display: flex; flex-direction: column;
}
.panel-body { padding: 14px 14px 40px; display: flex; flex-direction: column; gap: 14px; }
.sheet-grip { display: none; }
.fab { display: none; }
.panel h3 { margin: 0; font-size: 15px; }
.panel .sec { display: flex; flex-direction: column; gap: 8px; }
.panel .sec + .sec { border-top: 1px solid var(--line); padding-top: 14px; }
.row { display: flex; gap: 8px; align-items: center; }
.row > * { min-width: 0; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.f.inline { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
.inp {
  width: 100%; height: 36px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; background: var(--surface);
  color: var(--text); font-size: 15px;
}
textarea.inp { height: auto; min-height: 64px; padding: 6px 10px; resize: vertical; }
.inp:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.inp.num { font-variant-numeric: tabular-nums; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.grid4 label.f { font-size: 12px; }
.grid4 .inp { padding: 0 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.icon-row { display: flex; gap: 4px; flex-wrap: wrap; }
.icon-row .icon-btn { border: 1px solid var(--line); }
.tip { font-size: 13px; color: var(--muted); background: var(--surface-2); border-radius: 8px; padding: 10px 12px; line-height: 1.55; }
.tip b { color: var(--text); }
.kbd { font-family: ui-monospace, monospace; font-size: 12px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; }
.list { display: flex; flex-direction: column; gap: 2px; }
.list .it {
  display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 8px; border: 0; background: transparent;
  text-align: left; width: 100%;
}
.list .it:hover { background: var(--surface-2); }
.list .it.on { background: var(--accent-soft); }
.list .it small { color: var(--muted); margin-left: auto; white-space: nowrap; }
.fill-item { display: flex; flex-direction: column; gap: 4px; padding: 8px; border-radius: 9px; border: 1px solid transparent; }
.fill-item.on { border-color: var(--accent); background: var(--accent-soft); }
.fill-item .lbl { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.fill-item .lbl .req { color: var(--danger); }
.fill-item .sig-prev { height: 44px; border: 1px dashed var(--line); border-radius: 8px; display: grid; place-items: center; background: var(--surface-2); }
.fill-item .sig-prev img { max-height: 40px; max-width: 100%; }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--ok); border-radius: 3px; transition: width .2s; }
.switch { position: relative; width: 38px; height: 22px; flex: none; appearance: none; -webkit-appearance: none; background: var(--line); border-radius: 11px; cursor: pointer; transition: background .15s; margin: 0; }
.switch::after { content: ''; position: absolute; left: 3px; top: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(16px); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: -4px -14px 0; padding: 0 10px; }
.tabs button { border: 0; background: transparent; padding: 8px 10px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); font-weight: 700; }
.recnav { display: flex; align-items: center; gap: 6px; }
.recnav .pos { flex: 1; text-align: center; font-variant-numeric: tabular-nums; }
.maptable { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); gap: 6px; align-items: center; font-size: 14px; }
.maptable .nm { line-height: 1.7; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.maptable select { height: 32px; font-size: 14px; }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 10px; background: var(--surface-2); color: var(--muted); }

/* ---------- home ---------- */
.home { position: absolute; inset: 0; overflow-y: auto; }
.home-inner { max-width: 880px; margin: 0 auto; padding: 40px 20px 60px; }
.home h1 { font-size: 28px; margin: 0 0 6px; line-height: 1.35; }
.drop {
  margin-top: 22px; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 44px 20px;
  border: 2px dashed var(--line); border-radius: 16px; background: var(--surface); cursor: pointer; text-align: center;
  transition: border-color .15s, background .15s;
}
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop strong { font-size: 18px; }
.drop-icon svg.i { width: 40px; height: 40px; color: var(--accent); }
.recent { margin-top: 30px; }
.recent h2 { font-size: 17px; margin: 0 0 12px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; text-align: left; padding: 0; position: relative; }
.card:hover { border-color: var(--accent); }
.card .th { aspect-ratio: 3 / 3.4; background: var(--surface-2); display: grid; place-items: center; overflow: hidden; border-bottom: 1px solid var(--line); }
.card .th img { width: 100%; height: 100%; object-fit: cover; object-position: top; }
.card .meta { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 2px; }
.card .meta b { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .meta span { font-size: 12px; color: var(--muted); }
.card .del { position: absolute; top: 6px; right: 6px; width: 30px; height: 30px; background: var(--surface); border: 1px solid var(--line); opacity: 0; }
.card:hover .del, .card:focus-within .del { opacity: 1; }
@media (hover: none) { .card .del { opacity: 1; } }

/* ---------- dialogs, menus, toasts ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(10, 14, 24, .45); z-index: 100; display: grid; place-items: center; padding: 16px; animation: fade .12s; }
@keyframes fade { from { opacity: 0; } }
.dialog {
  background: var(--surface); border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.3); width: min(520px, 100%);
  max-height: calc(100% - 20px); display: flex; flex-direction: column; overflow: hidden;
}
.dialog.wide { width: min(720px, 100%); }
.dialog header { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
.dialog header h2 { margin: 0; font-size: 17px; flex: 1; }
.dialog .body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.dialog footer { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.opt { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; }
.opt:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.opt input { margin-top: 4px; accent-color: var(--accent); }
.opt b { display: block; }
.opt span { font-size: 13px; color: var(--muted); }
.menu {
  position: fixed; z-index: 90; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  padding: 6px; min-width: 260px; display: flex; flex-direction: column;
}
.menu button { display: flex; align-items: center; gap: 10px; border: 0; background: transparent; padding: 9px 10px; border-radius: 8px; text-align: left; }
.menu button:hover { background: var(--surface-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 2px; width: auto; }
.toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; width: max-content; max-width: calc(100% - 24px); }
.toast {
  background: #1d2433; color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25);
  display: flex; align-items: center; gap: 12px; pointer-events: auto; animation: up .18s ease-out; font-size: 14px;
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }
.toast button { background: rgba(255,255,255,.16); border: 0; color: #fff; border-radius: 6px; padding: 4px 10px; font-weight: 700; }
@keyframes up { from { transform: translateY(10px); opacity: 0; } }
.sigpad { width: 100%; aspect-ratio: 3 / 1.2; border: 1px dashed var(--line); border-radius: 10px; background: #fff; touch-action: none; display: block; }
.saved-sigs { display: flex; gap: 8px; flex-wrap: wrap; }
.saved-sigs button { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 4px; height: 54px; width: 120px; position: relative; }
.saved-sigs img { max-width: 100%; max-height: 100%; }
.saved-sigs .x { position: absolute; top: -8px; right: -8px; width: 22px; height: 22px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); display: grid; place-items: center; padding: 0; }
.saved-sigs .x svg.i { width: 12px; height: 12px; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.busy { position: fixed; inset: 0; z-index: 150; background: rgba(10,14,24,.35); display: grid; place-items: center; }
.busy > div { background: var(--surface); padding: 18px 22px; border-radius: 12px; display: flex; align-items: center; gap: 12px; box-shadow: var(--shadow); }

/* ---------- small screens ---------- */
@media (max-width: 860px) {
  .hide-sm { display: none !important; }
  .brand { display: none; }
  .seg button span, #btnExport span { display: none; }
  .seg button { padding: 6px 10px; }
  #btnExport { padding: 0 10px; }
  .doc-name { flex: 1 1 auto; max-width: none; }
  .save-state { display: none; }
  .workspace { flex-direction: column; }
  .toolbar {
    order: 3; width: 100%; height: 64px; flex-direction: row; border-right: 0; border-top: 1px solid var(--line);
    overflow-x: auto; overflow-y: hidden; padding: 4px 6px calc(4px + env(safe-area-inset-bottom)); height: auto;
  }
  .tool { min-width: 62px; }
  .tool-sep { width: 1px; height: auto; margin: 6px 2px; }
  .viewer { order: 1; }
  .pages { padding: 14px 10px 160px; gap: 14px; }
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; width: auto; max-height: 62vh; z-index: 40; border-left: 0;
    border-radius: 16px 16px 0 0; box-shadow: 0 -10px 30px rgba(0,0,0,.18); transform: translateY(105%);
    transition: transform .22s ease;
  }
  body.sheet-open .panel { transform: none; }
  .sheet-grip { display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; flex: none; }
  .fab {
    display: inline-flex; align-items: center; gap: 6px; position: fixed; right: 14px; bottom: 84px; z-index: 30;
    border: 0; border-radius: 24px; padding: 10px 16px; background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 700;
  }
  body.mode-fill .fab { bottom: 20px; }
  body.sheet-open .fab { display: none; }
  .home h1 { font-size: 22px; }
}
.fld.editing > svg { visibility: hidden; }

/* ---------- สร้างช่องอัตโนมัติ ---------- */
.mask-cv { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; opacity: .3; }
body:not(.tool-auto) .mask-cv, body.mode-fill .mask-cv { display: none; }
body.tool-auto.mode-design:not(.brush-pan) .overlay { touch-action: none; cursor: none; }
body.tool-auto.brush-pan .overlay { cursor: grab; }
body.tool-auto .fld { pointer-events: none; opacity: .55; }
.cand {
  position: absolute; z-index: 4; cursor: pointer; border-radius: 2px;
  left: calc(var(--x) * var(--z) * 1px); top: calc(var(--y) * var(--z) * 1px);
  width: calc(var(--w) * var(--z) * 1px); height: calc(var(--h) * var(--z) * 1px);
  border: 1.5px solid var(--c); background: color-mix(in srgb, var(--c) 20%, transparent);
}
.cand > span {
  position: absolute; left: -1.5px; bottom: 100%; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 10px; line-height: 1.25; padding: 1px 4px; border-radius: 3px 3px 3px 0; background: var(--c); color: #fff; pointer-events: none;
}
.cand:not(:hover) > span { opacity: .85; }
.cand.off { background: repeating-linear-gradient(45deg, transparent 0 4px, color-mix(in srgb, var(--c) 25%, transparent) 4px 6px); border-style: dashed; opacity: .5; }
.cand.off > span { text-decoration: line-through; }
.brushes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.brush { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 9px; padding: 7px 10px; background: var(--surface); text-align: left; }
.brush:hover { background: var(--surface-2); }
.brush.on { border-color: var(--accent); background: var(--accent-soft); font-weight: 700; }
i.dot { display: inline-block; width: 14px; height: 14px; border-radius: 50%; background: var(--c); flex: none; vertical-align: -2px; }
.cand.small > span { display: none; }
.cand:hover { z-index: 6; }
.cand:hover > span { display: block; max-width: none; opacity: 1; }
.brush-cursor {
  position: absolute; display: none; pointer-events: none; z-index: 7; border-radius: 50%; transform: translate(-50%, -50%);
  border: 2px solid var(--c); background: color-mix(in srgb, var(--c) 12%, transparent);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .9), inset 0 0 0 1px rgba(255, 255, 255, .6);
}
.brush-cursor::after { content: ''; position: absolute; left: 50%; top: 50%; width: 3px; height: 3px; margin: -1.5px 0 0 -1.5px; border-radius: 50%; background: var(--c); }
.brush-cursor.erase { border-style: dashed; background: transparent; }
.paint-rect { position: absolute; pointer-events: none; z-index: 6; border: 2px dashed var(--c); background: color-mix(in srgb, var(--c) 28%, transparent); }
.paint-rect.erase { background: repeating-linear-gradient(45deg, transparent 0 5px, color-mix(in srgb, var(--c) 30%, transparent) 5px 8px); }

/* ---------- พรีเซท & กลุ่ม ---------- */
.presets, .groups { display: flex; flex-direction: column; gap: 8px; }
.preset { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: var(--surface); }
.preset .pv { grid-column: 1 / -1; font-size: 17px; line-height: 1.5; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: #fff; color: #000; border-radius: 6px; padding: 2px 8px; }
.preset .pm { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.preset .pm span { font-size: 12.5px; color: var(--muted); }
.preset .pa { display: flex; align-items: center; gap: 2px; }
.group { display: flex; flex-direction: column; gap: 6px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--surface); background: var(--c); box-shadow: 0 0 0 1px var(--line); padding: 0; }
.swatch.on { box-shadow: 0 0 0 2px var(--accent); }
.swatch:disabled, .color-in:disabled { opacity: .4; cursor: default; }
.color-in { width: 34px; height: 28px; border: 1px solid var(--line); border-radius: 6px; padding: 1px; background: var(--surface); cursor: pointer; }
.preset-preview { background: #fff; color: #000; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; line-height: 1.5; min-height: 52px; }
.f > span .badge, label.f .badge { margin-left: 4px; vertical-align: 1px; }
.seg.small button:disabled { opacity: .45; cursor: default; }

/* ---------- สีช่องตามประเภท (หน้าออกแบบ) — ชุดเดียวกับสีแปรง ---------- */
.fld { --fc: 37 99 235; }
.fld.t-text { --fc: 37 99 235; }
.fld.t-multiline { --fc: 8 145 178; }
.fld.t-checkbox { --fc: 22 163 74; }
.fld.t-date { --fc: 234 88 12; }
.fld.t-signature { --fc: 147 51 234; }
body.mode-design .fld { background: rgb(var(--fc) / .12); outline: 1px solid rgb(var(--fc) / .85); }
body.mode-design .fld.sel { background: rgb(var(--fc) / .24); outline: 2px solid rgb(var(--fc)); }
body.mode-design .fld .fld-name { background: rgb(var(--fc)); }
body.mode-design .fld .h { border-color: rgb(var(--fc)); }
.tool[data-tool="text"] svg.i { color: rgb(37 99 235); }
.tool[data-tool="multiline"] svg.i { color: rgb(8 145 178); }
.tool[data-tool="checkbox"] svg.i { color: rgb(22 163 74); }
.tool[data-tool="date"] svg.i { color: rgb(234 88 12); }
.tool[data-tool="signature"] svg.i { color: rgb(147 51 234); }

/* ---------- สูตรคำนวณ ---------- */
.mono { font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace; }
textarea.inp.mono { font-size: 13.5px; min-height: 54px; }
.fx-status { font-size: 13px; color: var(--muted); padding: 2px 2px 0; }
.fx-status.ok { color: var(--ok); font-weight: 700; }
.fx-status.bad { color: var(--danger); }
.fld-fx {
  position: absolute; right: -1px; top: -1px; transform: translate(40%, -45%); z-index: 3; pointer-events: none;
  width: 15px; height: 15px; border-radius: 50%; display: grid; place-items: center;
  font: italic 700 10.5px/1 Georgia, serif; color: #fff; background: rgb(var(--fc, 37 99 235));
}
body.mode-fill .fld-fx { opacity: .55; transform: translate(40%, -45%) scale(.85); }
body.mode-fill .fld.fx { cursor: default; background: rgba(24, 135, 90, .07); }
.fld.fx-err { outline: 2px solid var(--danger) !important; }
.fld.fx-err .fld-fx { background: var(--danger); }
body.picking .fld { cursor: copy !important; }
body.picking .fld:hover { outline: 2px dashed var(--accent) !important; outline-offset: 1px; }
body.picking .overlay { cursor: default; }
.fx-readonly { display: flex; gap: 8px; align-items: center; padding: 6px 10px; border-radius: 8px; background: var(--surface-2); font-variant-numeric: tabular-nums; }
.fx-readonly b { font: italic 700 13px Georgia, serif; color: var(--ok); }

/* ---------- หน้าแรก: เมนูด้านข้าง ---------- */
.home { display: flex; overflow: hidden; }
.side-nav { width: 220px; flex: none; display: flex; flex-direction: column; gap: 4px; padding: 18px 12px; background: var(--surface); border-right: 1px solid var(--line); }
.side-nav button { display: flex; align-items: center; gap: 10px; border: 0; background: transparent; padding: 10px 12px; border-radius: 10px; text-align: left; color: var(--muted); font-size: 15px; }
.side-nav button:hover { background: var(--surface-2); color: var(--text); }
.side-nav button.on { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.side-nav .side-foot { margin-top: auto; font-size: 12px; padding: 0 12px; }
.home-main { flex: 1; overflow-y: auto; min-width: 0; }

/* ---------- จัดการหน้า PDF ---------- */
.pt { max-width: 1240px; margin: 0 auto; padding: 28px 22px 90px; display: flex; flex-direction: column; gap: 12px; }
.pt-head h1 { font-size: 24px; margin: 0 0 4px; }
.pt-head p { margin: 0; }
.pt-bar { position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 10px; margin: 0 -10px; background: var(--bg); border-bottom: 1px solid var(--line); }
.pt-bar .icon-btn { border: 1px solid var(--line); background: var(--surface); }
.pt-bar .btn:disabled, .pt-bar .icon-btn:disabled { opacity: .4; cursor: default; }
.pt-sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }
.pt-info { font-size: 13.5px; }
.pt-info [data-info] { font-weight: 700; }
.pt-empty { margin-top: 6px; }
.pt-pane.over .pt-grid, .pt-pane.over .pt-empty { outline: 2px dashed var(--accent); outline-offset: 6px; border-radius: 12px; }
.pt-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 16px; padding-top: 4px; min-height: 40px; }
.pt-card { position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 10px 8px; display: flex; flex-direction: column; gap: 6px; cursor: grab; user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.pt-card:hover { border-color: var(--accent); }
.pt-card.sel { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); background: var(--accent-soft); }
.pt-card.drop-before::before, .pt-card.drop-after::after { content: ''; position: absolute; top: 6px; bottom: 6px; width: 4px; border-radius: 2px; background: var(--accent); }
.pt-card.drop-before::before { left: -11px; }
.pt-card.drop-after::after { right: -11px; }
.pt-thumb { height: 190px; display: grid; place-items: center; }
.pt-paper { background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); max-width: 100%; max-height: 100%; overflow: hidden; display: grid; place-items: center; color: #999; font-size: 13px; }
.pt-paper canvas { width: 100%; height: 100%; display: block; }
.pt-paper.blank { background: #fff; border: 1px dashed #bbb; box-shadow: none; }
.pt-meta { display: flex; align-items: baseline; gap: 6px; font-size: 12.5px; min-width: 0; }
.pt-meta .pt-num { font-size: 14px; }
.pt-meta .pt-src { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pt-acts { display: flex; justify-content: space-between; opacity: 0; transition: opacity .12s; }
.pt-card:hover .pt-acts, .pt-card.sel .pt-acts { opacity: 1; }
.pt-acts .icon-btn { width: 30px; height: 30px; }
.pt-acts .icon-btn svg.i { width: 17px; height: 17px; }
@media (hover: none) { .pt-acts { opacity: 1; } }
.pt-ghost { position: fixed; z-index: 300; pointer-events: none; background: var(--accent); color: #fff; font-weight: 700; padding: 6px 12px; border-radius: 8px; box-shadow: var(--shadow); font-size: 13px; }
body.pt-dragging, body.pt-dragging * { cursor: grabbing !important; }

@media (max-width: 860px) {
  .home { flex-direction: column; }
  .side-nav { width: auto; flex-direction: row; padding: 8px 10px; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
  .side-nav button { flex: 1; justify-content: center; padding: 9px 10px; white-space: nowrap; }
  .side-nav .side-foot { display: none; }
  .pt { padding: 16px 12px 90px; }
  .pt-bar .btn span { display: none; }
  .pt-bar .btn { padding: 0 10px; }
  .pt-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; }
}

/* ซ่อนจริงเมื่อมี hidden (กันคลาสที่ตั้ง display ไว้ทับ) */
[hidden] { display: none !important; }

/* ---------- จัดการหน้า: ขนาดรูปย่อ + ดูหน้าเต็ม ---------- */
.pt-info { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.pt-zoom { display: flex; align-items: center; gap: 4px; }
.pt-zoom input[type=range] { width: 150px; accent-color: var(--accent); }
.pt-zoom .icon-btn { width: 32px; height: 32px; border: 1px solid var(--line); background: var(--surface); }
.pt-grid { grid-template-columns: repeat(auto-fill, minmax(min(100%, calc(var(--ptz, 190) * 0.84px)), 1fr)); }
.pt-thumb { height: calc(var(--ptz, 190) * 1px); }
.pt-acts { flex-wrap: wrap; gap: 2px; }
.pv-back { position: fixed; inset: 0; z-index: 160; background: rgba(8, 10, 16, .93); display: flex; flex-direction: column; animation: fade .12s; }
.pv-bar { display: flex; align-items: center; gap: 6px; padding: 8px 12px; background: #151a24; color: #e7eaf1; border-bottom: 1px solid #2a3140; }
.pv-bar .icon-btn { color: #e7eaf1; }
.pv-bar .icon-btn:hover { background: #262d3b; }
.pv-pos { min-width: 110px; text-align: center; font-variant-numeric: tabular-nums; }
.pv-zl { border: 0; background: transparent; color: #e7eaf1; min-width: 58px; padding: 6px; border-radius: 8px; font-variant-numeric: tabular-nums; }
.pv-zl:hover { background: #262d3b; }
.pv-stage { flex: 1; overflow: auto; display: flex; padding: 16px; }
.pv-page { margin: auto; flex: none; background: #fff; box-shadow: 0 6px 30px rgba(0,0,0,.5); }
.pv-page canvas { width: 100%; height: 100%; display: block; }
.pv-blank { width: 100%; height: 100%; display: grid; place-items: center; color: #999; }
@media (max-width: 860px) {
  .pt-thumb { height: calc(var(--ptz, 190) * 0.8px); }
  .pt-zoom input[type=range] { width: 100px; }
}
