:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --ok: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --chip: #f1f5f9;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: #f8fafc;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0 0; }

/* ---------- Login ---------- */
.login {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, #0f172a, #1e3a8a); padding: 20px;
}
.login-card {
  background: var(--panel); border-radius: 16px; padding: 28px; width: 100%; max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
.login-card h1 { margin: 0 0 4px; font-size: 24px; }
.login-card label { display: block; margin: 14px 0 0; font-size: 13px; font-weight: 600; color: var(--muted); }
.login-card input { width: 100%; margin-top: 6px; }

/* ---------- Shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 14px;
  background: var(--bg); color: #fff; padding: 10px 16px; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 18px; }
.nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.nav button {
  background: transparent; color: #cbd5e1; border: 0; padding: 8px 12px; border-radius: 8px;
  font-size: 14px; cursor: pointer; font-weight: 600;
}
.nav button:hover { background: var(--bg-soft); color: #fff; }
.nav button.active { background: var(--brand); color: #fff; }
.user-box { display: flex; align-items: center; gap: 10px; }
.user-box .muted { color: #94a3b8; font-size: 13px; }
.view { padding: 18px; max-width: 1100px; margin: 0 auto; }

/* ---------- Forms / inputs ---------- */
input, select {
  font: inherit; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--ink);
}
input:focus, select:focus { outline: 2px solid var(--brand); border-color: var(--brand); }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; font-weight: 600; color: var(--muted); }
.row { display: flex; gap: 14px; flex-wrap: wrap; }
.row .field { flex: 1; min-width: 160px; }

/* ---------- Toggle (Empty / Full) ---------- */
.toggle { display: inline-flex; gap: 8px; }
.toggle-btn {
  font: inherit; font-weight: 600; cursor: pointer; padding: 10px 18px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
}
.toggle-btn[data-val=empty].active { background: #dbeafe; border-color: #2563eb; color: #1d4ed8; }
.toggle-btn[data-val=full].active { background: #fef08a; border-color: #ca8a04; color: #854d0e; }

/* ---------- Buttons ---------- */
.btn {
  font: inherit; font-weight: 600; border: 1px solid transparent; border-radius: 10px;
  padding: 10px 16px; cursor: pointer; background: var(--chip); color: var(--ink);
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--brand-dark); }
.btn.ghost { background: transparent; color: #cbd5e1; border-color: #334155; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.block { width: 100%; margin-top: 18px; }

/* ---------- Cards / panels ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; margin-bottom: 16px; }
.card h2 { margin: 0 0 14px; font-size: 18px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; }

/* ---------- Stat ---------- */
.stat { display: flex; align-items: baseline; gap: 12px; }
.stat .num { font-size: 52px; font-weight: 800; color: var(--brand); line-height: 1; }
.stat .lbl { color: var(--muted); font-weight: 600; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: #f8fafc; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge.on_yard { background: #dcfce7; color: #166534; }
.badge.left { background: #e2e8f0; color: #475569; }
.badge.load-empty { background: #dbeafe; color: #1d4ed8; }   /* blue */
.badge.load-full { background: #fef08a; color: #854d0e; }    /* yellow */
.badge.dock { background: #ede9fe; color: #6d28d9; }         /* purple */
.badge.unclaimed { background: #ffedd5; color: #c2410c; }    /* orange */
.badge.claimed { background: #ccfbf1; color: #0f766e; }      /* teal */

/* Dashboard row highlight by claim state. */
tr.clickable.row-unclaimed { background: #fff7ed; }
tr.clickable.row-unclaimed:hover { background: #ffedd5; }
tr.clickable.row-claimed { background: #f0fdfa; }
tr.clickable.row-claimed:hover { background: #ccfbf1; }

.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Dashboard filter/sort controls. */
.dash-controls { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.hist-search { flex: 1 1 240px; }
.date-lbl { font-size: 13px; font-weight: 600; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }

/* ---------- System monitor ---------- */
.status-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.status-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.status-dot.ok { background: var(--ok); }
.status-dot.bad { background: var(--danger); }
.err-msg { max-width: 420px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { background: #fff; border: 0; padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--brand); color: #fff; }

/* ---------- Dock board ---------- */
.dock-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
/* Scoped to the dock board so it never collides with the `.badge.dock` pill. */
.dock-grid .dock {
  border: 2px solid var(--line); border-radius: 12px; padding: 12px 10px; text-align: center;
  display: flex; flex-direction: column; gap: 6px; min-height: 96px; background: #fff;
}
.dock-grid .dock.available { border-color: #86efac; background: #f0fdf4; }
.dock-grid .dock.occupied { border-color: #fbbf24; background: #fffbeb; }
.dock-grid .dock .dlabel { font-weight: 700; font-size: 14px; }
.dock-grid .dock .dstate { color: #16a34a; font-weight: 600; font-size: 13px; margin-top: auto; }
.dock-grid .dock .occ { font-size: 13px; line-height: 1.4; }
.dock-grid .dock .release { margin-top: auto; }

.yard-load {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.yard-load:last-child { border-bottom: 0; }
.yl-info { display: flex; align-items: center; gap: 8px; }
.yl-actions { display: flex; align-items: center; gap: 8px; }

/* ---------- Photo checklist ---------- */
.progress { font-weight: 700; margin: 6px 0 14px; }
.progress.done { color: var(--ok); }
.progress.pending { color: var(--warn); }
.slot-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.slot {
  border: 2px dashed var(--line); border-radius: 12px; padding: 10px; text-align: center; position: relative;
  display: flex; flex-direction: column; gap: 8px; background: #fff;
}
.slot.captured { border-style: solid; border-color: var(--ok); }
.slot .slot-label { font-weight: 700; font-size: 13px; }
.slot .req { font-size: 11px; font-weight: 700; }
.slot .req.required { color: var(--danger); }
.slot .req.optional { color: var(--muted); }
.slot .thumb { width: 100%; height: 96px; object-fit: cover; border-radius: 8px; background: #f1f5f9; }
.slot .ph { height: 96px; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 28px; background: #f8fafc; border-radius: 8px; }
.slot input[type=file] { display: none; }
.slot .cap-btn { font-size: 13px; }

/* ---------- Photo detail grid ---------- */
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; }
.photo {
  border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; position: relative;
}
.photo img { width: 100%; height: 150px; object-fit: cover; display: block; cursor: zoom-in; background: #f1f5f9; }
.photo .cap { padding: 8px 10px; font-size: 13px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.photo.missing { border-style: dashed; }
.photo.missing .ph { height: 150px; display: flex; align-items: center; justify-content: center; color: var(--danger); font-weight: 700; font-size: 13px; text-align: center; padding: 8px; }
.photo .del { background: var(--danger); color: #fff; border: 0; border-radius: 6px; padding: 3px 8px; font-size: 12px; cursor: pointer; }
.flag { color: var(--danger); font-size: 12px; font-weight: 700; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.modal-bg { position: absolute; inset: 0; background: rgba(0,0,0,.8); }
.modal img { position: relative; max-width: 92vw; max-height: 92vh; border-radius: 8px; }
.modal-card { position: relative; background: #fff; border-radius: 12px; padding: 24px;
  width: 92vw; max-width: 440px; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.modal-card h2 { margin: 0 0 6px; }
.modal-card p.muted { margin: 0 0 14px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 10px; font-weight: 600;
  box-shadow: 0 10px 30px rgba(0,0,0,.3); max-width: 90vw;
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ---------- Loading overlay (uploads) ---------- */
.loading-overlay {
  position: fixed; inset: 0; z-index: 70; display: flex; align-items: center; justify-content: center;
  background: rgba(15, 23, 42, .55);
}
.loading-box {
  background: #fff; border-radius: 14px; padding: 26px 34px; text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.35); display: flex; flex-direction: column; align-items: center; gap: 14px;
}
.spinner {
  width: 40px; height: 40px; border-radius: 50%;
  border: 4px solid var(--line); border-top-color: var(--brand); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-msg { font-weight: 700; color: var(--ink); }

/* ---------- Pagination ---------- */
.pager { display: flex; align-items: center; gap: 12px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.pager .info { color: var(--muted); font-size: 13px; }

.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 16px; }
.kv { background: var(--chip); border-radius: 10px; padding: 10px 12px; }
.kv .k { font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: .04em; }
.kv .v { font-weight: 700; margin-top: 2px; }
.section-title { font-size: 15px; font-weight: 700; margin: 18px 0 10px; }
.back-link { background: none; border: 0; color: var(--brand); font-weight: 600; cursor: pointer; padding: 0; margin-bottom: 10px; }

@media (max-width: 640px) {
  .view { padding: 14px; }
  .nav button { padding: 7px 9px; font-size: 13px; }
  .brand { font-size: 16px; }
}

/* ---------- TEMU – iMile / Customs Broker ---------- */
.badge.temu { background: #fef08a; color: #92400e; border: 1px solid #f59e0b; }

/* Accent checkbox on the check-in form so guards notice it. */
.temu-check { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; margin-top: 4px;
  padding: 12px 14px; border: 2px solid #f59e0b; border-radius: 10px; background: #fffbeb; }
.temu-check input { position: absolute; opacity: 0; width: 0; height: 0; }
.temu-check-box { width: 22px; height: 22px; border-radius: 6px; border: 2px solid #f59e0b; background: #fff;
  display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.temu-check input:checked + .temu-check-box { background: #f59e0b; }
.temu-check input:checked + .temu-check-box::after { content: "✓"; color: #fff; font-weight: 800; font-size: 14px; }
.temu-check-text { font-weight: 700; color: #92400e; }

/* Yellow warning shown before docking a TEMU trailer with no MAWB. */
.temu-warn { margin-top: 10px; padding: 10px 12px; border: 1px solid #f59e0b; background: #fffbeb;
  border-radius: 10px; color: #92400e; font-weight: 600; display: flex; align-items: center;
  justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.temu-warn-actions { display: flex; gap: 8px; }

/* Inline link-style button (e.g. trailer link in the broker shipments table). */
.link-btn { background: none; border: 0; padding: 0; color: var(--brand); font: inherit;
  font-weight: 600; cursor: pointer; text-decoration: underline; }

/* MAWB status shown to LPS on the dock board / assign list. */
.mawb-line { margin-top: 6px; font-size: 12px; color: var(--muted); display: flex; align-items: center;
  gap: 6px; flex-wrap: wrap; }
.mawb-line .mawb-k { font-weight: 700; }
.mawb-line.none { color: #92400e; font-weight: 700; }
.badge.mawb { background: #ccfbf1; color: #0f766e; word-break: break-all; }

/* Inline new-shipment form (broker portal). */
.ship-form { background: var(--chip); border-radius: 10px; padding: 14px; margin-bottom: 14px; }
.ship-form textarea { font: inherit; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--ink); width: 100%; resize: vertical; }

/* ---------- TEMU-iMile shipment lifecycle ---------- */
/* Stage badges (one per lifecycle step). */
.badge.stage-created { background: #e2e8f0; color: #475569; }
.badge.stage-inbound_matched { background: #dbeafe; color: #1d4ed8; }
.badge.stage-scanning { background: #dcfce7; color: #166534; }
.badge.stage-manipulated { background: #ffedd5; color: #c2410c; }
.badge.stage-loading { background: #ede9fe; color: #6d28d9; }
.badge.stage-loaded { background: #ccfbf1; color: #0f766e; }

/* Handling type: segregation (full scan flow) vs cross-dock (straight across). */
.badge.crossdock { background: #ede9fe; color: #6d28d9; border: 1px solid #a78bfa; }
.badge.segregation { background: #dbeafe; color: #1d4ed8; }

/* Per-MAWB pedimento numbers (Mexican CB). */
.badge.ped { background: #fef3c7; color: #92400e; }

/* Total time & exceptions. */
.ex-totals { display: flex; flex-wrap: wrap; gap: 18px; margin-bottom: 12px; }
.ex-total .ex-k { color: var(--muted); margin-right: 4px; }
.ex-adj { color: #0f766e; }
.ex-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
  padding: 8px 0; border-top: 1px solid var(--border, #e2e8f0); }
.ex-list { margin-bottom: 10px; }
.ex-bottleneck { margin: -4px 0 12px; font-size: 0.9em; }
.ped-row { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.ped-arrow { color: #94a3b8; }
.sp-peds .field { margin-top: 8px; }

/* Dynamic MAWB inputs on the new-shipment form. */
#mawb-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.mawb-input { display: flex; gap: 8px; }
.mawb-input .mawb-field { flex: 1; }

/* Trailer match / assign rows. */
.trailer-row { padding: 8px 0; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.trailer-row:last-child { border-bottom: 0; }
.trailer-k { font-weight: 700; color: var(--muted); min-width: 140px; }
.inline-pick { display: inline-flex; gap: 8px; align-items: center; }

/* Vertical timeline stepper. */
.timeline { display: flex; flex-direction: column; }
.tl-step { display: flex; gap: 12px; padding: 6px 0; position: relative; }
.tl-step:not(:last-child)::before { content: ""; position: absolute; left: 13px; top: 30px; bottom: -6px; width: 2px; background: var(--line); }
.tl-dot { flex: 0 0 auto; width: 28px; height: 28px; border-radius: 50%; background: var(--chip); color: var(--muted);
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 13px; border: 2px solid var(--line); z-index: 1; }
.tl-step.done .tl-dot { background: var(--ok); color: #fff; border-color: var(--ok); }
.tl-body { padding-top: 3px; }
.tl-label { font-weight: 700; }
.tl-time { font-size: 13px; color: var(--muted); margin-top: 2px; }
.tl-inline { display: flex; gap: 8px; align-items: center; margin-top: 6px; flex-wrap: wrap; }
.btn.ok-btn { background: var(--ok); color: #fff; }
.btn.warn-btn { background: var(--warn); color: #fff; }
.btn.blue-btn { background: var(--brand); color: #fff; }

/* Documents section. */
.doc-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.doc-row:last-child { border-bottom: 0; }
.doc-name { font-weight: 700; min-width: 150px; }
.doc-dl { flex: 1; min-width: 160px; }
.doc-up { display: inline-flex; gap: 8px; align-items: center; }

/* KPI dashboard. */
.kpi-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; text-align: center; }
.kpi-val { font-size: 28px; font-weight: 800; color: var(--brand); }
.kpi-lbl { color: var(--muted); font-weight: 600; font-size: 13px; margin-top: 4px; }
.hbar-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; }
.hbar-lbl { min-width: 120px; font-weight: 600; }
.hbar-track { flex: 1; height: 18px; background: var(--chip); border-radius: 999px; overflow: hidden; }
.hbar-fill { display: block; height: 100%; background: var(--brand); }
.hbar-val { min-width: 70px; text-align: right; font-weight: 700; color: var(--muted); }

