/* Aegis admin theme — no external deps */
* { box-sizing: border-box; }
:root {
  --bg: #f1f5f9; --card: #fff; --border: #e2e8f0;
  --text: #0f172a; --muted: #64748b;
  --primary: #2563eb; --primary-dark: #1d4ed8;
  --green: #16a34a; --red: #dc2626; --yellow: #d97706;
  --sidebar: #0f172a;
}
html, body { margin: 0; padding: 0; }
body { font-family: Tahoma, -apple-system, 'Segoe UI', 'Noto Sans Thai', sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }

/* Layout */
.layout { display: flex; min-height: 100vh; }
.sidebar { width: 230px; background: var(--sidebar); color: #e2e8f0; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; flex-shrink: 0; }
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 16px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-mark { width: 38px; height: 38px; border-radius: 10px; flex-shrink: 0; }
.brand-name { font-weight: 800; font-size: 18px; letter-spacing: .5px; }
.brand-sub { font-size: 11px; color: #94a3b8; }
.nav { flex: 1; padding: 12px 8px; overflow-y: auto; }
.nav-group { padding: 14px 12px 4px; font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px; color: #64748b; }
.nav-item { display: flex; align-items: center; gap: 8px; padding: 9px 12px; border-radius: 8px; color: #cbd5e1; text-decoration: none; margin-bottom: 2px; font-size: 13.5px; }
.nav-item:hover { background: rgba(255,255,255,.07); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; }
.sidebar-foot { padding: 12px; border-top: 1px solid rgba(255,255,255,.08); }
.impersonate-box { padding: 8px; margin-bottom: 8px; background: rgba(255,255,255,.05); border-radius: 8px; display: flex; flex-direction: column; gap: 6px; }
.imp-label { font-size: 11px; color: #94a3b8; font-weight: 600; }
.imp-select { width: 100%; font-size: 12px; padding: 5px 6px; border-radius: 6px; border: 1px solid rgba(255,255,255,.15); background: #1e293b; color: #e2e8f0; }
.impersonate-banner { background: #fef3c7; color: #92400e; border-bottom: 1px solid #fde68a; padding: 8px 24px; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.impersonate-banner b { font-weight: 700; }
.user-chip { padding: 8px 10px; margin-bottom: 8px; background: rgba(255,255,255,.05); border-radius: 8px; }
.user-chip-name { font-weight: 700; font-size: 13px; }
.user-chip-role { font-size: 11px; color: #94a3b8; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { background: var(--card); border-bottom: 1px solid var(--border); padding: 14px 24px; position: sticky; top: 0; z-index: 5; flex-shrink: 0; }
.page-title { margin: 0; font-size: 19px; }
.content { padding: 20px 24px; flex: 1 0 auto; display: flex; flex-direction: column; }
.content > .card:has(.table) { flex: 1 0 auto; display: flex; flex-direction: column; }
.content > .card:has(.table) .card-head, .content > .card:has(.table) > .hint { flex-shrink: 0; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 16px 18px; margin-bottom: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; gap: 10px; flex-wrap: wrap; }
.card-head h3 { margin: 0; font-size: 15px; }
.grid { display: grid; gap: 14px; margin-bottom: 16px; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-5 { grid-template-columns: repeat(5, 1fr); }
@media (max-width: 1100px) { .grid-4, .grid-5 { grid-template-columns: repeat(2, 1fr); } .grid-3 { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 760px) {
  .grid-2, .grid-3, .grid-4, .grid-5 { grid-template-columns: 1fr; }
  /* มือถือ: sidebar เปลี่ยนเป็นแถบบน (เมนูชิปเลื่อนแนวนอน มีชื่อเมนูอ่านได้) */
  .layout { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: sticky; top: 0; z-index: 6; flex-direction: row; align-items: center; padding: 6px 8px; gap: 6px; }
  .brand { border-bottom: 0; padding: 0 4px; gap: 6px; flex-shrink: 0; }
  .brand-mark { width: 30px; height: 30px; }
  .brand > div { display: none; }
  .nav { display: flex; flex-direction: row; overflow-x: auto; overflow-y: hidden; flex: 1; padding: 0; gap: 3px; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .nav::-webkit-scrollbar { display: none; }
  .nav-group { display: none; }
  .nav-item { font-size: 12.5px; padding: 7px 11px; margin: 0; white-space: nowrap; flex-shrink: 0; border-radius: 999px; border: 1px solid rgba(255,255,255,.08); }
  .nav-item.active { border-color: transparent; }
  .sidebar-foot { border-top: 0; padding: 0; display: flex; align-items: center; flex-shrink: 0; }
  .user-chip, .impersonate-box { display: none; }
  .sidebar-foot .btn { padding: 7px 10px; font-size: 12px; }
  .content { padding: 12px; }
  .topbar { position: static; padding: 10px 14px; }
  .page-title { font-size: 17px; }
  /* ตารางกว้าง → เลื่อนแนวนอนเฉพาะตาราง หน้าเว็บไม่เลื่อนซ้ายขวาทั้งหน้า */
  .table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table th, .table td { font-size: 12.5px; padding: 7px 6px; }
}
.stat .stat-num { font-size: 26px; font-weight: 800; }
.stat .stat-label { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.stat-sm .stat-num { font-size: 19px; }
.stat-green .stat-num { color: var(--green); }
.stat-red .stat-num { color: var(--red); }
.stat-ok .stat-num { color: var(--green); }
.stat-bad .stat-num { color: var(--red); }
.stat-warn .stat-num { color: var(--yellow); }
.stat-danger .stat-num { color: var(--red); }
.stat-blue .stat-num { color: var(--primary); }
.hint { color: var(--muted); font-size: 12.5px; margin: 6px 0 10px; }
.sub { color: var(--muted); font-size: 11.5px; }
.sel-stat { display: inline-block; margin-left: 6px; padding: 1px 9px; border-radius: 999px; background: #dbeafe; color: #1d4ed8; font-weight: 700; font-size: 12px; white-space: nowrap; }
.th-role { text-align: center; font-size: 11px; line-height: 1.35; }
.td-role { text-align: center; }
.td-role input { width: 16px; height: 16px; accent-color: #2563eb; cursor: pointer; }
.perm-group td { background: #f1f5f9; font-weight: 700; color: var(--muted); font-size: 12.5px; }
.row-gap { display: flex; gap: 10px; align-items: center; }

/* Tables */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; padding: 8px 8px; border-bottom: 2px solid var(--border); white-space: nowrap; }
.table td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table tr:hover td { background: #f8fafc; }
.num { text-align: right; }
.empty { text-align: center; color: var(--muted); padding: 26px !important; }
.nowrap { white-space: nowrap; }
.addr-cell { max-width: 240px; }
.raw-block { background: #0f172a; color: #cbd5e1; border-radius: 8px; padding: 10px 12px; font-size: 12px; white-space: pre-wrap; max-height: 220px; overflow-y: auto; font-family: 'SF Mono', Menlo, monospace; }
.text-danger { color: var(--red); }
.text-warning { color: var(--yellow); }
.text-success { color: var(--green); }
.comm-user { color: var(--primary); font-weight: 600; cursor: pointer; text-decoration: none; }
.comm-user:hover { text-decoration: underline; }

/* Badges */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; white-space: nowrap; }
.badge-green { background: #dcfce7; color: #15803d; }
.badge-red { background: #fee2e2; color: #b91c1c; }
.badge-yellow { background: #fef3c7; color: #b45309; }
.badge-blue { background: #dbeafe; color: #1d4ed8; }
.badge-gray { background: #e2e8f0; color: #475569; }
.badge-purple { background: #ede9fe; color: #6d28d9; }
.status-approved { background: #fef3c7; color: #b45309; }
.status-packed { background: #dbeafe; color: #1d4ed8; }
.status-shipped { background: #e0e7ff; color: #4338ca; }
.status-delivered { background: #dcfce7; color: #15803d; }
.status-tracking { background: #c7f9d9; color: #047857; }
.status-returned { background: #fce7f3; color: #be185d; }
.status-cancelled, .status-rejected { background: #fee2e2; color: #b91c1c; }
.status-draft { background: #e2e8f0; color: #475569; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 5px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: #fff; color: var(--text); font-size: 13px; cursor: pointer; text-decoration: none; font-family: inherit; }
.btn:hover { background: #f1f5f9; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--green); border-color: var(--green); color: #fff; }
.btn-danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-warning { background: #f59e0b; border-color: #f59e0b; color: #fff; }
.btn-warning:hover { background: #d97706; }
.btn-secondary { background: #f1f5f9; }
.btn-ghost { background: transparent; color: #cbd5e1; border-color: rgba(255,255,255,.2); }
.btn-ghost:hover { background: rgba(255,255,255,.08); color: #fff; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-xs { padding: 2px 8px; font-size: 11px; border-radius: 6px; }
.btn-block { width: 100%; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 4px; }
.field span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field input, .field select, .field textarea, .filter-bar input, .filter-bar select {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; font-family: inherit; background: #fff; color: var(--text);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.span-2 { grid-column: 1 / -1; }
@media (max-width: 760px) { .form-grid { grid-template-columns: 1fr; } }
.filter-bar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filter-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 0 14px 12px; }
.filter-row input[type=date], .filter-row select {
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; font-size: 13px; font-family: inherit; background: #fff; color: var(--text);
}
.pagination { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 14px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, .55); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 640px; max-height: 90vh; display: flex; flex-direction: column; overflow: hidden; }
.modal.modal-lg { max-width: 900px; }
.modal-head { flex: 0 0 auto; display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 16px 18px; }
.modal .modal-body { flex: 1 1 auto; overflow-y: auto; min-height: 0; }
.modal-foot { flex: 0 0 auto; display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }

/* Alerts & toast */
.alert { padding: 10px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 10px; }
.alert-danger { background: #fee2e2; color: #b91c1c; }
.alert-info { background: #dbeafe; color: #1d4ed8; }
.hidden { display: none !important; }
#toast-root { position: fixed; bottom: 20px; right: 20px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.25); animation: slidein .25s; }
@keyframes slidein { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Import page */
.dropzone { border: 2px dashed #cbd5e1; border-radius: 10px; padding: 26px; text-align: center; color: var(--muted); cursor: pointer; background: #f8fafc; font-size: 14px; }
.dropzone.over { border-color: var(--primary); background: #eff6ff; }
.table-edit td { padding: 4px 6px; }
.cell { border: 1px solid transparent; border-radius: 5px; padding: 5px 7px; font-size: 12.5px; width: 100%; background: transparent; font-family: inherit; }
.cell:hover, .cell:focus { border-color: #cbd5e1; background: #fff; outline: none; }
.cell-sm { width: 84px; }
.cell-sku { width: 92px; border: 1px solid var(--border); background: #fff; }
.cell-wide { min-width: 200px; }
.cell-addr { min-width: 160px; border: 1px solid var(--border); background: #fff; }
.combo { position: relative; display: inline-block; width: 100%; vertical-align: top; }
.combo-list { position: absolute; top: calc(100% + 2px); left: 0; min-width: 240px; max-height: 260px; overflow-y: auto; background: #fff; border: 1px solid #cbd5e1; border-radius: 8px; box-shadow: 0 10px 30px rgba(15,23,42,.18); z-index: 60; }
.combo-item { padding: 7px 10px; font-size: 12.5px; color: #1e293b; cursor: pointer; line-height: 1.35; }
.combo-item:hover { background: #eff6ff; color: #1d4ed8; }
.combo-hint { color: var(--muted); cursor: default; font-style: italic; }
.combo-hint:hover { background: transparent; color: var(--muted); }
.cell-block { display: flex; flex-direction: column; gap: 3px; min-width: 200px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg-btn { padding: 7px 14px; background: #fff; border: none; font-size: 13px; cursor: pointer; color: var(--muted); font-family: inherit; }
.seg-btn.active { background: var(--primary); color: #fff; }
.slip-preview { display: flex; align-items: flex-start; gap: 10px; margin-top: 4px; }
.slip-preview img { width: 120px; max-height: 120px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.addr-suggest { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.addr-suggest select { border: 1px solid var(--border); border-radius: 6px; padding: 3px 6px; font-size: 11.5px; max-width: 280px; background: #fff; }
.row-ok td { background: #f0fdf4; }
.row-warn td { background: #fffbeb; }
.row-bad td { background: #fef2f2; }
.row-purple td { background: #f3e8ff; }
.row-warns { font-size: 11px; color: var(--muted); margin-top: 3px; line-height: 1.4; }
.ws-table { margin: 0; }
.ws-table th { text-transform: none; letter-spacing: normal; font-size: 12px; }
.ws-table td { vertical-align: middle; }
.ws-table .ws-on { width: 44px; text-align: center; }
.ws-table .ws-color { width: 110px; }
.ws-table .ws-block { width: 200px; }
.ws-topic { font-weight: 600; font-size: 13.5px; }
.ws-topic + .sub { margin-top: 1px; }
.ws-table select { width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 12.5px; background: #fff; color: var(--text); }
.tier-chip { display: inline-block; background: #eff6ff; color: #1d4ed8; border-radius: 6px; padding: 2px 8px; font-size: 12px; margin: 2px 4px 2px 0; }
.att-chip { display: inline-flex; align-items: center; gap: 4px; background: #f8fafc; border: 1px solid var(--border); color: #475569; border-radius: 6px; padding: 1px 6px; font-size: 11px; margin: 1px 3px 1px 0; }
.att-x { color: #b91c1c; cursor: pointer; font-weight: 700; text-decoration: none; }
.att-x:hover { color: #7f1d1d; }
.report-table input.cell { background: #fff; border: 1px solid var(--border); }
.gift-chip { background: #fdf2f8; color: #be185d; }
.tier-row { display: flex; gap: 6px; margin-bottom: 6px; }
.tier-row .cell { border: 1px solid var(--border); width: 100px; }
.tier-row .gift-sel { flex: 1; min-width: 150px; }
.quick-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ใบกำกับภาษี (VAT bill) */
.invoice-doc { max-width: 820px; margin: 0 auto; font-size: 14px; }
.inv-company { font-size: 22px; font-weight: 800; }
.inv-sub { font-size: 13px; color: #334155; margin-top: 2px; }
.inv-meta { min-width: 320px; }
.inv-row { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border-bottom: 1px dotted var(--border); font-size: 13px; }
.inv-row span { color: var(--muted); }
.inv-table { margin: 16px 0; width: 100%; }
.inv-table th, .inv-table td { border: 1px solid var(--border) !important; padding: 6px 8px; }
.inv-table .num { text-align: right; }
.inv-table .empty-row { height: 26px; }
.inv-sum { min-width: 260px; }
.inv-total { font-size: 15px; font-weight: 800; border-top: 2px solid #0f172a; }
.inv-sign { display: flex; justify-content: space-between; gap: 16px; margin-top: 28px; text-align: center; }
.inv-sign > div { flex: 1; }
.inv-pay { margin-top: 14px; font-size: 13px; }
@media print {
  .sidebar, .topbar, .impersonate-banner, #toast-root, .btn { display: none !important; }
  .main { margin: 0; }
  .content { padding: 0; }
  .invoice-doc { box-shadow: none; border: none; max-width: 100%; }
}

/* Login */
.login-body { background: linear-gradient(135deg, #0f172a, #1e3a8a); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: #fff; border-radius: 14px; padding: 32px 36px; width: 100%; max-width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
@media (max-width: 420px) { .login-card { padding: 24px 18px; } .login-logo { width: 72px; height: 72px; } }
.login-brand { font-size: 26px; text-align: center; }
.login-logo { width: 92px; height: 92px; border-radius: 22px; display: block; margin: 0 auto 12px; box-shadow: 0 8px 24px rgba(37,99,235,.35); }
.login-sub { text-align: center; color: var(--muted); font-size: 13px; margin: 6px 0 20px; }
.login-card .field { margin-bottom: 14px; }
.login-card input { padding: 11px 12px; font-size: 14px; }

/* ---- modal ในหน้าเว็บ (แทน alert/confirm) ---- */
.modal-overlay { position: fixed; inset: 0; background: rgba(15, 23, 42, .55); display: flex; align-items: center; justify-content: center; z-index: 9999; padding: 16px; }
.modal-box { background: #fff; border-radius: 14px; max-width: 440px; width: 100%; padding: 20px 20px 16px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal-box .modal-body { font-size: 14.5px; line-height: 1.6; color: #1e293b; white-space: pre-wrap; word-break: break-word; max-height: 60vh; overflow-y: auto; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
