/* ===== CRM Theme — InApp Dashboard Style ===== */
:root {
  --brand:       #E66239;
  --brand-dark:  #cc5530;
  --brand-light: #fff4f0;
  --success:     #00C951;
  --danger:      #FB2C36;
  --warning:     #F0B100;
  --info:        #00B8DB;
  --bg:          #f5f5f5;
  --card:        #ffffff;
  --text:        #171717;
  --text-muted:  #737373;
  --border:      #e5e5e5;
  --shadow:      0 1px 4px rgba(0,0,0,.06);
  --shadow-lg:   0 8px 24px rgba(0,0,0,.08);
  --topbar-h:    60px;
  --sidebar-w:   260px;
  --sidebar-collapsed-w: 68px;
}

* { -webkit-tap-highlight-color: transparent; box-sizing: border-box; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Poppins', 'Noto Sans Lao', -apple-system, 'Segoe UI', sans-serif;
  font-size: .875rem;
  margin: 0;
}

/* ===== Overlay (mobile) ===== */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 190;
  display: none;
}
.overlay.show { display: block; }

/* ===== Topbar ===== */
.topbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  z-index: 200;
  transition: padding-left .25s;
}
.topbar .brand {
  font-weight: 700; font-size: 1.1rem;
  color: var(--brand);
  display: flex; align-items: center; gap: .6rem;
  text-decoration: none;
}
.topbar .brand-logo {
  width: 34px; height: 34px;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 800; font-size: 1rem;
}
.topbar-right {
  display: flex; align-items: center; gap: .5rem;
}
.btn-icon {
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border: none; background: transparent; border-radius: 50%;
  color: var(--text-muted); cursor: pointer;
  transition: background .15s;
  font-size: 1.1rem;
}
.btn-icon:hover { background: #f5f5f5; color: var(--text); }

/* User dropdown avatar */
.user-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  color: #fff; font-weight: 700; font-size: .85rem;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; border: none;
  flex-shrink: 0;
}

/* ===== Sidebar ===== */
.sidebar {
  position: fixed;
  top: 0; left: 0;
  width: var(--sidebar-w);
  height: 100vh;
  background: #fff;
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  z-index: 210;
  overflow-x: hidden;
  overflow-y: auto;
  transition: width .25s ease, transform .25s ease;
}
.sidebar-header {
  height: var(--topbar-h);
  display: flex; align-items: center;
  padding: 0 1.1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-header .brand {
  font-weight: 700; font-size: 1.05rem;
  color: var(--brand);
  display: flex; align-items: center; gap: .6rem;
  text-decoration: none; white-space: nowrap; overflow: hidden;
}
.sidebar-header .brand-logo {
  width: 32px; height: 32px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 800; font-size: .95rem;
}
.sidebar-nav {
  flex: 1;
  padding: .75rem .6rem;
  overflow-y: auto;
}
.nav-section {
  font-size: .7rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-muted);
  padding: .75rem .75rem .3rem;
  white-space: nowrap; overflow: hidden;
}
.nav-link-item {
  display: flex; align-items: center; gap: .75rem;
  padding: .6rem .75rem; margin-bottom: .15rem;
  color: var(--text-muted); text-decoration: none;
  border-radius: 8px; font-weight: 500;
  transition: all .15s; white-space: nowrap; overflow: hidden;
}
.nav-link-item i { font-size: 1.1rem; flex-shrink: 0; width: 20px; text-align: center; }
.nav-link-item:hover { background: var(--brand-light); color: var(--brand); }
.nav-link-item.active {
  background: var(--brand); color: #fff;
  box-shadow: 0 2px 8px rgba(230,98,57,.35);
}
.nav-link-item .nav-label { flex: 1; }

/* Sidebar collapsed (desktop) */
.sidebar.collapsed { width: var(--sidebar-collapsed-w); }
.sidebar.collapsed .nav-label,
.sidebar.collapsed .nav-section,
.sidebar.collapsed .brand-name { display: none; }
.sidebar.collapsed .nav-link-item { justify-content: center; padding: .6rem; }
.sidebar.collapsed .sidebar-header { justify-content: center; }

/* ===== Main content ===== */
.main-content {
  margin-left: var(--sidebar-w);
  padding-top: var(--topbar-h);
  min-height: 100vh;
  transition: margin-left .25s;
}
.main-content.expanded { margin-left: var(--sidebar-collapsed-w); }
.main-content-inner {
  padding: 1.5rem;
}

/* ===== Bottom Nav (mobile) ===== */
:root { --bottom-nav-h: 62px; }

.bottom-nav {
  display: none;
}

@media (max-width: 991px) {
  /* Hide sidebar & overlay on mobile — use bottom nav instead */
  .sidebar { display: none !important; }
  .overlay { display: none !important; }
  #mobileSidebarBtn { display: none !important; }

  .main-content { margin-left: 0; padding-bottom: var(--bottom-nav-h); }
  .main-content-inner { padding: 1rem; padding-bottom: calc(var(--bottom-nav-h) + .5rem); }

  /* Bottom nav bar */
  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: var(--bottom-nav-h);
    background: #fff;
    border-top: 1px solid var(--border);
    z-index: 300;
    box-shadow: 0 -2px 12px rgba(0,0,0,.08);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .bottom-nav a {
    flex: 1;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 3px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: .6rem; font-weight: 600;
    letter-spacing: .02em;
    transition: color .15s;
    padding: 6px 4px;
    position: relative;
  }
  .bottom-nav a i { font-size: 1.35rem; line-height: 1; }
  .bottom-nav a.active {
    color: var(--brand);
  }
  .bottom-nav a.active::before {
    content: '';
    position: absolute;
    top: 0; left: 20%; right: 20%;
    height: 3px;
    background: var(--brand);
    border-radius: 0 0 4px 4px;
  }
  .bottom-nav a:hover { color: var(--brand); }
}

/* ===== Cards ===== */
.card {
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  background: var(--card);
}
.card-header {
  background: transparent;
  border-bottom: 1px solid var(--border);
  padding: .9rem 1.25rem;
  font-weight: 600;
}

/* ===== Stat / Icon cards ===== */
.stat-card {
  padding: 1.25rem;
  border-radius: 12px;
  background: white;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  transition: transform .15s, box-shadow .15s;
}
.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.stat-card .label {
  font-size: .75rem; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .05em; margin-bottom: .5rem;
}
.stat-card .value { font-size: 1.65rem; font-weight: 700; color: var(--text); }
.icon-shape {
  width: 40px; height: 40px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.15rem; flex-shrink: 0;
}
.icon-primary   { background: var(--brand-light); color: var(--brand); }
.icon-success   { background: #d1fae5; color: #059669; }
.icon-warning   { background: #fef3c7; color: #d97706; }
.icon-danger    { background: #fee2e2; color: var(--danger); }
.icon-info      { background: #e0f2fe; color: #0369a1; }

/* ===== Buttons ===== */
.btn { font-weight: 500; border-radius: 8px; padding: .45rem .95rem; }
.btn-primary  { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover,
.btn-primary:focus { background: var(--brand-dark); border-color: var(--brand-dark); color: #fff; }
.btn-lg { padding: .7rem 1.5rem; font-size: .95rem; }

/* ===== Forms ===== */
.form-control, .form-select {
  border-radius: 8px;
  border-color: var(--border);
  padding: .5rem .85rem;
  font-size: .875rem;
}
.form-control:focus, .form-select:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(230,98,57,.15);
}
.form-label { font-weight: 500; font-size: .85rem; margin-bottom: .3rem; }

/* ===== Tables ===== */
.table { font-size: .875rem; }
.table thead th {
  background: #fafafa; font-weight: 600;
  color: var(--text-muted); text-transform: uppercase;
  font-size: .72rem; letter-spacing: .05em;
  border-bottom: 1px solid var(--border);
}
.table tbody tr:hover { background: var(--brand-light); }

/* ===== Badges ===== */
.badge { font-weight: 500; padding: .35em .65em; border-radius: 6px; }

/* ===== Login Page ===== */
.login-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #E66239 0%, #cc5530 50%, #a84328 100%);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.login-card {
  background: white; border-radius: 16px;
  padding: 2.5rem; width: 100%; max-width: 420px;
  box-shadow: 0 25px 50px -12px rgba(0,0,0,.25);
}

/* ===== Visit Items ===== */
.visit-item {
  border-left: 3px solid var(--brand);
  padding: .85rem 1rem;
  background: #fafafa;
  border-radius: 0 8px 8px 0;
  margin-bottom: .75rem;
}
.visit-item.result-success { border-left-color: var(--success); }
.visit-item.result-failed  { border-left-color: var(--danger); }

/* ===== Utility ===== */
.text-muted-2 { color: var(--text-muted); }
.cursor-pointer { cursor: pointer; }
.empty-state {
  text-align: center; padding: 3rem 1rem;
  color: var(--text-muted);
}
.empty-state i { font-size: 3rem; opacity: .3; margin-bottom: 1rem; }

/* ===== Toast ===== */
.toast-container { z-index: 9999; }

/* ===== Spinner Overlay ===== */
.spinner-overlay {
  position: fixed; inset: 0;
  background: rgba(255,255,255,.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 9998;
}

/* ===== Bootstrap primary override ===== */
.text-primary { color: var(--brand) !important; }
.bg-primary { background-color: var(--brand) !important; }
.btn-outline-primary { color: var(--brand); border-color: var(--brand); }
.btn-outline-primary:hover { background: var(--brand); border-color: var(--brand); color: #fff; }
.border-primary { border-color: var(--brand) !important; }

/* Tier badges */
.tier-badge { font-size: .72rem; padding: .25em .55em; border-radius: 6px; font-weight: 600; }
.tier-1 { background: #fde68a; color: #92400e; }
.tier-2 { background: #bbf7d0; color: #065f46; }
.tier-3 { background: #bae6fd; color: #075985; }
.tier-4 { background: #e5e5e5; color: #525252; }

.section-title {
  font-weight: 600; font-size: .82rem;
  color: var(--text-muted); text-transform: uppercase;
  letter-spacing: .04em; margin: 1.25rem 0 .5rem;
  padding-bottom: .35rem; border-bottom: 1px solid var(--border);
}
.contact-row {
  background: #f9f9f9;
  border: 1px solid var(--border);
  border-radius: 8px; padding: .6rem; margin-bottom: .5rem;
}
#map {
  height: 280px; border-radius: 8px;
  border: 1px solid var(--border); z-index: 1;
}
.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: .5rem;
}
.photo-thumb {
  position: relative; aspect-ratio: 1;
  border-radius: 8px; overflow: hidden;
  background: #f5f5f5;
}
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; }
.photo-thumb .del-btn {
  position: absolute; top: 4px; right: 4px;
  background: rgba(251,44,54,.85); color: #fff;
  border: none; border-radius: 50%;
  width: 22px; height: 22px; font-size: .75rem;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; opacity: 0; transition: opacity .15s;
}
.photo-thumb:hover .del-btn { opacity: 1; }

/* ===== Select2 — brand theme override ===== */

.select2-container { width: 100% !important; }
.select2-container .selection { display: block; }

/* ── Single selection box ── */
.select2-container--default .select2-selection--single {
  position: relative !important;
  height: 40px !important;
  padding: 0 !important;
  border: 1px solid var(--border) !important;
  border-radius: 8px !important;
  background: #fff !important;
  box-shadow: none !important;
  cursor: pointer !important;
  transition: border-color .18s, box-shadow .18s !important;
}
/* Rendered text — vertically centered via line-height */
.select2-container--default .select2-selection--single .select2-selection__rendered {
  display: block !important;
  padding: 0 34px 0 12px !important;
  line-height: 40px !important;
  font-size: .875rem !important;
  font-family: inherit !important;
  color: var(--text) !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
  white-space: nowrap !important;
}
.select2-container--default .select2-selection--single .select2-selection__placeholder {
  color: var(--text-muted) !important;
}
/* Arrow — absolutely positioned inside selection box */
.select2-container--default .select2-selection--single .select2-selection__arrow {
  position: absolute !important;
  top: 50% !important;
  right: 10px !important;
  transform: translateY(-50%) !important;
  width: 16px !important;
  height: 16px !important;
  margin: 0 !important;
}
.select2-container--default .select2-selection--single .select2-selection__arrow b {
  display: block !important;
  border-color: var(--text-muted) transparent transparent !important;
  border-style: solid !important;
  border-width: 5px 4px 0 !important;
  margin-top: 1px !important;
}
.select2-container--default.select2-container--open
  .select2-selection--single .select2-selection__arrow b {
  border-color: transparent transparent var(--brand) !important;
  border-width: 0 4px 5px !important;
  margin-top: -3px !important;
}
/* Clear × */
.select2-container--default .select2-selection--single .select2-selection__clear {
  position: absolute !important;
  right: 28px !important;
  top: 50% !important;
  transform: translateY(-50%) !important;
  color: var(--text-muted) !important;
  font-size: 1.1rem !important;
  font-weight: 400 !important;
  line-height: 1 !important;
  cursor: pointer !important;
  transition: color .15s !important;
}
.select2-container--default .select2-selection--single .select2-selection__clear:hover {
  color: var(--danger) !important;
}
/* Focus & open ring */
.select2-container--default.select2-container--focus .select2-selection--single,
.select2-container--default.select2-container--open .select2-selection--single {
  border-color: var(--brand) !important;
  box-shadow: 0 0 0 3px rgba(230,98,57,.15) !important;
  outline: none !important;
}

/* ── Dropdown panel ── */
.select2-dropdown {
  border: 1px solid var(--border) !important;
  border-radius: 10px !important;
  box-shadow: 0 8px 28px rgba(0,0,0,.13) !important;
  overflow: hidden !important;
  z-index: 1090 !important;
  background: #fff !important;
  font-family: inherit !important;
  animation: s2FadeIn .12s ease !important;
}
@keyframes s2FadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}
.select2-container--default .select2-search--dropdown { padding: 8px 8px 4px !important; }
.select2-container--default .select2-search--dropdown .select2-search__field {
  width: 100% !important;
  padding: .4rem .7rem !important;
  border: 1px solid var(--border) !important;
  border-radius: 6px !important;
  font-size: .85rem !important;
  font-family: inherit !important;
  outline: none !important;
  background: #fafafa !important;
  color: var(--text) !important;
  transition: border-color .15s, box-shadow .15s !important;
}
.select2-container--default .select2-search--dropdown .select2-search__field:focus {
  border-color: var(--brand) !important;
  box-shadow: 0 0 0 2px rgba(230,98,57,.12) !important;
  background: #fff !important;
}
.select2-results__options { padding: 4px 0 !important; }
.select2-results__option {
  padding: .45rem .9rem !important;
  font-size: .875rem !important;
  font-family: inherit !important;
  color: var(--text) !important;
  cursor: pointer !important;
  transition: background .1s, color .1s !important;
}
.select2-container--default .select2-results__option--highlighted[aria-selected] {
  background: var(--brand-light) !important;
  color: var(--brand) !important;
}
.select2-container--default .select2-results__option[aria-selected=true] {
  background: var(--brand) !important;
  color: #fff !important;
}
.select2-container--default .select2-results__option--disabled {
  color: var(--text-muted) !important;
  cursor: default !important;
}
.select2-results__message { padding: .6rem .9rem !important; color: var(--text-muted) !important; font-size: .875rem !important; }

/* ===== Flatpickr date picker ===== */
.fp-wrap {
  position: relative;
  display: flex;
  align-items: center;
  cursor: pointer;
}
.fp-icon {
  position: absolute;
  left: 10px;
  color: var(--text-muted);
  font-size: .9rem;
  pointer-events: none;
  z-index: 1;
}
.fp-input {
  padding-left: 2.2rem !important;
  cursor: pointer !important;
  background: #fff !important;
}
.fp-input:focus { border-color: var(--brand) !important; box-shadow: 0 0 0 3px rgba(230,98,57,.15) !important; }

/* Calendar popup */
.flatpickr-calendar {
  font-family: 'Poppins', 'Noto Sans Lao', sans-serif;
  font-size: .82rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 32px rgba(0,0,0,.13);
  padding: 6px;
  overflow: hidden;
  z-index: 9999 !important;
  position: fixed !important;
}
.flatpickr-calendar.arrowTop::before,
.flatpickr-calendar.arrowTop::after { display: none; }

/* Month nav header */
.flatpickr-months {
  background: var(--brand);
  border-radius: 8px 8px 0 0;
  padding: 6px 4px 8px;
}
.flatpickr-months .flatpickr-month { color: #fff; fill: #fff; }
.flatpickr-current-month { color: #fff; font-size: .9rem; font-weight: 600; }
.flatpickr-current-month input.cur-year { color: #fff; font-weight: 600; }
.flatpickr-current-month .numInputWrapper:hover { background: rgba(255,255,255,.15); border-radius: 4px; }
.flatpickr-months .flatpickr-prev-month,
.flatpickr-months .flatpickr-next-month { color: #fff; fill: #fff; padding: 4px 8px; }
.flatpickr-months .flatpickr-prev-month:hover,
.flatpickr-months .flatpickr-next-month:hover { color: #fff; background: rgba(255,255,255,.2); border-radius: 6px; }
.flatpickr-months .flatpickr-prev-month svg,
.flatpickr-months .flatpickr-next-month svg { fill: #fff; }

/* Day name row */
.flatpickr-weekdays { background: var(--brand-light); }
span.flatpickr-weekday { color: var(--brand); font-weight: 600; font-size: .75rem; }

/* Days */
.flatpickr-day {
  border-radius: 8px;
  color: var(--text);
  font-size: .82rem;
  height: 34px;
  line-height: 34px;
  max-width: 34px;
  transition: background .12s, color .12s;
}
.flatpickr-day:hover { background: var(--brand-light); color: var(--brand); border-color: transparent; }
.flatpickr-day.today { border-color: var(--brand); color: var(--brand); font-weight: 600; }
.flatpickr-day.today:hover { background: var(--brand-light); }
.flatpickr-day.selected,
.flatpickr-day.selected:hover {
  background: var(--brand) !important;
  border-color: var(--brand) !important;
  color: #fff !important;
  font-weight: 600;
}
.flatpickr-day.prevMonthDay,
.flatpickr-day.nextMonthDay { color: var(--text-muted); opacity: .5; }
.flatpickr-day.disabled { color: var(--text-muted); opacity: .35; }

/* ===== Select2 sm — ใช้ใน filter row คู่กับ form-control-sm ===== */
.select2-sm .select2-selection--single {
  height: 31px !important;
}
.select2-sm .select2-selection--single .select2-selection__rendered {
  font-size: .8rem !important;
  line-height: 31px !important;
  padding: 0 28px 0 10px !important;
}
.select2-sm .select2-selection--single .select2-selection__arrow {
  right: 8px !important;
}
