:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #1a1d23;
  --muted: #6b7280;
  --primary: #2f5bea;
  --primary-hover: #2449c4;
  --primary-text: #ffffff;
  --ok: #15803d;
  --ok-bg: #e7f6ec;
  --bad: #c2410c;
  --bad-bg: #fdeee6;
  --warn: #a16207;
  --warn-bg: #fdf6dc;
  --err: #b91c1c;
  --err-bg: #fdecec;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1a1d23;
    --surface-2: #22262e;
    --border: #2d323b;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --primary: #5b80f5;
    --primary-hover: #7593f7;
    --ok: #4ade80;
    --ok-bg: #153222;
    --bad: #fb923c;
    --bad-bg: #3a2216;
    --warn: #facc15;
    --warn-bg: #352d10;
    --err: #f87171;
    --err-bg: #3a1a1a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "IBM Plex Sans Thai", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

.wrap { max-width: 1280px; margin: 0 auto; padding: 0 24px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar-inner {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding-top: 14px; padding-bottom: 14px;
}
h1 { font-size: 20px; margin: 0; font-weight: 700; }
.sub { margin: 0; color: var(--muted); font-size: 13px; }
.topbar-actions { display: flex; align-items: center; gap: 14px; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; cursor: pointer;
  border-radius: 8px; padding: 8px 16px; border: 1px solid transparent;
  transition: background .15s, border-color .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: .6; cursor: progress; }
.btn-ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--surface-2); }

/* banners */
.banner {
  margin-top: 16px; padding: 10px 14px; border-radius: var(--radius);
  font-size: 13.5px; white-space: pre-wrap; word-break: break-word;
}
.banner-error { background: var(--err-bg); color: var(--err); }
.banner-warn { background: var(--warn-bg); color: var(--warn); }

/* empty / loading */
.empty {
  margin: 64px auto; max-width: 420px; text-align: center;
}
.empty h2 { font-size: 17px; margin: 12px 0 4px; }
.empty p { margin: 0; }
.empty-icon {
  width: 56px; height: 56px; margin: 0 auto; border-radius: 14px;
  display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--border); color: var(--muted);
}
.spinner {
  width: 36px; height: 36px; margin: 0 auto; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--primary);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.spinner-sm { width: 16px; height: 16px; border-width: 2px; margin: 0; flex: none; }

/* progress */
.progress-panel {
  margin-top: 20px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
}
.progress-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 10px;
}
.progress-title {
  display: flex; align-items: center; gap: 10px; min-width: 0; font-weight: 500;
}
.progress-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress-meta {
  display: flex; align-items: baseline; gap: 10px; flex: none;
  font-variant-numeric: tabular-nums;
}
.progress-meta b { font-size: 15px; min-width: 3.2em; text-align: right; }
.progress-track {
  height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden;
}
.progress-fill, .topline-fill {
  height: 100%; width: 0; background: var(--primary); border-radius: inherit;
  transition: width .35s ease;
  background-image: linear-gradient(
    -45deg, rgba(255,255,255,.25) 25%, transparent 25%, transparent 50%,
    rgba(255,255,255,.25) 50%, rgba(255,255,255,.25) 75%, transparent 75%, transparent);
  background-size: 20px 20px;
  animation: stripes .8s linear infinite;
}
@keyframes stripes { to { background-position: 20px 0; } }
.topline { position: absolute; left: 0; right: 0; bottom: -1px; height: 3px; overflow: hidden; }
.topline-fill { border-radius: 0; background-image: none; animation: none; }
#results.is-busy { opacity: .45; pointer-events: none; transition: opacity .2s; }
@media (prefers-reduced-motion: reduce) {
  .progress-fill { animation: none; }
}

/* summary cards */
.cards {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px;
  margin-top: 20px;
}
.card {
  font: inherit; text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 2px; color: var(--text);
  transition: border-color .15s;
}
.card:hover { border-color: var(--muted); }
.card.active { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.card-label { color: var(--muted); font-size: 13px; }
.card-value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.card-ok .card-value { color: var(--ok); }
.card-bad .card-value { color: var(--bad); }
.card-warn .card-value { color: var(--warn); }

/* files */
.files {
  margin-top: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 14px;
}
.files summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.files ul { margin: 8px 0 4px; padding-left: 18px; }
.files li { font-size: 13px; margin: 2px 0; }

/* toolbar */
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-top: 16px;
}
.search {
  flex: 1 1 260px; display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 0 10px; color: var(--muted);
}
.search input {
  flex: 1; border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; padding: 8px 0; min-width: 0;
}
.search:focus-within { border-color: var(--primary); }
select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
.seg {
  display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 3px;
}
.seg button {
  font: inherit; border: 0; background: transparent; color: var(--muted);
  padding: 5px 12px; border-radius: 6px; cursor: pointer;
}
.seg button.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

/* table */
.table-wrap {
  margin-top: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: auto; max-height: calc(100vh - 260px);
  box-shadow: var(--shadow);
}
table { width: 100%; border-collapse: collapse; min-width: 900px; }
th, td { padding: 9px 12px; text-align: left; vertical-align: top; }
thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-2); color: var(--muted);
  font-weight: 600; font-size: 12.5px; border-bottom: 1px solid var(--border);
}
tbody tr + tr td { border-top: 1px solid var(--border); }
tbody tr:hover td { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.num { font-weight: 500; }
.sku { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12.5px; }
.sku-clean { color: var(--muted); font-size: 11.5px; }
.prod-name { font-weight: 500; }
.prod-item {
  color: var(--muted); font-size: 12px; max-width: 420px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.shop { white-space: nowrap; }
.platform { color: var(--muted); font-size: 12px; text-transform: capitalize; }
.diff-up { color: var(--bad); }
.diff-down { color: var(--primary); }
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap;
}
.badge-match { background: var(--ok-bg); color: var(--ok); }
.badge-mismatch { background: var(--bad-bg); color: var(--bad); }
.badge-no_db { background: var(--warn-bg); color: var(--warn); }
.no-rows { padding: 32px; text-align: center; color: var(--muted); }
#rowCount { margin: 8px 2px 32px; }

@media (max-width: 720px) {
  .wrap { padding: 0 16px; }
  .topbar-inner { flex-wrap: wrap; }
  .cards { grid-template-columns: repeat(2, 1fr); }
  .card-value { font-size: 22px; }
  .table-wrap { max-height: none; }
}

/* folder panel */
.btn-sm { padding: 5px 10px; font-size: 13px; font-weight: 500; }
.folder {
  margin-top: 20px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.folder-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; cursor: pointer; list-style: none;
}
.folder-head::-webkit-details-marker { display: none; }
.folder-title { display: flex; align-items: center; gap: 8px; min-width: 0; flex-wrap: wrap; }
.chev { color: var(--muted); transition: transform .15s; flex: none; }
.folder[open] .chev { transform: rotate(90deg); }
.pill {
  font-size: 12px; padding: 1px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.folder-body { padding: 0 16px 16px; border-top: 1px solid var(--border); }
.folder-body .banner { margin-top: 12px; }
.coverage-wrap { overflow-x: auto; margin-top: 12px; }
.coverage { border-collapse: collapse; min-width: 0; width: auto; }
.coverage th, .coverage td { padding: 6px 14px; text-align: center; border: 1px solid var(--border); }
.coverage th { background: var(--surface-2); color: var(--muted); font-size: 12.5px; font-weight: 600; }
.coverage th:first-child, .coverage td:first-child { text-align: left; }
.coverage td { font-size: 13px; }
.cov-yes { color: var(--ok); font-weight: 600; }
.cov-no { color: var(--muted); }
.cov-dup { color: var(--warn); font-weight: 600; }
.cov-off { color: var(--muted); font-size: 12px; }
.files-table { max-height: 320px; box-shadow: none; }
.files-table table { min-width: 720px; }
.files-table td { font-size: 13px; vertical-align: middle; }
.fname { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12.5px; word-break: break-all; }
.badge-unsupported { background: var(--surface-2); color: var(--muted); }
.badge-invalid { background: var(--err-bg); color: var(--err); }
.file-note { color: var(--muted); font-size: 12px; margin-top: 2px; }

.note {
  margin: 12px 0 0; padding: 8px 12px; border-radius: 8px;
  background: var(--surface-2); color: var(--muted); font-size: 13px;
}
.note b { color: var(--text); font-weight: 600; }

/* history + accept */
.run-picker { display: flex; align-items: center; gap: 8px; }
.run-picker select { max-width: 280px; }
.checked-at { margin: 16px 2px 0; }
.banner-info { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.banner-info button {
  font: inherit; color: var(--primary); background: none; border: 0; padding: 0;
  cursor: pointer; text-decoration: underline; margin-left: 6px;
}
.card-acc .card-value { color: var(--primary); }
.col-acc { width: 64px; text-align: center; }
td.col-acc { text-align: center; }
.acc-box { width: 18px; height: 18px; accent-color: var(--primary); cursor: pointer; margin-top: 2px; }
.acc-box:disabled { cursor: progress; }
tr.is-accepted td { background: color-mix(in srgb, var(--primary) 5%, transparent); }
tr.is-accepted .badge { opacity: .55; }
.acc-tag {
  display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; color: var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, transparent); white-space: nowrap;
}
.acc-note {
  width: 100%; max-width: 320px; font: inherit; font-size: 12.5px;
  color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 8px;
}
.acc-note:focus { outline: 0; border-color: var(--primary); }
.acc-saved { color: var(--ok); font-size: 11.5px; margin-left: 6px; }
@media (max-width: 1000px) { .cards { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 720px) {
  .cards { grid-template-columns: repeat(2, 1fr); }
  .run-picker .muted { display: none; }
  .run-picker select { max-width: 170px; }
}

/* ฟอร์มหมายเหตุ + ยืนยัน */
.acc-form { display: flex; align-items: center; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.acc-form .acc-note { flex: 1 1 220px; padding: 5px 8px; }
tr.is-draft td { background: color-mix(in srgb, var(--primary) 7%, transparent); }
.acc-done { display: flex; align-items: baseline; gap: 6px; margin-top: 6px; flex-wrap: wrap; font-size: 12.5px; }
.acc-done-note { color: var(--text); }
.link-btn {
  font: inherit; font-size: 12px; color: var(--primary); background: none; border: 0;
  padding: 0; cursor: pointer; text-decoration: underline;
}

/* popup กลางจอ */
.dlg {
  border: 0; padding: 0; border-radius: 14px; width: min(420px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.dlg::backdrop { background: rgba(15, 18, 25, .45); backdrop-filter: blur(2px); }
.dlg[open] { animation: dlg-in .16s ease-out; }
.dlg[open]::backdrop { animation: fade-in .16s ease-out; }
.dlg.closing { animation: dlg-out .12s ease-in forwards; }
@keyframes dlg-in { from { opacity: 0; transform: translateY(8px) scale(.97); } }
@keyframes dlg-out { to { opacity: 0; transform: translateY(4px) scale(.98); } }
@keyframes fade-in { from { opacity: 0; } }
.dlg-box { padding: 22px 22px 18px; }
.dlg h3 { margin: 0 0 6px; font-size: 17px; }
.dlg p { margin: 0; color: var(--muted); }
.dlg-detail {
  margin-top: 14px; padding: 10px 12px; border-radius: 8px;
  background: var(--surface-2); font-size: 13px; word-break: break-word;
}
.dlg-note { margin-top: 6px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.dlg-danger #dlgOk { background: var(--err); color: #fff; }
.dlg-danger #dlgOk:hover { filter: brightness(1.08); }
@media (prefers-reduced-motion: reduce) {
  .dlg[open], .dlg[open]::backdrop, .dlg.closing { animation: none; }
}

/* ผลตรวจรายร้าน (ข้างตารางเช็คไฟล์) */
.folder-grid { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start; margin-top: 12px; }
.folder-grid .coverage-wrap { margin-top: 0; flex: 0 1 auto; }
.shop-summary {
  flex: 1 1 560px; min-width: 0; border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; background: var(--surface);
}
.shop-summary-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.shop-summary-wrap { overflow-x: auto; }
.shop-table { width: 100%; }
.shop-table td, .shop-table th { white-space: nowrap; }
.shop-table td.num, .shop-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.shop-name {
  font: inherit; font-weight: 600; color: var(--text); background: none; border: 0; padding: 0;
  cursor: pointer; text-align: left;
}
.shop-name:hover { color: var(--primary); text-decoration: underline; }
.shop-ok { color: var(--ok); font-weight: 600; }
.shop-bad { color: var(--bad); font-weight: 600; }
.shop-acc { color: var(--primary); }
.shop-done { color: var(--ok); font-size: 12.5px; font-weight: 600; }
.shop-table .btn-sm { padding: 3px 10px; font-size: 12.5px; }
.dlg-input {
  width: 100%; margin-top: 10px; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
}
.dlg-input:focus { outline: 0; border-color: var(--primary); }
.dlg-list { margin: 6px 0 0; padding-left: 18px; }

/* ===== ด้านบนแบบกระชับ (v11) ===== */
.folder { margin-top: 16px; }
.folder-head { padding: 10px 14px; }
.folder-ico { color: var(--muted); flex: none; }
.chip {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 6px;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.folder-body .coverage-wrap { margin-top: 12px; }
.file-list { margin-top: 12px; }
.file-list > summary {
  cursor: pointer; color: var(--primary); font-size: 13px; width: max-content; list-style: none;
}
.file-list > summary::-webkit-details-marker { display: none; }
.file-list > summary::before { content: "▸ "; }
.file-list[open] > summary::before { content: "▾ "; }
.file-list .files-table { margin-top: 8px; }

/* คำเตือนแบบพับ */
.warn-fold {
  margin-top: 16px; border-radius: var(--radius); background: var(--warn-bg); color: var(--warn);
  font-size: 13.5px;
}
.warn-fold > summary {
  cursor: pointer; padding: 9px 14px; list-style: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.warn-fold > summary::-webkit-details-marker { display: none; }
.warn-fold > summary .muted { color: inherit; opacity: .75; font-weight: 400; }
.warn-fold[open] > summary { white-space: normal; }
.warn-body { padding: 0 14px 12px; }
.warn-item + .warn-item { margin-top: 10px; }
.warn-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.warn-chips .chip { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 11.5px; }

/* การ์ดสรุปให้เตี้ยลง */
.cards { margin-top: 16px; gap: 10px; }
.card { padding: 10px 14px; }
.card-value { font-size: 22px; }

/* กล่องผลตรวจรายร้าน (อยู่ในส่วนผลตรวจ) */
#results .shop-summary { margin-top: 12px; flex: none; width: 100%; padding: 12px 14px; }
.shop-actions { display: flex; gap: 6px; justify-content: flex-end; align-items: center; flex-wrap: wrap; }
.shop-undo { color: var(--muted); }
.shop-undo:hover { color: var(--err); border-color: var(--err); }
.acc-undo { color: var(--muted); }
.acc-undo:hover { color: var(--err); }

/* ท้ายตาราง: จำนวนแถว + เงื่อนไขการคัด */
.table-foot {
  display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  margin: 8px 2px 32px;
}
.table-foot #rowCount { margin: 0; }
