/* ============================================================
   SALESUPPORT — Layout CSS (Sidebar + Topbar + Main)
   ============================================================
   ไฟล์นี้เก็บ "โครงหลัก" ของทุกหน้า — sidebar, topbar, content area

   👤 ใครแก้ได้: แอดมินระบบ
   🎯 แก้ทำไม: เปลี่ยนความกว้าง sidebar, สี, layout behavior

   หลักการ:
   - ทุกค่าใช้ CSS variables จาก tokens.css
   - รองรับ responsive — sidebar ย่อบนมือถือ
   - ไม่มี magic number — ทุกค่ามาจาก token
   ============================================================ */

/* ============================================================
   1. App Shell — โครงหลักของหน้า
   ============================================================ */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: var(--topbar-height) 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar content";
  height: 100vh;
  overflow: hidden;
  transition: grid-template-columns 0.25s ease;
}

/* Collapsed state — desktop only */
.app-shell:has(.app-sidebar.collapsed) {
  grid-template-columns: var(--sidebar-width-collapsed, 64px) 1fr;
}

@media (min-width: 769px) {
  .app-sidebar.collapsed {
    width: var(--sidebar-width-collapsed, 64px);
  }
  .app-sidebar.collapsed .sidebar-brand,
  .app-sidebar.collapsed .sidebar-group-label,
  .app-sidebar.collapsed .sidebar-item-label,
  .app-sidebar.collapsed .sidebar-user-info {
    display: none !important;
  }
  .app-sidebar.collapsed .sidebar-item {
    justify-content: center;
    padding: var(--space-2) !important;
  }
  .app-sidebar.collapsed .sidebar-header {
    justify-content: center;
  }
  .app-sidebar.collapsed .sidebar-user {
    justify-content: center;
  }
  .app-sidebar.collapsed .sidebar-badge {
    position: absolute;
    top: 2px;
    right: 2px;
    font-size: 9px;
    padding: 1px 4px;
    min-width: auto;
  }
  .app-sidebar.collapsed .sidebar-item {
    position: relative;
  }
}

/* พื้นที่แต่ละส่วน */
.app-sidebar { grid-area: sidebar; }
.app-topbar  { grid-area: topbar; }
.app-content { grid-area: content; overflow-y: auto; }


/* ============================================================
   2. Sidebar
   ============================================================ */

.app-sidebar {
  background: var(--color-white);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* โลโก้ / ชื่อแบรนด์ด้านบน sidebar */
.sidebar-header {
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-5);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.sidebar-logo {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-emerald-500);
  color: white;
  font-weight: var(--font-bold);
  font-size: var(--text-sm);
  border-radius: var(--radius-md);
  flex-shrink: 0;
}

.sidebar-brand {
  font-weight: var(--font-semibold);
  font-size: var(--text-lg);
  color: var(--color-heading);
  letter-spacing: 0.02em;
}

/* Sidebar icon button (shared: collapse + dark mode) */
.sidebar-collapse-btn,
.sidebar-icon-btn {
  margin-left: auto;
  background: transparent;
  border: 1px solid transparent;
  padding: 8px;
  min-width: 36px;
  min-height: 36px;
  border-radius: var(--radius-md, 6px);
  cursor: pointer;
  color: var(--color-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.sidebar-collapse-btn:hover,
.sidebar-icon-btn:hover {
  background: var(--color-surface);
  color: var(--color-heading);
  border-color: var(--color-border);
}
.sidebar-collapse-btn:active,
.sidebar-icon-btn:active {
  transform: scale(0.95);
}
/* dark mode button — ไม่มี margin-left:auto (อยู่ติดปุ่ม collapse) */
.sidebar-icon-btn {
  margin-left: 0;
}
.app-sidebar.collapsed .sidebar-collapse-btn,
.app-sidebar.collapsed .sidebar-icon-btn {
  margin: 0 auto;
}
.app-sidebar.collapsed .sidebar-header {
  flex-direction: column;
  gap: 6px;
  padding: 8px 4px;
  height: auto;
  min-height: var(--topbar-height);
}

/* Sidebar custom tooltip (visible when collapsed) */
.sidebar-tooltip {
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%) translateX(8px);
  background: var(--color-heading, #1f2937);
  color: white;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s, transform 0.15s;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.sidebar-tooltip::before {
  content: '';
  position: absolute;
  left: -4px;
  top: 50%;
  transform: translateY(-50%);
  border: 4px solid transparent;
  border-right-color: var(--color-heading, #1f2937);
}
.app-sidebar.collapsed .sidebar-item:hover .sidebar-tooltip,
.app-sidebar.collapsed .sidebar-user:hover .sidebar-tooltip {
  opacity: 1;
  transform: translateY(-50%) translateX(12px);
}

/* ทำให้ sidebar-item ที่ collapsed มี hit area ใหญ่ขึ้น */
@media (min-width: 769px) {
  .app-sidebar.collapsed .sidebar-item {
    padding: 10px 8px !important;
    margin: 2px 4px;
    border-radius: 8px;
  }
  .app-sidebar.collapsed .sidebar-item .icon {
    width: 24px;
    height: 24px;
  }
  .app-sidebar.collapsed .sidebar-user {
    padding: 8px 4px;
    justify-content: center;
  }
}

/* เมนู navigation */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4) var(--space-3);
}

/* กลุ่มเมนู */
.sidebar-group {
  margin-bottom: var(--space-4);
}

.sidebar-group-label {
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* รายการเมนู */
.sidebar-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  margin: var(--space-1) 0;
  color: var(--color-secondary);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-base);
  position: relative;
  font-size: var(--text-base);
  text-decoration: none;
}

.sidebar-item:hover {
  background: var(--color-surface);
  color: var(--color-text);
}

.sidebar-item.active {
  background: var(--color-emerald-50);
  color: var(--color-emerald-700);
  font-weight: var(--font-medium);
}

.sidebar-item.active::before {
  content: '';
  position: absolute;
  left: -3px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 60%;
  background: var(--color-emerald-500);
  border-radius: var(--radius-full);
}

/* icon ในเมนู */
.sidebar-item .icon {
  flex-shrink: 0;
  color: inherit;
}

/* badge ตัวเลขแจ้งเตือน */
.sidebar-badge {
  margin-left: auto;
  min-width: 20px;
  height: 20px;
  padding: 0 var(--space-2);
  background: var(--color-error);
  color: white;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  display: flex;
  align-items: center;
  justify-content: center;
}

.sidebar-item.active .sidebar-badge {
  background: var(--color-emerald-600);
}

/* ด้านล่าง sidebar — user info + logout */
.sidebar-footer {
  padding: var(--space-3);
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
}

/* Logout button — visible + prominent */
.sidebar-logout-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px var(--space-3);
  margin-top: var(--space-2);
  background: #fee2e2;
  color: #991b1b;
  border: 1px solid #fca5a5;
  border-radius: var(--radius-md, 6px);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.sidebar-logout-btn:hover {
  background: #ef4444;
  color: white;
  border-color: #dc2626;
}
.app-sidebar.collapsed .sidebar-logout-btn {
  padding: 10px;
}
.app-sidebar.collapsed .sidebar-logout-btn span:last-child {
  display: none;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background var(--transition-base);
}

.sidebar-user:hover {
  background: var(--color-surface);
}

.sidebar-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--color-emerald-100);
  color: var(--color-emerald-700);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: var(--font-semibold);
  font-size: var(--text-sm);
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-user-info {
  flex: 1;
  min-width: 0;
}

.sidebar-user-name {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--color-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-user-role {
  font-size: var(--text-xs);
  color: var(--color-muted);
}


/* ============================================================
   3. Topbar
   ============================================================ */

.app-topbar {
  background: var(--color-white);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  padding: 0 var(--space-6);
  gap: var(--space-4);
}

/* ปุ่ม toggle sidebar (mobile) */
.topbar-toggle {
  display: none;
  padding: var(--space-2);
  border-radius: var(--radius-md);
  color: var(--color-secondary);
  cursor: pointer;
  transition: background var(--transition-base);
}

.topbar-toggle:hover {
  background: var(--color-surface);
}

/* Breadcrumb */
.topbar-breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-muted);
}

.topbar-breadcrumb a {
  color: var(--color-muted);
  text-decoration: none;
  transition: color var(--transition-fast);
}

.topbar-breadcrumb a:hover {
  color: var(--color-emerald-600);
}

.topbar-breadcrumb .current {
  color: var(--color-text);
  font-weight: var(--font-medium);
}

.topbar-breadcrumb .separator {
  color: var(--color-border);
}

/* พื้นที่ว่างกลาง — ดันส่วนขวาไปขวา */
.topbar-spacer {
  flex: 1;
}

/* ปุ่ม icon ใน topbar */
.topbar-icon-btn {
  position: relative;
  padding: var(--space-2);
  border-radius: var(--radius-md);
  color: var(--color-secondary);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  border: none;
  background: transparent;
}

.topbar-icon-btn:hover {
  background: var(--color-surface);
  color: var(--color-text);
  text-decoration: none;
}

.topbar-icon-btn:active {
  background: var(--color-border);
}

/* จุดแจ้งเตือน */
.topbar-icon-btn .notification-dot {
  position: absolute;
  top: 2px;
  right: 2px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  background: var(--color-error);
  color: white;
  border-radius: var(--radius-full);
  border: 2px solid var(--color-white);
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}


/* ============================================================
   4. Content Area
   ============================================================ */

.app-content {
  background: var(--color-bg);
}

.content-wrapper {
  max-width: var(--content-max-width);
  margin: 0 auto;
  padding: var(--space-6);
}

/* หัวข้อหน้า */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: var(--space-6);
  gap: var(--space-4);
  flex-wrap: wrap;
}

.page-title-block {
  flex: 1;
  min-width: 0;
}

.page-title {
  font-size: var(--text-2xl);
  font-weight: var(--font-bold);
  color: var(--color-heading);
  margin-bottom: var(--space-1);
}

.page-description {
  color: var(--color-secondary);
  font-size: var(--text-sm);
}

.page-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}


/* ============================================================
   5. Responsive — มือถือ
   ============================================================ */

/* ซ่อน sidebar บนมือถือ — ใช้ toggle แทน */
@media (max-width: 768px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "content";
  }

  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--sidebar-width);
    z-index: var(--z-sidebar);
    transform: translateX(-100%);
    transition: transform var(--transition-base);
    box-shadow: var(--shadow-lg);
  }

  /* แสดง sidebar เมื่อมี class .open */
  .app-sidebar.open {
    transform: translateX(0);
  }

  /* แสดงปุ่ม toggle บนมือถือ */
  .topbar-toggle {
    display: flex;
  }

  .content-wrapper {
    padding: var(--space-4);
  }

  .page-header {
    flex-direction: column;
    align-items: stretch;
  }
}

/* มือถือเล็กมาก */
@media (max-width: 480px) {
  :root {
    --content-padding: 16px;
  }

  .page-title {
    font-size: var(--text-xl);
  }
}

/* Overlay ตอน sidebar เปิดบนมือถือ */
.sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: calc(var(--z-sidebar) - 1);
}

.sidebar-overlay.show {
  display: block;
}


/* ============================================================
   6. Layout แบบ "blank" (หน้า login, error)
   ============================================================ */

.layout-blank {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-bg);
  padding: var(--space-6);
}


/* ============================================================
   7. Card (เบื้องต้น — จะขยายใน components.css ของ Step 5)
   ============================================================ */

.card {
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-6);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition-base);
}

.card:hover {
  box-shadow: var(--shadow-md);
}

/* ============================================================
   8. Mobile Responsive — Tables + Forms
   ============================================================ */

/* Table responsive wrapper — ใช้ wrap ตารางเพื่อให้ scroll แนวนอนได้ */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  /* ปรับแต่ง scrollbar */
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

.table-responsive::-webkit-scrollbar {
  height: 8px;
}

.table-responsive::-webkit-scrollbar-track {
  background: transparent;
}

.table-responsive::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 4px;
}

.table-responsive::-webkit-scrollbar-thumb:hover {
  background: var(--color-muted);
}

/* บนมือถือ (< 768px) — table ใช้ min-width เพื่อบังคับให้ scroll */
@media (max-width: 768px) {
  .table-responsive .table {
    min-width: 720px;
  }

  /* ปรับ stat-card ให้เต็มความกว้าง */
  .stat-card {
    flex: 1 1 calc(50% - 8px) !important;
    min-width: calc(50% - 8px) !important;
  }

  /* ปรับ flex ให้ wrap บนมือถือ */
  .flex {
    flex-wrap: wrap;
  }

  /* Form rows stack vertically */
  .form-row {
    flex-direction: column;
    gap: var(--space-2);
  }

  .form-row .form-group {
    width: 100%;
  }

  /* Filter bars — stack + button full width */
  .card-body .flex {
    flex-direction: column;
    align-items: stretch;
  }

  .card-body .flex > .form-group {
    min-width: 100% !important;
  }

  .card-body .flex > button {
    width: 100%;
  }

  /* Buttons stack on mobile in action grids */
  .action-grid {
    grid-template-columns: 1fr !important;
  }

  /* Modal — full width on mobile */
  .modal {
    width: 95% !important;
    max-width: none !important;
  }

  .modal-body {
    max-height: 65vh !important;
  }

  /* Sidebar overlay visible เมื่อเปิดบนมือถือ */
  .sidebar-overlay.show {
    background: rgba(0, 0, 0, 0.65);
  }

  /* Ticket table cells — smaller font on mobile */
  .table {
    font-size: 12px;
  }

  .table thead th {
    white-space: nowrap;
  }

  /* Stat values smaller */
  .stat-value {
    font-size: var(--text-xl);
  }
}

/* มือถือเล็ก (< 480px) — stat-card 1 คอลัมน์ */
@media (max-width: 480px) {
  .stat-card {
    flex: 1 1 100% !important;
    min-width: 100% !important;
  }

  .page-actions {
    width: 100%;
  }

  .page-actions .btn {
    flex: 1;
  }
}

/* Dashboard widget grid */
.dashboard-widgets {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--space-4, 16px);
}

@media (max-width: 768px) {
  .dashboard-widgets {
    grid-template-columns: 1fr;
  }
}

.widget-card {
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.widget-card.hidden-widget {
  display: none;
}

.widget-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
}

.widget-header .drag-handle {
  cursor: move;
  color: var(--color-muted);
  padding: 4px;
}

.widget-title {
  font-weight: 600;
  color: var(--color-heading);
  font-size: 14px;
}

.widget-body {
  padding: 16px;
  flex: 1;
  overflow-y: auto;
  max-height: 400px;
}

/* ============================================================
   99. Global Scrollbar Fixes — ensure all pages can scroll
   ============================================================ */

/* ⭐ Force app-content to be scrollable — overrides any inline styles */
.app-content {
  overflow-y: auto !important;
  overflow-x: hidden !important;
  -webkit-overflow-scrolling: touch;
  height: 100%;
  max-height: calc(100vh - var(--topbar-height, 56px));
}

/* ⭐ Ensure content-wrapper doesn't clip overflow */
.content-wrapper {
  overflow: visible;
}

/* ⭐ Custom scrollbar styling — modern thin scrollbar */
.app-content::-webkit-scrollbar {
  width: 10px;
}
.app-content::-webkit-scrollbar-track {
  background: #f1f5f9;
}
.app-content::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 5px;
  border: 2px solid #f1f5f9;
}
.app-content::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}
.app-content {
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 #f1f5f9;
}

/* ⭐ Tables — make them scroll horizontally if too wide */
.table {
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.table thead, .table tbody {
  display: table;
  width: 100%;
  table-layout: auto;
}

/* ⭐ Cards — ensure content can scroll if needed */
.card-body {
  overflow: visible;
}

/* ⭐ Modal body — scroll if content is long */
.modal-body {
  max-height: calc(100vh - 200px);
  overflow-y: auto;
}

/* ⭐ Print — hide scrollbars when printing */
@media print {
  .app-content {
    overflow: visible !important;
    max-height: none !important;
  }
}
