/* ═══════════════════════════════════════════════════════════════════
   TrackMate — Main Stylesheet
   ═══════════════════════════════════════════════════════════════════ */

/* ── Reset & Base ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:       #2563eb;
  --primary-dark:  #1d4ed8;
  --primary-light: #dbeafe;
  --secondary:     #64748b;
  --success:       #16a34a;
  --warning:       #d97706;
  --danger:        #dc2626;
  --bg:            #f8fafc;
  --surface:       #ffffff;
  --border:        #e2e8f0;
  --text:          #1e293b;
  --text-muted:    #64748b;
  --shadow:        0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg:     0 10px 15px rgba(0,0,0,.1), 0 4px 6px rgba(0,0,0,.05);
  --radius:        8px;
  --radius-lg:     12px;
  --font:          'Segoe UI', system-ui, -apple-system, sans-serif;
  --navbar-h:      60px;
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  line-height: 1.5;
}

/* ── Utilities ─────────────────────────────────────────────────────── */
.hidden    { display: none !important; }
.flex      { display: flex; }
.items-center { align-items: center; }
.gap-2     { gap: .5rem; }
.gap-4     { gap: 1rem; }
.text-sm   { font-size: .875rem; }
.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.w-full    { width: 100%; }
.mt-2      { margin-top: .5rem; }
.mt-4      { margin-top: 1rem; }

/* ── Buttons ───────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .5rem 1rem;
  border-radius: var(--radius);
  border: none; cursor: pointer;
  font-size: .875rem; font-weight: 500;
  text-decoration: none; line-height: 1.4;
  transition: all .15s;
}
.btn:active { transform: scale(.97); }
.btn-primary  { background: var(--primary);   color: #fff; }
.btn-primary:hover  { background: var(--primary-dark); }
.btn-secondary { background: #e2e8f0; color: var(--text); }
.btn-secondary:hover { background: #cbd5e1; }
.btn-danger   { background: var(--danger);    color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-ghost    { background: transparent; color: var(--primary); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--primary-light); }
.btn-text     { background: none; border: none; color: var(--primary); cursor: pointer; font-size: .875rem; padding: .25rem; text-decoration: underline; }
.btn-icon     { background: none; border: none; cursor: pointer; padding: .4rem; border-radius: var(--radius); font-size: 1rem; line-height: 1; }
.btn-icon:hover { background: #f1f5f9; }
.btn-sm       { padding: .3rem .7rem; font-size: .8rem; }
.btn-lg       { padding: .75rem 1.5rem; font-size: 1rem; }
.btn-back     { background: none; border: none; cursor: pointer; color: var(--primary); font-size: .9rem; display: inline-flex; align-items: center; gap: .3rem; }
.btn-google   {
  background: #fff; color: #374151;
  border: 1.5px solid #d1d5db;
  padding: .75rem 1.5rem;
  font-size: 1rem; font-weight: 500;
  border-radius: 8px;
  width: 100%; justify-content: center;
  gap: .75rem;
  box-shadow: var(--shadow);
}
.btn-google:hover { background: #f9fafb; box-shadow: var(--shadow-lg); }

/* ── Inputs ────────────────────────────────────────────────────────── */
.input {
  width: 100%;
  padding: .5rem .75rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: .9rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s;
  outline: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }
.input.textarea { resize: vertical; min-height: 80px; }
.input-sm { padding: .35rem .6rem; font-size: .8rem; }

/* ── Forms ─────────────────────────────────────────────────────────── */
.form-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.5rem;
}
.form-section { margin-bottom: 1.75rem; }
.form-section:last-child { margin-bottom: 0; }
.form-section h3 {
  font-size: 1rem; font-weight: 600;
  color: var(--text); margin-bottom: 1rem;
  padding-bottom: .5rem;
  border-bottom: 1px solid var(--border);
}
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: .35rem; color: var(--text); }
.form-row { display: flex; gap: 1rem; flex-wrap: wrap; }
.form-row-2 > * { flex: 1 1 180px; }
.form-actions { padding-top: 1rem; display: flex; gap: .75rem; flex-wrap: wrap; }

/* ── Navbar ────────────────────────────────────────────────────────── */
.navbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--navbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  padding: 0 1rem;
  gap: 1rem;
  z-index: 100;
  box-shadow: var(--shadow);
}
.navbar-brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; min-width: 140px; }
.navbar-logo  { font-size: 1.4rem; }
.navbar-center { display: flex; align-items: center; gap: .25rem; flex: 1; overflow-x: auto; }
.navbar-right  { display: flex; align-items: center; gap: .75rem; margin-left: auto; }
.nav-link {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .4rem .7rem;
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text-muted);
  font-size: .85rem; font-weight: 500;
  white-space: nowrap;
  transition: all .15s;
}
.nav-link:hover, .nav-link.active {
  background: var(--primary-light); color: var(--primary);
}
.nav-icon { font-size: 1.1rem; }

/* User menu */
.user-menu { position: relative; cursor: pointer; }
.user-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; border: 2px solid var(--border); }
.user-dropdown {
  display: none; position: absolute; right: 0; top: 40px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .75rem 1rem;
  box-shadow: var(--shadow-lg); min-width: 160px; z-index: 200;
  flex-direction: column; gap: .5rem;
}
.user-menu:hover .user-dropdown, .user-menu:focus-within .user-dropdown { display: flex; }
.user-name { font-size: .85rem; font-weight: 600; color: var(--text); }

/* ── Pages ─────────────────────────────────────────────────────────── */
#main-content {
  padding-top: calc(var(--navbar-h) + 1rem);
  padding-bottom: 2rem;
  padding-left: 1rem;
  padding-right: 1rem;
  max-width: 1200px;
  margin: 0 auto;
}
.page { display: block; animation: fadeIn .2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.page-header {
  display: flex; align-items: center; gap: 1rem;
  margin-bottom: 1.5rem; flex-wrap: wrap;
}
.page-header h2 { font-size: 1.5rem; font-weight: 700; flex: 1; }
.page-header-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.section-header h3 { font-size: 1.1rem; font-weight: 600; }

/* ── Auth Page ─────────────────────────────────────────────────────── */
#page-login { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 2rem; }
.page-auth { padding-top: 0 !important; }
.auth-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 3rem 2.5rem;
  max-width: 420px; width: 100%;
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.auth-logo    { font-size: 4rem; margin-bottom: 1rem; }
.auth-title   { font-size: 2rem; font-weight: 700; margin-bottom: .25rem; }
.auth-subtitle { color: var(--text-muted); margin-bottom: 2rem; }
.auth-message { padding: 1rem; border-radius: var(--radius); margin-bottom: 1.5rem; font-size: .9rem; }
.auth-message.error   { background: #fee2e2; color: #991b1b; }
.auth-message.warning { background: #fef3c7; color: #92400e; }
.auth-note    { margin-top: 1.5rem; font-size: .8rem; color: var(--text-muted); }

/* ── Stat Cards ────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  text-align: center;
}
.stat-icon  { font-size: 2rem; margin-bottom: .5rem; }
.stat-value { font-size: 1.75rem; font-weight: 700; color: var(--primary); }
.stat-label { font-size: .8rem; color: var(--text-muted); margin-top: .25rem; }

/* ── Tables ────────────────────────────────────────────────────────── */
.table-wrapper { overflow-x: auto; border-radius: var(--radius-lg); box-shadow: var(--shadow); }
table {
  width: 100%; border-collapse: collapse;
  background: var(--surface);
  font-size: .875rem;
}
thead th {
  background: #f1f5f9; color: var(--text-muted);
  padding: .75rem 1rem; text-align: left;
  font-weight: 600; font-size: .8rem; text-transform: uppercase;
  letter-spacing: .05em;
  border-bottom: 1px solid var(--border);
}
tbody tr { border-bottom: 1px solid var(--border); transition: background .1s; }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: #f8fafc; cursor: pointer; }
td { padding: .75rem 1rem; vertical-align: middle; }
tfoot td { padding: .75rem 1rem; font-weight: 700; background: #f8fafc; border-top: 2px solid var(--border); }

/* ── Status Badges ─────────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: .2rem .6rem;
  border-radius: 999px;
  font-size: .75rem; font-weight: 600; line-height: 1.4;
}
.badge-pending   { background: #fef3c7; color: #92400e; }
.badge-shipped   { background: #dbeafe; color: #1e40af; }
.badge-delivered { background: #d1fae5; color: #065f46; }
.badge-cancelled { background: #fee2e2; color: #991b1b; }
.badge-active    { background: #d1fae5; color: #065f46; }
.badge-inactive  { background: #fee2e2; color: #991b1b; }
.badge-pending-user { background: #fef3c7; color: #92400e; }

/* ── Filters Bar ───────────────────────────────────────────────────── */
.filters-bar {
  display: flex; gap: .75rem; flex-wrap: wrap;
  margin-bottom: 1.25rem; align-items: center;
}
.filters-bar .input-sm { max-width: 180px; }
.search-bar { margin-bottom: 1rem; }

/* ── Loading & Empty ───────────────────────────────────────────────── */
.loading-spinner {
  text-align: center; padding: 3rem 1rem;
  color: var(--text-muted); font-size: .9rem;
}
.loading-spinner::before {
  content: ''; display: block; width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 0 auto 1rem;
}
@keyframes spin { to { transform: rotate(360deg); } }
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--text-muted); }

/* ── Cards / List Items ────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 1rem;
  margin-bottom: .75rem;
  transition: box-shadow .15s;
}
.card:hover { box-shadow: var(--shadow); }
.card-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.card-title { font-weight: 600; font-size: .95rem; }
.card-meta  { font-size: .8rem; color: var(--text-muted); }

/* ── Order Items in form ───────────────────────────────────────────── */
.order-item-row {
  display: flex; gap: .75rem; align-items: flex-end;
  padding: .75rem;
  background: #f8fafc;
  border-radius: var(--radius);
  margin-bottom: .5rem;
}
.order-item-row select, .order-item-row input { flex: 1; }
.order-item-row .item-remove { color: var(--danger); flex-shrink: 0; }
.order-total-bar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 1rem 1.5rem;
  background: var(--primary-light);
  border-radius: var(--radius);
  margin-top: 1rem; margin-bottom: 1rem;
  font-size: 1.1rem;
}
.order-total-bar strong { font-size: 1.4rem; color: var(--primary); }

/* ── Waybill Preview ───────────────────────────────────────────────── */
.waybill-controls {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}
@media (max-width: 768px) { .waybill-controls { grid-template-columns: 1fr; } }
.waybill-checklist { max-height: 300px; overflow-y: auto; margin-top: .75rem; }
.waybill-checklist label {
  display: flex; align-items: center; gap: .5rem;
  padding: .5rem; border-radius: var(--radius);
  cursor: pointer; font-size: .85rem;
}
.waybill-checklist label:hover { background: #f1f5f9; }
.waybill-preview-area {
  background: var(--surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  padding: 1.5rem;
  min-height: 400px;
}

/* ── Toast Notifications ───────────────────────────────────────────── */
.toast-container {
  position: fixed; bottom: 1.5rem; right: 1.5rem;
  display: flex; flex-direction: column; gap: .5rem;
  z-index: 1000;
}
.toast {
  background: var(--text);
  color: #fff;
  padding: .75rem 1.25rem;
  border-radius: var(--radius);
  font-size: .875rem;
  box-shadow: var(--shadow-lg);
  animation: slideInRight .25s ease;
  max-width: 320px;
}
.toast.success { background: var(--success); }
.toast.error   { background: var(--danger); }
.toast.warning { background: var(--warning); }
@keyframes slideInRight { from { transform: translateX(120%); opacity: 0; } to { transform: none; opacity: 1; } }

/* ── Modal ─────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 500; padding: 1rem;
}
.modal-box {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: 100%; max-width: 500px;
  box-shadow: var(--shadow-lg);
  animation: modalIn .2s ease;
  overflow: hidden;
}
@keyframes modalIn { from { transform: scale(.95); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 1.25rem 1.5rem; border-bottom: 1px solid var(--border); }
.modal-header h3 { font-size: 1.1rem; font-weight: 600; }
.modal-body   { padding: 1.5rem; }
.modal-footer { padding: 1rem 1.5rem; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: .75rem; }

/* ── Install Banner ────────────────────────────────────────────────── */
.install-banner {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--primary);
  color: #fff;
  padding: .75rem 1.5rem;
  display: flex; align-items: center; gap: 1rem;
  z-index: 90;
}
.install-banner span { flex: 1; font-size: .9rem; }

/* ── Order Detail ──────────────────────────────────────────────────── */
.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}
.detail-block { background: var(--surface); border-radius: var(--radius-lg); padding: 1.25rem; box-shadow: var(--shadow); }
.detail-block h4 { font-size: .9rem; font-weight: 600; margin-bottom: .75rem; color: var(--text-muted); text-transform: uppercase; font-size: .75rem; letter-spacing: .05em; }
.detail-row { display: flex; justify-content: space-between; padding: .35rem 0; border-bottom: 1px solid #f1f5f9; font-size: .875rem; }
.detail-row:last-child { border-bottom: none; }
.detail-row label { color: var(--text-muted); }
.detail-row span  { font-weight: 500; }

/* ── Courier Colors ────────────────────────────────────────────────── */
.courier-slpost      { color: #065f46; background: #d1fae5; }
.courier-koobiyo     { color: #1e40af; background: #dbeafe; }
.courier-royalexpress { color: #6b21a8; background: #f3e8ff; }
.courier-custom      { color: var(--text); background: #f1f5f9; }

/* ── Checkbox hack ─────────────────────────────────────────────────── */
.checkbox-row { display: flex; align-items: center; gap: .5rem; }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--primary); cursor: pointer; }

/* ── Responsive ────────────────────────────────────────────────────── */
@media (max-width: 640px) {
  .navbar-center { gap: 0; }
  .nav-text { display: none; }
  .nav-link { padding: .4rem .5rem; }
  #main-content { padding-left: .75rem; padding-right: .75rem; }
  .form-card { padding: 1rem; }
  .auth-card { padding: 2rem 1.25rem; }
}

/* ══════════════════════════════════════════════ PRINT STYLES ════════ */
@media print {
  .navbar, #install-banner, .toast-container, .modal-overlay,
  .btn, .filters-bar, .page-header-actions, #export-csv-btn, #export-pdf-btn { display: none !important; }
  body { background: #fff; }
  #main-content { padding: 0; margin: 0; max-width: none; }
  .page { break-inside: avoid; }
}

/* ══════════════════════════════════════════════ WAYBILL PRINT ═══════ */
.waybill-print-sheet {
  display: flex;
  flex-wrap: wrap;
  gap: 4mm;
}

.waybill {
  border: 2px solid #000;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 11px;
  background: #fff;
  page-break-inside: avoid;
  break-inside: avoid;
  overflow: hidden;
}

.waybill-header {
  padding: 6px 10px;
  font-weight: bold;
  font-size: 13px;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.waybill-header.slpost      { background: #065f46; }
.waybill-header.koobiyo     { background: #1e40af; }
.waybill-header.royalexpress { background: #6b21a8; }
.waybill-header.custom      { background: #374151; }

.waybill-body { padding: 8px 10px; }
.waybill-section { margin-bottom: 6px; }
.waybill-section-title { font-weight: bold; font-size: 9px; text-transform: uppercase; color: #666; margin-bottom: 2px; }
.waybill-section p  { margin: 1px 0; line-height: 1.4; }
.waybill-divider    { border: none; border-top: 1px dashed #ccc; margin: 6px 0; }
.waybill-items      { width: 100%; border-collapse: collapse; font-size: 10px; }
.waybill-items th   { background: #f5f5f5; padding: 2px 4px; text-align: left; font-size: 9px; }
.waybill-items td   { padding: 2px 4px; border-bottom: 1px solid #eee; }
.waybill-cod        { text-align: center; padding: 6px; border-top: 1.5px solid #000; }
.waybill-cod strong { font-size: 16px; }
.waybill-cod span   { font-size: 9px; display: block; }

/* Waybill page sizes */
.waybill-A4        { width: 200mm; }
.waybill-A5        { width: 140mm; }
.waybill-thermal80 { width: 72mm; font-size: 9px; }
.waybill-thermal58 { width: 50mm; font-size: 8px; }

/* Print: 1 per page */
.per-page-1 .waybill { page-break-after: always; break-after: page; width: 100%; }
/* Print: 2 per page */
.per-page-2 .waybill { width: calc(50% - 2mm); }
/* Print: 4 per page */
.per-page-4 .waybill { width: calc(25% - 3mm); }

/* Sinhala font support */
.lang-si { font-family: 'Noto Sans Sinhala', 'Iskoola Pota', sans-serif; }
