/* ============================================================
   WMS ERP - Main CSS
   ============================================================ */

:root {
  --primary:    #1a56db;
  --primary-dk: #1347b0;
  --success:    #057a55;
  --danger:     #e02424;
  --warning:    #d97706;
  --text:       #111827;
  --muted:      #6b7280;
  --border:     #e5e7eb;
  --bg:         #f3f4f6;
  --card:       #ffffff;
  --sidebar-w:  240px;
  --topbar-h:   56px;
}

* { box-sizing: border-box; }
body {
  font-family: 'Inter', 'Noto Sans Thai', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  margin: 0;
}

/* ============================================================
   TOPBAR
   ============================================================ */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1030;
  height: var(--topbar-h);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; padding: 0 20px;
  gap: 12px; box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.topbar .brand {
  font-weight: 700; font-size: 16px; color: var(--primary);
  text-decoration: none; white-space: nowrap;
}
.topbar .spacer { flex: 1; }
.topbar .user-name { font-weight: 500; font-size: 13px; }

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
  position: fixed; top: var(--topbar-h); left: 0; bottom: 0;
  width: var(--sidebar-w); overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: #1e293b; z-index: 1020;
  transition: transform .25s ease;
}
.sidebar-section { padding: 4px 0; }
.sidebar-label {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: #94a3b8;
  padding: 8px 14px 8px 16px;
}
.sidebar-section.collapsible .sidebar-label {
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer; border-radius: 4px; margin: 0 6px;
  transition: background .15s;
}
.sidebar-section.collapsible .sidebar-label:hover {
  background: rgba(255,255,255,.07); color: #e2e8f0;
}
.sidebar-chevron {
  font-size: 11px; color: #64748b; transition: transform .2s; flex-shrink: 0;
}
.sidebar-links { display: none; overflow: hidden; }
.sidebar-link {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 16px; color: #cbd5e1; text-decoration: none;
  font-size: 13.5px; border-radius: 0; transition: background .15s;
}
.sidebar-link:hover { background: rgba(255,255,255,.07); color: #fff; }
.sidebar-link.active { background: var(--primary); color: #fff; }
.sidebar-link i { width: 18px; text-align: center; font-size: 15px; }
.sidebar-sub { padding-left: 44px; }
.sidebar-sub .sidebar-link { font-size: 13px; padding: 7px 16px; }

/* ============================================================
   MAIN CONTENT
   ============================================================ */
.main-content {
  margin-left: var(--sidebar-w);
  padding-top: var(--topbar-h);
  min-height: 100vh;
}
.page-wrapper { padding: 24px; }

/* ============================================================
   PAGE HEADER
   ============================================================ */
.page-header {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 20px; flex-wrap: wrap;
}
.page-header h1 {
  font-size: 20px; font-weight: 700; margin: 0; flex: 1;
  color: var(--text);
}
.breadcrumb { font-size: 12px; color: var(--muted); }

/* ============================================================
   CARDS
   ============================================================ */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
.card-title {
  font-size: 15px; font-weight: 600; margin-bottom: 16px;
  padding-bottom: 12px; border-bottom: 1px solid var(--border);
}
.stat-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 16px 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
.stat-card .label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.stat-card .value { font-size: 22px; font-weight: 700; color: var(--text); }
.stat-card .sub   { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ============================================================
   TABLES
   ============================================================ */
.table-wrapper { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th {
  background: #f8fafc; font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  padding: 10px 14px; border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
.table td {
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tbody tr:hover { background: #f8fafc; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
/* ตารางมีขอบทุกด้าน (items + totals ในบิล) */
.table-bordered th,
.table-bordered td {
  border: 1px solid #d1d5db !important;
}
.table-bordered th {
  border-bottom-width: 2px !important;
  background: #f0f4ff;
}
.totals-4col.totals-bordered td {
  border: 1px solid #d1d5db;
}
.totals-4col.totals-bordered .t4-grand td {
  border-top: 2px solid #1a56db !important;
}

/* ============================================================
   FORMS
   ============================================================ */
.form-label { font-size: 13px; font-weight: 500; margin-bottom: 4px; display: block; }
.form-control, .form-select {
  width: 100%; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px; color: var(--text); background: var(--card);
  transition: border-color .15s, box-shadow .15s;
}
.form-control:focus, .form-select:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26,86,219,.12);
}
.form-control.is-invalid { border-color: var(--danger); }
.input-group { display: flex; }
.input-group .form-control { border-radius: 6px 0 0 6px; flex: 1; }
.input-group .btn { border-radius: 0 6px 6px 0; }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: 6px; font-size: 13.5px;
  font-weight: 500; cursor: pointer; border: 1px solid transparent;
  transition: all .15s; text-decoration: none; white-space: nowrap;
}
.btn-primary   { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dk); border-color: var(--primary-dk); color: #fff; }
.btn-success   { background: var(--success); color: #fff; border-color: var(--success); }
.btn-danger    { background: var(--danger);  color: #fff; border-color: var(--danger); }
.btn-warning   { background: var(--warning); color: #fff; border-color: var(--warning); }
.btn-secondary { background: #6b7280; color: #fff; border-color: #6b7280; }
.btn-outline   { background: transparent; color: var(--primary); border-color: var(--primary); }
.btn-outline:hover { background: var(--primary); color: #fff; }
.btn-light     { background: #f3f4f6; color: var(--text); border-color: var(--border); }
.btn-light:hover { background: #e5e7eb; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11px; }

/* ============================================================
   BADGES
   ============================================================ */
.badge {
  display: inline-block; padding: 3px 8px; border-radius: 20px;
  font-size: 11px; font-weight: 600;
}
.badge-primary   { background: #dbeafe; color: #1e40af; }
.badge-success   { background: #d1fae5; color: #065f46; }
.badge-danger    { background: #fee2e2; color: #991b1b; }
.badge-warning   { background: #fef3c7; color: #92400e; }
.badge-secondary { background: #f3f4f6; color: #374151; }
.badge-info      { background: #e0f2fe; color: #0c4a6e; }

/* ============================================================
   CREDIT INFO BAR
   ============================================================ */
.credit-bar {
  display: flex; gap: 20px; flex-wrap: wrap;
  background: #f8fafc; border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 16px;
}
.credit-bar .ci { text-align: center; }
.credit-bar .ci .lbl { font-size: 11px; color: var(--muted); }
.credit-bar .ci .val { font-size: 16px; font-weight: 700; }
.credit-bar .ci .val.over { color: var(--danger); }
.credit-bar .ci .val.ok   { color: var(--success); }

/* ============================================================
   LINE ITEMS TABLE
   ============================================================ */
.line-table th, .line-table td { padding: 8px 10px; vertical-align: middle; }
.line-table tfoot td { font-weight: 600; background: #f8fafc; }
.line-table .del-row { cursor: pointer; color: var(--danger); }
.line-table .del-row:hover { opacity: .7; }

/* ============================================================
   TOAST
   ============================================================ */
.toast-container {
  position: fixed; top: 70px; right: 20px; z-index: 9999;
  display: flex; flex-direction: column; gap: 8px;
}
.toast {
  min-width: 280px; max-width: 360px;
  padding: 12px 16px; border-radius: 8px;
  font-size: 13.5px; font-weight: 500;
  display: flex; align-items: center; gap: 10px;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
  animation: slideIn .2s ease;
}
.toast-success { background: #d1fae5; color: #065f46; border-left: 4px solid var(--success); }
.toast-danger  { background: #fee2e2; color: #991b1b; border-left: 4px solid var(--danger); }
.toast-info    { background: #e0f2fe; color: #0c4a6e; border-left: 4px solid #0ea5e9; }
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ============================================================
   MODAL
   ============================================================ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  z-index: 2000; display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-box {
  background: var(--card); border-radius: 12px;
  width: 100%; max-width: 520px;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.modal-box .modal-header {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-box .modal-header h5 { margin: 0; font-weight: 600; font-size: 15px; }
.modal-box .modal-body  { padding: 20px; }
.modal-box .modal-footer {
  padding: 12px 20px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}

/* ============================================================
   MISC
   ============================================================ */
.divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
.text-muted { color: var(--muted) !important; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.fw-600 { font-weight: 600; }
.gap-2 { gap: 8px; }
.d-flex { display: flex; }
.align-center { align-items: center; }
.flex-1 { flex: 1; }
.w-100 { width: 100%; }

@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); transition: transform .25s ease; padding-bottom: env(safe-area-inset-bottom, 24px); }
  .sidebar.open { transform: translateX(0); }
  .main-content { margin-left: 0; }
}

/* Sidebar overlay (mobile) */
#sidebar-overlay {
  display: none;
  position: fixed; inset: 0; z-index: 1019;
  background: rgba(0,0,0,.4);
}
#sidebar-overlay.show { display: block; }

/* ============================================================
   USER DROPDOWN (topbar)
   ============================================================ */
.user-dropdown { position: relative; }

.user-dropdown-btn {
  display: flex; align-items: center; gap: 7px;
  background: none; border: none; cursor: pointer;
  font-size: 13px; font-weight: 500; color: var(--text);
  padding: 5px 10px; border-radius: 8px;
  transition: background .15s;
}
.user-dropdown-btn:hover { background: #f1f5f9; }
.user-dropdown-btn .bi-person-circle { font-size: 17px; color: #9ca3af; }
.user-dropdown-chevron { font-size: 11px; color: #9ca3af; transition: transform .2s; }
.user-dropdown.open .user-dropdown-chevron { transform: rotate(180deg); }

.user-dropdown-menu {
  display: none;
  position: absolute; top: calc(100% + 8px); right: 0;
  min-width: 220px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
  z-index: 9999;
  overflow: hidden;
  animation: dropdownFadeIn .15s ease;
}
.user-dropdown.open .user-dropdown-menu { display: block; }

@keyframes dropdownFadeIn {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.user-dropdown-header {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px 12px;
  background: #f8fafc;
}
.user-dropdown-avatar { font-size: 32px; color: #9ca3af; line-height: 1; }
.user-dropdown-name { font-size: 14px; font-weight: 600; color: #1e293b; }
.user-dropdown-company { font-size: 12px; color: #6b7280; margin-top: 2px; }

.user-dropdown-divider { height: 1px; background: #f1f5f9; }

.user-dropdown-item {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 16px;
  font-size: 13px; font-weight: 500; color: #374151;
  text-decoration: none;
  transition: background .12s;
}
.user-dropdown-item:hover { background: #f8fafc; color: #1a56db; }
.user-dropdown-item--danger { color: #dc2626; }
.user-dropdown-item--danger:hover { background: #fef2f2; color: #b91c1c; }
.user-dropdown-item .bi { font-size: 15px; width: 18px; text-align: center; }

/* ——— Entity Select v2 — Mobile-friendly searchable single-select ——— */
.ess-wrap { position: relative; }

/* Trigger button — looks like a <select> */
.ess-trigger {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; padding: 8px 12px;
  background: #fff; border: 1px solid #d1d5db; border-radius: 8px;
  font-size: 14px; color: #111827; cursor: pointer; text-align: left;
  transition: border-color .15s, box-shadow .15s;
  min-height: 42px;
}
.ess-trigger:hover  { border-color: #9ca3af; }
.ess-trigger:focus  { outline: none; border-color: #1a56db; box-shadow: 0 0 0 3px rgba(26,86,219,.15); }
.ess-open .ess-trigger { border-color: #1a56db; box-shadow: 0 0 0 3px rgba(26,86,219,.15); border-bottom-left-radius: 0; border-bottom-right-radius: 0; }

.ess-trigger-text { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ess-placeholder  { color: #9ca3af; }
.ess-chevron      { flex-shrink: 0; font-size: 12px; color: #6b7280; margin-left: 8px; transition: transform .2s; }
.ess-open .ess-chevron { transform: rotate(180deg); }

/* Dropdown panel */
.ess-panel {
  display: none;
  position: absolute; left: 0; right: 0; z-index: 1050;
  background: #fff;
  border: 1px solid #1a56db; border-top: none;
  border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
}

/* Search row inside panel */
.ess-search-row {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-bottom: 1px solid #e5e7eb;
}
.ess-search-icon { color: #9ca3af; flex-shrink: 0; font-size: 14px; }
.ess-search-input {
  flex: 1; border: none; outline: none; font-size: 14px; background: transparent;
  color: #111827; min-width: 0;
}
.ess-search-input::placeholder { color: #9ca3af; }
.ess-search-clr {
  display: none; align-items: center; justify-content: center;
  background: none; border: none; color: #9ca3af; font-size: 17px;
  width: 22px; height: 22px; border-radius: 50%; cursor: pointer; padding: 0; flex-shrink: 0;
  transition: background .12s, color .12s;
}
.ess-search-clr:hover { background: #f3f4f6; color: #374151; }

/* Results list */
.ess-list { max-height: 220px; overflow-y: auto; }
.ess-hint { padding: 12px 14px; font-size: 13px; color: #9ca3af; text-align: center; }
.ess-hint--err { color: #dc2626; }
.ess-item {
  padding: 10px 14px; cursor: pointer;
  border-bottom: 1px solid #f3f4f6; outline: none;
}
.ess-item:last-child { border-bottom: none; }
.ess-item:hover, .ess-item:focus { background: #eef2ff; }
.ess-item-name { font-size: 13px; font-weight: 500; color: #111827; }
.ess-item-sub  { font-size: 11px; color: #6b7280; margin-top: 2px; }
.ess-item-addr { font-size: 11px; color: #9ca3af; margin-top: 2px; font-style: italic; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 360px; }
.ess-addr-info { font-size: 11px; color: #6b7280; margin-top: 4px; padding: 3px 8px; background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 4px; font-style: italic; }

/* ——— EntitySelect Mobile Bottom-Sheet ——— */
.ess-backdrop {
  display: none; position: fixed; inset: 0; z-index: 1040;
  background: rgba(0,0,0,.4);
}
.ess-backdrop.ess-active { display: block; }
@media (max-width: 639px) {
  .ess-open .ess-panel {
    position: fixed !important;
    left: 0 !important; right: 0 !important;
    bottom: 0 !important; top: auto !important;
    border-radius: 16px 16px 0 0 !important;
    border: none !important; border-top: 1px solid #e5e7eb !important;
    box-shadow: 0 -4px 32px rgba(0,0,0,.18) !important;
    z-index: 1045 !important;
    max-height: 70vh;
    display: flex !important; flex-direction: column;
  }
  .ess-open .ess-list { flex: 1; max-height: none; }
  .ess-item { padding: 14px 16px; font-size: 15px; }
  .ess-item-name { font-size: 14px; }
  .ess-search-input { font-size: 15px; }
  /* Handle bar at top of bottom sheet */
  .ess-panel::before {
    content: ''; display: block;
    width: 40px; height: 4px; border-radius: 2px;
    background: #d1d5db; margin: 10px auto 4px;
    flex-shrink: 0;
  }
}

/* ——— Customer Credit Info Bar ——— */
.credit-info-bar {
  display: none; flex-wrap: wrap; gap: 12px 20px; align-items: center;
  padding: 8px 14px; border-radius: 8px; font-size: 13px;
  margin-bottom: 12px;
  background: #f9fafb; border: 1px solid #e5e7eb; color: #374151;
}
.credit-info-bar.cib--ok  { background: #f0fdf4; border-color: #bbf7d0; color: #166534; }
.credit-info-bar.cib--over { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
.credit-info-bar .cib-item { display: inline-flex; align-items: center; gap: 5px; }
#cib-over { display: none; align-items: center; gap: 5px; font-weight: 600; color: #dc2626; }

/* ——— 4-Column Totals Table ——— */
.totals-4col { width:100%; border-collapse:collapse; }
.totals-4col td { vertical-align:middle; }
.t4-note-cell {
  vertical-align: top; padding: 12px 14px;
  border-right: 1px solid #e5e7eb; background: #fafafa;
}
.t4-label { padding: 6px 12px; color: #6b7280; font-size: 13px; white-space: nowrap; }
.t4-input { padding: 4px 8px; }
.t4-amount { padding: 6px 14px; text-align: right; font-weight: 600; font-size: 14px; white-space: nowrap; }
.t4-grand { font-size: 18px; font-weight: 700; border-top: 2px solid #e5e7eb; background: #f9fafb; }
.t4-grand .t4-label { font-size: 16px; font-weight: 700; color: #111827; }

/* Credit bar inside card */
.card .credit-info-bar { margin: 8px 0 4px; }

/* ——— Product Smart Dropdown ——— */
.pd-group {
  padding: 9px 12px; cursor: pointer;
  border-bottom: 1px solid #f3f4f6; background: #fff;
}
.pd-group:hover { background: #eff6ff; }
.pd-item {
  padding: 8px 12px 8px 22px; cursor: pointer;
  border-bottom: 1px solid #f3f4f6; font-size: 13px;
}
.pd-item:hover { background: #eef2ff; }
.pd-variant {
  padding-left: 20px !important;
  background: #fafbff;
  border-left: 3px solid #dbeafe;
}
.pd-variant:hover { background: #eff6ff; }
.drop-item:hover { background: #eef2ff; }
