:root {
  --navy: #1d3461;
  --blue: #1f78c1;
  --sky: #e8f2fb;
  --bg: #f3f6fa;
  --card: #ffffff;
  --ink: #1c2533;
  --muted: #6b778a;
  --line: #dde4ee;
  --red: #c8303c;
  --green: #2d7a4b;
  --amber: #b26a00;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: 'Sarabun', 'Thonburi', 'Leelawadee UI', sans-serif; font-size: 15px; }
a { color: var(--blue); }
h2 { font-family: 'Prompt', sans-serif; font-weight: 500; color: var(--navy); font-size: 1.25rem; margin: 0 0 12px; }
h3 { font-family: 'Prompt', sans-serif; font-weight: 500; color: var(--navy); font-size: 1.05rem; margin: 18px 0 8px; }
.muted { color: var(--muted); font-weight: 400; }
.sm { font-size: 0.82rem; }
.pad { padding: 24px; }
.hint { color: var(--muted); font-size: 0.9rem; }
code { background: #eef2f7; padding: 1px 5px; border-radius: 4px; font-size: 0.85em; }

.btn { font: inherit; border: 1px solid var(--line); background: #fff; color: var(--ink); border-radius: 8px; padding: 8px 14px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.btn:hover { border-color: var(--blue); }
.btn.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn.accent { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
.btn.sm { padding: 4px 10px; font-size: 0.85rem; }

input, select, textarea { font: inherit; width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--blue) 35%, transparent); border-color: var(--blue); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 0.88rem; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.span2 { grid-column: span 2; }

.pill { display: inline-block; padding: 1px 10px; border-radius: 20px; background: #eef2f7; font-size: 0.85rem; }
.pill.red { background: #fde8ea; color: var(--red); font-weight: 600; }
.pill.green { background: #e5f5ec; color: var(--green); }
.pill.amber { background: #fff3dd; color: var(--amber); }

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; gap: 24px; padding: 8px 20px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--navy); font-family: 'Prompt', sans-serif; font-weight: 600; }
.brand img { height: 40px; }
.brand small { display: block; font-weight: 400; color: var(--blue); font-size: 0.75rem; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a { text-decoration: none; color: var(--muted); padding: 8px 14px; border-radius: 8px; }
.topbar nav a.on { background: var(--sky); color: var(--navy); font-weight: 600; }
.who { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; }
.role { font-size: 0.75rem; background: var(--sky); color: var(--navy); padding: 1px 8px; border-radius: 10px; }
.main { padding: 20px; max-width: 1600px; margin: 0 auto; }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; background: linear-gradient(160deg, #e8f2fb, #f7f9fc); }
.auth-card { width: 100%; max-width: 380px; background: #fff; border-radius: 16px; padding: 28px; box-shadow: 0 10px 40px rgba(29, 52, 97, 0.12); display: flex; flex-direction: column; gap: 12px; }
.auth-card img { height: 120px; align-self: center; }
.auth-card h1 { font-family: 'Prompt', sans-serif; font-weight: 500; color: var(--navy); text-align: center; margin: 0; font-size: 1.4rem; }
.auth-card .sub { text-align: center; color: var(--muted); margin: -6px 0 6px; }
.auth-card .btn { justify-content: center; margin-top: 6px; }
.err { color: var(--red); min-height: 1.2em; text-align: center; }

/* ---------- list ---------- */
.drop { border: 2px dashed #b8cbe3; border-radius: 14px; background: #fff; padding: 22px; text-align: center; margin-bottom: 18px; transition: 0.15s; }
.drop.over { border-color: var(--blue); background: var(--sky); }
.drop-ic { font-size: 2rem; }
#upload-log { margin-top: 10px; text-align: left; max-width: 640px; margin-inline: auto; }
.log-line { font-size: 0.9rem; padding: 2px 0; }
.log-line.bad { color: var(--red); }
.card-panel { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 18px; margin-bottom: 18px; }
.list-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.list-head h2 { margin: 0; }
.list-head input { max-width: 320px; }
.table-wrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-weight: 600; color: var(--muted); font-size: 0.85rem; padding: 8px; border-bottom: 1px solid var(--line); }
table.list td { padding: 9px 8px; border-bottom: 1px solid #edf1f6; }
table.list tbody tr[data-id] { cursor: pointer; }
table.list tbody tr[data-id]:hover { background: var(--sky); }
.r { text-align: right; }

/* ---------- editor ---------- */
.editor { display: grid; grid-template-columns: minmax(380px, 460px) 1fr; gap: 18px; align-items: start; }
.ed-form { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 16px; max-height: calc(100vh - 100px); overflow: auto; position: sticky; top: 76px; }
.ed-actions { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.status { font-size: 0.8rem; padding: 2px 10px; border-radius: 12px; background: #fff3dd; color: var(--amber); }
.status.approved { background: #e5f5ec; color: var(--green); }
.saved { margin-left: auto; font-size: 0.85rem; }
.warnbox { background: #fff6e6; border: 1px solid #ffd99a; border-radius: 10px; padding: 10px 12px; font-size: 0.88rem; margin-bottom: 12px; }
.warnbox ul { margin: 6px 0 0; padding-left: 18px; max-height: 160px; overflow: auto; }
.res-filter { margin-bottom: 6px; }
.res-filter label { flex-direction: row; align-items: center; gap: 6px; }
.res-filter input { width: auto; }
table.res { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.res th { text-align: left; color: var(--muted); font-weight: 600; font-size: 0.8rem; padding: 4px; border-bottom: 1px solid var(--line); position: sticky; top: -16px; background: #fff; }
table.res td { padding: 4px; border-bottom: 1px solid #f0f3f7; vertical-align: middle; }
table.res tr.pos { background: #fff7f0; }
table.res tr.edited td:first-child { box-shadow: inset 3px 0 0 var(--blue); }
table.res input.val { width: 84px; padding: 4px 6px; text-align: right; }
.cls { display: inline-block; min-width: 26px; text-align: center; border-radius: 6px; background: #eef2f7; font-weight: 700; padding: 2px 0; }
.src { margin-top: 10px; font-size: 0.8rem; }

.ed-preview { min-width: 0; }
.pv-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; position: sticky; top: 64px; z-index: 3; background: var(--bg); padding: 8px 0; }
.chk { flex-direction: row; align-items: center; gap: 6px; color: var(--ink); }
.chk input { width: auto; }
.pv-scroll { overflow: auto; }
.pv-scale { zoom: 1; }
.pv-scale .page { margin: 0 auto 10mm; box-shadow: 0 4px 24px rgba(29, 52, 97, 0.14); }

/* ---------- images ---------- */
.img-cat { border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.img-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.img-slot { position: relative; border: 1px solid var(--line); border-radius: 10px; padding: 8px; background: #fff; }
.img-slot.over { border-color: var(--blue); background: var(--sky); }
.img-slot.has { border-color: #b9dcc7; }
.img-box { aspect-ratio: 1; border-radius: 8px; background: #f1f5fa; display: flex; align-items: center; justify-content: center; font-size: 2.6rem; cursor: pointer; overflow: hidden; border: 1px dashed #c9d6e6; }
.img-slot.has .img-box { border-style: solid; border-color: transparent; }
.img-box img { width: 100%; height: 100%; object-fit: cover; }
.img-slot .nm { font-weight: 600; font-size: 0.88rem; margin-top: 6px; line-height: 1.2; }
.img-slot .en { color: var(--muted); font-size: 0.78rem; }
.img-slot code { font-size: 0.75rem; }
.img-slot .x { position: absolute; top: 4px; right: 4px; border: 0; background: rgba(0, 0, 0, 0.55); color: #fff; width: 24px; height: 24px; border-radius: 50%; cursor: pointer; }

.settings { max-width: 980px; margin: 0 auto; }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(120px); background: var(--navy); color: #fff; padding: 10px 18px; border-radius: 10px; transition: 0.25s; z-index: 50; }
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.bad { background: var(--red); }

#print-root { display: none; }

@media (max-width: 1100px) {
  .editor { grid-template-columns: 1fr; }
  .ed-form { position: static; max-height: none; }
  .pv-scale { zoom: 0.85; }
}
@media (max-width: 640px) {
  .main { padding: 12px 16px; }
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 16px; }
  .topbar nav { order: 3; flex: 0 0 100%; overflow-x: auto; }
  .topbar nav a { white-space: nowrap; }
  .who { margin-left: auto; }
  table.list { min-width: 760px; }
  .grid2 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .pv-scale { zoom: 0.62; }
}

@media print {
  body { background: #fff; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block !important; }
}
