/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f0f2f7;
  --panel: #ffffff;
  --border: #e2e6ef;
  --text: #0f172a;
  --text-secondary: #475569;
  --muted: #94a3b8;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: #eff6ff;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --warning-bg: #fefce8;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 10px 40px rgba(15, 23, 42, 0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --header-height: 72px;
}

html {
  font-size: 15px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  line-height: 1.5;
  min-height: 100vh;
}

/* ============================================================
   HEADER & NAVBAR
   ============================================================ */
.site-header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 200;
  box-shadow: var(--shadow-sm);
}

.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: stretch;
  gap: 32px;
  height: var(--header-height);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.brand-icon {
  width: 40px;
  height: 40px;
  background: var(--accent);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}

.brand-name {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}

.brand-sub {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 1px;
}

.navbar {
  display: flex;
  align-items: stretch;
  gap: 2px;
  flex: 1;
}

.nav-tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 20px;
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 0.9rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  border-radius: 0;
  white-space: nowrap;
  position: relative;
}

.nav-tab svg {
  flex-shrink: 0;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.nav-tab:hover {
  color: var(--text);
  background: var(--accent-light);
  box-shadow: none;
  transform: none;
}

.nav-tab:hover svg {
  opacity: 1;
}

.nav-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.nav-tab.active svg {
  opacity: 1;
}

/* ============================================================
   MAIN LAYOUT
   ============================================================ */
.main-content {
  max-width: 1280px;
  margin: 0 auto;
  padding: 28px 24px 64px;
}

/* Tab panes */
.tab-pane {
  display: none;
}

.tab-pane.active {
  display: block;
}

.page-layout {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ============================================================
   PANELS
   ============================================================ */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow);
}

.panel-header {
  margin-bottom: 20px;
}

.panel-header h2 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}

.panel-header p {
  font-size: 0.85rem;
  color: var(--muted);
}

.panel-header-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.panel-header-row h2 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}

.panel-header-row p {
  font-size: 0.85rem;
  color: var(--muted);
}

.row-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

/* ============================================================
   FORMS
   ============================================================ */
.form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}

.form-row:last-child {
  margin-bottom: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
}

input[type="text"],
input[type="date"],
input[type="file"],
select {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: #f8fafc;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
  width: 100%;
}

input[type="text"]:focus,
input[type="date"]:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
  background: #fff;
}

.select-sm {
  padding: 7px 10px;
  font-size: 0.85rem;
  width: auto;
}

/* Date range */
.date-range {
  display: flex;
  gap: 10px;
  width: 100%;
  min-width: 0;
  flex-wrap: wrap;
}

.date-field {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

.date-field input[type="date"] {
  min-width: 0;
  width: 100%;
}

.date-label {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
}

/* ============================================================
   BUTTONS
   ============================================================ */
button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s, transform 0.1s;
  white-space: nowrap;
}

button:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.3);
  transform: translateY(-1px);
}

button:active:not(:disabled) {
  transform: translateY(0);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-lg {
  padding: 12px 24px;
  font-size: 0.95rem;
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg);
  border-color: var(--text-secondary);
  box-shadow: none;
  color: var(--text);
}

.btn-sm {
  padding: 7px 12px;
  font-size: 0.82rem;
}

.btn-danger {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

.btn-danger:hover:not(:disabled) {
  background: var(--danger-bg);
  box-shadow: none;
}

.action-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

/* ============================================================
   SESSION CARDS
   ============================================================ */
.sessions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}

.scroll-container {
  max-height: 420px;
  overflow-y: auto;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  background: #fafbfc;
}

.session-card {
  padding: 14px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
}

.session-card:hover {
  border-color: var(--accent);
  box-shadow: 0 4px 16px rgba(37, 99, 235, 0.12);
  transform: translateY(-1px);
}

.card-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.card-title {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
}

.card-meta {
  font-size: 0.8rem;
  color: var(--muted);
}

/* ============================================================
   STATUS BADGES
   ============================================================ */
.status {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.status.pending,
.status.running {
  background: #f1f5f9;
  color: #475569;
}

.status.completed {
  background: var(--success-bg);
  color: var(--success);
}

.status.failed {
  background: var(--danger-bg);
  color: var(--danger);
}

/* ============================================================
   TEMPLATES LIST
   ============================================================ */
.templates-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 480px;
  overflow-y: auto;
  padding-right: 4px;
}

/* ============================================================
   TEMPLATES ADMIN LAYOUT
   ============================================================ */
.templates-layout {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

.templates-top-controls {
  display: flex;
  gap: 16px;
  align-items: flex-end;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.top-control {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 220px;
}

.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.templates-sidebar {
  width: 320px;
  max-width: 40%;
  flex-shrink: 0;
}

.templates-sidebar-list {
  margin-top: 12px;
  max-height: 520px;
  overflow-y: auto;
  padding-right: 4px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: #fafbfc;
}

.tpl-sidebar-item {
  padding: 12px 14px;
  border-bottom: 1px solid #f1f5f9;
  cursor: default;
  color: var(--text);
  font-weight: 600;
  font-size: 0.92rem;
}

.tpl-sidebar-item.active {
  background: var(--accent-light);
  color: var(--accent);
  cursor: pointer;
}

.tpl-sidebar-item:hover {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  transition: background 0.15s;
}

.tpl-sidebar-item:last-child {
  border-bottom: none;
}

.templates-table-area {
  flex: 1;
  min-width: 0;
}

.templates-table-wrap {
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  overflow: hidden;
}

.templates-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.templates-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #f1f5f9;
  padding: 10px 12px;
  text-align: left;
  font-weight: 700;
  color: var(--text-secondary);
  border-bottom: 1.5px solid var(--border);
  white-space: nowrap;
}

.templates-table tbody td {
  padding: 10px 12px;
  border-bottom: 1px solid #f1f5f9;
  color: var(--text);
  vertical-align: middle;
  word-break: break-word;
}

.templates-table tbody tr.tpl-table-row {
  cursor: pointer;
  transition: background 0.15s;
}

.templates-table tbody tr.tpl-table-row:hover td {
  background: var(--accent-light);
}

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

.tpl-actions-inline {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  align-items: center;
}

.tpl-icon-button {
  padding: 6px 10px;
}

.templates-table th .th-icon {
  margin-left: 6px;
  vertical-align: middle;
  opacity: 0.6;
}

.tpl-last-parsed {
  display: inline-block;
  margin-right: 10px;
}

/* ============================================================
   TEMPLATE ROW PROGRESS
   ============================================================ */
.tpl-progress-inline {
  margin-top: 10px;
}

.tpl-progress-track {
  width: 100%;
  background: #e5e7eb;
  height: 8px;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.tpl-progress-bar {
  width: 0%;
  height: 100%;
  background: var(--accent);
  transition: width 0.25s ease;
}

.tpl-progress-text {
  margin-top: 7px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-secondary);
}

.tpl-progress-inline.failed .tpl-progress-bar {
  background: var(--danger);
}

.tpl-progress-inline.completed .tpl-progress-bar {
  background: var(--success);
}

.template-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.template-detail-area {
  margin-top: 16px;
}

/* ============================================================
   HISTORY
   ============================================================ */
.history-results-area {
  min-height: 120px;
}

#history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}

.history-actions-inline {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* ============================================================
   REGION CHECKBOXES
   ============================================================ */
.region-checks {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.region-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: #f8fafc;
  font-size: 0.82rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.region-check:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.region-check input {
  accent-color: var(--accent);
}

/* ============================================================
   EMPTY STATE
   ============================================================ */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  color: var(--muted);
  text-align: center;
  gap: 12px;
}

.empty-state svg {
  opacity: 0.35;
}

.empty-state p {
  font-size: 0.9rem;
  color: var(--muted);
}

/* ============================================================
   LOADER
   ============================================================ */
.loader-text {
  display: none;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  padding: 12px 0;
}

/* ============================================================
   MODAL
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
  backdrop-filter: blur(2px);
}

.modal-overlay.open {
  display: flex;
}

.modal {
  background: #fff;
  border-radius: var(--radius-lg);
  padding: 24px;
  width: min(1000px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  position: relative;
}

.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--border);
  background: #fff;
  color: var(--muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
}

.modal-close:hover {
  background: var(--bg);
  color: var(--text);
  border-color: var(--text-secondary);
  box-shadow: none;
  transform: none;
}

.modal-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg);
  border-radius: var(--radius);
  margin-bottom: 20px;
  margin-right: 48px;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-label {
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.radio-label,
.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  cursor: pointer;
}

.radio-label input,
.checkbox-label input {
  accent-color: var(--accent);
  width: auto;
}

/* ============================================================
   DETAIL CARDS (session/template results)
   ============================================================ */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.card {
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  background: #fff;
  box-shadow: var(--shadow-sm);
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 0;
  border-bottom: 1px solid #f1f5f9;
  font-size: 0.85rem;
  gap: 8px;
}

.row:last-child {
  border-bottom: none;
}

.row span:first-child {
  color: var(--muted);
  flex-shrink: 0;
}

.row span:last-child {
  color: var(--text);
  text-align: right;
  word-break: break-word;
}

.price {
  font-weight: 700;
  color: var(--text);
}

.highlight {
  color: var(--accent);
}

/* ============================================================
   MISC
   ============================================================ */
a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.muted {
  color: var(--muted);
  font-size: 0.82rem;
}

.danger {
  color: var(--danger) !important;
  border-color: var(--danger) !important;
}

.danger:hover:not(:disabled) {
  background: var(--danger-bg) !important;
  box-shadow: none !important;
}

h1, h2, h3 {
  line-height: 1.3;
}

h3 {
  font-size: 1rem;
  font-weight: 700;
}

p {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ============================================================
   TEMPLATE MODAL LAYOUT
   ============================================================ */
.tpl-modal-header {
  margin-bottom: 4px;
  padding-right: 52px; /* avoid overlap with X close button */
}

.tpl-modal-header h3 {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.tpl-parse-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--accent-light);
  border: 1.5px solid rgba(37, 99, 235, 0.2);
  border-radius: var(--radius);
  margin: 16px 0;
}

.tpl-modal-sections {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.tpl-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tpl-section-title {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding-bottom: 8px;
  border-bottom: 1.5px solid var(--border);
}

.tpl-history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 380px;
  overflow-y: auto;
}

/* SKU Table */
.sku-table-wrap {
  max-height: 380px;
  overflow-y: auto;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
}

.sku-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.sku-table thead {
  position: sticky;
  top: 0;
  background: #f1f5f9;
  z-index: 1;
}

.sku-table th {
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1.5px solid var(--border);
  white-space: nowrap;
}

.sku-table td {
  padding: 7px 10px;
  border-bottom: 1px solid #f1f5f9;
  color: var(--text);
  vertical-align: middle;
}

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

.sku-table tbody tr:hover td {
  background: var(--accent-light);
}

.sku-num {
  color: var(--muted);
  width: 36px;
  text-align: center;
}

.sku-code {
  font-family: "Courier New", monospace;
  font-weight: 600;
  color: var(--accent);
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 700px) {
  .tpl-modal-sections {
    grid-template-columns: 1fr;
  }
  .templates-layout {
    flex-direction: column;
  }
  .templates-sidebar {
    width: 100%;
    max-width: none;
  }
}

@media (max-width: 768px) {
  .header-inner {
    flex-direction: column;
    height: auto;
    padding: 12px 16px 0;
    gap: 0;
  }

  .brand {
    padding: 8px 0;
  }

  .navbar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .navbar::-webkit-scrollbar {
    display: none;
  }

  .nav-tab {
    padding: 12px 16px;
    font-size: 0.85rem;
  }

  .main-content {
    padding: 16px 12px 48px;
  }

  .panel {
    padding: 16px;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .panel-header-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .row-actions {
    width: 100%;
  }

  .date-range {
    flex-direction: column;
  }
}
