:root {
  --patinter-red: #C8102E;
  --patinter-red-dark: #9A0C22;
  --patinter-yellow: #F5B700;
  --asphalt: #2B2D31;
  --asphalt-light: #3D4046;
  --paper: #F7F5F2;
  --ink: #1C1C1C;
  --muted: #6B6B6B;
  --line: #E3E0DA;
  --ok: #1E8E3E;
  --warn: #B25E00;
  --danger: #C8102E;

  --dept-gasoleo: #C8102E;
  --dept-apoio: #1F6FB2;
  --dept-super: #2B2D31;

  --radius: 10px;
  --shadow: 0 2px 10px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden; /* o scroll acontece dentro de .panel-map e .entity-list/table, nunca na página */
}

body {
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--paper);
  color: var(--ink);
}

h1, h2, h3 { margin: 0 0 4px; font-family: "Georgia", "Times New Roman", serif; }
h1 { font-size: 20px; }
h2 { font-size: 17px; }
h3 { font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 8px;
  background: linear-gradient(135deg, var(--patinter-red), var(--patinter-red-dark));
  color: white; font-weight: 800; font-size: 13px; letter-spacing: 0.02em;
  flex-shrink: 0;
}

/* ───────── LOGIN ───────── */
.login-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background:
    repeating-linear-gradient(135deg, var(--asphalt) 0px, var(--asphalt) 2px, transparent 2px, transparent 14px),
    var(--asphalt-light);
}
.login-card {
  background: white; border-radius: 16px; padding: 36px 32px;
  width: 100%; max-width: 360px; box-shadow: 0 12px 40px rgba(0,0,0,0.35);
}
.login-brand { text-align: center; margin-bottom: 24px; }
.login-brand .brand-mark { margin: 0 auto 12px; width: 52px; height: 52px; font-size: 16px; }
.login-brand h1 { font-size: 19px; }
.login-brand p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
#loginForm label { display: block; margin-bottom: 14px; font-size: 13px; font-weight: 600; color: var(--muted); }
#loginForm input {
  display: block; width: 100%; margin-top: 6px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 8px; font-size: 14px;
}
.error-text { color: var(--danger); font-size: 13px; margin: 10px 0 0; }

/* ───────── BUTTONS ───────── */
.btn {
  border: none; border-radius: 8px; padding: 10px 16px; font-size: 13px;
  font-weight: 700; cursor: pointer; transition: transform 0.05s, opacity 0.15s;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--patinter-red); color: white; }
.btn.primary:hover { background: var(--patinter-red-dark); }
.btn.secondary { background: var(--line); color: var(--ink); }
.btn.danger { background: transparent; color: var(--patinter-red); border: 1px solid var(--patinter-red); }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.btn.full { width: 100%; padding: 12px; }
.btn.small { padding: 6px 10px; font-size: 12px; }

/* ───────── APP SHELL ───────── */
.app-screen {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--asphalt); color: white;
  flex-shrink: 0;
}
.topbar-brand { display: flex; align-items: center; gap: 12px; }
.topbar h1 { color: white; }
.subtitle { margin: 0; font-size: 12px; color: #B9BBC0; }
.topbar-user { display: flex; align-items: center; gap: 12px; }
.user-pill { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.dept-badge {
  padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 800;
  text-transform: uppercase; letter-spacing: 0.03em; background: var(--patinter-yellow); color: var(--ink);
}

.tabbar {
  display: flex; gap: 4px; padding: 10px 24px; background: white; border-bottom: 1px solid var(--line);
  overflow-x: auto; flex-shrink: 0;
}
.tab-btn {
  border: none; background: transparent; padding: 9px 16px; border-radius: 8px;
  font-size: 13px; font-weight: 700; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.tab-btn.active { background: var(--patinter-red); color: white; }
.tab-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.layout {
  display: grid; grid-template-columns: 420px 1fr; gap: 0;
  flex: 1; min-height: 0;
}
.panel { overflow: hidden; min-height: 0; }
.panel-form { padding: 0; background: white; border-right: 1px solid var(--line); display: flex; flex-direction: column; overflow-x: hidden; overflow-y: auto; }
.panel-map { position: relative; display: flex; flex-direction: column; min-height: 0; }

.tab-content { display: none; }
.tab-content.active { display: flex; flex-direction: column; min-height: 100%; }

/* Tudo dentro do separador ativo, exceto a lista, fica fixo (não faz scroll). */
.tab-content.active > *:not(.list-wrap) { flex-shrink: 0; padding-left: 24px; padding-right: 24px; }
.tab-content.active > h2 { margin-top: 20px; }
.tab-content.active > .form-buttons,
.tab-content.active > form.entity-form { padding-left: 24px; padding-right: 24px; }

/* A lista de registos é a única parte que cresce e faz scroll própria,
   com altura mínima suficiente para mostrar ~5 registos. */
.tab-content.active > .list-wrap {
  flex-shrink: 0; display: flex; flex-direction: column;
  padding-left: 24px; padding-right: 24px; padding-bottom: 20px;
}
.tab-content.active > .list-wrap > .entity-list,
.tab-content.active > .list-wrap > table {
  max-height: 420px; overflow-y: auto;
}

.dept-hint { font-size: 12px; color: var(--muted); margin: 0 0 16px; }
.dept-hint strong { color: var(--ink); }

.entity-form label {
  display: block; font-size: 12px; font-weight: 700; color: var(--muted);
  margin-bottom: 12px; text-transform: uppercase; letter-spacing: 0.02em;
}
.entity-form input, .entity-form select, .entity-form textarea {
  display: block; width: 100%; margin-top: 6px; padding: 9px 10px;
  border: 1px solid var(--line); border-radius: 8px; font-size: 14px;
  font-weight: 400; text-transform: none; letter-spacing: normal; color: var(--ink);
  font-family: inherit;
}
.checkbox-inline { display: flex !important; align-items: center; gap: 8px; }
.checkbox-inline input { width: auto !important; margin: 0 !important; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.check-row label { display: flex; align-items: center; gap: 6px; font-size: 12px; text-transform: none; }
.check-row input { width: auto; margin: 0; }

.form-buttons { display: flex; gap: 10px; margin-top: 18px; }

.list-wrap { margin-top: 20px; border-top: 1px solid var(--line); padding-top: 18px; }
.list-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; flex-shrink: 0; }
.entity-list { display: flex; flex-direction: column; gap: 8px; }

.entity-card {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px;
  display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
  cursor: pointer; background: white;
}
.entity-card:hover { border-color: var(--patinter-red); }
.entity-card.inactive { opacity: 0.5; }
.entity-card .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.entity-card .badges { display: flex; gap: 4px; margin-top: 6px; flex-wrap: wrap; }
.badge { font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: var(--line); color: var(--ink); }
.badge.ok { background: #E2F4E7; color: var(--ok); }
.badge.warn { background: #FCEBD6; color: var(--warn); }
.badge.danger { background: #FBDDE2; color: var(--danger); }
.entity-card .row-actions { display: flex; gap: 6px; flex-shrink: 0; }
.icon-btn { background: none; border: none; cursor: pointer; font-size: 14px; padding: 4px; opacity: 0.6; }
.icon-btn:hover { opacity: 1; }

.map-layers {
  display: flex; gap: 14px; padding: 12px 20px; background: white; border-bottom: 1px solid var(--line);
}
.layer-toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 700; cursor: pointer; }
.stations-toggle { color: var(--dept-gasoleo); }
.rest-toggle { color: var(--dept-apoio); }
.restrictions-toggle { color: var(--warn); }
.routes-toggle { color: #5B3A9E; }
.announcements-toggle { color: #B8860B; }
.occurrences-toggle { color: #C8102E; }
.facilities-toggle { color: #C8102E; }

#map { flex: 1; min-height: 0; }

.route-points-box {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 4px 0 16px;
  background: #FAF9F7;
}
.route-points-header {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.02em;
  color: var(--muted); margin-bottom: 8px;
}
.route-points-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.route-point-item {
  display: flex; align-items: center; justify-content: space-between;
  background: white; border: 1px solid var(--line); border-radius: 6px; padding: 6px 9px; font-size: 12px;
}
.route-point-item .point-label { font-weight: 600; }
.route-point-item button { background: none; border: none; cursor: pointer; opacity: 0.6; font-size: 13px; }
.route-point-item button:hover { opacity: 1; }
.route-calc-info { font-size: 12px; color: var(--muted); margin-top: 8px; min-height: 16px; }
.route-calc-info.success { color: var(--ok); font-weight: 600; }
.route-calc-info.error { color: var(--danger); font-weight: 600; }

.info-box {
  padding: 10px 20px; background: white; border-top: 1px solid var(--line);
  font-size: 12px; color: var(--muted);
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-shrink: 0;
}
#infoText { flex: 1; min-width: 0; }
.info-credit { flex-shrink: 0; font-size: 10px; color: #B5B2AC; white-space: nowrap; }

/* Leaflet marker color helpers */
.marker-pin { border-radius: 50% 50% 50% 0; transform: rotate(-45deg); width: 26px; height: 26px; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 5px rgba(0,0,0,0.4); }
.marker-pin span { transform: rotate(45deg); font-size: 13px; }
.marker-station { background: var(--dept-gasoleo); }
.marker-rest { background: var(--dept-apoio); }
.marker-restriction { background: var(--warn); }
.marker-announcement { background: #F5B700; }
.marker-announcement.severity-critico { background: var(--danger); }
.marker-facility { background: #C8102E; }

.occ-detail-panel { margin-top: 16px; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); background: #FAF9F7; }
.occ-filters { margin-bottom: 8px; }
.occ-filters .grid-2 { margin-bottom: 4px; }

.route-point-marker {
  width: 24px; height: 24px; border-radius: 50%; background: #5B3A9E; color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 800; box-shadow: 0 2px 5px rgba(0,0,0,0.4); border: 2px solid white;
}

@media (max-width: 900px) {
  html, body { overflow: auto; height: auto; }
  .app-screen { height: auto; min-height: 100vh; }
  .layout { grid-template-columns: 1fr; height: auto; flex: none; }
  .panel { overflow: visible; }
  .panel-map { height: 420px; }
  .tab-content.active > .list-wrap > .entity-list,
  .tab-content.active > .list-wrap > table { overflow-y: visible; }
}

/* ── Toasts (showSuccess/showError/showWarning) ──────────────────────── */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  background: white;
  border-left: 5px solid var(--muted);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 10px 14px;
  font-size: 13px;
  color: var(--ink);
  display: flex;
  align-items: flex-start;
  gap: 8px;
  animation: toast-in 0.18s ease-out;
}
.toast.success { border-left-color: var(--ok); }
.toast.error   { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warn); }
.toast .toast-icon { font-size: 15px; line-height: 1.4; }
.toast .toast-message { flex: 1; line-height: 1.4; }
.toast .toast-close {
  background: none; border: none; cursor: pointer; opacity: 0.5; font-size: 14px; line-height: 1;
  padding: 0 2px;
}
.toast .toast-close:hover { opacity: 1; }
.toast.toast-leaving { animation: toast-out 0.15s ease-in forwards; }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(16px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes toast-out {
  from { opacity: 1; transform: translateX(0); }
  to   { opacity: 0; transform: translateX(16px); }
}

/* ── Botão em estado "a guardar..." ───────────────────────────────────── */
.btn.is-loading {
  opacity: 0.75;
  cursor: wait;
  pointer-events: none;
}

/* ── Campo inválido (validação de formulário) ────────────────────────── */
.field-invalid {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 2px rgba(200, 16, 46, 0.15);
}

/* ── Marcador selecionado a partir da lista (destaque temporário) ───── */
.marker-selected {
  filter: drop-shadow(0 0 6px rgba(37, 99, 235, 0.9));
  animation: marker-pulse 1s ease-in-out 2;
}
@keyframes marker-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.25); }
}

/* ── Marcador de aviso/alerta ativo — pisca continuamente para chamar a
   atenção; anima opacidade (não transform) para não entrar em conflito
   com o rotate(-45deg) já aplicado em .marker-pin. Um aviso inativo/
   expirado nunca chega a ter esta classe — fica só na lista (ver
   getEffectiveActive/listenCollection). ───────────────────────────── */
.marker-blink {
  animation: marker-blink 1.2s ease-in-out infinite;
}
@keyframes marker-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* ── Lista compacta de Instalações Patinter no painel do mapa ────────── */
.facilities-list {
  max-height: 110px;
  overflow-y: auto;
  border-bottom: 1px solid var(--line);
  background: white;
}
.facilities-list .facility-item {
  padding: 6px 20px;
  font-size: 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.facilities-list .facility-item:last-child { border-bottom: none; }
.facilities-list .facility-item:hover { background: #FAF9F7; }
