/* ==========================================================================
   retail-route — design system + layout
   Mapa a toda a largura com um painel a flutuar (ecrãs largos) ou uma folha
   por baixo do mapa (telemóvel). Cores da marca: teal petróleo + ardósia.
   1. Tokens (claro / escuro)   2. Base   3. Layout (barra, painel, mapa)
   4. Componentes               5. Vistas 6. Mapa / Leaflet   7. Responsivo
   ========================================================================== */

/* 1. Tokens ---------------------------------------------------------------- */
:root {
  color-scheme: light;

  /* Marca (só as cores) */
  --brand: #008eaa;          /* teal petróleo: preenchimentos, rotas, foco, texto grande (3,9:1 em branco) */
  --brand-strong: #006e89;   /* botões e texto normal sobre branco (5,9:1) */
  --brand-light: #4eb0ce;
  --brand-ink: #2e3644;      /* ardósia: barra de topo */
  --magenta: #b80058;        /* acento secundário (gráficos, rotas) — usar pouco */
  --purple: #850c70;

  /* Superfícies e texto */
  --bg: #f5f5f5;
  --surface: #ffffff;
  --surface-2: #f6f7f8;
  --surface-3: #eceef1;
  --surface-4: #e1e5e9;
  --text: #12171d;
  --text-2: #3a434e;
  --text-3: #5d6670;         /* >= 5:1 sobre --surface, --surface-2 e --bg */
  --border: #e2e5e9;
  --border-strong: #c5cbd2;
  --input-bg: #f6f7f8;

  /* Acção (botão principal: teal escuro com texto branco, 5,9:1) */
  --accent: var(--brand);
  --accent-strong: #006e89;
  --accent-hover: #005a70;
  --accent-soft: #e3f3f7;
  --accent-text: #006e89;    /* 5,2:1 sobre --accent-soft */
  --on-accent: #ffffff;

  /* Estados (sempre com texto ou ícone, nunca só a cor) */
  --ok: #1d7a35;
  --ok-soft: #e5f4ea;
  --warn: #8a5a00;
  --warn-soft: #fff3d6;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --on-danger: #ffffff;
  --focus: #008eaa;

  /* Barra de topo e cartão de destaque (ardósia) */
  --topbar-bg: #2e3644;
  --topbar-text: #ffffff;
  --topbar-text-2: #c9ced6;  /* 7,7:1 */
  --topbar-hover: rgba(255, 255, 255, 0.1);
  --topbar-line: rgba(255, 255, 255, 0.14);
  --topbar-on: #ffffff;
  --topbar-on-text: #12171d;
  --hero-bg: #2e3644;
  --hero-text: #ffffff;
  --hero-text-2: #c9ced6;
  --badge-bg: #4eb0ce;
  --badge-text: #071217;     /* 7,6:1 */

  /* Mapa */
  --map-bg: #e9ebee;
  --tile-filter: grayscale(0.88) contrast(0.96) brightness(1.03);
  --depot: #12171d;
  --stop-neutral: #7a838e;
  --marker-stroke: #ffffff;

  /* Cores de rota (lidas pelo app.js → linhas, marcadores e cartões iguais) */
  --route-1: #008eaa;
  --route-2: #b80058;
  --route-3: #e08600;
  --route-4: #3a5bd9;
  --route-5: #23883f;
  --route-6: #7b4bd6;
  --route-7: #8c5a2b;
  --route-8: #d9480f;

  /* Gráficos: 1 = 9 kg (teal), 2 = 45 kg (magenta), ref = todos os clientes */
  --series-1: #008eaa;
  --series-2: #b80058;
  --series-ref: #6b7480;

  /* Espaço (grelha de 4 px) */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;

  /* Tipografia */
  --font: "Figtree", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --text-md: 1.0625rem;
  --text-lg: 1.1875rem;
  --text-xl: 1.625rem;
  --text-2xl: 2rem;
  --weight-regular: 400;
  --weight-medium: 500;
  --weight-semibold: 600;
  --weight-bold: 700;
  --weight-black: 800;
  --leading-tight: 1.15;
  --leading: 1.45;

  /* Forma */
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --radius-xl: 22px;
  --radius-pill: 999px;
  --shadow-xs: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 2px 8px rgba(16, 24, 40, 0.05);
  --shadow: 0 4px 16px rgba(16, 24, 40, 0.12);
  --shadow-lg: 0 16px 40px rgba(16, 24, 40, 0.18);
  --shadow-panel: 0 2px 6px rgba(16, 24, 40, 0.08), 0 18px 48px rgba(16, 24, 40, 0.2);

  /* Layout */
  --panel-w: 440px;
  --gap: 12px;
  --topbar-h: 64px;
  --control-h: 2.5rem;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

/* Escuro: paleta própria (ardósia profunda + teal claro), não uma inversão */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d1116;
    --surface: #161b22;
    --surface-2: #1c232b;
    --surface-3: #252d36;
    --surface-4: #2f3842;
    --text: #f1f4f7;
    --text-2: #c4cbd3;
    --text-3: #98a2ad;
    --border: #2a323c;
    --border-strong: #3f4954;
    --input-bg: #1c232b;
    --accent: #3db3cf;
    --accent-strong: #4eb0ce;
    --accent-hover: #6cc3dd;
    --accent-soft: #0e3340;
    --accent-text: #6cc6e0;
    --on-accent: #071217;
    --ok: #57c97e;
    --ok-soft: #10301c;
    --warn: #f2b544;
    --warn-soft: #322610;
    --danger: #ff7b72;
    --danger-soft: #3b1719;
    --on-danger: #160a0a;
    --focus: #4eb0ce;
    --topbar-bg: #0a0e13;
    --topbar-text: #f1f4f7;
    --topbar-text-2: #aeb7c1;
    --topbar-hover: rgba(255, 255, 255, 0.08);
    --topbar-line: rgba(255, 255, 255, 0.1);
    --topbar-on: #f1f4f7;
    --topbar-on-text: #0a0e13;
    --hero-bg: #0f3440;
    --hero-text: #ffffff;
    --hero-text-2: #a9d7e4;
    --map-bg: #1b2027;
    --tile-filter: grayscale(1) invert(1) brightness(0.92) contrast(0.88);
    --depot: #0a0e13;
    --stop-neutral: #8a939e;
    --route-1: #3fc1de;
    --route-2: #f0589e;
    --route-3: #f7b23b;
    --route-4: #7c9bff;
    --route-5: #4cc870;
    --route-6: #af8cff;
    --route-7: #cf9a62;
    --route-8: #ff7f45;
    --series-1: #3db3cf;
    --series-2: #e0418a;
    --series-ref: #98a2ad;
    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 2px 8px rgba(0, 0, 0, 0.25);
    --shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 18px 44px rgba(0, 0, 0, 0.55);
    --shadow-panel: 0 0 0 1px rgba(255, 255, 255, 0.05), 0 18px 48px rgba(0, 0, 0, 0.6);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1116;
  --surface: #161b22;
  --surface-2: #1c232b;
  --surface-3: #252d36;
  --surface-4: #2f3842;
  --text: #f1f4f7;
  --text-2: #c4cbd3;
  --text-3: #98a2ad;
  --border: #2a323c;
  --border-strong: #3f4954;
  --input-bg: #1c232b;
  --accent: #3db3cf;
  --accent-strong: #4eb0ce;
  --accent-hover: #6cc3dd;
  --accent-soft: #0e3340;
  --accent-text: #6cc6e0;
  --on-accent: #071217;
  --ok: #57c97e;
  --ok-soft: #10301c;
  --warn: #f2b544;
  --warn-soft: #322610;
  --danger: #ff7b72;
  --danger-soft: #3b1719;
  --on-danger: #160a0a;
  --focus: #4eb0ce;
  --topbar-bg: #0a0e13;
  --topbar-text: #f1f4f7;
  --topbar-text-2: #aeb7c1;
  --topbar-hover: rgba(255, 255, 255, 0.08);
  --topbar-line: rgba(255, 255, 255, 0.1);
  --topbar-on: #f1f4f7;
  --topbar-on-text: #0a0e13;
  --hero-bg: #0f3440;
  --hero-text: #ffffff;
  --hero-text-2: #a9d7e4;
  --map-bg: #1b2027;
  --tile-filter: grayscale(1) invert(1) brightness(0.92) contrast(0.88);
  --depot: #0a0e13;
  --stop-neutral: #8a939e;
  --route-1: #3fc1de;
  --route-2: #f0589e;
  --route-3: #f7b23b;
  --route-4: #7c9bff;
  --route-5: #4cc870;
  --route-6: #af8cff;
  --route-7: #cf9a62;
  --route-8: #ff7f45;
  --series-1: #3db3cf;
  --series-2: #e0418a;
  --series-ref: #98a2ad;
  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 2px 8px rgba(0, 0, 0, 0.25);
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 18px 44px rgba(0, 0, 0, 0.55);
  --shadow-panel: 0 0 0 1px rgba(255, 255, 255, 0.05), 0 18px 48px rgba(0, 0, 0, 0.6);
}

/* 2. Base ------------------------------------------------------------------ */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: var(--leading);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, p {
  margin: 0;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

a {
  color: var(--accent-text);
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.num {
  font-variant-numeric: tabular-nums;
}

.skip-link {
  position: absolute;
  left: var(--space-2);
  top: -3rem;
  z-index: 2000;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-pill);
  background: var(--surface);
  color: var(--text);
  font-weight: var(--weight-bold);
  box-shadow: var(--shadow);
}

.skip-link:focus {
  top: var(--space-2);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* 3. Layout ---------------------------------------------------------------- */
/* Grelha com o mapa e o painel na mesma célula: o mapa ocupa tudo, o painel flutua por cima. */
.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

/* Barra de topo */
.topbar {
  grid-row: 1;
  grid-column: 1;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  min-height: var(--topbar-h);
  padding: var(--space-2) var(--space-4) var(--space-2) var(--space-5);
  background: var(--topbar-bg);
  color: var(--topbar-text);
  border-bottom: 1px solid var(--topbar-line);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
  flex: none;
}

.brand-logo {
  flex: none;
  width: 34px;
  height: 34px;
  display: block;
}

.brand-text {
  min-width: 0;
}

.brand-mark {
  font-size: var(--text-lg);
  font-weight: var(--weight-black);
  letter-spacing: -0.03em;
  line-height: var(--leading-tight);
}

.brand-tag {
  font-size: var(--text-xs);
  color: var(--topbar-text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.view-tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px 1.5rem 2px 2px;
  /* Separadores que não cabem: esbatidos à direita (há mais para deslizar) */
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 1.5rem), transparent);
  mask-image: linear-gradient(to right, #000 calc(100% - 1.5rem), transparent);
}

.view-tabs::-webkit-scrollbar {
  display: none;
}

.view-tab {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  min-height: 2.5rem;
  padding: 0 var(--space-4);
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--topbar-text-2);
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
  white-space: nowrap;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease;
}

.view-tab:hover {
  background: var(--topbar-hover);
  color: var(--topbar-text);
}

.view-tab[aria-selected="true"] {
  background: var(--topbar-on);
  color: var(--topbar-on-text);
}

.view-tab:focus-visible {
  outline: 2px solid var(--brand-light);
  outline-offset: 1px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: none;
  margin-left: auto;
}

/* Painel flutuante */
.panel {
  grid-row: 2;
  grid-column: 1;
  z-index: 10;
  align-self: start;
  justify-self: start;
  display: flex;
  flex-direction: column;
  width: var(--panel-w);
  max-width: calc(100% - 2 * var(--gap));
  max-height: calc(100% - 2 * var(--gap));
  margin: var(--gap);
  background: var(--surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-panel);
  overflow: hidden;
}

.panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 var(--space-5);
  scroll-padding-top: var(--space-4);
  scroll-padding-bottom: 6rem;
}

/* Mapa a toda a largura (contexto de empilhamento próprio para os z-index do Leaflet) */
.map-shell {
  grid-row: 2;
  grid-column: 1;
  position: relative;
  z-index: 0;
  min-width: 0;
  min-height: 0;
  background: var(--map-bg);
}

#map {
  position: absolute;
  inset: 0;
}

/* Vistas sem mapa (Entregas, Relatórios): o painel ocupa a largura toda */
.app.is-wide .map-shell {
  display: none;
}

.app.is-wide .panel {
  justify-self: stretch;
  align-self: stretch;
  width: auto;
  max-width: none;
  max-height: none;
  margin: 0;
  background: var(--bg);
  border-radius: 0;
  box-shadow: none;
}

.app.is-wide .panel-body {
  padding: var(--space-6);
}

.app.is-wide .panel-body > div:not([hidden]),
.app.is-wide .alerts-panel {
  max-width: 1120px;
  margin-left: auto;
  margin-right: auto;
}

.app.is-wide .section {
  margin-bottom: var(--space-4);
  padding: var(--space-6);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-xs);
}

.app.is-wide .section:last-child {
  border-bottom: 1px solid var(--border);
}

/* 4. Componentes ----------------------------------------------------------- */

/* Theme toggle (na barra de topo) */
.theme-toggle {
  display: inline-flex;
  padding: 3px;
  border-radius: var(--radius-pill);
  background: var(--topbar-hover);
}

.theme-toggle-option {
  min-height: 1.875rem;
  border: 0;
  background: transparent;
  padding: 0 0.7rem;
  border-radius: var(--radius-pill);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  color: var(--topbar-text-2);
  cursor: pointer;
}

.theme-toggle-option:hover {
  color: var(--topbar-text);
}

.theme-toggle-option[aria-checked="true"] {
  background: var(--topbar-on);
  color: var(--topbar-on-text);
}

.theme-toggle-option:focus-visible {
  outline-color: var(--brand-light);
}

/* Alertas e utilizador (barra de topo) */
.alerts-btn,
.topbar .btn-ghost {
  color: var(--topbar-text);
}

.topbar .btn-ghost:hover:not(:disabled) {
  background: var(--topbar-hover);
  color: var(--topbar-text);
}

.topbar .btn:focus-visible {
  outline-color: var(--brand-light);
}

.alerts-btn svg {
  width: 1.05rem;
  height: 1.05rem;
  flex: none;
}

.alerts-btn.has-alerts {
  background: var(--topbar-hover);
}

.alerts-btn[aria-expanded="true"] {
  background: var(--topbar-on);
  color: var(--topbar-on-text);
}

.user-box {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--topbar-text-2);
}

/* Contadores nos separadores e no botão Alertas */
.tab-count:not(:empty) {
  display: inline-grid;
  place-items: center;
  min-width: 1.3rem;
  height: 1.3rem;
  margin-left: 2px;
  padding: 0 0.35rem;
  border-radius: var(--radius-pill);
  background: var(--badge-bg);
  color: var(--badge-text);
  font-size: 0.6875rem;
  font-weight: var(--weight-black);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* Alertas por ver: vermelho vivo na barra escura (texto branco 4,8:1), nos dois temas */
.alerts-btn .tab-count:not(:empty) {
  background: #d92d20;
  color: #ffffff;
}

/* Secções */
.section {
  padding: var(--space-6) 0;
  border-bottom: 1px solid var(--border);
}

.section:last-child {
  border-bottom: 0;
}

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-3);
  margin-bottom: var(--space-4);
}

.section-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-lg);
  font-weight: var(--weight-black);
  letter-spacing: -0.02em;
  line-height: var(--leading-tight);
  color: var(--text);
}

.section-solve .section-title,
#results .section-title,
.section > .section-title:first-child {
  margin-bottom: var(--space-4);
}

.section-title:focus {
  outline: none;
}

.section-title:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.step {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 1.625rem;
  height: 1.625rem;
  border-radius: 50%;
  background: var(--text);
  color: var(--surface);
  font-size: var(--text-xs);
  font-weight: var(--weight-black);
  letter-spacing: 0;
}

.section-meta,
.legend,
.hint,
.stops-summary {
  font-size: var(--text-sm);
  color: var(--text-3);
}

.section-meta {
  font-weight: var(--weight-semibold);
}

.legend {
  margin-top: var(--space-3);
  font-size: var(--text-xs);
}

.section-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.field-label {
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-3);
}

.empty {
  padding: var(--space-5) var(--space-4);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text-3);
  font-size: var(--text-sm);
  text-align: center;
}

.empty strong {
  color: var(--text-2);
}

/* Botões: pílulas; um principal por ecrã */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--control-h);
  padding: 0 1.125rem;
  border: 1px solid transparent;
  border-radius: var(--radius-pill);
  font-weight: var(--weight-bold);
  font-size: var(--text-sm);
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: background 140ms ease, border-color 140ms ease, color 140ms ease, box-shadow 140ms ease, transform 80ms ease;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  min-height: 3rem;
  padding: 0 1.5rem;
  background: var(--accent-strong);
  color: var(--on-accent);
  font-size: var(--text-base);
  font-weight: var(--weight-black);
  box-shadow: var(--shadow-xs);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface-3);
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-4);
}

.btn-ghost {
  background: transparent;
  color: var(--text-2);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-3);
  color: var(--text);
}

/* Branco sobre ardósia (cartão de destaque) */
.btn-on-dark {
  background: #ffffff;
  color: #12171d;
}

.btn-on-dark:hover:not(:disabled) {
  background: #e7ebef;
}

.btn-on-dark:focus-visible {
  outline-color: #ffffff;
}

/* Tamanho pequeno ganha a qualquer variante */
.btn.btn-sm {
  min-height: 2rem;
  padding: 0 0.875rem;
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
}

.btn-icon {
  width: 2rem;
  min-height: 2rem;
  padding: 0;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--text-3);
  background: transparent;
}

.btn-icon:hover:not(:disabled) {
  background: var(--danger-soft);
  color: var(--danger);
}

/* Spinner */
.spinner {
  display: none;
  width: 1rem;
  height: 1rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

.spinner-lg {
  display: inline-block;
  width: 1.25rem;
  height: 1.25rem;
  color: var(--accent);
}

.is-loading .spinner {
  display: inline-block;
}

.btn-primary.is-loading:disabled {
  opacity: 0.85;
  cursor: progress;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Campos */
.input {
  width: 100%;
  min-width: 0;
  height: 2.375rem;
  padding: 0 var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--input-bg);
  color: var(--text);
  font-size: var(--text-sm);
  transition: border-color 120ms ease, background 120ms ease, box-shadow 120ms ease;
}

.input:hover {
  border-color: var(--border-strong);
}

.input:focus {
  outline: none;
  border-color: var(--focus);
  background: var(--surface);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 28%, transparent);
}

.input::placeholder {
  color: var(--text-3);
  opacity: 1;
}

.input[aria-invalid="true"] {
  border-color: var(--danger);
  background: var(--danger-soft);
}

.input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.input-num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.input-sm {
  height: 2rem;
  padding: 0 var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
}

select.input {
  padding-right: var(--space-1);
  cursor: pointer;
}

input[type="checkbox"] {
  width: 1.05rem;
  height: 1.05rem;
  accent-color: var(--accent-strong);
}

/* Chips de estado: pílulas com texto (e um ponto/✓ nos estados) */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.125rem 0.55rem;
  border: 0;
  border-radius: var(--radius-pill);
  background: var(--surface-3);
  color: var(--text-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  font-variant-numeric: tabular-nums;
  line-height: 1.45;
  white-space: nowrap;
  vertical-align: 0.05em;
}

.chip-accent {
  background: var(--accent-soft);
  color: var(--accent-text);
}

.chip-warn {
  background: var(--warn-soft);
  color: var(--warn);
}

.chip-warn::before {
  content: "";
  flex: none;
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  background: currentColor;
}

.chip-ok {
  background: var(--ok-soft);
  color: var(--ok);
}

.chip-ok::before {
  content: "✓";
  font-weight: var(--weight-black);
}

.chip-tw {
  font-variant-numeric: tabular-nums;
}

/* Banner (erros de carregamento) */
.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-top: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--danger-soft);
  box-shadow: inset 4px 0 0 var(--danger);
  font-size: var(--text-sm);
}

.banner-text {
  display: flex;
  flex-direction: column;
}

/* Estado (mensagens por baixo dos botões) */
.hint {
  margin-bottom: var(--space-3);
}

.status {
  min-height: 1.25rem;
  margin-top: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-3);
}

.status.is-error {
  color: var(--danger);
  font-weight: var(--weight-semibold);
}

.status .btn {
  margin-left: var(--space-1);
}

/* Avisos (erros / inviável) */
.alert {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--danger-soft);
  box-shadow: inset 4px 0 0 var(--danger);
  font-size: var(--text-sm);
}

.alert + .alert,
.alert + .summary {
  margin-top: var(--space-3);
}

.alert-warn {
  background: var(--warn-soft);
  box-shadow: inset 4px 0 0 var(--warn);
}

.alert-title {
  font-weight: var(--weight-black);
  margin-bottom: var(--space-1);
}

.alert-title::before {
  content: "!";
  display: inline-grid;
  place-items: center;
  width: 1.15rem;
  height: 1.15rem;
  margin-right: var(--space-2);
  border-radius: 50%;
  background: var(--danger);
  color: var(--on-danger);
  font-size: 0.75rem;
  vertical-align: 0.1em;
}

.alert-warn .alert-title::before {
  background: var(--warn);
  color: var(--surface);
}

.alert-detail {
  color: var(--text-2);
  word-break: break-word;
}

.alert ul {
  margin: var(--space-2) 0 0;
  padding-left: var(--space-5);
  color: var(--text-2);
}

.alert-code {
  margin-top: var(--space-2);
  font-size: var(--text-xs);
  color: var(--text-3);
}

/* Cartões de lista (encomendas, clientes, planos, previsões, entregas, alertas) */
.order-list {
  display: grid;
  gap: var(--space-2);
  margin: var(--space-3) 0;
  padding: 0;
  list-style: none;
}

.order-list > li {
  min-width: 0; /* formulários dentro dos cartões não alargam a lista */
}

.order {
  --order-accent: var(--accent);
  padding: var(--space-3) var(--space-4) var(--space-3) 1.125rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: inset 4px 0 0 var(--order-accent), var(--shadow-xs);
  transition: box-shadow 140ms ease, border-color 140ms ease;
}

.order:hover {
  border-color: var(--border-strong);
}

.order.is-entregue {
  --order-accent: var(--ok);
}

/* Cancelada: fundo apagado em vez de transparência (o texto mantém o contraste AA) */
.order.is-cancelada {
  --order-accent: var(--border-strong);
  background: var(--surface-2);
}

.order.plan,
.order.customer,
.order.delivery,
.order.forecast-item {
  --order-accent: transparent;
  padding-left: var(--space-4);
}

.order-main {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-3);
  font-size: var(--text-base);
}

.order-customer {
  min-width: 0;
  font-weight: var(--weight-black);
  letter-spacing: -0.01em;
}

.order-qty {
  flex: none;
  font-weight: var(--weight-bold);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--text);
}

.order-address,
.order-notes {
  margin: 3px 0 0;
  font-size: var(--text-sm);
  color: var(--text-2);
}

.order-notes {
  color: var(--text-3);
}

.order-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1) var(--space-2);
  margin-top: var(--space-3);
}

.order-status {
  width: auto;
  min-width: 7.5rem;
}

.order-delivered {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1) var(--space-2);
  margin: var(--space-2) 0 0;
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  color: var(--ok);
}

.order-delivered::before {
  content: "✓";
  font-weight: var(--weight-black);
}

/* Formulários */
.order-form {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
}

.form-field {
  display: grid;
  gap: 4px;
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  color: var(--text-2);
}

.form-wide {
  grid-column: 1 / -1;
}

.form-actions-inline,
.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.form-actions {
  margin-top: var(--space-2);
}

.order-form .hint {
  margin: 0;
}

.order-form .hint:empty {
  display: none;
}

.form-error {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  color: var(--danger);
}

.form-check {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-2);
  cursor: pointer;
}

.tw-inputs {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  color: var(--text-3);
}

.tw-inputs .input {
  min-width: 0;
}

.coords {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-1);
}

.coord {
  position: relative;
}

.coord > span {
  position: absolute;
  left: var(--space-2);
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.6875rem;
  font-weight: var(--weight-bold);
  color: var(--text-3);
  pointer-events: none;
}

.coord .input {
  padding-left: 1.875rem;
}

.fieldset {
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

.fieldset legend {
  margin-bottom: var(--space-2);
  padding: 0;
}

/* Controlo segmentado (Distâncias) */
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px;
  margin: 0 0 var(--space-2);
  padding: 3px;
  border-radius: var(--radius-pill);
  background: var(--surface-3);
}

.segmented-option {
  position: relative;
}

.segmented-option input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.segmented-option span {
  display: block;
  padding: var(--space-2);
  border-radius: var(--radius-pill);
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
  text-align: center;
  color: var(--text-2);
}

.segmented-option input:checked + span {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.segmented-option input:focus-visible + span {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.segmented-option input:disabled,
.segmented-option input:disabled + span {
  cursor: not-allowed;
  color: var(--text-3);
  opacity: 0.7;
}

/* Skeleton */
.skeleton {
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--surface-2) 0%, var(--surface-3) 50%, var(--surface-2) 100%);
  background-size: 200% 100%;
  animation: shimmer 1.2s linear infinite;
}

.skeleton-card {
  height: 5rem;
}

.skeleton-route {
  height: 6rem;
  margin-bottom: var(--space-2);
}

@keyframes shimmer {
  to {
    background-position: -200% 0;
  }
}

/* 5. Vistas ---------------------------------------------------------------- */

/* Planeamento: cartão de destaque «próximo dia» */
.plan-hero {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-top: var(--space-5);
  padding: var(--space-4) var(--space-4) var(--space-4) var(--space-5);
  border-radius: var(--radius-lg);
  background: var(--hero-bg);
  color: var(--hero-text);
}

.plan-hero-kicker {
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--hero-text-2);
}

.plan-hero-day {
  font-size: var(--text-xl);
  font-weight: var(--weight-black);
  letter-spacing: -0.02em;
  line-height: var(--leading-tight);
}

.plan-hero-day::first-letter {
  text-transform: uppercase;
}

.section-solve {
  padding-bottom: var(--space-3);
  border-bottom: 0;
}

/* Barra «Calcular rotas» presa ao fundo do painel enquanto se editam as paragens */
.solve-bar {
  position: sticky;
  bottom: 0;
  z-index: 5;
  margin: 0 calc(-1 * var(--space-5));
  padding: var(--space-3) var(--space-5) var(--space-3);
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -10px 18px -14px rgba(16, 24, 40, 0.35);
}

.solve-bar .status {
  min-height: 0;
  text-align: center;
}

.solve-bar .status:empty {
  display: none;
}

.solve-bar .btn-primary {
  min-height: 3.25rem;
  font-size: var(--text-md);
}

/* Frota */
.fleet-list {
  display: grid;
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.vehicle {
  min-width: 0; /* item de grelha: não alargar o painel além da largura disponível */
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-xs);
}

.vehicle-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0 var(--space-2);
}

.vehicle-name {
  font-weight: var(--weight-black);
  font-size: var(--text-base);
  letter-spacing: -0.01em;
}

.vehicle-type {
  font-size: var(--text-xs);
  color: var(--text-3);
  white-space: nowrap;
}

.vehicle-cap {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  margin-top: var(--space-2);
}

.vehicle-shift {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
  font-size: var(--text-xs);
  color: var(--text-2);
}

.vehicle-shift .input {
  flex: 1 1 5rem;
  width: auto;
  min-width: 5.25rem;
  max-width: 6.75rem;
  font-variant-numeric: tabular-nums;
}

.vehicle-shift-label {
  min-width: 3rem;
  font-weight: var(--weight-bold);
}

.vehicle-shift span {
  white-space: nowrap;
}

.vehicle-shift .vehicle-break-min {
  flex: 0 0 3.75rem;
  min-width: 0;
}

.vehicle-msg {
  margin-top: var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--danger);
}

/* Depósito */
.depot-card {
  display: grid;
  grid-template-columns: 2rem minmax(0, 1fr);
  gap: var(--space-2) var(--space-3);
  align-items: start;
  margin-bottom: var(--space-2);
  padding: var(--space-3);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
}

.depot-name {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: var(--weight-black);
  font-size: var(--text-sm);
  line-height: 2rem;
}

.badge-depot {
  display: inline-grid;
  place-items: center;
  width: 1.75rem;
  height: 1.75rem;
  margin-top: 0.125rem;
  border-radius: var(--radius-sm);
  background: var(--depot);
  color: #ffffff;
  box-shadow: 0 0 0 2px var(--surface);
  font-size: var(--text-xs);
  font-weight: var(--weight-black);
}

/* Paragens */
.stops-summary {
  margin: calc(-1 * var(--space-2)) 0 var(--space-3);
  font-weight: var(--weight-semibold);
}

.stop-list {
  display: grid;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.stop-list-head,
.stop {
  display: grid;
  grid-template-columns: 1.75rem minmax(0, 1fr) 3.75rem 3.75rem 2rem;
  gap: var(--space-1) var(--space-2);
}

.stop-list-head {
  padding: 0 var(--space-3) var(--space-1);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  color: var(--text-3);
}

.stop-list-head span:nth-child(3),
.stop-list-head span:nth-child(4) {
  text-align: right;
}

.stop {
  align-items: start;
  padding: var(--space-2) var(--space-3);
  border: 1px solid transparent;
  border-radius: var(--radius);
  transition: background 120ms ease, border-color 120ms ease;
}

.stop:hover,
.stop:focus-within {
  background: var(--surface-2);
  border-color: var(--border);
}

.stop.is-flash {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.stop.is-invalid {
  border-color: var(--danger);
}

/* Número da paragem: anel na cor da rota (texto legível em qualquer tema) */
.stop-index {
  display: inline-grid;
  place-items: center;
  width: 1.625rem;
  height: 1.625rem;
  margin-top: 0.375rem;
  border: 2.5px solid var(--stop-color, var(--stop-neutral));
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  font-size: 0.6875rem;
  font-weight: var(--weight-black);
  font-variant-numeric: tabular-nums;
}

.stop-main {
  display: grid;
  gap: var(--space-1);
  min-width: 0;
}

.stop-coords {
  grid-column: 2 / 5;
}

.stop-msg {
  grid-column: 2 / -1;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--danger);
}

.stop-msg.is-warn {
  color: var(--warn);
}

.stop-window {
  grid-column: 2 / -1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-3);
}

.stop-window .input {
  width: 6.25rem;
  font-variant-numeric: tabular-nums;
}

/* Tempos fixos */
.timing {
  margin: var(--space-3) 0;
  border-radius: var(--radius);
  background: var(--surface-2);
}

.timing summary {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--space-1) var(--space-2);
  padding: var(--space-3) var(--space-4);
  cursor: pointer;
}

.timing summary .field-label {
  margin: 0;
}

.timing-summary {
  font-size: var(--text-xs);
  color: var(--text-3);
}

.timing-grid {
  display: grid;
  gap: var(--space-2);
  padding: 0 var(--space-4) var(--space-2);
}

.timing-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 4.75rem 2.25rem;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-2);
}

.timing-field .unit {
  font-size: var(--text-xs);
  color: var(--text-3);
}

.timing .hint {
  padding: 0 var(--space-4) var(--space-3);
  margin: 0;
}

/* Resultados */
.results-stale {
  margin-bottom: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--warn-soft);
  box-shadow: inset 4px 0 0 var(--warn);
  color: var(--text);
  font-size: var(--text-sm);
}

.summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.kpi {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
}

.kpi-value {
  font-size: var(--text-xl);
  font-weight: var(--weight-black);
  letter-spacing: -0.03em;
  line-height: var(--leading-tight);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.kpi-value .kpi-unit {
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
  letter-spacing: 0;
  color: var(--text-3);
}

.kpi-label {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-3);
}

.results-meta {
  margin-bottom: var(--space-3);
  font-size: var(--text-xs);
  color: var(--text-3);
}

.day-plan {
  margin-bottom: var(--space-4);
}

.plan-list {
  display: grid;
  gap: var(--space-2);
  margin: var(--space-2) 0 0;
  padding: 0;
  list-style: none;
}

.plan-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 2px var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: var(--text-sm);
}

.plan-name {
  font-weight: var(--weight-black);
}

.plan-hours {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.plan-hours strong {
  font-weight: var(--weight-black);
}

.plan-detail,
.plan-break {
  grid-column: 1 / -1;
  font-size: var(--text-xs);
  color: var(--text-3);
}

.plan-break {
  color: var(--text-2);
}

.plan-row.is-idle .plan-hours {
  color: var(--text-3);
}

.route-list {
  display: grid;
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.route {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: inset 5px 0 0 var(--route-color), var(--shadow-xs);
  transition: box-shadow 140ms ease, border-color 140ms ease;
}

.route:hover,
.route:focus-within {
  box-shadow: inset 5px 0 0 var(--route-color), var(--shadow);
}

.route.is-active {
  border-color: var(--route-color);
  box-shadow: inset 5px 0 0 var(--route-color), 0 0 0 1px var(--route-color), var(--shadow);
}

.route-head {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-3) var(--space-4) var(--space-2) var(--space-5);
  border: 0;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.route-head:focus-visible {
  outline-offset: -2px;
}

.swatch {
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  background: var(--route-color);
}

.route-title {
  font-weight: var(--weight-black);
  font-size: var(--text-sm);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.route-title small {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-3);
}

.route-km {
  font-size: var(--text-lg);
  font-weight: var(--weight-black);
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.route-body {
  display: grid;
  gap: var(--space-2);
  padding: 0 var(--space-4) var(--space-4) var(--space-5);
}

.meter {
  display: grid;
  grid-template-columns: 4.5rem minmax(0, 1fr) 5.75rem;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-2);
}

.meter-track {
  display: block;
  height: 0.5rem;
  border-radius: var(--radius-pill);
  background: var(--surface-3);
  overflow: hidden;
}

.meter-fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--route-color);
}

.meter-value {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: var(--weight-bold);
  color: var(--text);
  white-space: nowrap;
}

.route-profile {
  font-size: var(--text-xs);
  color: var(--text-3);
}

.route-timing {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--text-2);
}

.route-times {
  display: grid;
  gap: 3px;
  margin: var(--space-1) 0 0;
  padding: 0;
  list-style: none;
  font-size: var(--text-sm);
}

.route-times li {
  display: grid;
  grid-template-columns: 2.75rem auto 1fr;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-2);
}

.route-times .t {
  font-variant-numeric: tabular-nums;
  font-weight: var(--weight-black);
  color: var(--text);
}

.route-times .is-depot .nm {
  color: var(--text-3);
}

.route-times .ord {
  display: inline-grid;
  place-items: center;
  min-width: 1.375rem;
  height: 1.375rem;
  padding: 0 0.2rem;
  border: 2px solid var(--route-color);
  border-radius: var(--radius-pill);
  background: var(--surface);
  color: var(--text);
  font-size: 0.625rem;
  font-weight: var(--weight-black);
}

.route-times .is-depot .ord {
  border-color: var(--depot);
  background: var(--depot);
  color: #ffffff;
}

/* Guardar plano */
.plan-save {
  display: grid;
  grid-template-columns: 10rem minmax(0, 1fr);
  align-items: end;
  gap: var(--space-3) var(--space-2);
  margin-top: var(--space-4);
  padding: var(--space-4);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
}

.plan-save .btn {
  grid-column: 1 / -1;
}

.plan-saved {
  margin-top: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  background: var(--ok-soft);
  color: var(--text);
  font-size: var(--text-sm);
}

.plan-saved::before {
  content: "✓ ";
  color: var(--ok);
  font-weight: var(--weight-black);
}

.plan-saved .btn {
  display: flex;
  width: fit-content;
  margin-top: var(--space-2);
}

/* Encomendas */
.orders-date {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  color: var(--text-3);
}

.orders-date .input {
  width: 9.75rem;
}

/* Alertas (painel aberto pelo botão da barra de topo) */
.alerts-panel {
  margin: var(--space-4) 0 var(--space-2);
  padding: var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  box-shadow: var(--shadow-sm);
}

.alerts-panel.section {
  padding: var(--space-4);
}

.alert-item {
  padding: var(--space-3) var(--space-4) var(--space-3) 1.125rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: inset 4px 0 0 var(--border-strong);
}

.alert-item.is-open.is-reagendada {
  box-shadow: inset 4px 0 0 var(--warn);
}

.alert-item.is-open.is-nao_cabe,
.alert-item.is-open.is-fora_do_turno,
.alert-item.is-open.is-por_servir,
.alert-item.is-open.is-nao_entregue {
  box-shadow: inset 4px 0 0 var(--danger);
}

.alert-item:not(.is-open) {
  background: var(--surface-2);
}

/* Importar de Excel */
.import-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
}

.import-form .input[type="file"] {
  flex: 1 1 12rem;
  height: auto;
  padding: var(--space-2);
  border-style: dashed;
  border-color: var(--border-strong);
}

.import-report {
  margin-top: var(--space-2);
  font-size: var(--text-sm);
}

.import-report ul {
  margin: var(--space-1) 0 0;
  padding-left: var(--space-4);
  font-size: var(--text-xs);
  color: var(--text-2);
}

.import-report details {
  margin-top: var(--space-2);
}

.import-report summary {
  cursor: pointer;
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
}

.import-line {
  color: var(--text-3);
  font-variant-numeric: tabular-nums;
}

.import-bad {
  color: var(--danger);
}

.import-option {
  flex-basis: 100%;
}

.import-errors {
  margin-top: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--danger-soft);
  box-shadow: inset 4px 0 0 var(--danger);
}

.import-errors-title {
  margin: 0;
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  color: var(--danger);
}

.import-errors ul {
  color: var(--text);
}

.import-errors-file:empty {
  display: none;
}

.import-errors-file {
  margin: var(--space-2) 0 0;
}

/* Registo de entregas */
.deliveries-filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  gap: var(--space-3);
  margin-top: var(--space-2);
}

.deliveries-totals {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(7.5rem, 1fr));
  gap: var(--space-2);
  margin: var(--space-4) 0;
}

.deliveries-totals:empty {
  display: none;
}

.deliveries-stat {
  display: flex;
  flex-direction: column;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
}

.deliveries-stat strong {
  font-size: var(--text-xl);
  font-weight: var(--weight-black);
  letter-spacing: -0.03em;
  line-height: var(--leading-tight);
  font-variant-numeric: tabular-nums;
}

.deliveries-stat span {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-3);
}

.deliveries-unlinked {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
  margin-bottom: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--warn-soft);
  box-shadow: inset 4px 0 0 var(--warn);
  font-size: var(--text-sm);
}

.deliveries-unlinked p {
  margin: 0;
  flex: 1 1 14rem;
}

.delivery-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius);
  background: var(--surface-2);
}

.delivery-form .form-field:first-child {
  flex: 1 1 11rem;
}

.delivery-form .input-num {
  width: 4.75rem;
}

.delivery-form .form-error {
  flex-basis: 100%;
  margin: 0;
}

/* Previsões de pedidos */
.forecast-when {
  margin: var(--space-1) 0 0;
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
}

.forecast-late {
  color: var(--danger);
  font-weight: var(--weight-black);
}

.forecast-late::before {
  content: "! ";
}

.forecast-conf.is-alta {
  color: var(--ok);
  background: var(--ok-soft);
}

.forecast-conf.is-baixa {
  color: var(--text-3);
}

.forecast-item a[href^="tel:"] {
  color: var(--accent-text);
  font-weight: var(--weight-bold);
}

.forecast-form .hint,
.forecast-form .form-wide-row {
  flex-basis: 100%;
  margin: 0;
}

.forecast-with-order {
  margin-top: var(--space-3);
  font-size: var(--text-sm);
  color: var(--text-2);
}

.forecast-with-order summary {
  cursor: pointer;
  font-weight: var(--weight-semibold);
}

.forecast-with-order ul {
  margin: var(--space-1) 0 0;
  padding-left: var(--space-4);
}

/* Perfil do cliente */
.customer-profile {
  margin-top: var(--space-4);
  padding: var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.customer-profile.section:last-child {
  border-bottom: 1px solid var(--border);
}

.profile-meta {
  margin: 0 0 var(--space-3);
  font-size: var(--text-sm);
  color: var(--text-3);
}

.profile-hero {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-2);
}

.profile-hero strong {
  display: block;
  font-size: var(--text-2xl);
  font-weight: var(--weight-black);
  letter-spacing: -0.03em;
  line-height: var(--leading-tight);
  font-variant-numeric: tabular-nums;
}

.profile-hero span {
  font-size: var(--text-sm);
  color: var(--text-3);
}

.profile-change {
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
}

.profile-change.is-up {
  color: var(--ok);
}

.profile-change.is-down {
  color: var(--text-2);
}

.profile-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(7.5rem, 1fr));
  gap: var(--space-2);
  margin: var(--space-4) 0;
}

.profile-stat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: var(--space-3);
  border-radius: var(--radius);
  background: var(--surface-2);
}

.profile-stat strong {
  font-size: var(--text-md);
  font-weight: var(--weight-black);
  font-variant-numeric: tabular-nums;
}

.profile-stat small {
  font-weight: var(--weight-regular);
  color: var(--text-3);
}

.profile-stat span {
  font-size: var(--text-xs);
  color: var(--text-3);
}

.profile-h {
  margin: var(--space-5) 0 var(--space-2);
  font-size: var(--text-base);
  font-weight: var(--weight-black);
  letter-spacing: -0.01em;
}

.profile-h small {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-3);
}

.profile-recent {
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: var(--text-sm);
}

.profile-recent li {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

/* Gráficos (charts.js) */
.viz-wrap {
  position: relative;
}

.viz-svg {
  display: block;
  max-width: 100%;
  height: auto;
  overflow: visible;
}

.viz-grid {
  stroke: var(--border);
  stroke-width: 1;
}

.viz-baseline {
  stroke: var(--border-strong);
  stroke-width: 1;
}

.viz-tick {
  fill: var(--text-3);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
}

.viz-s1 {
  fill: var(--series-1);
}

.viz-s2 {
  fill: var(--series-2);
}

.viz-ref-line {
  fill: none;
  stroke: var(--series-ref);
  stroke-width: 2;
}

.viz-ref-dot {
  fill: var(--series-ref);
  stroke: var(--surface);
  stroke-width: 2;
}

.viz-hit {
  fill: transparent;
  cursor: default;
}

.viz-hit.is-hover {
  fill: var(--text);
  fill-opacity: 0.05;
}

.viz-label {
  fill: var(--text-2);
  font-size: 11px;
}

.viz-value {
  fill: var(--text);
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.viz-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-4);
  margin-bottom: var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-2);
}

.viz-key {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px;
  border-radius: 3px;
  vertical-align: -1px;
}

.viz-key-s1 {
  background: var(--series-1);
}

.viz-key-s2 {
  background: var(--series-2);
}

.viz-key-ref {
  height: 2px;
  vertical-align: 3px;
  background: var(--series-ref);
}

.viz-tooltip {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-xs);
  color: var(--text);
  white-space: nowrap;
}

.viz-table {
  margin-top: var(--space-2);
  font-size: var(--text-xs);
}

.viz-table summary {
  cursor: pointer;
  font-weight: var(--weight-semibold);
  color: var(--text-3);
}

.viz-table table {
  width: 100%;
  margin-top: var(--space-1);
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.viz-table th,
.viz-table td {
  padding: 3px var(--space-1);
  border-bottom: 1px solid var(--border);
  text-align: right;
}

.viz-table th:first-child,
.viz-table td:first-child {
  text-align: left;
}

/* Relatórios */
.reports-filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  gap: var(--space-3);
  max-width: 40rem;
}

.reports-period {
  margin: var(--space-3) 0 var(--space-4);
  font-size: var(--text-sm);
  color: var(--text-3);
}

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr));
  gap: var(--space-2);
}

.kpi strong {
  font-size: var(--text-xl);
  white-space: nowrap;
  font-weight: var(--weight-black);
  letter-spacing: -0.03em;
  line-height: var(--leading-tight);
  font-variant-numeric: tabular-nums;
}

.kpis .kpi > span {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-3);
}

.kpi .kpi-delta {
  margin-top: 2px;
  font-weight: var(--weight-bold);
  color: var(--text-2);
}

.kpi .kpi-delta.is-good {
  color: var(--ok);
}

.kpi .kpi-delta.is-bad {
  color: var(--danger);
}

.report-warnings {
  margin: var(--space-4) 0 0;
  padding: var(--space-3) var(--space-4) var(--space-3) var(--space-8);
  border-radius: var(--radius);
  background: var(--warn-soft);
  box-shadow: inset 4px 0 0 var(--warn);
  font-size: var(--text-sm);
}

.report-card {
  min-width: 0;
  margin-top: var(--space-4);
  padding: var(--space-4) var(--space-5) var(--space-5);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

.report-card .profile-h {
  margin-top: 0;
}

.report-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 22rem), 1fr));
  gap: 0 var(--space-4);
}

/* Telemóveis dos distribuidores: código QR por carrinha */
.driver-list {
  display: grid;
  gap: var(--space-3);
  margin: var(--space-2) 0 0;
  padding: 0;
  list-style: none;
}

.driver-card {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
  align-items: center;
  min-width: 0;
  padding: var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
}

.driver-qr {
  flex: none;
  padding: 6px;
  background: #ffffff; /* o QR precisa de fundo claro, também no tema escuro */
  border-radius: var(--radius-sm);
}

.driver-info {
  display: grid;
  gap: var(--space-1);
  min-width: 0;
  flex: 1 1 12rem;
}

.driver-url {
  font-size: var(--text-xs);
  color: var(--text-2);
  overflow-wrap: anywhere;
}

/* 6. Mapa / Leaflet --------------------------------------------------------- */
.leaflet-container {
  font-family: var(--font);
  background: var(--map-bg);
}

.leaflet-tile-pane {
  filter: var(--tile-filter);
}

#map.is-picking,
#map.is-picking .leaflet-interactive {
  cursor: crosshair;
}

.map-shell .leaflet-bar {
  border: 0;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.map-shell .leaflet-bar a {
  width: 2.5rem;
  height: 2.5rem;
  line-height: 2.5rem;
  background: var(--surface);
  color: var(--text);
  border-bottom-color: var(--border);
  font-size: 1.25rem;
}

.map-shell .leaflet-bar a:hover,
.map-shell .leaflet-bar a:focus {
  background: var(--surface-2);
  color: var(--text);
}

.map-shell .leaflet-bar a.leaflet-disabled {
  background: var(--surface-2);
  color: var(--text-3);
}

.map-shell .leaflet-top.leaflet-right {
  margin-top: 2px;
}

.map-shell .leaflet-control-attribution {
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  color: var(--text-3);
  border-top-left-radius: var(--radius-sm);
}

.map-shell .leaflet-control-attribution a {
  color: var(--accent-text);
}

.leaflet-tooltip {
  padding: 0.3rem 0.6rem;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
}

.leaflet-tooltip-top::before {
  border-top-color: var(--surface);
}

.leaflet-tooltip-bottom::before {
  border-bottom-color: var(--surface);
}

.leaflet-tooltip-left::before {
  border-left-color: var(--surface);
}

.leaflet-tooltip-right::before {
  border-right-color: var(--surface);
}

.map-overlay {
  position: absolute;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg) 35%, transparent);
}

.map-overlay-card {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  border-radius: var(--radius-pill);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  font-weight: var(--weight-bold);
  font-size: var(--text-sm);
}

/* Legenda das rotas: canto inferior direito (o painel ocupa a esquerda) */
.map-legend {
  position: absolute;
  right: var(--gap);
  bottom: 1.75rem;
  z-index: 900;
  display: grid;
  gap: var(--space-1);
  max-width: min(18rem, calc(100% - 2rem));
  max-height: calc(100% - 7rem);
  overflow-y: auto;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
}

.map-legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.map-legend-item .swatch {
  flex: none;
}

.map-legend .swatch-depot {
  background: var(--depot);
  box-shadow: 0 0 0 2px var(--marker-stroke), 0 0 0 3px var(--border-strong);
}

/* 7. Responsivo ------------------------------------------------------------ */
@media (max-width: 1280px) {
  .brand-tag {
    display: none;
  }

  .view-tab {
    padding: 0 var(--space-3);
  }
}

@media (max-width: 1100px) {
  :root {
    --panel-w: 390px;
  }

  .topbar {
    gap: var(--space-3);
    padding-left: var(--space-4);
  }
}

/* Tablet retrato e telemóvel: barra em duas linhas, mapa em cima, folha por baixo */
@media (max-width: 820px) {
  .app {
    grid-template-rows: auto 44vh auto;
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
  }

  .topbar {
    flex-wrap: wrap;
    row-gap: var(--space-2);
    min-height: 0;
    padding: var(--space-3) var(--space-3) var(--space-2);
  }

  .brand-logo {
    width: 30px;
    height: 30px;
  }

  .view-tabs {
    order: 3;
    flex: 1 1 100%;
    margin: 0 calc(-1 * var(--space-3));
    padding: 2px 1.5rem var(--space-1) var(--space-3);
  }

  .topbar-actions {
    gap: var(--space-1);
  }

  .alerts-btn .alerts-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .map-shell {
    grid-row: 2;
    min-height: 280px;
  }

  /* A folha sobe um pouco sobre o mapa, com a pega */
  .panel {
    grid-row: 3;
    justify-self: stretch;
    width: auto;
    max-width: none;
    max-height: none;
    margin: -1.25rem 0 0;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    box-shadow: 0 -8px 28px rgba(16, 24, 40, 0.18);
    overflow: visible;
  }

  .panel::before {
    content: "";
    display: block;
    width: 2.5rem;
    height: 0.3rem;
    margin: var(--space-2) auto 0;
    border-radius: var(--radius-pill);
    background: var(--border-strong);
  }

  .panel-body {
    overflow: visible;
    padding: 0 var(--space-4) var(--space-6);
  }

  .solve-bar {
    margin: 0 calc(-1 * var(--space-4));
    padding: var(--space-3) var(--space-4);
  }

  /* Controlos do Leaflet acima da borda da folha */
  .map-shell .leaflet-bottom {
    bottom: 1.25rem;
  }

  /* Legenda compacta: uma faixa de pontos por cima do mapa pequeno */
  .map-legend {
    top: var(--gap);
    bottom: auto;
    left: var(--gap);
    right: auto;
    display: flex;
    flex-wrap: wrap;
    gap: 2px var(--space-3);
    max-width: calc(100% - 4.5rem);
    max-height: 40%;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius);
    font-size: 0.6875rem;
  }

  .map-legend .swatch {
    width: 0.625rem;
    height: 0.625rem;
  }

  .app.is-wide {
    grid-template-rows: auto 1fr;
  }

  .app.is-wide .panel {
    grid-row: 2;
    margin: 0;
    border-radius: 0;
  }

  .app.is-wide .panel::before {
    display: none;
  }

  .app.is-wide .panel-body {
    padding: var(--space-3);
  }

  .app.is-wide .section {
    padding: var(--space-4);
  }
}

@media (max-width: 480px) {
  .order-form {
    grid-template-columns: minmax(0, 1fr);
  }

  .plan-save {
    grid-template-columns: minmax(0, 1fr);
  }

  .summary {
    gap: var(--space-1);
  }

  .kpi {
    padding: var(--space-2) var(--space-3);
  }

  .kpi-value,
  .kpi strong {
    font-size: var(--text-lg);
  }

  .kpis {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .deliveries-totals {
    grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
  }

  .deliveries-stat strong {
    font-size: var(--text-lg);
  }

  .theme-toggle-option {
    padding: 0 0.5rem;
  }

  .brand-mark {
    font-size: var(--text-md);
  }
}

/* ---------------------------------------------------------------- calendário */

.cal-head { flex-wrap: wrap; gap: var(--space-2); }
.cal-nav { display: flex; align-items: center; gap: var(--space-2); }
.cal-month { margin: 0; min-width: 11rem; text-align: center; font-weight: var(--weight-black); font-size: var(--text-lg); }
.cal-legend { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin: 0 0 var(--space-3); padding: 0; list-style: none; font-size: var(--text-xs); color: var(--text-2); }
.cal-legend li { display: inline-flex; align-items: center; gap: 6px; }
.cal-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--text-3); }
.cal-dot.is-route { background: var(--accent); }
.cal-dot.is-pending { background: var(--warn); }
.cal-dot.is-done { background: var(--ok); }
.cal-dot.is-forecast { background: transparent; box-shadow: inset 0 0 0 2px var(--series-2); }
.cal-bar-key { width: 22px; height: 6px; border-radius: var(--radius-pill); background: linear-gradient(90deg, var(--accent) 60%, var(--surface-3) 60%); }
.cal-totals { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: var(--space-2); margin-bottom: var(--space-4); }

.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.cal-weekday { font-size: var(--text-xs); font-weight: var(--weight-bold); color: var(--text-3); text-transform: uppercase; letter-spacing: 0.04em; padding: 0 var(--space-1); }
.cal-day {
  display: flex; flex-direction: column; gap: 4px; min-height: 8.5rem; min-width: 0; padding: var(--space-2);
  text-align: left; font: inherit; color: var(--text); cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.cal-day:hover { border-color: var(--accent); }
.cal-day:focus-visible { outline: 2px solid var(--focus, var(--accent)); outline-offset: 2px; }
.cal-day.is-outside { opacity: 0.55; }
.cal-day.is-past { background: var(--surface-2); }
.cal-day.is-off { background: var(--surface-2); }
.cal-day.is-today { border: 2px solid var(--accent); }
.cal-day.is-selected { box-shadow: 0 0 0 3px var(--accent-soft), inset 0 0 0 1px var(--accent); }
.cal-num { font-weight: var(--weight-black); font-variant-numeric: tabular-nums; }
.cal-wd { display: none; }
.cal-today-tag { font-size: var(--text-xs); font-weight: var(--weight-bold); color: var(--accent-text); }
.cal-off { margin: 0; font-size: var(--text-xs); color: var(--text-3); }
.cal-lines { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.cal-line { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 5px; font-size: var(--text-xs); }
.cal-line-label { color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-line-value { font-weight: var(--weight-bold); font-variant-numeric: tabular-nums; white-space: nowrap; }
.cal-plan { margin: 0; font-size: var(--text-xs); color: var(--text-2); }
.cal-warn { color: var(--danger); }
.cal-load { margin-top: auto; display: grid; gap: 3px; }
.cal-load-track { display: block; height: 6px; border-radius: var(--radius-pill); background: var(--surface-3); overflow: hidden; }
.cal-load-fill { display: block; height: 100%; border-radius: inherit; background: var(--accent); }
.cal-load.is-high .cal-load-fill { background: var(--warn); }
.cal-load.is-over .cal-load-fill { background: var(--danger); }
.cal-load-text { font-size: 0.6875rem; color: var(--text-3); font-variant-numeric: tabular-nums; }
.cal-load.is-over .cal-load-text { color: var(--danger); font-weight: var(--weight-bold); }

.cal-detail { margin-top: var(--space-4); padding: var(--space-4); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); display: grid; gap: var(--space-3); }
.cal-detail-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: var(--space-2); }
.cal-detail-title { margin: 0; font-size: var(--text-lg); font-weight: var(--weight-black); }
.cal-detail-title::first-letter { text-transform: uppercase; }
.cal-detail-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.cal-detail-plan { margin: 0; }
.cal-detail-sub { margin: 0; font-size: var(--text-sm); }
.cal-detail .cal-load { max-width: 28rem; }
.cal-table-wrap { overflow-x: auto; }
.cal-table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.cal-table th { text-align: left; font-size: var(--text-xs); color: var(--text-3); padding: var(--space-1) var(--space-2); border-bottom: 1px solid var(--border); }
.cal-table td { padding: var(--space-2); border-bottom: 1px solid var(--border); vertical-align: top; }
.cal-sub { display: block; color: var(--text-3); font-size: var(--text-xs); }
.cal-state { display: inline-flex; align-items: center; gap: 5px; font-weight: var(--weight-bold); font-size: var(--text-xs); white-space: nowrap; }
.cal-state::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); }
.cal-state.is-route::before { background: var(--accent); }
.cal-state.is-pending::before { background: var(--warn); }
.cal-state.is-done::before { background: var(--ok); }
.cal-state.is-cancelled { color: var(--text-3); }
.cal-forecast { margin: 0; padding-left: 1.1rem; display: grid; gap: 4px; font-size: var(--text-sm); }

@media (max-width: 1100px) {
  .cal-totals { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* Telemóvel: a grelha vira uma agenda (um dia por linha, só os dias com algo ou dias de entrega) */
@media (max-width: 820px) {
  .cal-totals { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cal-grid { grid-template-columns: 1fr; }
  .cal-weekday, .cal-day.is-outside { display: none; }
  .cal-day { min-height: 0; flex-direction: row; flex-wrap: wrap; align-items: baseline; column-gap: var(--space-3); }
  .cal-num { min-width: 4.5rem; }
  .cal-wd { display: inline; color: var(--text-3); }
  .cal-lines, .cal-plan, .cal-load { flex: 1 1 14rem; }
}

/* ---------------------------------------------------------------- escolha dentro de um separador (Encomendas | Clientes) */

.seg { display: flex; gap: 4px; padding: 4px; margin-bottom: var(--space-4); background: var(--surface-3); border-radius: var(--radius-pill); }
.seg-option {
  flex: 1 1 0; min-height: 2.25rem; border: 0; border-radius: var(--radius-pill); background: transparent;
  font: inherit; font-weight: var(--weight-bold); color: var(--text-2); cursor: pointer;
}
.seg-option:hover { color: var(--text); }
.seg-option.is-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }
.seg-option:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.cal-free-trips { margin: 0; padding-left: 1rem; display: grid; gap: 2px; font-size: var(--text-xs); }
.cal-free-total { font-weight: var(--weight-black); white-space: nowrap; font-variant-numeric: tabular-nums; }
.route-free { margin: 2px 0 0; font-size: var(--text-xs); color: var(--text-2); }
.cal-load-text.cal-free { color: var(--text-2); font-weight: var(--weight-semibold); }
.stop-pick { display: grid; gap: 4px; margin-bottom: var(--space-3); }
.stop-pick .hint:empty { display: none; }
.stop-main { position: relative; }
.stop-registry { margin-top: 4px; }

/* ---------------------------------------------------------------- clientes perto da rota */
.nearby-filters { display: flex; flex-wrap: wrap; gap: var(--space-3); margin: var(--space-2) 0; }
.nearby-list { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-2); }
.nearby-item { padding: var(--space-3); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); display: grid; gap: 4px; }
.nearby-head { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.nearby-meta { margin: 0; font-size: var(--text-xs); color: var(--text-2); }
.nearby-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-top: 2px; }
.provisional-bar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); margin: var(--space-2) 0; }

/* ---------------------------------------------------------------- registo de clientes (lista compacta) */
.cust-toolbar { display: grid; gap: var(--space-2); }
.cust-filters { display: flex; flex-wrap: wrap; gap: 6px; }
.cust-filter {
  display: inline-flex; align-items: center; gap: 0.35rem; min-height: 2rem; padding: 0 0.75rem;
  border: 1px solid var(--border-strong); border-radius: var(--radius-pill); background: var(--surface);
  font: inherit; font-size: var(--text-xs); font-weight: var(--weight-bold); color: var(--text-2); cursor: pointer;
}
.cust-filter:hover { border-color: var(--text-3); color: var(--text); }
.cust-filter.is-active { border-color: var(--accent-strong); background: var(--accent-soft); color: var(--accent-text); }
.cust-filter:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.cust-filter-count { font-variant-numeric: tabular-nums; font-weight: var(--weight-black); }
.cust-sortbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-2); }
.cust-sortbar .stops-summary { margin: 0; }
.cust-sort { display: inline-flex; align-items: center; gap: var(--space-2); font-size: var(--text-xs); font-weight: var(--weight-bold); color: var(--text-3); }
.cust-sort .input { width: auto; }
.cust-toolbar .provisional-bar { margin: 0; gap: var(--space-2); }
.cust-toolbar .provisional-bar .hint { font-weight: var(--weight-semibold); color: var(--warn); }

.cust-list { list-style: none; margin: var(--space-3) 0; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.cust-list:empty { display: none; }
.cust-item + .cust-item { border-top: 1px solid var(--border); }
.cust-item:first-child .cust-row { border-radius: var(--radius) var(--radius) 0 0; }
.cust-item:last-child:not(.is-open) .cust-row { border-radius: 0 0 var(--radius) var(--radius); }
.cust-row {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: var(--space-2); row-gap: 1px;
  width: 100%; padding: var(--space-2) var(--space-3); border: 0; background: transparent;
  font: inherit; color: var(--text); text-align: left; cursor: pointer;
}
.cust-row:hover { background: var(--surface-2); }
.cust-row:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.cust-item.is-open { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.cust-item.is-open .cust-row:hover { background: transparent; }
.cust-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--text-sm); font-weight: var(--weight-black); }
.cust-code { font-size: var(--text-xs); font-weight: var(--weight-bold); color: var(--text-3); font-variant-numeric: tabular-nums; white-space: nowrap; }
.cust-meta { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--text-xs); color: var(--text-3); }
.cust-chips { display: flex; justify-content: flex-end; gap: 4px; }
.cust-chips .chip { padding: 0 0.45rem; font-size: 0.6875rem; }
.cust-detail { padding: 0 var(--space-3) var(--space-3); }
.cust-detail .order-actions { margin-top: var(--space-2); }
.cust-facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 2px var(--space-3); margin: 0; font-size: var(--text-sm); }
.cust-facts dt { font-size: var(--text-xs); font-weight: var(--weight-bold); color: var(--text-3); padding-top: 1px; }
.cust-facts dd { margin: 0; color: var(--text-2); overflow-wrap: anywhere; }
.cust-facts a { color: var(--accent-text); font-weight: var(--weight-bold); }
.cust-remove:hover:not(:disabled) { color: var(--danger); background: var(--danger-soft); }
.cust-edit { margin: var(--space-3) 0; padding: var(--space-4); border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--surface); }
.cust-edit .section-title { margin-bottom: var(--space-3); font-size: var(--text-md); }
.cust-detail .cust-edit { margin: 0; padding: var(--space-3); }

/* Secções secundárias recolhidas (<details class="section">) */
.cust-collapsible > summary { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--space-1) var(--space-3); cursor: pointer; list-style: none; }
.cust-collapsible > summary::-webkit-details-marker { display: none; }
.cust-collapsible > summary::before { content: "▸"; color: var(--text-3); transition: transform 140ms ease; }
.cust-collapsible[open] > summary::before { transform: rotate(90deg); }
.cust-collapsible > summary .section-title { margin: 0; }
.cust-collapsible > summary .hint { margin: 0; }
.cust-collapsible[open] > summary { margin-bottom: var(--space-3); }
.cust-collapsible > summary:focus-visible { outline: 2px solid var(--focus); outline-offset: 4px; border-radius: var(--radius-sm); }

@media (max-width: 480px) {
  .cust-facts { grid-template-columns: minmax(0, 1fr); }
  .cust-facts dt { padding-top: var(--space-1); }
}
