:root {
  --bg0: #f3f6f4;
  --bg1: #e7efe9;
  --ink: #15231c;
  --muted: #5a6b62;
  --card: #ffffff;
  --line: #d5e0d9;
  --accent: #0f6b4c;
  --accent-2: #1a8f66;
  --warn: #e8a22f;
  --danger: #c0392b;
  --ok: #1e7a45;
  --radius: 18px;
  --shadow: 0 12px 40px rgba(21, 35, 28, 0.08);
  --font: "IBM Plex Sans Thai", "Sarabun", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 500px at 10% -10%, #d9eee3 0%, transparent 55%),
    radial-gradient(900px 420px at 100% 0%, #e8f2ec 0%, transparent 50%),
    linear-gradient(180deg, var(--bg0), var(--bg1));
  min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  max-width: 1100px;
  margin: 0 auto;
}
.brand {
  display: flex;
  align-items: center;
  gap: .75rem;
  color: inherit;
  text-decoration: none;
}
.brand-mark {
  width: 44px; height: 44px;
  border-radius: 12px;
  display: grid; place-items: center;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #fff; font-weight: 700; font-size: .85rem;
  letter-spacing: .02em;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-size: 1.05rem; }
.brand-text small { color: var(--muted); font-size: .78rem; }

.nav { display: flex; flex-wrap: wrap; gap: .85rem; align-items: center; font-weight: 500; }
.nav a { color: var(--ink); text-decoration: none; }
.nav a:hover { color: var(--accent); }
.nav-user { color: var(--muted); font-size: .9rem; }
.inline-form { display: inline; }
.link-btn {
  background: none; border: 0; color: var(--accent); font: inherit; cursor: pointer; padding: 0;
}

.main { max-width: 1100px; margin: 0 auto; padding: 0 1.25rem 3rem; }

.hero {
  padding: 1.5rem 0 2rem;
}
.hero h1 {
  font-size: clamp(1.7rem, 4vw, 2.6rem);
  line-height: 1.25;
  margin: 0 0 .75rem;
  font-weight: 700;
}
.hero h1 span { color: var(--accent); }
.lead { color: var(--muted); max-width: 46rem; font-size: 1.05rem; }
.stats {
  margin-top: 1.25rem;
  display: flex; gap: 1.5rem; flex-wrap: wrap;
  background: rgba(255,255,255,.55);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: .9rem 1.1rem;
  width: fit-content;
}
.stats-label { display: block; color: var(--muted); font-size: .8rem; }
.stats strong { font-size: 1.35rem; }

.tools-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 1rem;
}
.tool-card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.15rem 1.1rem 1.2rem;
  box-shadow: var(--shadow);
  color: inherit;
  text-decoration: none;
  transition: transform .18s ease, border-color .18s ease, box-shadow .18s ease;
  min-height: 170px;
}
.tool-card:hover {
  transform: translateY(-3px);
  border-color: #b7d0c3;
  box-shadow: 0 16px 44px rgba(21, 35, 28, 0.12);
  text-decoration: none;
}
.tool-card h2 { margin: .55rem 0 .35rem; font-size: 1.05rem; }
.tool-card p { margin: 0; color: var(--muted); font-size: .9rem; line-height: 1.45; }
.server-dot {
  position: absolute; top: 12px; right: 12px;
  width: 9px; height: 9px; border-radius: 50%;
  background: #e8a22f; box-shadow: 0 0 0 3px rgba(232,162,47,.2);
}

.tool-icon {
  width: 42px; height: 42px; border-radius: 12px;
  display: inline-block;
  background: linear-gradient(145deg, #e8f5ef, #d5ebe0);
  position: relative;
}
.tool-icon::after {
  content: "";
  position: absolute; inset: 10px;
  border: 2px solid var(--accent);
  border-radius: 4px;
  opacity: .85;
}

.tool-page { padding-top: 1rem; max-width: 720px; }
.tool-page-wide { max-width: 1100px; }
.tool-page h1 { margin-bottom: .35rem; }
.back { display: inline-block; margin-bottom: .75rem; color: var(--muted); }

.tool-form {
  display: grid; gap: .9rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}
.tool-form-split {
  grid-template-columns: minmax(280px, 1fr) minmax(280px, 1.1fr);
  align-items: start;
}
.tool-form-main { display: grid; gap: .9rem; }
.tool-form label { display: grid; gap: .35rem; font-weight: 500; font-size: .95rem; }
.tool-form input[type=text],
.tool-form input[type=number],
.tool-form input[type=email],
.tool-form input[type=password],
.tool-form select {
  font: inherit; padding: .65rem .75rem;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.dropzone {
  border: 2px dashed #b9d0c4;
  border-radius: 14px;
  padding: 1.6rem 1rem;
  text-align: center;
  background: #f7fbf8;
  cursor: pointer;
  position: relative;
}
.dropzone input[type=file] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer;
}
.dz-title { display: block; font-weight: 600; }
.dz-sub { display: block; color: var(--muted); font-size: .88rem; margin-top: .35rem; }
.dropzone.is-drag { border-color: var(--accent); background: #eef8f2; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: .4rem; border: 0; border-radius: 999px;
  padding: .75rem 1.2rem; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-2); text-decoration: none; color: #fff; }
.btn-ghost { background: #eef3f0; color: var(--ink); }
.btn-sm { padding: .35rem .7rem; font-size: .9rem; border-radius: 10px; }
.hint, .muted { color: var(--muted); font-size: .9rem; }
.grid-4 { display: grid; grid-template-columns: repeat(2, 1fr); gap: .75rem; }

/* PDF live preview */
.pdf-preview-panel {
  position: sticky; top: 1rem;
  background: #0f1a15;
  border-radius: 14px;
  padding: .85rem;
  color: #e8f2ec;
  min-height: 280px;
}
.pdf-preview-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  gap: .75rem; margin-bottom: .65rem; flex-wrap: wrap;
}
.pdf-preview-nav { display: flex; align-items: center; gap: .45rem; font-size: .9rem; }
.pdf-preview-stage {
  position: relative;
  display: grid; place-items: center;
  background: #1a2a22;
  border-radius: 10px;
  overflow: auto;
  min-height: 240px;
  max-height: 70vh;
  padding: .5rem;
}
.pdf-preview-frame {
  position: relative;
  display: inline-block;
  line-height: 0;
  max-width: 100%;
  box-shadow: 0 8px 24px rgba(0,0,0,.35);
}
#pdf-canvas { display: block; max-width: 100%; height: auto; }
.pdf-overlay {
  position: absolute;
  left: 0; top: 0;
  width: 100%;
  height: 100%;
  pointer-events: auto;
  z-index: 2;
}
.pdf-crop-box, .pdf-place-box {
  position: absolute;
  box-sizing: border-box;
  border: 2px solid #3dd68c;
  background: rgba(61, 214, 140, 0.18);
  cursor: move;
  touch-action: none;
  z-index: 3;
}
.pdf-place-box {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: .85rem;
  font-weight: 700;
  letter-spacing: .02em;
  background: rgba(15, 107, 76, 0.45);
  border: 2px dashed #7dffb2;
  box-shadow: 0 0 0 1px rgba(0,0,0,.25);
  overflow: hidden;
  min-width: 48px;
  min-height: 28px;
}
.pdf-place-box .place-label {
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(0,0,0,.5);
}
.pdf-place-box.has-img {
  border-style: solid;
  background: rgba(255,255,255,.85);
}
.pdf-place-box.has-img .place-label { display: none; }
.pdf-place-box img {
  position: absolute; inset: 0;
  width: 100%; height: 100%; object-fit: contain;
  pointer-events: none;
}
.pdf-handle {
  position: absolute; width: 12px; height: 12px;
  background: #fff; border: 2px solid #0f6b4c; border-radius: 2px;
}
.pdf-handle-nw { left: -7px; top: -7px; cursor: nwse-resize; }
.pdf-handle-ne { right: -7px; top: -7px; cursor: nesw-resize; }
.pdf-handle-sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.pdf-handle-se { right: -7px; bottom: -7px; cursor: nwse-resize; }
.pdf-handle-n { left: 50%; top: -7px; transform: translateX(-50%); cursor: ns-resize; }
.pdf-handle-s { left: 50%; bottom: -7px; transform: translateX(-50%); cursor: ns-resize; }
.pdf-handle-e { right: -7px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.pdf-handle-w { left: -7px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.pdf-guide-marker {
  position: absolute;
  background: rgba(232, 162, 47, 0.9);
  color: #15231c;
  border-radius: 6px;
  font-size: .85rem;
  font-weight: 700;
  pointer-events: none;
}
#pdf-preview-hint { margin: .65rem 0 0; color: #9bb5a8; }

@media (max-width: 900px) {
  .tool-form-split { grid-template-columns: 1fr; }
  .pdf-preview-panel { position: static; }
}

.alert {
  max-width: 1100px; margin: 0 auto .75rem; padding: .75rem 1rem;
  border-radius: 12px; width: calc(100% - 2.5rem);
}
.alert-ok { background: #e6f6ec; color: var(--ok); }
.alert-err { background: #fdecea; color: var(--danger); }

.result-card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1.25rem; box-shadow: var(--shadow);
  display: grid; gap: .75rem;
}
.file-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .65rem; }
.file-list li {
  display: flex; justify-content: space-between; gap: 1rem; align-items: center;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: .85rem 1rem;
}
.file-list .muted { display: block; }

.sort-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .4rem; }
.sort-list li {
  background: #f4f8f5; border: 1px solid var(--line); border-radius: 10px;
  padding: .55rem .75rem; cursor: grab; display: flex; justify-content: space-between;
}

#sig-pad {
  width: 100%; max-width: 480px; height: 160px;
  border: 1px solid var(--line); border-radius: 12px; background: #fff; touch-action: none;
}

.site-footer {
  max-width: 1100px; margin: 0 auto; padding: 0 1.25rem 2.5rem;
  color: var(--muted); font-size: .88rem;
}

@media (max-width: 640px) {
  .site-header { flex-direction: column; align-items: flex-start; }
  .file-list li { flex-direction: column; align-items: flex-start; }
  .grid-4 { grid-template-columns: 1fr; }
}

/* —— Scan / CamScanner-like UI —— */
.scan-page { max-width: 980px; }
.scan-status { margin: 0 0 1rem; font-size: .88rem; }
.scan-toolbar, .scan-options {
  display: flex; flex-wrap: wrap; gap: .75rem 1rem; align-items: center;
}
.scan-inline-check {
  display: inline-flex !important; flex-direction: row !important;
  align-items: center; gap: .45rem; font-weight: 500; grid-template-columns: none !important;
}
.scan-inline-check input { width: auto; }
.scan-layout {
  display: grid;
  grid-template-columns: 1fr 200px;
  gap: 1rem;
  align-items: start;
}
.scan-editor {
  position: relative;
  background: #0d1a14;
  border-radius: 14px;
  min-height: 280px;
  display: grid;
  place-items: center;
  overflow: hidden;
}
#scan-canvas { display: block; max-width: 100%; touch-action: none; cursor: crosshair; }
.scan-placeholder {
  position: absolute; inset: 0; display: grid; place-items: center;
  margin: 0; color: #9bb5a8; padding: 1rem; text-align: center; pointer-events: none;
}
.scan-placeholder[hidden] { display: none !important; }
.scan-editor-actions {
  display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .75rem; align-items: center;
}
.page-mode-label {
  display: inline-grid !important; gap: .25rem; font-size: .85rem; min-width: 140px;
}
.page-thumbs {
  list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem;
  max-height: 520px; overflow: auto;
}
.page-thumbs li {
  border: 2px solid var(--line); border-radius: 10px; overflow: hidden;
  cursor: pointer; background: #fff;
}
.page-thumbs li.is-active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(15,107,76,.2); }
.page-thumbs img { display: block; width: 100%; height: 88px; object-fit: cover; }
.page-thumbs span { display: block; font-size: .78rem; padding: .25rem .4rem; color: var(--muted); }
.scan-pages h2 { font-size: 1rem; margin: 0 0 .5rem; }
.danger-text { color: var(--danger) !important; }

.camera-modal {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(8, 16, 12, .82);
  display: grid; place-items: center; padding: 1rem;
}
.camera-modal[hidden] { display: none !important; }
.camera-sheet {
  width: min(560px, 100%);
  background: #111;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 24px 60px rgba(0,0,0,.4);
}
#camera-video {
  display: block; width: 100%; max-height: 70vh; object-fit: cover; background: #000;
}
.camera-bar {
  display: flex; gap: .5rem; justify-content: space-between; padding: .85rem;
  background: #1a1a1a;
}
.camera-hint { padding: 0 .85rem .85rem; margin: 0; color: #aaa; }

@media (max-width: 760px) {
  .scan-layout { grid-template-columns: 1fr; }
  .page-thumbs {
    display: flex; max-height: none; overflow-x: auto; gap: .5rem;
  }
  .page-thumbs li { min-width: 96px; flex: 0 0 auto; }
  .page-thumbs img { height: 72px; }
}
