/*
 * Unified frontend foundation.
 *
 * Step117 owns cross-application design tokens, shared shell primitives,
 * floating-layer geometry and compatibility foundations used by all UI modules.
 */
:root {
  /* Step117: one cross-application design-token source of truth. */
  --frontend-extraction-foundation: 1;

  /* Color system */
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --surface-strong: #eef2f7;
  --border: #d7dee7;
  --border-strong: #c4ced9;
  --text: #0f172a;
  --text-muted: #475569;
  --text-soft: #64748b;
  --accent: #1f3a5f;
  --accent-hover: #16304f;
  --accent-soft: #e8eef6;
  --success: #166534;
  --success-soft: #ecfdf3;
  --warning: #92400e;
  --warning-soft: #fff7ed;
  --danger: #991b1b;
  --danger-soft: #fef2f2;

  /* Typography: 11 px is the smallest application text token. */
  --font-family-ui: Inter, "Segoe UI", Roboto, Arial, sans-serif;
  --font-size-xs: 11px;
  --font-size-sm: 12px;
  --font-size-body-sm: 13px;
  --font-size-body: 14px;
  --font-size-subtitle: 16px;
  --font-size-title-sm: 18px;
  --font-size-title: 22px;
  --font-size-title-lg: 24px;
  --font-size-display: 28px;
  --font-size-display-lg: 30px;
  --font-weight-regular: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;
  --font-weight-heavy: 800;
  --line-height-tight: 1.2;
  --line-height-compact: 1.35;
  --line-height-normal: 1.5;

  /* 4 px rhythm */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;

  /* Geometry */
  --radius-xs: 8px;
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --radius-xl: 20px;
  --radius-pill: 999px;
  --control-height-compact: 32px;
  --control-height-default: 40px;
  --touch-target-min: 44px;

  /* Elevation */
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 18px 42px rgba(15, 23, 42, 0.16);
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
  --overlay-scrim: rgba(15, 23, 42, 0.48);

  /* One global layer scale. Local stacking (0–99) may remain component-owned. */
  --z-page: 0;
  --z-content: 10;
  --z-stats: 20;
  --z-details: 30;
  --z-header: 500;
  --z-sticky-toolbar: 1100;
  --z-dropdown: 1200;
  --z-mobile-dock: 1320;
  --z-mobile-nav: 1330;
  --z-mobile-selection: 1340;
  --z-mobile-selection-scrim: 1350;
  --z-mobile-sheet: 1360;
  --z-popover: 1380;
  --z-modal: 1400;
  --z-admin-menu-scrim: 1440;
  --z-admin-menu: 1450;
  --z-drawer-backdrop: 1490;
  --z-drawer: 1500;
  --z-picker: 1550;
  --z-confirm: 1600;
  --z-critical: 2000;

  /* Shared floating-layer geometry. */
  --floating-layer-viewport-padding: 12px;
  --floating-layer-gap: 8px;
}

html {
  font-family: var(--font-family-ui);
  color-scheme: light;
}

body {
  font-family: var(--font-family-ui);
  font-size: var(--font-size-body);
  line-height: var(--line-height-normal);
}

button,
input,
select,
textarea {
  font-family: inherit;
}

.ds-floating-layer {
  position: fixed !important;
  inset: auto !important;
  top: var(--floating-layer-top, 12px) !important;
  left: var(--floating-layer-left, 12px) !important;
  right: auto !important;
  bottom: auto !important;
  max-height: var(--floating-layer-max-height, min(360px, calc(100dvh - 24px)));
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0;
  z-index: var(--z-popover);
}

.ds-floating-layer[popover]:not(:popover-open) {
  display: none;
}

.ds-floating-layer[popover]::backdrop {
  display: none;
}


/* Step 51 v1.1.0: notification visibility. */
.alert.open { display: block; }
.alert:not(.open) { display: none; }

/* Step114 — shared mobile-first logistics dock and safe-area behavior. */
.mobile-logistics-dock { display: none; }
@media (max-width: 760px) {
  body.has-mobile-logistics-dock {
    padding-bottom: calc(82px + env(safe-area-inset-bottom));
  }
  .mobile-logistics-dock {
    position: fixed;
    z-index: var(--z-mobile-dock);
    left: max(10px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    align-items: end;
    min-height: 62px;
    padding: 6px;
    border: 1px solid rgba(148, 163, 184, .48);
    border-radius: var(--radius-lg);
    background: rgba(255, 255, 255, .96);
    box-shadow: 0 14px 34px rgba(15, 23, 42, .18);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    transition: transform .18s ease, opacity .18s ease;
  }
  .mobile-logistics-dock.is-obscured {
    opacity: 0;
    pointer-events: none;
    transform: translateY(calc(100% + 24px));
  }
  .mobile-logistics-dock__item {
    position: relative;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-width: 0;
    min-height: 50px;
    padding: 4px 5px;
    border: 0;
    border-radius: 13px;
    background: transparent;
    color: #475569;
    font: inherit;
    font-size: var(--font-size-xs);
    font-weight: 700;
    line-height: 1.05;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
  .mobile-logistics-dock__item:active { background: #eef2f7; transform: translateY(1px); }
  .mobile-logistics-dock__item svg {
    width: 21px;
    height: 21px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .mobile-logistics-dock__item--primary {
    background: #1f3a5f;
    color: #fff;
    box-shadow: 0 7px 16px rgba(31, 58, 95, .24);
  }
  .mobile-logistics-dock__item--primary:active { background: #17314f; }
  .mobile-logistics-dock__item[hidden] { display: none; }
}

/* Step115 — permission-aware application navigation shared by all workspaces. */
.application-navigation {
  font-family: var(--font-family-ui);
}
.application-navigation--desktop {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin: -4px 0 20px;
  padding: 0 0 16px;
  border-bottom: 1px solid var(--border, #d7dee7);
}
.application-navigation__brand {
  flex: 0 0 auto;
  color: var(--text-soft, #64748b);
  font-size: var(--font-size-sm);
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.application-navigation__links {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  min-width: 0;
  flex-wrap: wrap;
}
.application-navigation__link,
.application-navigation__mobile-link {
  text-decoration: none;
}
.application-navigation__link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: 11px;
  color: var(--text-muted, #475569);
  font-size: var(--font-size-body-sm);
  font-weight: 700;
  line-height: 1;
  transition: border-color .16s ease, background-color .16s ease, color .16s ease, box-shadow .16s ease;
}
.application-navigation__link:hover {
  border-color: var(--border, #d7dee7);
  background: var(--surface-muted, #f8fafc);
  color: var(--text, #0f172a);
}
.application-navigation__link.is-active {
  border-color: rgba(31, 58, 95, .18);
  background: var(--accent-soft, #e8eef6);
  color: var(--accent, #1f3a5f);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .45);
}
.application-navigation__link:focus-visible,
.application-navigation__mobile-link:focus-visible {
  outline: 3px solid rgba(59, 130, 246, .28);
  outline-offset: 2px;
}
.application-navigation__icon {
  width: 19px;
  height: 19px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.application-navigation--mobile {
  display: none;
}

@media (max-width: 760px) {
  body.has-application-navigation {
    padding-bottom: calc(82px + env(safe-area-inset-bottom));
  }
  body.has-application-navigation.has-mobile-logistics-dock {
    padding-bottom: calc(144px + env(safe-area-inset-bottom));
  }
  .application-navigation--desktop {
    display: none;
  }
  .application-navigation--mobile {
    position: fixed;
    z-index: var(--z-mobile-nav);
    left: max(10px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
    align-items: stretch;
    min-height: 62px;
    padding: 6px;
    border: 1px solid rgba(148, 163, 184, .48);
    border-radius: var(--radius-lg);
    background: rgba(255, 255, 255, .97);
    box-shadow: 0 14px 34px rgba(15, 23, 42, .18);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    transition: transform .18s ease, opacity .18s ease;
  }
  .application-navigation--mobile.is-obscured {
    opacity: 0;
    pointer-events: none;
    transform: translateY(calc(100% + 24px));
  }
  .application-navigation__mobile-link {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    min-width: 0;
    min-height: 50px;
    padding: 4px 5px;
    border: 1px solid transparent;
    border-radius: 13px;
    color: #64748b;
    font-size: var(--font-size-xs);
    font-weight: 750;
    line-height: 1.05;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
  .application-navigation__mobile-link.is-active {
    border-color: rgba(31, 58, 95, .14);
    background: #e8eef6;
    color: #1f3a5f;
  }
  .application-navigation__mobile-link:active {
    transform: translateY(1px);
  }

  /* The local action dock is deliberately separated from the global module navigation. */
  .mobile-logistics-dock {
    z-index: var(--z-mobile-dock);
    bottom: calc(78px + env(safe-area-inset-bottom));
    left: max(22px, env(safe-area-inset-left));
    right: max(22px, env(safe-area-inset-right));
    min-height: 50px;
    padding: 4px;
    border-radius: 15px;
  }
  .mobile-logistics-dock__item {
    min-height: 42px;
    font-size: var(--font-size-xs);
  }
  .mobile-logistics-dock__item svg {
    width: 18px;
    height: 18px;
  }
}

/* Step121 — shared keyboard-first date and datetime field. */
.ds-date-field {
  min-width: 0;
}

.ds-date-field__control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--control-height-default);
  align-items: stretch;
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  overflow: hidden;
  transition: border-color .15s ease, box-shadow .15s ease;
}

.ds-date-field__control:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.ds-date-field.has-error .ds-date-field__control,
.ds-datetime-field.has-error .ds-date-field__control {
  border-color: var(--danger);
}

.ds-date-field__input {
  min-width: 0;
  width: 100%;
  min-height: var(--control-height-default);
  padding: 0 var(--space-3);
  border: 0 !important;
  border-radius: 0 !important;
  outline: 0 !important;
  background: transparent !important;
  box-shadow: none !important;
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
}

.ds-date-field__input::placeholder {
  color: var(--text-soft);
}

.ds-date-field__trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--control-height-default);
  min-width: var(--control-height-default);
  min-height: var(--control-height-default);
  padding: 0;
  border: 0;
  border-left: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--accent);
  cursor: pointer;
}

.ds-date-field__trigger:hover:not(:disabled) {
  background: var(--accent-soft);
}

.ds-date-field__trigger:focus-visible {
  position: relative;
  outline: 2px solid var(--accent);
  outline-offset: -3px;
}

.ds-date-field__trigger:disabled {
  color: var(--text-soft);
  cursor: default;
  opacity: .6;
}

.ds-date-field__trigger svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ds-date-field__error,
.ds-datetime-field__error {
  display: block;
  margin-top: var(--space-1);
  color: var(--danger);
  font-size: var(--font-size-xs);
  line-height: var(--line-height-compact);
}

.ds-date-field__error[hidden],
.ds-datetime-field__error[hidden] {
  display: none;
}

.ds-datetime-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(92px, .45fr);
  gap: var(--space-2);
  align-items: start;
  min-width: 0;
}

.ds-datetime-field__time {
  min-width: 0;
  width: 100%;
  min-height: var(--control-height-default);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 var(--space-3);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
}

.ds-datetime-field__time:focus {
  border-color: var(--accent);
  outline: 0;
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.ds-datetime-field.has-error .ds-datetime-field__time {
  border-color: var(--danger);
}

.ds-datetime-field__error {
  grid-column: 1 / -1;
  margin-top: calc(-1 * var(--space-1));
}

.ds-date-picker {
  z-index: var(--z-picker);
  width: var(--floating-layer-width, 312px);
  max-width: calc(100vw - (var(--floating-layer-viewport-padding) * 2));
  padding: var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  color: var(--text);
}

.ds-date-picker__header {
  display: grid;
  grid-template-columns: var(--control-height-compact) minmax(0, 1fr) var(--control-height-compact);
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
}

.ds-date-picker__month {
  text-align: center;
  font-size: var(--font-size-body);
  font-weight: var(--font-weight-semibold);
  text-transform: capitalize;
}

.ds-date-picker__nav,
.ds-date-picker__footer-action {
  min-height: var(--control-height-compact);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--accent);
  font: inherit;
  font-weight: var(--font-weight-semibold);
  cursor: pointer;
}

.ds-date-picker__nav {
  width: var(--control-height-compact);
  padding: 0;
  font-size: var(--font-size-title-sm);
  line-height: 1;
}

.ds-date-picker__nav:hover,
.ds-date-picker__footer-action:hover {
  background: var(--surface-muted);
}

.ds-date-picker__weekdays,
.ds-date-picker__grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: var(--space-1);
}

.ds-date-picker__weekdays {
  margin-bottom: var(--space-1);
}

.ds-date-picker__weekdays span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  color: var(--text-soft);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
}

.ds-date-picker__day,
.ds-date-picker__blank {
  aspect-ratio: 1;
  min-width: 0;
}

.ds-date-picker__day {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: var(--font-size-body-sm);
  cursor: pointer;
}

.ds-date-picker__day:hover,
.ds-date-picker__day:focus-visible {
  background: var(--accent-soft);
  outline: 0;
}

.ds-date-picker__day.is-today {
  border-color: var(--border-strong);
  font-weight: var(--font-weight-bold);
}

.ds-date-picker__day.is-selected {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--surface);
  font-weight: var(--font-weight-bold);
}

.ds-date-picker__day.is-selected:hover,
.ds-date-picker__day.is-selected:focus-visible {
  background: var(--accent-hover);
}

.ds-date-picker__footer {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--border);
}

.ds-date-picker__footer-action {
  flex: 1 1 0;
  padding: 0 var(--space-2);
  font-size: var(--font-size-sm);
}

@media (max-width: 700px) {
  .ds-date-field__control {
    grid-template-columns: minmax(0, 1fr) var(--touch-target-min);
  }
  .ds-date-field__input,
  .ds-date-field__trigger,
  .ds-datetime-field__time {
    min-height: var(--touch-target-min);
  }
  .ds-date-field__trigger {
    width: var(--touch-target-min);
    min-width: var(--touch-target-min);
  }
  .ds-date-picker {
    width: min(328px, calc(100vw - 20px));
    padding: var(--space-3);
  }
  .ds-date-picker__day {
    min-height: 38px;
  }
  .ds-date-picker__nav,
  .ds-date-picker__footer-action {
    min-height: var(--touch-target-min);
  }
  .ds-datetime-field {
    grid-template-columns: minmax(0, 1fr) minmax(88px, .4fr);
  }
}
/* Step124: cross-module shell and viewport stabilization. */
:root {
  --app-shell-max-width: 1920px;
  --app-shell-gutter-x: 24px;
  --app-shell-gutter-y: 24px;
  --app-shell-panel-radius: 18px;
  --app-shell-border: #d7dee7;
  --app-shell-shadow: 0 1px 2px rgba(15, 23, 42, .04);
  --app-shell-background: linear-gradient(180deg, #f8fafc 0%, #f3f4f6 100%);
}

/* Keep the content width stable when a page is shorter/taller than its neighbour. */
html {
  scrollbar-gutter: stable;
}

@supports not (scrollbar-gutter: stable) {
  @media (min-width: 761px) {
    html { overflow-y: scroll; }
  }
}

body.tracking-page,
body.transport-page,
body.admin-page {
  min-height: 100vh;
  background: var(--app-shell-background);
  overflow-x: hidden;
}

/* All three modules use one outer width contract. */
body.tracking-page > .container,
body.transport-page > .transport-shell,
body.admin-page > .admin-container {
  width: 100%;
  max-width: var(--app-shell-max-width);
  margin-inline: auto;
}

/* Top-level page frames use the same visible edge geometry. */
body.tracking-page .header,
body.transport-page .transport-header,
body.admin-page .main-content > .header {
  border: 1px solid var(--app-shell-border);
  border-radius: var(--app-shell-panel-radius);
  box-shadow: var(--app-shell-shadow);
}

@media (min-width: 761px) {
  body.tracking-page,
  body.transport-page {
    padding: var(--app-shell-gutter-y) var(--app-shell-gutter-x);
  }

  /* Admin keeps the sidebar, but its working surface begins on the same 24px rhythm. */
  body.admin-page .main-content {
    padding-top: var(--app-shell-gutter-y);
    padding-right: var(--app-shell-gutter-x);
    padding-bottom: var(--app-shell-gutter-y);
    padding-left: var(--app-shell-gutter-x);
  }
}

@media (max-width: 760px) {
  html { scrollbar-gutter: auto; }
}
