@import url('https://fonts.googleapis.com/css2?family=Chakra+Petch:wght@500;600;700&family=Sarabun:wght@400;500;600;700&display=swap');

:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --border: #e4e4e0;
  --text: #1c1c1a;
  --text-muted: #6b6b64;
  --accent: #0f6b5c;
  --accent-soft: #e3f1ee;
  --warn: #9a6b0f;
  --warn-soft: #fdf3df;
  --danger: #a83228;
  --danger-soft: #fbe9e7;
  --success: #2f7d4f;
  --success-soft: #e8f5ec;
  --shadow: 0 1px 2px rgba(20, 20, 15, 0.06), 0 1px 8px rgba(20, 20, 15, 0.04);
}

[data-theme="dark"] {
  --bg: #15171a;
  --surface: #1d2024;
  --border: #2c3036;
  --text: #ece9e4;
  --text-muted: #9a9da2;
  --accent: #4fd1b5;
  --accent-soft: #1c3733;
  --warn: #e6b566;
  --warn-soft: #3a2f18;
  --danger: #e5847a;
  --danger-soft: #3a201d;
  --success: #7cc99a;
  --success-soft: #1d3324;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 10px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Sarabun', sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background .15s ease, color .15s ease;
}

h1, h2, h3, .num, .brand {
  font-family: 'Chakra Petch', sans-serif;
}

a { color: inherit; text-decoration: none; }

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  gap: 4px;
}

.sidebar .brand {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 24px;
  padding: 0 8px;
}

.sidebar a.nav-link {
  display: block;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
}

.sidebar a.nav-link:hover { background: var(--accent-soft); color: var(--text); }
.sidebar a.nav-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.main-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.topbar h1 { font-size: 18px; margin: 0; }

.content { padding: 24px 28px; flex: 1; overflow-y: auto; }

.theme-toggle {
  width: 52px; height: 28px;
  border-radius: 999px;
  background: var(--border);
  border: none;
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
}
.theme-toggle span {
  position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--surface);
  transition: left .15s ease;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px;
}
[data-theme="dark"] .theme-toggle span { left: 27px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}

.card h2 { font-size: 15px; margin: 0 0 14px; }

.stat-row { display: flex; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.stat-tile {
  flex: 1; min-width: 160px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.stat-tile .label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat-tile .value { font-family: 'Chakra Petch', sans-serif; font-size: 24px; font-weight: 700; }

.btn {
  font-family: 'Sarabun', sans-serif;
  font-size: 14px; font-weight: 600;
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.05); }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 600;
  padding: 3px 10px; border-radius: 999px;
}
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.verified { background: var(--success-soft); color: var(--success); }
.badge.dup { background: var(--danger-soft); color: var(--danger); }
/* Shown when a document was edited again after it was last confirmed
   ("verified") -- its data may no longer match what was originally
   confirmed. Used on both the Validate page and the tax report's ref view. */
.badge.edit-warn { background: var(--warn-soft); color: var(--warn); }
/* ใบลดหนี้ (credit note) -- shown wherever a document appears (documents
   list, tax report ref panel) so it's never mistaken for a regular purchase
   document at a glance. */
.badge.credit-note { background: var(--accent-soft); color: var(--accent); }
/* "รวมจาก <unit> แล้ว" -- shown on a product-summary row that has had at
   least one non-base unit merged into it via "ยืนยันแปลงหน่วย" (see
   product_summary.js's appliedConversionsHtml()). font-size is smaller than
   the default .badge since this sits inside a table cell alongside the unit
   name and the "หน่วยแปลง" editor toggle, not in its own stat row. */
.badge.converted { background: var(--success-soft); color: var(--success); font-size: 11px; padding: 2px 8px; }

/* "ref" slide-in panel (tax report) -- slides in from the right edge rather
   than opening a modal or a new tab. */
.slide-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.35);
  opacity: 0; pointer-events: none; transition: opacity .2s ease; z-index: 150;
}
.slide-overlay.open { opacity: 1; pointer-events: auto; }
.slide-panel {
  position: fixed; top: 0; right: 0; height: 100vh; width: 460px; max-width: 92vw;
  background: var(--surface); box-shadow: var(--shadow); z-index: 151;
  transform: translateX(100%); transition: transform .25s ease;
  display: flex; flex-direction: column;
}
.slide-panel.open { transform: translateX(0); }
.slide-panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--border);
}
.slide-panel-body { padding: 18px; overflow-y: auto; flex: 1; }
.slide-panel-image { width: 100%; max-height: 320px; object-fit: contain; background: #12131a;
  border-radius: 10px; margin-bottom: 14px; }
.slide-panel-close { cursor: pointer; font-size: 20px; line-height: 1; color: var(--text-muted); background: none; border: none; }
.slide-panel-close:hover { color: var(--danger); }
.slide-field { display: flex; justify-content: space-between; padding: 6px 0; font-size: 13.5px; border-bottom: 1px dashed var(--border); }
.slide-field .label { color: var(--text-muted); }

/* Shared pagination bar -- see renderPagination() in common.js */
.pagination-bar {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px;
  padding: 12px 16px; font-size: 12.5px; color: var(--text-muted);
}
.pagination-controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.pagination-label { font-size: 12.5px; color: var(--text); margin: 0 4px; white-space: nowrap; }
.pagination-controls select { padding: 5px 8px; font-size: 12.5px; }

.banner {
  padding: 12px 16px; border-radius: 10px;
  background: var(--danger-soft); color: var(--danger);
  font-size: 13.5px; font-weight: 500;
  margin-bottom: 18px;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .02em; }
th.sortable-th { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable-th:hover { color: var(--text); }
th.sortable-th::after { content: "\2195"; margin-left: 5px; opacity: .35; font-size: 11px; }
th.sortable-th.sort-asc::after { content: "\2191"; opacity: 1; color: var(--accent); }
th.sortable-th.sort-desc::after { content: "\2193"; opacity: 1; color: var(--accent); }
tr:last-child td { border-bottom: none; }
tr.is-dup td:first-child { border-left: 3px solid var(--danger); }

input, select, textarea {
  font-family: 'Sarabun', sans-serif;
  font-size: 13.5px;
  padding: 7px 10px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { font-size: 12.5px; color: var(--text-muted); display: block; margin-bottom: 4px; }
.field { margin-bottom: 12px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.modal { background: var(--surface); border-radius: 14px; padding: 24px; width: 420px; max-width: 92vw; box-shadow: var(--shadow); }

.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab {
  padding: 7px 14px; border-radius: 999px; font-size: 13px; font-weight: 600;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-muted); cursor: pointer;
}
.tab.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

/* Global loading overlay -- injected by common.js's api() wrapper on any
   fetch call that ends up taking a while (see ensureLoadingOverlay() /
   showLoading() there). Shared by every page automatically. */
.global-loading-overlay {
  position: fixed; inset: 0; background: rgba(15, 15, 12, 0.28);
  display: none; align-items: center; justify-content: center; z-index: 500;
}
.global-loading-overlay.show { display: flex; }
.global-loading-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 22px 30px; box-shadow: var(--shadow); display: flex; flex-direction: column;
  align-items: center; gap: 12px;
}
.global-spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 4px solid var(--border); border-top-color: var(--accent);
  animation: global-spin 0.7s linear infinite;
}
@keyframes global-spin { to { transform: rotate(360deg); } }
.global-loading-text { font-size: 13px; color: var(--text-muted); text-align: center; max-width: 260px; }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.toolbar .actions { display: flex; gap: 10px; }

.muted { color: var(--text-muted); }
.num { font-variant-numeric: tabular-nums; }
.empty-state { text-align: center; padding: 48px 20px; color: var(--text-muted); }

.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input[type="checkbox"] { width: auto; }

/* Shared "hotel-style" search bar -- one rounded segmented pill (search +
   a date-range pair + a clear button), like a hotel booking site's search
   widget. See filterBarHtml()/wireFilterBar() in common.js. */
.filter-bar {
  display: flex; align-items: stretch; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); margin-bottom: 18px; overflow: hidden;
}
.filter-seg {
  display: flex; flex-direction: column; justify-content: center;
  padding: 9px 18px; min-width: 150px; flex: 1;
  border-right: 1px solid var(--border);
}
.filter-seg:last-of-type { border-right: none; }
.filter-seg-search { flex: 1.6; min-width: 220px; }
.filter-seg label {
  margin: 0 0 2px; font-size: 10.5px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-muted);
}
.filter-seg input {
  border: none; padding: 2px 0; background: transparent; font-size: 13.5px; width: 100%;
}
.filter-seg input:focus { outline: none; }
.filter-clear-btn {
  align-self: center; margin: 0 14px; flex-shrink: 0; border-radius: 999px; white-space: nowrap;
}
@media (max-width: 720px) {
  .filter-seg { border-right: none; border-bottom: 1px solid var(--border); }
  .filter-clear-btn { margin: 10px 14px; }
}

/* Home dashboard -- a compact date/period filter row (no search box, unlike
   the shared filter-bar above) and a 2x2 grid of "top 10" bar-list panels. */
.home-filter-bar {
  display: flex; align-items: stretch; flex-wrap: wrap; gap: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); margin-bottom: 18px; overflow: hidden;
}
.home-filter-bar .filter-seg select { border: none; padding: 2px 0; background: transparent; font-size: 13.5px; width: 100%; color: var(--text); }
.home-filter-bar .filter-seg select:focus { outline: none; }

.home-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 4px; }
@media (max-width: 980px) { .home-grid { grid-template-columns: 1fr; } }

.top-list-empty { color: var(--text-muted); font-size: 13px; padding: 10px 0; }
.top-list-item { display: flex; align-items: center; gap: 10px; padding: 7px 0; }
.top-list-rank {
  flex-shrink: 0; width: 22px; height: 22px; border-radius: 6px; background: var(--accent-soft);
  color: var(--accent); font-size: 11.5px; font-weight: 700; display: flex; align-items: center;
  justify-content: center; font-family: 'Chakra Petch', sans-serif;
}
.top-list-body { flex: 1; min-width: 0; }
.top-list-label {
  font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 4px;
}
.top-list-bar-track { background: var(--border); border-radius: 999px; height: 6px; overflow: hidden; }
.top-list-bar-fill { background: var(--accent); height: 100%; border-radius: 999px; }
.top-list-value {
  flex-shrink: 0; font-family: 'Chakra Petch', sans-serif; font-size: 12.5px; font-weight: 600;
  min-width: 76px; text-align: right; font-variant-numeric: tabular-nums;
}
