:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: rgba(255,255,255,0.08);
  --text: #e8eaf0;
  --muted: #8b90a7;
  --accent: #4f8ef7;
  --accent2: #7c5cf6;
  --success: #34d399;
  --warning: #fbbf24;
  --danger: #f87171;
  --info: #60a5fa;
  --radius: 10px;
  --radius-lg: 16px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Sarabun', 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 15px;
}
@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');

header {
  padding: 1.5rem 2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
}
.logo {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}
header h1 { font-size: 18px; font-weight: 600; letter-spacing: -0.3px; }
header .subtitle { font-size: 13px; color: var(--muted); margin-top: 2px; }
.header-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }

#status-badge {
  padding: 5px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid transparent;
}
.badge-idle { background: rgba(139,144,167,0.15); border-color: rgba(139,144,167,0.3); color: var(--muted); }
.badge-loading { background: rgba(79,142,247,0.15); border-color: rgba(79,142,247,0.3); color: var(--accent); }
.badge-done { background: rgba(52,211,153,0.15); border-color: rgba(52,211,153,0.3); color: var(--success); }
.badge-error { background: rgba(248,113,113,0.15); border-color: rgba(248,113,113,0.3); color: var(--danger); }

#refresh-btn {
  padding: 6px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  font-weight: 500;
  transition: opacity 0.15s;
}
#refresh-btn:hover { opacity: 0.85; }
#refresh-btn:disabled { opacity: 0.4; cursor: not-allowed; }

main { padding: 2rem; max-width: 1400px; margin: 0 auto; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 2rem;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.1rem 1.25rem;
}
.stat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }
.stat-value { font-size: 26px; font-weight: 600; font-family: 'JetBrains Mono', monospace; letter-spacing: -1px; }
.stat-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.stat-accent { color: var(--accent); }
.stat-success { color: var(--success); }
.stat-warning { color: var(--warning); }
.stat-danger { color: var(--danger); }

.progress-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  margin-bottom: 2rem;
  display: none;
}
.progress-wrap.visible { display: block; }
.progress-label { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.progress-bar-bg {
  height: 6px; background: var(--border); border-radius: 3px; overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 3px;
  transition: width 0.3s ease;
  width: 0%;
}
.progress-text { font-size: 13px; color: var(--text); margin-top: 8px; font-family: 'JetBrains Mono', monospace; }

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.table-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.table-header h2 { font-size: 15px; font-weight: 600; }
.table-count { font-size: 13px; color: var(--muted); }

.table-scroll { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
thead th {
  padding: 10px 16px;
  text-align: left;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: var(--surface2);
}
tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--surface2); }
tbody td {
  padding: 10px 16px;
  color: var(--text);
  white-space: nowrap;
}
tbody td.wrap { white-space: normal; max-width: 220px; }

.status-pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  color: var(--muted);
}
.empty-state .icon { font-size: 48px; margin-bottom: 1rem; }
.empty-state p { font-size: 15px; }

.error-banner {
  background: rgba(248,113,113,0.1);
  border: 1px solid rgba(248,113,113,0.3);
  border-radius: var(--radius);
  padding: 1rem 1.5rem;
  margin-bottom: 1.5rem;
  color: var(--danger);
  font-size: 14px;
  display: none;
}
.error-banner.visible { display: block; }

.filter-bar {
  display: flex;
  gap: 14px;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  align-items: center;
}
.search-box {
  flex: 1;
  min-width: 250px;
}
.search-box input {
  width: 100%;
  padding: 10px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}
.search-box input:focus {
  border-color: var(--accent);
}
.filter-group {
  display: flex;
  gap: 10px;
}
.filter-group select {
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  outline: none;
  cursor: pointer;
}
.filter-group select:focus {
  border-color: var(--accent);
}

.tracking-link {
  text-decoration: none;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 500;
  transition: filter 0.2s;
}
.tracking-link:hover {
  filter: brightness(1.2);
  text-decoration: underline;
}
.carrier-kex {
  background: rgba(255, 103, 0, 0.15);
  color: #ff6700; /* KEX Orange */
}
.carrier-flash {
  background: rgba(255, 220, 0, 0.15);
  color: #ffdc00; /* Flash Yellow */
}

/* Modal Styles */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}
.modal-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 90%;
  max-width: 800px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
  transform: translateY(20px);
  transition: transform 0.3s;
}
.modal-overlay.active .modal-content {
  transform: translateY(0);
}
.modal-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  line-height: 1.6;
}
.details-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
}
.detail-item {
  margin-bottom: 12px;
}
.detail-label {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  margin-bottom: 4px;
}
.detail-value {
  color: var(--text);
  word-break: break-all;
}
.btn-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 24px;
  cursor: pointer;
}
.btn-view {
  background: rgba(79, 142, 247, 0.1);
  color: var(--accent);
  border: 1px solid rgba(79, 142, 247, 0.2);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-view:hover {
  background: var(--accent);
  color: #fff;
}

/* Enhanced Modal Styles */
.avatar-big {
  width: 64px; height: 64px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  margin-bottom: 1rem;
  object-fit: cover;
}
.timeline {
  border-left: 2px solid var(--border);
  margin-left: 10px;
  padding-left: 20px;
  position: relative;
}
.timeline-item {
  margin-bottom: 1rem;
  position: relative;
}
.timeline-item::before {
  content: '';
  position: absolute;
  left: -27px;
  top: 5px;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--surface2);
  border: 2px solid var(--accent);
}
.timeline-date {
  font-size: 11px;
  color: var(--muted);
}
.product-img {
  width: 40px; height: 40px;
  border-radius: 4px;
  object-fit: cover;
  background: var(--surface);
}
.finance-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}
.finance-label { color: var(--muted); }
.finance-value { font-family: 'JetBrains Mono', monospace; font-weight: 500; }

.slip-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  display: grid;
  gap: 0.85rem;
}
.slip-thumbnail-link {
  display: inline-block;
  max-width: 100%;
  width: 100%;
  text-decoration: none;
}
.slip-thumbnail {
  width: 100%;
  max-width: 360px;
  border-radius: 12px;
  border: 1px solid var(--border);
  object-fit: contain;
  background: #090b10;
}
.slip-thumbnail-link:hover .slip-thumbnail {
  filter: brightness(1.05);
}
.slip-meta {
  display: grid;
  gap: 0.55rem;
  font-size: 12px;
  color: var(--muted);
}
.slip-meta div {
  word-break: break-word;
}
.slip-card .btn-view {
  margin-top: 0.5rem;
}

/* =========================================
   Skeleton Loading Animation
   ========================================= */
.skeleton {
  background: var(--surface2);
  background-image: linear-gradient(
    90deg,
    rgba(255, 255, 255, 0) 0,
    rgba(255, 255, 255, 0.05) 20%,
    rgba(255, 255, 255, 0) 40%,
    rgba(255, 255, 255, 0) 100%
  );
  background-size: 200px 100%;
  background-repeat: no-repeat;
  animation: shimmer 1.5s infinite linear;
  border-radius: 4px;
}

@keyframes shimmer {
  0% {
    background-position: -200px 0;
  }
  100% {
    background-position: calc(200px + 100%) 0;
  }
}

/* กำหนดขนาดให้รูปทรงต่างๆ ของ Skeleton */
.skeleton-text {
  height: 16px;
  margin: 4px 0;
}
.skeleton-badge {
  height: 26px;
  width: 80px;
  border-radius: 12px;
}
.skeleton-btn {
  height: 32px;
  width: 100%;
  border-radius: var(--radius);
}

@media (max-width: 600px) {
  main { padding: 1rem; }
  header { padding: 1rem; }
  .stat-value { font-size: 20px; }
}