:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --ink: #111827;
  --ink-2: #4b5563;
  --ink-3: #9ca3af;
  --line: #e5e7eb;
  --line-2: #f1f3f5;
  --accent: #b91c1c;
  --accent-bg: #fef2f2;
  --accent-bg-strong: #fee2e2;
  --ok: #15803d;
  --ok-bg: #f0fdf4;
  --warn: #b45309;
  --shadow: 0 1px 2px rgba(17, 24, 39, .04), 0 1px 1px rgba(17, 24, 39, .03);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Inter', 'Noto Sans Thai', -apple-system, BlinkMacSystemFont,
    'Segoe UI', system-ui, 'Helvetica Neue', Arial, 'Apple Color Emoji', sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  font-size: 15px;
  line-height: 1.45;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1200px; margin: 0 auto;
  padding: 10px 16px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.brand { display: flex; flex-direction: column; line-height: 1.1; }
.brand strong { font-size: 17px; letter-spacing: -.01em; }
.brand .sub { font-size: 12px; color: var(--ink-2); }
.status { margin-left: auto; display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); }
.status .dot {
  width: 8px; height: 8px; border-radius: 999px;
  background: var(--ink-3); display: inline-block;
}
.status.ok .dot { background: var(--ok); }
.status.err .dot { background: var(--accent); }
.status.loading .dot { background: #d97706; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }
#refresh-btn {
  font: inherit; font-size: 12px; padding: 4px 10px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px; cursor: pointer;
}
#refresh-btn:hover { background: var(--line-2); }
#refresh-btn:active { transform: translateY(1px); }

/* ---------- Main layout ---------- */
main {
  max-width: 1200px; margin: 0 auto;
  padding: 16px;
}

/* ---------- Branch cards ---------- */
.branches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
.branch-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 3px solid var(--ink-3);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .05s;
}
.branch-card:hover { background: var(--line-2); }
.branch-card:active { transform: scale(.99); }
.branch-card.active { border-color: var(--ink); background: #fafafa; }
.branch-card.alert { border-left-color: var(--accent); background: var(--accent-bg); }
.branch-card.alert:hover { background: var(--accent-bg-strong); }
.branch-card.ok { border-left-color: var(--ok); }
.branch-card .branch-name {
  font-weight: 600; font-size: 14px;
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
}
.branch-card .business { font-size: 11px; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; }
.branch-card .count {
  font-size: 28px; font-weight: 700; line-height: 1;
  margin: 6px 0 2px;
}
.branch-card.alert .count { color: var(--accent); }
.branch-card.ok .count { color: var(--ok); }
.branch-card .count-label { font-size: 12px; color: var(--ink-2); }
.branch-card .updated { font-size: 11px; color: var(--ink-2); margin-top: 8px; }

/* ---------- Controls ---------- */
.controls {
  display: flex; flex-wrap: wrap; gap: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.control { display: flex; flex-direction: column; gap: 4px; min-width: 140px; }
.control.grow { flex: 1 1 200px; }
.control label { font-size: 11px; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; }
.control select, .control input[type="search"] {
  font: inherit; font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink);
  min-height: 38px;
}
.control select:focus, .control input:focus { outline: 2px solid #93c5fd; outline-offset: -1px; }
.check-label { display: flex !important; flex-direction: row !important; align-items: center; gap: 8px; font-size: 13px !important; color: var(--ink) !important; text-transform: none !important; letter-spacing: 0 !important; padding-top: 18px; }
.check-label input { width: 18px; height: 18px; }

/* ---------- Table ---------- */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
thead th {
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--ink-2);
  background: #fafafa;
  border-bottom: 1px solid var(--line);
  padding: 10px 12px;
  white-space: nowrap;
}
thead th.num { text-align: right; }
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { color: var(--ink); }
thead th.sortable.sort-asc::after  { content: ' ↑'; color: var(--ink); }
thead th.sortable.sort-desc::after { content: ' ↓'; color: var(--ink); }

tbody td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--line-2);
  vertical-align: middle;
}
tbody td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.below { background: var(--accent-bg); }
tbody tr.below td.qty { color: var(--accent); font-weight: 700; }
tbody tr:hover { background: var(--line-2); }
tbody tr.below:hover { background: var(--accent-bg-strong); }

.tag {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.tag-below { background: var(--accent-bg-strong); color: var(--accent); }
.tag-ok    { background: var(--ok-bg);     color: var(--ok); }

.empty {
  text-align: center;
  padding: 32px 16px;
  color: var(--ink-2);
  font-size: 14px;
  margin: 0;
}

.foot {
  margin: 16px 0 32px;
  color: var(--ink-2);
  font-size: 12px;
  text-align: center;
}
.foot a { color: var(--ink-2); }

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  main { padding: 12px; }
  .branches { grid-template-columns: 1fr 1fr; gap: 8px; }
  .branch-card { padding: 10px; }
  .branch-card .count { font-size: 22px; }
  .branch-card .branch-name { font-size: 13px; }
  .controls { padding: 8px; }
  .control { min-width: 100%; }
  .control.grow { flex-basis: 100%; }
  .check-label { padding-top: 0; }
  thead th, tbody td { padding: 8px 8px; font-size: 13px; }
  thead th { font-size: 11px; }
}
@media (max-width: 400px) {
  body { font-size: 14px; }
  .branches { grid-template-columns: 1fr; }
  .topbar-inner { padding: 8px 12px; gap: 8px; }
  .brand strong { font-size: 15px; }
  thead th:nth-child(1), tbody td:nth-child(1) { display: none; } /* hide Business col, redundant with card */
}
