:root {
  --navy: #0f172a;
  --blue: #2563eb;
  --blue-dark: #1e40af;
  --blue-light: #dbeafe;
  --green: #16a34a;
  --amber: #d97706;
  --red: #dc2626;
  --gray-50: #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-400: #94a3b8;
  --gray-500: #64748b;
  --gray-700: #334155;
  --gray-900: #0f172a;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15,23,42,.08), 0 1px 2px rgba(15,23,42,.04);
  --shadow-md: 0 4px 12px rgba(15,23,42,.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: 'Noto Sans Thai', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--gray-50);
  color: var(--gray-900);
  -webkit-font-smoothing: antialiased;
}
* { -webkit-tap-highlight-color: transparent; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }
a { text-decoration: none; color: inherit; }

/* ---------- App Shell ---------- */
#app { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  background: var(--navy);
  color: #fff;
  box-shadow: var(--shadow-md);
}
.topbar .brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.topbar .brand .box-icon {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--blue), var(--blue-dark));
  display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 800;
}
.topbar .actions { display: flex; align-items: center; gap: 10px; }
.topbar .icon-btn {
  background: rgba(255,255,255,.08); border: none; color: #fff;
  width: 34px; height: 34px; border-radius: 8px; display: flex; align-items: center; justify-content: center;
  font-size: 16px; position: relative;
}
.topbar .icon-btn .dot {
  position: absolute; top: 4px; right: 4px; width: 8px; height: 8px; border-radius: 50%;
  background: var(--red); border: 2px solid var(--navy);
}
.topbar .icon-btn.logout-btn { width: auto; padding: 0 12px; gap: 6px; font-size: 14px; }
.topbar .icon-btn.logout-btn .logout-label { font-size: 12px; font-weight: 600; }
@media (max-width: 640px) { .topbar .icon-btn.logout-btn .logout-label { display: none; } .topbar .icon-btn.logout-btn { width: 34px; padding: 0; } }
.gsearch-wrap { position: relative; flex: 1; max-width: 360px; margin: 0 12px; }
.gsearch-wrap input {
  width: 100%; padding: 8px 12px 8px 32px; border-radius: 20px; border: 1px solid rgba(255,255,255,.15);
  background: rgba(255,255,255,.08); color: #fff; font-size: 13px;
}
.gsearch-wrap input::placeholder { color: rgba(255,255,255,.5); }
.gsearch-wrap input:focus { outline: none; background: rgba(255,255,255,.16); }
.gsearch-wrap .gs-ic { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); font-size: 12px; opacity: .6; }
.gsearch-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0; background: #fff; border: 1px solid var(--gray-200);
  border-radius: 10px; box-shadow: var(--shadow-md); max-height: 340px; overflow-y: auto; z-index: 60; color: var(--gray-900);
}
.gsearch-group-title { font-size: 10px; text-transform: uppercase; font-weight: 700; color: var(--gray-400); padding: 8px 12px 2px; }
.gsearch-item { padding: 8px 12px; display: flex; align-items: center; justify-content: space-between; cursor: pointer; font-size: 13px; gap: 8px; }
.gsearch-item:hover { background: var(--gray-50); }
.gsearch-empty { padding: 14px; color: var(--gray-400); font-size: 12px; text-align: center; }
@media (max-width: 860px) { .gsearch-wrap { display: none; } }

.topbar .user-chip {
  display: flex; align-items: center; gap: 6px; font-size: 13px;
  background: rgba(255,255,255,.08); padding: 6px 10px; border-radius: 20px;
}
.badge-role { font-size: 10px; padding: 1px 6px; border-radius: 8px; background: var(--blue); }

.layout { display: flex; flex: 1; min-height: 0; }

.sidebar {
  width: 220px; flex-shrink: 0; background: #fff; border-right: 1px solid var(--gray-200);
  padding: 14px 10px; display: flex; flex-direction: column; gap: 2px;
  position: sticky; top: 53px; align-self: flex-start; height: calc(100vh - 53px); overflow-y: auto;
}
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px;
  color: var(--gray-700); font-size: 14px; font-weight: 500; border: none; background: none; width: 100%; text-align: left;
}
.nav-item .ic { font-size: 16px; width: 20px; text-align: center; }
.nav-item:hover { background: var(--gray-100); }
.nav-item.active { background: var(--blue-light); color: var(--blue-dark); font-weight: 700; }
.nav-section-title { font-size: 11px; text-transform: uppercase; color: var(--gray-400); font-weight: 700; padding: 12px 12px 4px; }

.main { flex: 1; padding: 18px; min-width: 0; padding-bottom: 80px; }

.bottom-nav {
  display: none;
  position: sticky; bottom: 0; z-index: 20;
  background: #fff; border-top: 1px solid var(--gray-200);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  justify-content: space-around;
}
.bottom-nav .nav-item { flex-direction: column; gap: 2px; font-size: 10px; padding: 6px 4px; border-radius: 8px; }
.bottom-nav .nav-item .ic { font-size: 18px; }

@media (max-width: 860px) {
  .sidebar { display: none; }
  .bottom-nav { display: flex; }
  .main { padding: 12px; padding-bottom: 76px; }
}

/* ---------- Page header ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.page-head h1 { font-size: 20px; margin: 0; }
.page-head p { margin: 2px 0 0; color: var(--gray-500); font-size: 13px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; border-radius: 9px;
  border: 1px solid var(--gray-200); background: #fff; color: var(--gray-700); font-size: 13px; font-weight: 600;
  transition: .15s;
}
.btn:hover { background: var(--gray-100); }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dark); }
.btn-danger { background: #fff; border-color: var(--red); color: var(--red); }
.btn-danger:hover { background: #fef2f2; }
.btn-success { background: var(--green); border-color: var(--green); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-icon { width: 32px; height: 32px; padding: 0; justify-content: center; }

/* ---------- Cards / Grid ---------- */
.grid { display: grid; gap: 14px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 900px) { .grid-4 { grid-template-columns: repeat(2,1fr); } .grid-3 { grid-template-columns: repeat(2,1fr); } .grid-2 { grid-template-columns: 1fr; } }
@media (max-width: 520px) { .grid-4 { grid-template-columns: 1fr 1fr; } }

.card { background: #fff; border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); border: 1px solid var(--gray-200); }
.stat-card { display: flex; flex-direction: column; gap: 6px; }
.stat-card .label { font-size: 12px; color: var(--gray-500); font-weight: 600; }
.stat-card .value { font-size: 26px; font-weight: 800; }
.stat-card .sub { font-size: 11px; color: var(--gray-400); }
.stat-card.accent-blue .value { color: var(--blue); }
.stat-card.accent-green .value { color: var(--green); }
.stat-card.accent-amber .value { color: var(--amber); }
.stat-card.accent-red .value { color: var(--red); }

.card-title { font-size: 14px; font-weight: 700; margin: 0 0 12px; display: flex; align-items: center; justify-content: space-between; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data-table th { text-align: left; padding: 10px 12px; color: var(--gray-500); font-weight: 600; font-size: 12px; border-bottom: 1px solid var(--gray-200); white-space: nowrap; }
table.data-table td { padding: 10px 12px; border-bottom: 1px solid var(--gray-100); vertical-align: middle; }
table.data-table tr:hover td { background: var(--gray-50); }
table.data-table td.actions-cell { display: flex; gap: 6px; }

.pill { display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 700; }
.pill-blue { background: var(--blue-light); color: var(--blue-dark); }
.pill-green { background: #dcfce7; color: #15803d; }
.pill-amber { background: #fef3c7; color: #92400e; }
.pill-red { background: #fee2e2; color: #b91c1c; }
.pill-gray { background: var(--gray-100); color: var(--gray-500); }

/* ---------- Forms ---------- */
.form-row { margin-bottom: 12px; display: flex; flex-direction: column; gap: 5px; }
.form-row label { font-size: 12px; font-weight: 700; color: var(--gray-700); }
.form-row input, .form-row select, .form-row textarea {
  padding: 9px 11px; border: 1px solid var(--gray-200); border-radius: 8px; font-size: 13px; background: #fff; color: var(--gray-900);
  width: 100%;
}
.form-row input:focus, .form-row select:focus, .form-row textarea:focus { outline: 2px solid var(--blue-light); border-color: var(--blue); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
@media (max-width: 600px) { .form-grid { grid-template-columns: 1fr; } }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.search-box { position: relative; flex: 1; min-width: 180px; }
.search-box input { width: 100%; padding: 9px 12px 9px 32px; border: 1px solid var(--gray-200); border-radius: 8px; font-size: 13px; }
.search-box .ic { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--gray-400); font-size: 13px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.5); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center; padding: 30px 14px; overflow-y: auto;
}
.modal { background: #fff; border-radius: var(--radius); width: 100%; max-width: 560px; padding: 20px; box-shadow: var(--shadow-md); }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h2 { font-size: 16px; margin: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #0f172a, #1e3a8a);
  padding: 20px;
}
.login-card { background: #fff; border-radius: 18px; padding: 32px; width: 100%; max-width: 380px; box-shadow: var(--shadow-md); }
.login-logo { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 20px; }
.login-logo .box-icon { width: 54px; height: 54px; border-radius: 14px; background: linear-gradient(135deg, var(--blue), var(--blue-dark)); display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 800; font-size: 20px; }
.login-logo h1 { margin: 0; font-size: 20px; }
.login-logo p { margin: 0; font-size: 12px; color: var(--gray-500); }
.role-select { display: flex; gap: 8px; margin-bottom: 14px; }
.role-select button { flex: 1; padding: 10px; border-radius: 9px; border: 1px solid var(--gray-200); background: #fff; font-size: 13px; font-weight: 700; color: var(--gray-500); }
.role-select button.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.demo-hint { font-size: 11px; color: var(--gray-400); text-align: center; margin-top: 10px; }

/* ---------- LINE simulation ---------- */
.line-phone { max-width: 380px; margin: 0 auto; border-radius: 24px; overflow: hidden; box-shadow: var(--shadow-md); border: 8px solid #111; background: #9db6c9; }
.line-header { background: #06c755; color: #fff; padding: 12px 14px; display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 14px; }
.line-body { padding: 14px; min-height: 340px; display: flex; flex-direction: column; gap: 10px; background: #d3e4ec; }
.line-msg { max-width: 80%; padding: 10px 12px; border-radius: 14px; font-size: 13px; line-height: 1.4; }
.line-msg.bot { background: #fff; align-self: flex-start; border-bottom-left-radius: 4px; }
.line-msg.user { background: #a6f0b6; align-self: flex-end; border-bottom-right-radius: 4px; }
.line-msg .meta { font-size: 10px; color: var(--gray-400); margin-top: 4px; }
.line-quick { display: flex; gap: 8px; flex-wrap: wrap; padding: 10px 14px; background: #fff; border-top: 1px solid var(--gray-200); }
.line-quick button { border: 1px solid var(--blue); color: var(--blue); background: #fff; border-radius: 20px; padding: 6px 12px; font-size: 12px; font-weight: 700; }

/* ---------- QR tag ---------- */
.tag-card { border: 2px dashed var(--gray-200); border-radius: var(--radius); padding: 16px; text-align: center; background: #fff; }
.tag-card canvas, .tag-card img { margin: 0 auto; }
.tag-code { font-family: monospace; font-weight: 700; margin-top: 8px; }

/* ---------- Empty state ---------- */
.empty-state { text-align: center; padding: 40px 20px; color: var(--gray-400); }
.empty-state .ic { font-size: 34px; margin-bottom: 8px; }

/* ---------- Toast ---------- */
#toast-wrap { position: fixed; bottom: 16px; right: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--gray-900); color: #fff; padding: 10px 16px; border-radius: 9px; font-size: 13px; box-shadow: var(--shadow-md); animation: toast-in .2s ease; max-width: 320px; }
.toast.success { background: #15803d; }
.toast.error { background: #b91c1c; }
@keyframes toast-in { from { opacity: 