@font-face {
  font-family: "PDFE Khmer";
  src: url(fonts/NotoSansKhmer-Regular.ttf) format("truetype");
  font-weight: 400;
}
@font-face {
  font-family: "PDFE Khmer";
  src: url(fonts/NotoSansKhmer-Bold.ttf) format("truetype");
  font-weight: 700;
}

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #d9dce1;
  --text: #1d2027;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --hover: #eef1f5;
  --canvas-bg: #e3e6ea;
  --sel: #2563eb;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17191d;
    --panel: #202328;
    --border: #353a42;
    --text: #e6e8eb;
    --muted: #9aa1ab;
    --accent: #4c8dff;
    --hover: #2a2e35;
    --canvas-bg: #121417;
    --sel: #4c8dff;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  color: inherit;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

input[type="number"], input[type="text"], select, textarea.field {
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 6px;
}
input[type="number"] { width: 56px; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; margin-right: 4px; }
.group { display: flex; align-items: center; gap: 4px; }
.props label { display: flex; align-items: center; gap: 4px; color: var(--muted); }
#zoomLabel { min-width: 44px; text-align: center; color: var(--muted); }
input[type="color"] { width: 32px; height: 30px; padding: 0 2px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel); }
#fontFamily { max-width: 190px; }
.style-btn { width: 30px; padding: 4px 0; }
.swatches { display: flex; gap: 3px; }
.swatches button { width: 20px; height: 20px; padding: 0; border-radius: 50%; background: var(--c); }
.swatches button:hover:not(:disabled) { background: var(--c); transform: scale(1.12); }

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);
  min-height: 0;
}
.side-actions { display: flex; flex-wrap: wrap; gap: 4px; padding: 8px; border-bottom: 1px solid var(--border); }
.side-actions button { padding: 3px 7px; font-size: 13px; }
.hint { font-size: 11.5px; color: var(--muted); padding: 6px 10px; }
#thumbs { flex: 1; overflow-y: auto; padding: 4px 14px 16px; display: flex; flex-direction: column; gap: 12px; }
.thumb {
  position: relative;
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 4px;
  cursor: pointer;
  text-align: center;
  user-select: none;
}
.thumb img { display: block; width: 100%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }
.thumb span { font-size: 12px; color: var(--muted); }
.thumb.current { border-color: var(--border); }
.thumb.sel { border-color: var(--sel); background: color-mix(in srgb, var(--sel) 12%, transparent); }
.thumb.dragging { opacity: 0.4; }
.thumb.drop-before { box-shadow: 0 -3px 0 var(--sel); }
.thumb.drop-after { box-shadow: 0 3px 0 var(--sel); }

#viewer {
  flex: 1;
  overflow: auto;
  background: var(--canvas-bg);
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 34px;
  position: relative;
}
#viewer.drag-over { outline: 3px dashed var(--accent); outline-offset: -10px; }
.empty { margin: auto; text-align: center; max-width: 440px; }
.empty h1 { font-size: 22px; margin-bottom: 8px; }
.muted { color: var(--muted); font-size: 13px; }

.page-wrap { position: relative; flex: none; background: #fff; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25); }
.page-wrap canvas { position: absolute; left: 0; top: 0; }
.page-num { position: absolute; bottom: -24px; width: 100%; text-align: center; font-size: 12px; color: var(--muted); }
.tool-text .ov, .tool-edittext .ov { cursor: text; }
.tool-draw .ov, .tool-highlight .ov, .tool-rect .ov { cursor: crosshair; }
.placing .ov { cursor: copy !important; }

.text-editor {
  position: absolute;
  z-index: 5;
  margin: 0;
  padding: 0;
  border: none;
  outline: 1px dashed var(--sel);
  background: transparent; /* never tint what is underneath while typing */
  resize: none;
  overflow: hidden;
  white-space: pre;
  min-width: 40px;
  font-family: Helvetica, Arial, sans-serif;
}

#formsPanel {
  width: 300px;
  flex: none;
  background: var(--panel);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 10px 14px;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.panel-head button { padding: 0 8px; }
#formsBody h3 { font-size: 13px; margin: 14px 0 6px; word-break: break-all; }
.field-row { display: flex; flex-direction: column; gap: 3px; margin-bottom: 10px; }
.field-row > span { font-size: 12px; color: var(--muted); word-break: break-all; }
.field-row.check { flex-direction: row; align-items: center; gap: 6px; }
.field-row input[type="text"], .field-row select, .field-row textarea { width: 100%; }

dialog {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  padding: 18px;
  max-width: calc(100vw - 32px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
#signPad { display: block; max-width: 100%; background: #fff; border: 1px solid var(--border); border-radius: 6px; touch-action: none; cursor: crosshair; }
.dlg-actions { display: flex; gap: 6px; margin-top: 14px; }
.spacer { flex: 1; }
.row { display: block; margin: 6px 0; }
#splitRanges { width: 100%; margin-top: 4px; }

#fontDlg { width: 640px; }
.font-head { display: flex; justify-content: space-between; align-items: center; }
.font-head h2 { margin: 0; }
.font-head button { padding: 0 9px; font-size: 18px; }
#fontSearch { width: 100%; margin: 10px 0; }
#fontList { height: min(60vh, 520px); overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; }
.font-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  padding: 8px 12px;
  text-align: left;
  background: transparent;
}
.font-row.current { background: color-mix(in srgb, var(--sel) 14%, transparent); }
.font-name { font-size: 12px; color: var(--muted); }
.font-sample { font-size: 22px; line-height: 1.6; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  max-width: calc(100vw - 32px);
}
#toast.show { opacity: 0.95; }

@media (max-width: 720px) {
  #sidebar { width: 120px; }
  #thumbs { padding: 4px 8px; }
  .hint { display: none; }
  #formsPanel { position: fixed; inset: auto 0 0 0; width: auto; max-height: 55vh; z-index: 10; border-top: 1px solid var(--border); }
  #viewer { padding: 16px; }
}
