:root {
  --bg: #f6f5f2; --surface: #fff; --ink: #1d1f23; --muted: #5d636e; --line: #dedbd3;
  --accent: #1f5fa8; --accent-ink: #fff; --mark: #ffe58a; --ok: #1d7a3d; --warn: #9a6500; --err: #b42318;
  --radius: 12px; --tap: 44px;
  font-family: "Leelawadee UI", Thonburi, "Noto Sans Thai", Tahoma, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #15171b; --surface: #1f2228; --ink: #eceae6; --muted: #a2a8b3; --line: #343842;
          --accent: #7fb0ec; --accent-ink: #0d1726; --mark: #6b5800; --ok: #6fd08f; --warn: #e5b55a; --err: #ff8a80; }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 17px; line-height: 1.55; }
a { color: var(--accent); }
h1 { font-size: 1.35rem; margin: .4rem 0 .8rem; }
h2 { font-size: 1.1rem; margin: 0 0 .6rem; }
.wrap { overflow-wrap: anywhere; }

.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: .5rem;
  padding: .4rem max(16px, env(safe-area-inset-left)); background: var(--surface); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { font-weight: 700; text-decoration: none; color: var(--ink); margin-right: auto; min-height: var(--tap); display: flex; align-items: center; }
.nav { display: flex; gap: .2rem; overflow-x: auto; }
.nav a { padding: 0 .7rem; min-height: var(--tap); display: flex; align-items: center; border-radius: 8px; text-decoration: none; color: var(--muted); white-space: nowrap; }
.nav a[aria-current] { color: var(--ink); background: var(--bg); font-weight: 600; }
.page { max-width: 860px; margin: 0 auto; padding: 12px 16px 48px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .3rem; min-height: var(--tap); padding: 0 1rem;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink); font: inherit; text-decoration: none; cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.small { min-height: 36px; padding: 0 .7rem; font-size: .92rem; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 99px; padding: 0 .5rem; font-size: .8rem; }

input[type=text], input[type=search], input[type=number], select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  min-height: var(--tap); padding: .4rem .7rem; width: 100%; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 1px; }
[aria-invalid=true] { border-color: var(--err); }

.searchform { display: grid; gap: .6rem; }
.searchbox { display: flex; gap: .5rem; }
.searchbox input { font-size: 1.1rem; min-height: 50px; }
.searchbox .btn { min-height: 50px; }
.filters > summary { list-style: none; width: fit-content; }
.filters > summary::-webkit-details-marker { display: none; }
.filter-body { margin-top: .6rem; padding: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); display: grid; gap: .8rem; }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 600; margin-bottom: .3rem; }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span { display: inline-flex; align-items: center; gap: .3rem; min-height: 40px; padding: 0 .8rem; border: 1px solid var(--line); border-radius: 99px; background: var(--bg); cursor: pointer; }
.chip input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip input:focus-visible + span { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); }
.chip small { opacity: .75; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.field { display: grid; gap: .25rem; font-size: .95rem; color: var(--muted); }
.field input, .field select, .field textarea { color: var(--ink); }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; }

.summary { color: var(--muted); margin: 1rem 0 .5rem; }
.empty { padding: 1.5rem; text-align: center; color: var(--muted); background: var(--surface); border-radius: var(--radius); border: 1px dashed var(--line); }
.results { list-style: none; margin: 0; padding: 0; display: grid; gap: .7rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.card-head { display: block; padding: 12px 14px 6px; color: inherit; text-decoration: none; }
.meta { display: flex; flex-wrap: wrap; gap: .2rem .7rem; font-size: .88rem; color: var(--muted); }
.docno { color: var(--ink); font-weight: 600; overflow-wrap: anywhere; }
.subject { font-size: 1.05rem; margin: .25rem 0 0; font-weight: 600; overflow-wrap: anywhere; }
.hits { list-style: none; margin: 0; padding: 0 6px 8px; }
.hits a { display: block; padding: 8px; border-radius: 8px; color: inherit; text-decoration: none; font-size: .95rem; }
.hits a:hover, .hits a:active { background: var(--bg); }
.hits .pg { display: inline-block; font-size: .8rem; font-weight: 600; color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); padding: 0 .45rem; border-radius: 6px; margin-right: .3rem; }
.hits .more a, .hits .open { color: var(--accent); }
.snip { overflow-wrap: anywhere; }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; }
.tag { display: inline-block; font-size: .78rem; padding: 0 .45rem; border-radius: 6px; border: 1px solid currentColor; }
.tag-needs_ocr, .tag-queued, .tag-processing { color: var(--warn); }
.tag-error { color: var(--err); }
.tag-ready { color: var(--ok); }
.pager { display: flex; justify-content: center; align-items: center; gap: 1rem; margin: 1.2rem 0; }

.back { display: inline-flex; min-height: var(--tap); align-items: center; }
.detail, .panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: .8rem; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: .3rem .9rem; margin: 0 0 1rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.form { display: grid; gap: .7rem; margin: .5rem 0; }
.inline { display: inline-flex; flex-wrap: wrap; align-items: center; gap: .4rem; margin: .2rem 0; }
.inline input[type=text] { width: auto; flex: 1 1 10rem; }
.auto { margin-top: 1rem; padding-top: .8rem; border-top: 1px solid var(--line); font-size: .95rem; }
.ok { color: var(--ok); } .warn { color: var(--warn); } .err { color: var(--err); }
.box { padding: .7rem 1rem; border: 1px solid currentColor; border-radius: 10px; }
.hint { color: var(--muted); font-size: .93rem; }
.banner { margin: 0 0 .8rem; }
.narrow { max-width: 460px; margin: 1rem auto; }
.uplist { padding-left: 1.1rem; display: grid; gap: .4rem; }
.drop { display: grid; place-items: center; gap: .3rem; text-align: center; padding: 1.6rem 1rem; border: 2px dashed var(--line); border-radius: var(--radius); background: var(--surface); cursor: pointer; color: var(--muted); }
.drop.over { border-color: var(--accent); }
.drop input { max-width: 100%; }
.chosen { font-size: .9rem; color: var(--ink); }
.stats { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .5rem; }
.stat a { display: grid; padding: .8rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); text-decoration: none; color: var(--ink); }
.stat b { font-size: 1.5rem; }
.stat span { color: var(--muted); font-size: .9rem; }

.wide .page { max-width: 1300px; }
.pagecmp { display: grid; gap: 12px; grid-template-columns: 1fr; margin: .6rem 0; }
.pageimg { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; max-height: 80vh; }
.pageimg img { display: block; width: 100%; height: auto; }
.pagetext textarea { min-height: 50vh; font-size: 1rem; line-height: 1.6; }
@media (min-width: 900px) { .pagecmp { grid-template-columns: 1fr 1fr; } .pagetext textarea { min-height: 70vh; } }
h1 .tag { vertical-align: middle; font-size: .8rem; }

/* ตัวดูไฟล์ */
.viewer-page .page { max-width: none; padding: 0 0 40px; }
.viewer-page .topbar { position: static; }
.viewer-bar { position: sticky; top: 0; z-index: 9; display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; padding: 6px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.vtitle { flex: 1 1 8rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.pgctl, .zoom { display: inline-flex; align-items: center; gap: .3rem; }
#pgnum { width: 4.2rem; min-height: 36px; text-align: center; padding: 0 .3rem; }
#viewer { display: grid; justify-items: center; gap: 12px; padding: 12px 0; overflow-x: auto; }
.pdfpage { position: relative; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.25); }
.pdfpage canvas { display: block; }
.pdfpage .pnum { position: absolute; top: -1px; left: -1px; background: rgba(0,0,0,.55); color: #fff; font-size: 12px; padding: 0 6px; border-radius: 0 0 6px 0; }
.textLayer { position: absolute; inset: 0; overflow: hidden; line-height: 1; text-align: initial; opacity: 1; forced-color-adjust: none; transform-origin: 0 0; z-index: 2; }
.textLayer span, .textLayer br { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.hl { background: rgba(255, 200, 0, .45); border-radius: 2px; }
.textLayer ::selection { background: rgba(0, 100, 255, .3); }
.textLayer .markedContent { top: 0; height: 0; }
.loading { color: var(--muted); padding: 2rem; }
.imgview { padding: 12px 16px; }
.imgview img { max-width: 100%; height: auto; display: block; margin: 0 auto; }
.textview { white-space: pre-wrap; overflow-wrap: anywhere; padding: 16px; margin: 12px 16px; background: var(--surface); border-radius: var(--radius); font-family: inherit; }

@media (max-width: 520px) {
  body { font-size: 16px; }
  .row2 { grid-template-columns: 1fr; }
  .nav a { padding: 0 .5rem; }
  .facts { grid-template-columns: 1fr; gap: 0; }
  .facts dt { margin-top: .4rem; font-size: .85rem; }
}
