:root {
  --primary: #0f766e;
  --primary-dark: #115e59;
  --primary-soft: #ccfbf1;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warning: #b45309;
  --warning-soft: #fef3c7;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --info-soft: #dbeafe;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .04);
  --shadow-lg: 0 10px 25px rgba(15, 23, 42, .14);
  --radius: 12px;
  --topbar-h: 60px;
  --bottomnav-h: 64px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: "Segoe UI", "Suphan", "Tahoma", "Noto Sans Thai", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

/* ---------- top bar ---------- */
.topbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--topbar-h);
  background: var(--primary-dark);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 50;
  box-shadow: var(--shadow);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.logo {
  width: 36px; height: 36px;
  background: #fff; color: var(--primary-dark);
  border-radius: 10px;
  display: grid; place-items: center;
  font-weight: 700; font-size: 15px;
  flex: none;
}

.brand-text { font-weight: 600; font-size: 16px; white-space: nowrap; }

.desktop-nav { display: none; gap: 4px; }

.nav-link {
  display: flex; align-items: center; gap: 8px;
  color: #cbd5e1; text-decoration: none;
  padding: 8px 12px; border-radius: 9px;
  font-size: 14px; font-weight: 500;
  transition: background .15s, color .15s;
}
.nav-link:hover { background: rgba(255,255,255,.1); color: #fff; }
.nav-link.active { background: #fff; color: var(--primary-dark); }
.nav-link svg { width: 18px; height: 18px; flex: none; }

/* ---------- layout ---------- */
.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: calc(var(--topbar-h) + 20px) 16px 28px;
  min-height: 100vh;
}

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { font-size: 22px; }
.page-head p { color: var(--muted); font-size: 14px; }

.loading { text-align: center; color: var(--muted); padding: 48px 0; }

/* ---------- cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}
.card h2 { font-size: 16px; margin-bottom: 12px; }
.card h3 { font-size: 14px; margin-bottom: 8px; }

/* ---------- stat cards ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.stat-card .label { color: var(--muted); font-size: 13px; }
.stat-card .value { font-size: 26px; font-weight: 700; margin-top: 4px; }
.stat-card .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat-card .value.primary { color: var(--primary-dark); }
.stat-card .value.danger { color: var(--danger); }
.stat-card .value.warning { color: var(--warning); }

/* ---------- forms ---------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

input[type=text], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 15px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, .15);
}
textarea { resize: vertical; min-height: 60px; }

.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--primary); color: #fff;
  border: none; border-radius: 9px;
  padding: 10px 16px; font-size: 14px; font-weight: 600;
  cursor: pointer; text-decoration: none;
  transition: background .15s;
}
.button:hover { background: var(--primary-dark); }
.button:disabled { opacity: .6; cursor: not-allowed; }

.button.outline {
  background: #fff; color: var(--primary-dark);
  border: 1px solid var(--primary);
}
.button.outline:hover { background: var(--primary-soft); }

.button.ghost {
  background: transparent; color: var(--muted);
  border: 1px solid var(--border);
}
.button.ghost:hover { background: var(--bg); color: var(--text); }

.button.danger { background: var(--danger); }
.button.danger:hover { background: #b91c1c; }

.button.small { padding: 6px 10px; font-size: 13px; border-radius: 8px; }

.button-row { display: flex; gap: 8px; flex-wrap: wrap; }
.button-row .button { flex: 1; }

.icon-btn {
  width: 34px; height: 34px;
  display: inline-grid; place-items: center;
  border: 1px solid var(--border); background: #fff;
  border-radius: 8px; cursor: pointer;
  color: var(--muted);
}
.icon-btn:hover { border-color: var(--primary); color: var(--primary-dark); }
.icon-btn.danger:hover { border-color: var(--danger); color: var(--danger); }
.icon-btn svg { width: 17px; height: 17px; }

/* ---------- quick quantity buttons ---------- */
.quick-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.quick {
  flex: 1; padding: 8px 0; text-align: center;
  border: 1px solid var(--border); background: #fff;
  border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 600;
  color: var(--primary-dark);
}
.quick:hover { background: var(--primary-soft); border-color: var(--primary); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-size: 12px; font-weight: 600; white-space: nowrap; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }

.lots-cell { font-size: 13px; }
.lot-item { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 3px 0; }

/* ---------- badges ---------- */
.badge {
  display: inline-block; padding: 3px 9px;
  border-radius: 999px; font-size: 12px; font-weight: 600;
  white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warning-soft); color: var(--warning); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.neutral { background: #f1f5f9; color: var(--muted); }
.badge.in { background: var(--ok-soft); color: var(--ok); }
.badge.out { background: var(--danger-soft); color: var(--danger); }

/* ---------- chips (หมวดหมู่) ---------- */
.chip-row { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: #f8fafc; border: 1px solid var(--border);
  padding: 5px 10px; border-radius: 999px; font-size: 13px;
}
.chip .count { color: var(--muted); font-size: 12px; }
.chip .rm {
  border: none; background: transparent; cursor: pointer;
  color: var(--muted); font-size: 14px; line-height: 1;
}
.chip .rm:hover { color: var(--danger); }

/* ---------- modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(15, 23, 42, .45);
  display: grid; place-items: center;
  padding: 16px;
}
.modal {
  background: #fff; border-radius: 14px;
  width: 100%; max-width: 420px;
  box-shadow: var(--shadow-lg);
  padding: 20px;
  max-height: 90vh; overflow-y: auto;
}
.modal h3 { font-size: 17px; margin-bottom: 16px; }
.modal .button-row { margin-top: 6px; }

/* ---------- toast ---------- */
.toast-wrap {
  position: fixed; right: 16px; bottom: calc(var(--bottomnav-h) + 16px);
  z-index: 200; display: flex; flex-direction: column; gap: 8px;
  align-items: flex-end; max-width: calc(100vw - 32px);
}
@media (min-width: 900px) {
  .toast-wrap { bottom: 20px; }
}
.toast {
  background: var(--text); color: #fff;
  padding: 11px 16px; border-radius: 10px;
  font-size: 14px; box-shadow: var(--shadow-lg);
  opacity: 0; transform: translateY(8px);
  transition: opacity .2s, transform .2s;
  max-width: 100%;
}
.toast.show { opacity: 1; transform: none; }
.toast.toast-danger { background: var(--danger); }
.toast.toast-success { background: var(--primary-dark); }

/* ---------- bottom nav (mobile) ---------- */
.bottom-nav {
  position: fixed; inset: auto 0 0 0;
  height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: #fff; border-top: 1px solid var(--border);
  display: grid; grid-template-columns: repeat(5, 1fr);
  z-index: 60;
  box-shadow: 0 -2px 10px rgba(15, 23, 42, .06);
}
.bottom-nav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 500;
}
.bottom-nav a svg { width: 22px; height: 22px; }
.bottom-nav a.active { color: var(--primary-dark); }

/* ---------- utilities ---------- */
.muted { color: var(--muted); }
.center { text-align: center; }
.hidden { display: none !important; }
.mt-8 { margin-top: 8px; }
.mb-8 { margin-bottom: 8px; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 16px; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.empty { text-align: center; color: var(--muted); padding: 24px 8px; font-size: 14px; }
.link { background: none; border: none; color: var(--primary-dark); cursor: pointer; font-size: 13px; text-decoration: underline; padding: 0; }
.filter-bar { display: grid; grid-template-columns: 1fr; gap: 10px; margin-bottom: 14px; }

.alert {
  border-radius: 10px; padding: 12px 14px; font-size: 14px; margin-bottom: 14px;
}
.alert.danger { background: var(--danger-soft); color: var(--danger); }
.alert.warning { background: var(--warning-soft); color: var(--warning); }
.alert.info { background: var(--info-soft); color: #1d4ed8; }

.warn-list { list-style: none; }
.warn-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 9px 0; border-bottom: 1px dashed var(--border); font-size: 14px;
}
.warn-list li:last-child { border-bottom: none; }
.warn-list .row-sub { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ---------- responsive ---------- */
@media (min-width: 900px) {
  .desktop-nav { display: flex; }
  .bottom-nav { display: none; }
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
  .grid-2 { grid-template-columns: 1fr 1fr; }
  .filter-bar { grid-template-columns: 1fr 1fr 1fr auto; align-items: end; }
}

@media (max-width: 899px) {
  body { padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom) + 16px); }

  /* ตาราง -> การ์ดแบบซ้อน */
  .stack-table-wrap { overflow: visible; }
  table.responsive { display: block; }
  table.responsive thead { display: none; }
  table.responsive tbody { display: block; }
  table.responsive tr {
    display: block;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 6px 12px;
    margin-bottom: 10px;
    background: #fff;
  }
  table.responsive td {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    border-bottom: 1px dashed var(--border);
    padding: 7px 0;
    text-align: right;
  }
  table.responsive td:last-child { border-bottom: none; }
  table.responsive td::before {
    content: attr(data-label);
    font-weight: 600; font-size: 12px; color: var(--muted);
    flex: none;
  }
  table.responsive td.lots-cell { flex-direction: column; align-items: stretch; text-align: left; }
  table.responsive td.lots-cell::before { margin-bottom: 4px; }
  table.responsive tr.lot-row td { text-align: left; }
  table.responsive tr.lot-row td[colspan] { display: block; }
  .lot-item { justify-content: space-between; }
}

@media (min-width: 640px) and (max-width: 899px) {
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
}
/* ---------- เพิ่มเติมสำหรับเวอร์ชัน server-rendered ---------- */
.alert.success { background: var(--ok-soft); color: var(--ok); }
.chip { align-items: center; }
.chip-rm { display: inline-flex; align-items: center; }
.chip .rm { font-size: 16px; padding: 0 2px; }
details > summary.link { list-style: none; cursor: pointer; user-select: none; }
details > summary.link::-webkit-details-marker { display: none; }
details[open] > summary.link { margin-bottom: 8px; }
