/* ============================================================
   SALESUPPORT — Component Library
   ============================================================
   ไฟล์นี้เก็บ "component styles" ทั้งหมด — button, card, table,
   form, badge, modal, toast, empty state, pagination

   👤 ใครแก้ได้: แอดมินระบบ
   🎯 แก้ทำไม: ปรับรูปลักษณ์ของ component

   หลักการ:
   - ทุกค่าใช้ CSS variables จาก tokens.css
   - แต่ละ component มี variant ชัดเจน (modifier class)
   - รองรับ responsive อัตโนมัติ
   - ไม่มี magic number
   ============================================================ */


/* ============================================================
   1. Button
   ============================================================
   ใช้: <button class="btn btn-primary">บันทึก</button>
        <button class="btn btn-secondary btn-sm">ยกเลิก</button>
   ============================================================ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-family: var(--font-body);
  font-size: var(--text-base);
  font-weight: var(--font-medium);
  line-height: 1.4;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all var(--transition-base);
  text-decoration: none;
  white-space: nowrap;
  user-select: none;
}

.btn:focus-visible {
  outline: 2px solid var(--color-emerald-500);
  outline-offset: 2px;
}

/* Variant: Primary (เขียว) */
.btn-primary {
  background: var(--color-emerald-500);
  color: white;
}
.btn-primary:hover {
  background: var(--color-emerald-600);
  box-shadow: var(--shadow-sm);
}
.btn-primary:active {
  background: var(--color-emerald-700);
}

/* Variant: Secondary (ขาว มีขอบ) */
.btn-secondary {
  background: var(--color-white);
  color: var(--color-text);
  border-color: var(--color-border);
}
.btn-secondary:hover {
  background: var(--color-surface);
  border-color: var(--color-muted);
}

/* Variant: Ghost (โปร่งใส) */
.btn-ghost {
  background: transparent;
  color: var(--color-secondary);
}
.btn-ghost:hover {
  background: var(--color-surface);
  color: var(--color-text);
}

/* Variant: Danger (แดง) */
.btn-danger {
  background: var(--color-error);
  color: white;
}
.btn-danger:hover {
  background: #DC2626;
  box-shadow: var(--shadow-sm);
}

/* Size: sm */
.btn-sm {
  padding: var(--space-1) var(--space-3);
  font-size: var(--text-sm);
}

/* Size: lg */
.btn-lg {
  padding: var(--space-3) var(--space-6);
  font-size: var(--text-lg);
}

/* State: disabled */
.btn:disabled,
.btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

/* ⭐ v20260921 — Button Micro-interactions (ทุกปุ่มทั้งระบบ)
   1. :active  → กดยุบเล็กน้อย (tactile feedback ทันที ทุกปุ่ม ไม่ต้องแก้ทีละหน้า)
   2. .is-busy → กำลังทำงาน (async): หมุน spinner + ล็อกกัน double-click
   3. .is-done → ทำสำเร็จ: กระพริบเขียว 0.8 วิ
   ใช้งานอัตโนมัติผ่าน window.ButtonFeedback ใน js/components.js */
.btn {
  transition: transform 0.12s ease, box-shadow 0.18s ease,
              background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.btn:active:not(:disabled) {
  transform: scale(0.96);
}

/* กำลังทำงาน — ปุ่ม async ที่ ButtonFeedback จัดการให้ */
.btn.is-busy {
  pointer-events: none;
  opacity: 0.75;
  position: relative;
}
.btn.is-busy > *:not(.btn-spin) {
  opacity: 0.45;
}

/* spinner ขนาดตามปุ่ม (btn-sm ใช้อันเล็ก) */
.btn .btn-spin {
  display: none;
  position: absolute;
  left: 50%;
  top: 50%;
  width: 18px;
  height: 18px;
  margin: -9px 0 0 -9px;
  border: 2px solid rgba(0, 0, 0, 0.18);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: btn-spin 0.7s linear infinite;
}
.btn.btn-sm .btn-spin {
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border-width: 2px;
}
.btn.is-busy .btn-spin {
  display: block;
}
@keyframes btn-spin {
  to { transform: rotate(360deg); }
}

/* ทำสำเร็จ — กระพริบ ring เขียวรอบปุ่ม */
.btn.is-done {
  animation: btn-done 0.8s ease;
}
@keyframes btn-done {
  0%   { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.55); }
  60%  { box-shadow: 0 0 0 7px rgba(16, 185, 129, 0.12); }
  100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}

/* Icon-only button */
.btn-icon {
  padding: var(--space-2);
  width: 36px;
  height: 36px;
}
.btn-icon.btn-sm {
  width: 32px;
  height: 32px;
  padding: var(--space-1);
}


/* ============================================================
   2. Card (expanded from layout.css)
   ============================================================
   ใช้: <div class="card">
          <div class="card-header">
            <h3 class="card-title">หัวข้อ</h3>
          </div>
          <div class="card-body">เนื้อหา</div>
        </div>
   ============================================================ */

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

.card-body {
  padding: var(--space-6);
}

.card-header {
  padding: var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.card-title {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--color-heading);
  margin: 0;
}

.card-footer {
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
}

/* Card แบบไม่มี padding (สำหรับใส่ table หรือ list) */
.card-flush .card-body {
  padding: 0;
}


/* ============================================================
   3. Table
   ============================================================
   ใช้: <table class="table">
          <thead><tr><th>หัวข้อ</th></tr></thead>
          <tbody><tr><td>ข้อมูล</td></tr></tbody>
        </table>
   ============================================================ */

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.table thead th {
  background: var(--color-emerald-600);
  color: white;
  padding: var(--space-3) var(--space-4);
  text-align: left;
  font-weight: var(--font-semibold);
  font-size: var(--text-sm);
  white-space: nowrap;
}

.table thead th:first-child {
  border-top-left-radius: var(--radius-md);
}
.table thead th:last-child {
  border-top-right-radius: var(--radius-md);
}

.table tbody td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
  color: var(--color-text);
  vertical-align: middle;
}

.table tbody tr:nth-child(even) {
  background: var(--color-bg);
}

.table tbody tr:hover {
  background: var(--color-emerald-50);
}

.table tbody tr:last-child td {
  border-bottom: none;
}

/* Table แบบไม่มี header background */
.table-clean thead th {
  background: transparent;
  color: var(--color-muted);
  font-weight: var(--font-semibold);
  border-bottom: 2px solid var(--color-border);
  text-transform: uppercase;
  font-size: var(--text-xs);
  letter-spacing: 0.05em;
}

/* Table cell modifiers */
.table .cell-mono {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
}
.table .cell-truncate {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.table .cell-right { text-align: right; }
.table .cell-center { text-align: center; }


/* ============================================================
   4. Form Controls
   ============================================================
   ใช้: <div class="form-group">
          <label class="form-label">ชื่อ</label>
          <input type="text" class="form-control" placeholder="กรอกชื่อ">
          <span class="form-hint">กรอกชื่อ-นามสกุลจริง</span>
        </div>
   ============================================================ */

.form-group {
  margin-bottom: var(--space-4);
}

.form-label {
  display: block;
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--color-text);
  margin-bottom: var(--space-2);
}

.form-label .required {
  color: var(--color-error);
  margin-left: 2px;
}

.form-control {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
  font-family: var(--font-body);
  color: var(--color-text);
  transition: all var(--transition-base);
}

.form-control::placeholder {
  color: var(--color-muted);
}

.form-control:focus {
  outline: none;
  border-color: var(--color-emerald-500);
  box-shadow: 0 0 0 3px var(--color-emerald-50);
}

.form-control:disabled {
  background: var(--color-surface);
  color: var(--color-muted);
  cursor: not-allowed;
}

/* State: error */
.form-control.is-error {
  border-color: var(--color-error);
}
.form-control.is-error:focus {
  box-shadow: 0 0 0 3px var(--color-error-bg);
}

/* Size: sm */
.form-control-sm {
  padding: var(--space-1) var(--space-2);
  font-size: var(--text-sm);
}

/* Textarea */
textarea.form-control {
  min-height: 100px;
  resize: vertical;
}

/* Select */
select.form-control {
  appearance: none;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right var(--space-3) center;
  padding-right: var(--space-8);
  cursor: pointer;
}

/* Form hint (ข้อความช่วยเหลือใต้ input) */
.form-hint {
  display: block;
  font-size: var(--text-xs);
  color: var(--color-muted);
  margin-top: var(--space-1);
}

.form-error {
  display: block;
  font-size: var(--text-xs);
  color: var(--color-error);
  margin-top: var(--space-1);
}

/* Form row — วาง 2 field ข้างกัน */
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-4);
}

@media (max-width: 640px) {
  .form-row {
    grid-template-columns: 1fr;
  }
}

/* Checkbox + Radio */
.form-check {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
}

.form-check input[type="checkbox"],
.form-check input[type="radio"] {
  width: 16px;
  height: 16px;
  accent-color: var(--color-emerald-500);
  cursor: pointer;
}


/* ============================================================
   5. Badge (สถานะ)
   ============================================================
   ใช้: <span class="badge badge-success">เปิด</span>
        <span class="badge badge-warning">รอ</span>
   ============================================================ */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  border-radius: var(--radius-full);
  line-height: 1.4;
  white-space: nowrap;
}

.badge-success {
  background: var(--color-success-bg);
  color: var(--color-success-text);
}
.badge-warning {
  background: var(--color-warning-bg);
  color: var(--color-warning-text);
}
.badge-error {
  background: var(--color-error-bg);
  color: var(--color-error-text);
}
.badge-info {
  background: var(--color-info-bg);
  color: var(--color-info-text);
}
.badge-neutral {
  background: var(--color-surface);
  color: var(--color-secondary);
}

/* Dot — จุดสถานะเล็ก ๆ */
.badge-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  display: inline-block;
}
.badge-dot.success { background: var(--color-success); }
.badge-dot.warning { background: var(--color-warning); }
.badge-dot.error   { background: var(--color-error); }
.badge-dot.info    { background: var(--color-info); }


/* ============================================================
   6. Modal / Dialog
   ============================================================
   ใช้ผ่าน JS: Modal.open({ title, body, actions })
   ============================================================ */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  opacity: 0;
  visibility: hidden;
  transition: all var(--transition-base);
}

.modal-overlay.open {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--color-white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  max-width: 500px;
  width: 100%;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transform: scale(0.95);
  transition: transform var(--transition-base);
}

.modal-overlay.open .modal {
  transform: scale(1);
}

.modal-lg { max-width: 800px; }
.modal-sm { max-width: 400px; }

.modal-header {
  padding: var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.modal-title {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--color-heading);
  margin: 0;
}

.modal-close {
  padding: var(--space-1);
  color: var(--color-muted);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}
.modal-close:hover {
  background: var(--color-surface);
  color: var(--color-text);
}

.modal-body {
  padding: var(--space-6);
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
}


/* ============================================================
   7. Toast Notification
   ============================================================
   ใช้ผ่าน JS: Toast.show('บันทึกสำเร็จ', 'success')
   ============================================================ */

.toast-container {
  position: fixed;
  top: var(--space-6);
  right: var(--space-6);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  pointer-events: none;
}

.toast {
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-left: 4px solid var(--color-secondary);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-lg);
  min-width: 300px;
  max-width: 400px;
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  pointer-events: auto;
  animation: toast-in 0.2s ease;
}

@keyframes toast-in {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

.toast.success { border-left-color: var(--color-success); }
.toast.warning { border-left-color: var(--color-warning); }
.toast.error   { border-left-color: var(--color-error); }
.toast.info    { border-left-color: var(--color-info); }

.toast-icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
}
.toast.success .toast-icon { color: var(--color-success); }
.toast.warning .toast-icon { color: var(--color-warning); }
.toast.error   .toast-icon { color: var(--color-error); }
.toast.info    .toast-icon { color: var(--color-info); }

.toast-content {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  color: var(--color-heading);
}

.toast-message {
  font-size: var(--text-sm);
  color: var(--color-secondary);
  margin-top: 2px;
}

.toast-close {
  color: var(--color-muted);
  cursor: pointer;
  padding: var(--space-1);
  flex-shrink: 0;
}


/* ============================================================
   8. Empty State (ไม่มีข้อมูล)
   ============================================================
   ใช้: <div class="empty-state">
          <div class="empty-icon">🎫</div>
          <h3 class="empty-title">ยังไม่มี ticket</h3>
          <p class="empty-message">คลิกปุ่มด้านล่างเพื่อสร้าง ticket แรก</p>
          <button class="btn btn-primary">สร้าง ticket</button>
        </div>
   ============================================================ */

.empty-state {
  text-align: center;
  padding: var(--space-12) var(--space-6);
  color: var(--color-secondary);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: var(--space-4);
  opacity: 0.5;
}

.empty-title {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--color-text);
  margin-bottom: var(--space-2);
}

.empty-message {
  font-size: var(--text-sm);
  color: var(--color-muted);
  margin-bottom: var(--space-4);
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
}


/* ============================================================
   9. Pagination
   ============================================================
   ใช้: <div class="pagination">
          <button class="btn btn-ghost btn-icon btn-sm">‹</button>
          <button class="btn btn-primary btn-sm">1</button>
          <button class="btn btn-ghost btn-sm">2</button>
          <button class="btn btn-ghost btn-icon btn-sm">›</button>
        </div>
   ============================================================ */

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  padding: var(--space-4) 0;
}

.pagination-info {
  font-size: var(--text-sm);
  color: var(--color-muted);
  margin-right: var(--space-4);
}


/* ============================================================
   10. Stat Card (KPI Card)
   ============================================================
   ใช้: <div class="stat-card">
          <div class="stat-label">Ticket เปิดอยู่</div>
          <div class="stat-value">24</div>
          <div class="stat-trend trend-up">+3 จากเมื่อวาน</div>
        </div>
   ============================================================ */

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

.stat-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.stat-label {
  font-size: var(--text-sm);
  color: var(--color-secondary);
  margin-bottom: var(--space-2);
}

.stat-value {
  font-size: var(--text-3xl);
  font-weight: var(--font-bold);
  color: var(--color-heading);
  line-height: 1.2;
  margin-bottom: var(--space-1);
}

.stat-trend {
  font-size: var(--text-xs);
  color: var(--color-muted);
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.stat-trend.trend-up { color: var(--color-success); }
.stat-trend.trend-down { color: var(--color-error); }

/* Variant: colored value */
.stat-card.variant-success .stat-value { color: var(--color-success); }
.stat-card.variant-warning .stat-value { color: var(--color-warning); }
.stat-card.variant-error .stat-value { color: var(--color-error); }
.stat-card.variant-info .stat-value { color: var(--color-info); }


/* ============================================================
   11. Loading Spinner
   ============================================================ */

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-emerald-500);
  border-radius: var(--radius-full);
  animation: spin 0.6s linear infinite;
}

.spinner-lg { width: 32px; height: 32px; border-width: 3px; }
.spinner-sm { width: 14px; height: 14px; border-width: 1.5px; }

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-8);
  color: var(--color-muted);
  font-size: var(--text-sm);
}


/* ============================================================
   12. Breadcrumb (in page header)
   ============================================================ */

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

.breadcrumb a {
  color: var(--color-muted);
  text-decoration: none;
}
.breadcrumb a:hover {
  color: var(--color-emerald-600);
}

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