/* =====================================================================
   Container Tracking : visual system

   Reading this as: an internal field-ops tool for freight staff, in a warm
   utilitarian operations language, dial ENERGY 2 / RHYTHM 2 / MOTION 1.

   Navy #1d3557 is the colour the app already shipped, so it stays the core
   and the tool stays recognisable (R-01).
   One accent, amber #9a5c00, marks the current lifecycle stage and nothing
   else, so the eye always lands on where a container is right now (R-29).
   One motif, the segmented progress rail, repeats in every list row and in
   the map popup; that repetition is what makes the screen identifiable (R-20).
   Motion covers state changes the user caused: hover, focus, the mobile
   drawer, the toast. Nothing animates on its own and nothing glows (R-13, R-19).
   system-ui with no webfont, so the panel still renders on the mobile
   connection out in the yard (R-06).
   The rail is built from tgl_muat, tgl_dooring and completed_at only, so no
   figure in this UI is invented (R-17).
   ===================================================================== */

:root {
  --navy: #1d3557;
  --navy-hi: #2a4a74;
  --navy-lo: #142943;

  --ink: #1b2431;
  --ink-2: #5a6675;

  --line: #dde3ea;
  --line-2: #eef1f5;

  --surface: #ffffff;
  --canvas: #f4f6f9;

  --accent: #9a5c00;
  --accent-soft: #fdf3e3;

  --danger: #8f1d14;
  --danger-soft: #fdeceb;
  --danger-line: #f2c8c4;

  --topbar: 58px;
  --panel-w: 340px;

  --r-sm: 6px;
  --r-md: 9px;
  --r-lg: 14px;

  /* Shadow only where something genuinely lifts above the page: the modal,
     the mobile drawer, and the fixed top bar that floats over the map (R-12). */
  --shadow-1: 0 1px 3px rgba(16, 26, 40, 0.14);
  --shadow-3: 0 18px 44px -14px rgba(16, 26, 40, 0.42);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--canvas);
  -webkit-text-size-adjust: 100%;
}

h1,
h2,
h3,
p,
ul,
dl,
dd {
  margin: 0;
}

button {
  font: inherit;
  color: inherit;
}

/* One visible focus ring everywhere. It is never removed without a
   replacement, which is why the search field moves its ring onto the
   wrapper instead (R-32). */
:focus-visible {
  outline: 2px solid var(--navy-hi);
  outline-offset: 2px;
  border-radius: 3px;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 0 14px;
  font-weight: 550;
  line-height: 1;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  cursor: pointer;
  text-decoration: none;
}
.btn:hover {
  border-color: var(--ink-2);
  background: #fbfcfe;
}
.btn:active {
  background: var(--line-2);
}

.btn-primary {
  color: #fff;
  background: var(--navy);
  border-color: var(--navy);
}
.btn-primary:hover {
  background: var(--navy-hi);
  border-color: var(--navy-hi);
}
.btn-primary:active {
  background: var(--navy-lo);
}

/* Ghost reads on navy without competing with the primary action. */
.btn-ghost {
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.22);
  text-decoration: none;
}
.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.38);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  color: inherit;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  cursor: pointer;
}
.btn-icon:hover {
  background: rgba(255, 255, 255, 0.2);
}

.btn-danger {
  color: var(--danger);
  background: var(--danger-soft);
  border-color: var(--danger-line);
}
.btn-danger:hover {
  background: #fbdcd9;
  border-color: #e2a9a3;
}

.btn[disabled] {
  opacity: 0.55;
  cursor: default;
}

/* The dialog holds the screen while it is open. */
body.modal-open {
  overflow: hidden;
}

/* Minimum 44px tap target wherever the pointer is a finger (R-03). */
@media (pointer: coarse) {
  .btn,
  .btn-icon,
  .btn-restore
  .btn-de {
    min-height: 44px;
  }
  .btn-icon {
    width: 44px;
    height: 44px;
  }
}

/* ---------------------------------------------------------------- top bar */

#topbar {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--topbar);
  padding: 0 10px;
  color: #fff;
  background: var(--navy);
  /* The bar floats over the map, so its edge has to hold over bright tiles. */
  box-shadow: var(--shadow-1);
}

.brand {
  font-size: 15px;
  font-weight: 650;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

/* Marked state, not decoration: the button is armed and the map is listening. */
#btn-add[aria-pressed="true"] {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}
#btn-add[aria-pressed="true"]:hover {
  background: #8a5200;
  border-color: #8a5200;
}

#hint {
  font-size: 13px;
  color: #dde1e6;
}
#hint:empty {
  display: none;
}

.topbar-end {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

@media (max-width: 640px) {
  #topbar .brand,
  .btn-label-full {
    display: none;
  }
}

@media (min-width: 641px) {
  .btn-label-short {
    display: none;
  }
}

/* ------------------------------------------------------------------- map */

#map {
  position: absolute;
  inset: var(--topbar) 0 0 var(--panel-w);
  background: var(--canvas);
}
body.panel-hidden #map {
  left: 0;
}
body.adding #map {
  cursor: crosshair;
}

/* ----------------------------------------------------------------- panel */

#panel {
  position: fixed;
  top: var(--topbar);
  left: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  width: var(--panel-w);
  background: var(--surface);
  border-right: 1px solid var(--line);
}
body.panel-hidden #panel {
  display: none;
}

/* The tabs double as the running tally, so the counts are the same numbers
   the list below is showing. Nothing here is estimated (R-17). */
.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 8px 0;
  border-bottom: 1px solid var(--line);
}

.tab {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 6px 8px;
  font-weight: 550;
  color: var(--ink-2);
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  cursor: pointer;
}
.tab:hover {
  color: var(--navy);
}
.tab[aria-selected="true"] {
  color: var(--navy);
  border-bottom-color: var(--navy);
  font-weight: 650;
}

.count {
  min-width: 24px;
  padding: 1px 7px;
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--ink-2);
  background: var(--line-2);
  border-radius: var(--r-sm);
}
.count:empty {
  display: none;
}
.tab[aria-selected="true"] .count {
  color: #fff;
  background: var(--navy);
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 10px 8px;
  padding: 0 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
}
.search-wrap:focus-within {
  border-color: var(--navy);
  box-shadow: 0 0 0 3px rgba(29, 53, 87, 0.15);
}
.search-wrap svg {
  flex: none;
  color: var(--ink-2);
}
#search {
  flex: 1;
  min-width: 0;
  height: 40px;
  font: inherit;
  color: var(--ink);
  background: none;
  border: 0;
}
/* The visible ring lives on .search-wrap:focus-within above; this only clears
   the default one so the field never shows two rings at once (R-32). */
#search:focus {
  outline: none;
}
#search::placeholder {
  color: var(--ink-2);
  opacity: 1;
}
#search::-webkit-search-cancel-button {
  cursor: pointer;
}

.panel-note {
  padding: 0 12px 10px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.panel-note[hidden] {
  display: none;
}

#panel-body {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.rows {
  list-style: none;
  padding: 0 0 18px;
}

.row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.row + .row {
  border-top: 1px solid var(--line-2);
}
.row[hidden] {
  display: none;
}

/* Shared row layout. .row-btn is the same block plus interactivity, so an
   active row (which zooms the map) and a completed row (which does nothing)
   stay visually identical without a dead control (R-26). */
.row-body {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: 7px;
  padding: 12px 12px 13px;
  text-align: left;
}
.row-btn {
  background: none;
  border: 0;
  cursor: pointer;
}
.row-btn:hover {
  background: #f7f9fc;
}
.row-btn:active {
  background: var(--line-2);
}

.row-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.row-title {
  min-width: 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}
.row-no {
  margin-left: auto;
  flex: none;
  font-size: 12px;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.row-route {
  font-size: 12.5px;
  color: var(--ink-2);
  /* Ellipsis rather than overflow, so a long factory name can never push the
     panel wider than its 340px (R-03). */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-route .to {
  padding: 0 3px;
  color: var(--ink-2);
}

.row-foot {
  display: flex;
  align-items: center;
  gap: 8px;
}
.row-date {
  margin-left: auto;
  font-size: 12px;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* The motif. Three segments always mean the same three stages, in every row
   and in the map popup. */
.rail {
  display: flex;
  gap: 3px;
}
.rail i {
  flex: 1;
  height: 4px;
  background: var(--line);
  border-radius: 2px;
}
.rail i.is-done {
  background: var(--navy);
}
.rail i.is-now {
  background: var(--accent);
}

.chip {
  display: inline-flex;
  align-items: center;
  flex: none;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: var(--r-sm);
}
.chip-done {
  color: var(--navy);
  background: var(--line-2);
}
.chip-idle {
  color: var(--ink-2);
  background: var(--line-2);
}

.btn-restore {
  flex: none;
  margin-right: 10px;
  min-height: 32px;
  padding: 0 12px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  cursor: pointer;
}
.btn-restore:hover {
  color: var(--navy);
  border-color: var(--navy);
}

.btn-delete {
  flex: none;
  margin-right: 10px;
  min-height: 32px;
  padding: 0 12px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  cursor: pointer;
}
.btn-delete:hover {
  color: var(--navy);
  border-color: var(--navy);
}


/* Every state names its cause and the next move, never just "no data" (R-27). */
.state {
  padding: 30px 22px;
  font-size: 13px;
  color: var(--ink-2);
  text-align: center;
}
.state-title {
  margin-bottom: 5px;
  font-size: 14px;
  font-weight: 650;
  color: var(--ink);
}
.state .btn {
  margin-top: 14px;
}

/* ----------------------------------------------------------------- modal */

#modal-backdrop,
#customer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(16, 26, 40, 0.5);
}
#modal-backdrop[hidden],
#customer-backdrop[hidden] {
  display: none;
}

#modal,
#customer-modal {
  width: 100%;
  max-width: 460px;
  max-height: 100%;
  overflow: auto;
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-3);
}
#customer-modal {
  max-width: 520px;
}

.modal-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 12px 12px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.modal-head h2 {
  font-size: 16px;
  font-weight: 650;
}
.modal-close {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  color: var(--ink-2);
  background: none;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  cursor: pointer;
}
.modal-close:hover {
  color: var(--ink);
  background: var(--line-2);
}

.modal-body {
  padding: 18px;
}

fieldset {
  padding: 0;
  margin: 0 0 18px;
  border: 0;
}
fieldset + fieldset {
  padding-top: 16px;
  border-top: 1px solid var(--line-2);
}
legend {
  padding: 0 0 10px;
  font-size: 13px;
  font-weight: 650;
  color: var(--navy);
}

/* The modal groups mirror the rail stages in order: Muat, then Dooring. */
.field {
  margin-bottom: 12px;
}
.field label {
  display: block;
  margin-bottom: 5px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--ink-2);
}
#modal input:not([type="hidden"]):not([type="radio"]):not([type="file"]):not([type="color"]),
#modal select,
#customer-modal input:not([type="hidden"]):not([type="color"]) {
  width: 100%;
  min-height: 40px;
  padding: 8px 11px;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
}
#modal input:not([type="hidden"]):not([type="radio"]):not([type="file"]):not([type="color"]):focus,
#modal select:focus,
#customer-modal input:not([type="hidden"]):not([type="color"]):focus {
  border-color: var(--navy);
}
.field-note {
  margin-top: 5px;
  font-size: 12px;
  color: var(--ink-2);
}

.form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 4px;
}

#customer-modal input[type="color"] {
  width: 56px;
  height: 40px;
  min-height: 40px;
  padding: 4px;
  cursor: pointer;
}

.customer-list {
  display: grid;
  gap: 8px;
  max-height: 250px;
  overflow: auto;
  margin-bottom: 18px;
}
.customer-item {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 44px;
  padding: 5px 7px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
}
.customer-swatch {
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  background: var(--customer-color);
  border: 1px solid rgba(0, 0, 0, 0.18);
  border-radius: 50%;
}
.customer-item-name {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}
.customer-item-color {
  font: 12px/1.2 ui-monospace, SFMono-Regular, Consolas, monospace;
  color: var(--ink-2);
}
.customer-item .btn {
  min-height: 34px;
  padding: 0 9px;
}
.customer-empty {
  padding: 10px 0;
  color: var(--ink-2);
}
.customer-manager #customer-form {
  padding-top: 16px;
  border-top: 1px solid var(--line-2);
}

/* ---------------------------------------------------------------- toasts */

#toasts {
  position: fixed;
  left: 50%;
  bottom: 22px;
  z-index: 3000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transform: translateX(-50%);
  pointer-events: none;
}

.toast {
  max-width: min(92vw, 440px);
  padding: 11px 15px;
  font-size: 13px;
  font-weight: 550;
  color: #fff;
  background: var(--navy);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-3);
  transition: opacity 0.15s ease;
}
.toast-error {
  background: var(--danger);
}

/* ---------------------------------------------------------- map markers */

.ct-marker {
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.3));
}

/* Customer color is shared by the label and its pointer. */
.ct-marker-box .ct-label-box {
  position: relative;
  display: block;
  width: max-content;
  max-width: 220px;
  padding: 5px 9px;
  font: 600 11.5px/1.35 system-ui, sans-serif;
  color: var(--label-ink, #fff);
  text-align: center;
  overflow-wrap: anywhere;
  background: var(--customer-color, var(--navy));
  border-radius: var(--r-sm);
  transform: translate(-50%, -100%);
}
.ct-marker-box .ct-label-box::after {
  content: "";
  position: absolute;
  bottom: -5px;
  left: 50%;
  margin-left: -5px;
  border: 5px solid transparent;
  border-top: 5px solid var(--customer-color, var(--navy));
  border-bottom: 0;
}

.popup {
  min-width: 232px;
  font: 13px/1.45 system-ui, sans-serif;
  color: var(--ink);
}
.popup-head {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 4px;
}
.popup-title {
  font-weight: 650;
}
.popup-customer {
  font-weight: 600;
}
.popup-stage {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 10px;
}
.popup-stage .rail {
  flex: 1;
}
.popup dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 10px;
  margin-top: 9px;
}
.popup dt {
  color: var(--ink-2);
  white-space: nowrap;
}
.popup dd {
  overflow-wrap: anywhere;
}
.popup-actions {
  display: flex;
  gap: 6px;
  margin-top: 12px;
}
.popup-actions button {
  flex: 1;
  min-height: 32px;
  padding: 0 8px;
  font: inherit;
  font-weight: 550;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  cursor: pointer;
}
.popup-actions button:hover {
  color: var(--navy);
  border-color: var(--navy);
}
.popup-actions .danger {
  color: var(--danger);
}
.popup-actions .danger:hover {
  color: var(--danger);
  border-color: var(--danger);
}

/* ---------------------------------------------------------- auth screens */

body.auth-page {
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--canvas);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  padding: 26px 24px 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1);
}
.auth-card .brand {
  display: block;
  margin-bottom: 16px;
  color: var(--navy);
}
.auth-card h1 {
  font-size: 20px;
  font-weight: 650;
}
.auth-lede {
  margin: 6px 0 20px;
  font-size: 13px;
  color: var(--ink-2);
}
.auth-card input[type="password"],
.auth-card input[type="text"] {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
}
.auth-card input:focus {
  border-color: var(--navy);
}
.auth-card .btn {
  width: 100%;
  min-height: 44px;
  margin-top: 16px;
}
.auth-foot {
  margin-top: 18px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.auth-foot a {
  color: var(--navy);
}

.alert {
  margin-bottom: 16px;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid var(--danger-line);
  border-radius: var(--r-md);
}
.alert-ok {
  color: var(--navy);
  background: var(--line-2);
  border-color: #cfdbe9;
}
.alert-warn {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: #f0dcb8;
}

/* --------------------------------------------------- scrim + small screens */

#scrim {
  display: none;
  position: fixed;
  inset: var(--topbar) 0 0 0;
  z-index: 950;
  background: rgba(16, 26, 40, 0.42);
}

@media (max-width: 900px) {
  :root {
    --topbar: 54px;
  }

  /* The panel becomes a drawer over the map: one thumb, no split layout. */
  #panel {
    display: flex;
    width: min(86vw, 340px);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    border-right: 0;
  }
  body.panel-hidden #panel {
    display: flex;
  }
  body:not(.panel-hidden) #panel {
    transform: none;
    box-shadow: var(--shadow-3);
  }
  #map {
    left: 0;
  }

  #scrim {
    display: block;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }
  body:not(.panel-hidden) #scrim {
    opacity: 1;
    pointer-events: auto;
  }

  #hint:not(:empty) {
    position: fixed;
    top: calc(var(--topbar) + 10px);
    left: 50%;
    z-index: 1300;
    transform: translateX(-50%);
    max-width: calc(100vw - 24px);
    padding: 9px 14px;
    color: #fff;
    background: var(--navy);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-1);
    text-align: center;
  }
}

@media (max-width: 600px) {
  /* The form becomes a bottom sheet, where the thumb already is. */
  #modal-backdrop,
  #customer-backdrop {
    align-items: flex-end;
    padding: 0;
  }
  #modal,
  #customer-modal {
    max-width: none;
    max-height: 94vh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
  }
  .modal-body {
    padding: 16px 16px 20px;
  }
  #toasts {
    bottom: 14px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}