:root{
  --bg:#f1f3f5; --bar:#1f2937; --bar2:#374151; --accent:#2563eb; --accent2:#1d4ed8;
  --danger:#dc2626; --text:#e5e7eb; --line:#4b5563; --page-shadow:0 2px 12px rgba(0,0,0,.18);
}
*{box-sizing:border-box}
html,body{height:100%}
body{
  margin:0; background:var(--bg); color:#111827;
  font-family:'Sarabun','Noto Sans Thai',system-ui,Segoe UI,Tahoma,sans-serif;
  -webkit-font-smoothing:antialiased;
}

/* ---------- Toolbar ---------- */
#toolbar{
  position:sticky; top:0; z-index:50; display:flex; align-items:center; gap:8px;
  padding:8px 12px; background:var(--bar); color:var(--text);
  box-shadow:0 1px 6px rgba(0,0,0,.25); flex-wrap:wrap;
}
#toolbar .group{display:flex; align-items:center; gap:6px}
.brand{font-weight:700; font-size:15px; margin-right:4px; white-space:nowrap}
.sep{width:1px; height:26px; background:var(--line); margin:0 2px}
.spacer{flex:1}
#zoomLabel{min-width:48px; text-align:center; font-size:13px}

.btn{
  background:var(--bar2); color:var(--text); border:1px solid #00000022; border-radius:8px;
  padding:7px 11px; font-size:13px; cursor:pointer; white-space:nowrap; line-height:1;
  font-family:inherit; transition:background .12s, transform .04s;
}
.btn:hover{background:#4b5563}
.btn:active{transform:translateY(1px)}
.btn.primary{background:var(--accent)}
.btn.primary:hover{background:var(--accent2)}
.btn.danger:hover{background:var(--danger)}
.btn.icon{min-width:34px; text-align:center; padding:7px 9px; font-size:15px}
.btn.active{outline:2px solid #93c5fd; background:var(--accent)}
.btn:disabled{opacity:.4; cursor:default}

select{
  background:var(--bar2); color:var(--text); border:1px solid #00000022; border-radius:8px;
  padding:7px 8px; font-size:13px; font-family:inherit; cursor:pointer;
}
#fontSelect{max-width:190px}
input[type=color]{
  width:32px; height:32px; padding:0; border:1px solid #00000033; border-radius:8px;
  background:none; cursor:pointer;
}

/* color swatches */
.swatches{display:flex; gap:4px}
.swatch{
  width:22px; height:22px; border-radius:6px; cursor:pointer; border:2px solid #ffffff33;
}
.swatch.active{border-color:#fff; box-shadow:0 0 0 2px var(--accent)}

/* ---------- Viewer ---------- */
#viewer{position:relative; height:calc(100vh - 56px); overflow:auto; padding:24px}
#pages{display:flex; flex-direction:column; align-items:center; gap:24px}

.page-wrap{position:relative; background:#fff; box-shadow:var(--page-shadow)}
.page-wrap canvas{display:block}
.overlay{position:absolute; inset:0; z-index:2}
.overlay.adding{cursor:crosshair}

/* กล่องข้อความที่วางบนเอกสาร */
.field{
  position:absolute; z-index:3; border:1px dashed transparent; border-radius:3px;
  background:rgba(37,99,235,.04);
}
.field:hover{border-color:#93c5fd}
.field.selected{border:1px solid var(--accent); background:rgba(37,99,235,.06)}
.field textarea{
  width:100%; height:100%; margin:0; border:0; outline:0; resize:none; overflow:hidden;
  background:transparent; padding:1px 2px; line-height:1.35; white-space:pre-wrap;
  word-break:break-word; color:inherit; font-family:inherit;
}
.field .handle{
  position:absolute; width:12px; height:12px; right:-7px; bottom:-7px; background:var(--accent);
  border:2px solid #fff; border-radius:3px; cursor:nwse-resize; display:none;
}
.field .move{
  position:absolute; left:-9px; top:-9px; width:18px; height:18px; background:var(--accent);
  border:2px solid #fff; border-radius:50%; cursor:move; display:none;
  color:#fff; font-size:11px; line-height:14px; text-align:center;
}
.field .del{
  position:absolute; right:-9px; top:-9px; width:18px; height:18px; background:var(--danger);
  border:2px solid #fff; border-radius:50%; cursor:pointer; display:none;
  color:#fff; font-size:12px; line-height:14px; text-align:center; font-weight:700;
}
.field.selected .handle,.field.selected .move,.field.selected .del{display:block}

/* กล่องรูปภาพ */
.field.image{background:rgba(22,163,74,.06)}
.field.image img{width:100%; height:100%; display:block; object-fit:fill; pointer-events:none; user-select:none}
.field.image:hover{border-color:#86efac}
.field.image.selected{border-color:#16a34a; background:rgba(22,163,74,.08)}
.field.image .move,.field.image .handle{background:#16a34a}

/* ---------- Dropzone ---------- */
.dropzone{
  position:absolute; inset:24px; border:3px dashed #adb5bd; border-radius:16px;
  display:flex; align-items:center; justify-content:center; text-align:center; color:#495057;
  background:#fff; transition:border-color .15s, background .15s;
}
.dropzone.hidden{display:none}
.dropzone.dragover{border-color:var(--accent); background:#eef4ff}
.dz-inner{max-width:560px; padding:24px}
.dz-icon{font-size:46px; margin-bottom:8px}
.dz-inner h2{margin:6px 0; font-size:20px}
.dz-inner p{margin:6px 0; color:#6b7280; font-size:14px}
.dz-inner .hint{font-size:13px; color:#9aa1a9}

/* ---------- Status toast ---------- */
.status{
  position:fixed; bottom:18px; left:50%; transform:translateX(-50%); z-index:100;
  background:#111827; color:#fff; padding:10px 16px; border-radius:10px; font-size:14px;
  box-shadow:0 4px 16px rgba(0,0,0,.3);
}
.status.err{background:var(--danger)}
