:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f7f9fa;
  --text: #16202a;
  --muted: #5d6b78;
  --border: #dde3e8;
  --border-strong: #c5cfd8;
  --accent: #00a95c;
  --accent-strong: #008a4b;
  --accent-soft: #e6f6ee;
  --accent-text: #ffffff;
  --gold: #f5b700;
  --red: #d01c1f;
  --ok-bg: #e6f6ee;
  --ok-text: #03703f;
  --bad-bg: #fde8e8;
  --bad-text: #a31215;
  --warn-bg: #fff4dc;
  --warn-text: #8a5a00;
  --neutral-bg: #eef1f4;
  --neutral-text: #3f4c58;
  --row-hover: #f0f7f3;
  --brand-tile: #16202a;
  /* Couleurs des carnets, comme dans l'app (MV vert, LV orange, SP rouge, Licence bleu). */
  --carnet-lv: #ea580c;
  --carnet-mv: #00a95c;
  --carnet-sp: #d01c1f;
  --carnet-licence: #2563eb;
  --shadow-sm: 0 1px 2px rgb(16 32 42 / 0.06);
  --shadow-lg: 0 24px 64px rgb(16 32 42 / 0.24);
  --backdrop: rgb(10 16 22 / 0.5);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0f1418;
    --surface: #182027;
    --surface-2: #1c252d;
    --text: #e6ebef;
    --muted: #9aa8b4;
    --border: #2b3640;
    --border-strong: #3b4855;
    --accent: #2cc47c;
    --accent-strong: #5ad69a;
    --accent-soft: #123827;
    --accent-text: #06140d;
    --ok-bg: #123827;
    --ok-text: #7fe0ae;
    --bad-bg: #3d1618;
    --bad-text: #ff9c9e;
    --warn-bg: #3a2c0c;
    --warn-text: #ffd27a;
    --neutral-bg: #25303a;
    --neutral-text: #c6d0d8;
    --row-hover: #1d2a24;
    --brand-tile: #26323c;
    --carnet-lv: #fb923c;
    --carnet-mv: #2cc47c;
    --carnet-sp: #ff6b6e;
    --carnet-licence: #60a5fa;
    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.3);
    --shadow-lg: 0 24px 64px rgb(0 0 0 / 0.55);
    --backdrop: rgb(0 0 0 / 0.65);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

/* Sinon « display » des boutons l'emporte sur l'attribut hidden (bouton « Charger plus »). */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans Arabic', 'Noto Sans', Tahoma, sans-serif;
}

/* Une fenêtre de dialogue ouverte : la page derrière ne défile plus. */
body:has(dialog[open]) {
  overflow: hidden;
}

.sprite {
  display: none;
}

.icon {
  flex: none;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Onglets : libellé court sur les écrans étroits. Icônes orientées (sortie) retournées : page de droite à gauche. */
.tab-short {
  display: none;
}
.icon.flip {
  transform: scaleX(-1);
}

.skip-link {
  position: absolute;
  top: 8px;
  inset-inline-start: 8px;
  z-index: 100;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  transform: translateY(-200%);
}
.skip-link:focus {
  transform: none;
}

.flag {
  display: flex;
  height: 4px;
}
.flag span {
  flex: 1;
}
.flag span:nth-child(1) {
  background: var(--accent);
}
.flag span:nth-child(2) {
  background: var(--gold);
}
.flag span:nth-child(3) {
  background: var(--red);
}

h1 {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.3;
}
.subtitle {
  max-width: 75ch;
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

/* Boutons */
button,
.button {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition:
    background-color 0.15s,
    border-color 0.15s,
    color 0.15s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:hover,
.button:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}
button.ghost,
.button.ghost {
  border-color: var(--border-strong);
  background: var(--surface);
  color: var(--text);
}
button.ghost:hover,
.button.ghost:hover {
  border-color: var(--muted);
  background: var(--surface-2);
}
button.danger {
  color: var(--bad-text);
}
button.danger:hover {
  border-color: var(--bad-text);
  background: var(--bad-bg);
}
button.small {
  gap: 6px;
  min-height: 34px;
  padding: 0 12px;
  font-size: 0.85rem;
}
button.small .icon {
  width: 16px;
  height: 16px;
}
button.link {
  min-height: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--ok-text);
  font-size: 0.85rem;
  text-decoration: underline;
  text-underline-offset: 2px;
}
button.link:hover {
  background: none;
  color: var(--text);
}
.icon-button {
  width: 40px;
  padding: 0;
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
button[aria-busy='true'] {
  cursor: progress;
}
button[aria-busy='true'] .icon {
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

button:focus-visible,
.button:focus-visible,
input:focus-visible,
select:focus-visible,
summary:focus-visible,
a:focus-visible,
tr:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Champs */
input,
select {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  padding: 6px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
input:hover,
select:hover {
  border-color: var(--muted);
}
input:focus-visible,
select:focus-visible {
  border-color: var(--accent);
  outline-offset: 0;
}
input::placeholder {
  color: var(--muted);
  opacity: 0.8;
}

/* En-tête : marque, sections, déconnexion */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.app-bar {
  display: flex;
  gap: 16px;
  align-items: stretch;
  max-width: 1400px;
  min-height: 60px;
  margin: 0 auto;
  padding: 0 16px;
}
.brand {
  display: flex;
  flex: none;
  gap: 10px;
  align-items: center;
  color: var(--text);
  text-decoration: none;
}
.brand-mark {
  width: 32px;
  height: 32px;
}
.brand-mark .bm-tile {
  fill: var(--brand-tile);
}
.brand-mark .bm-g {
  fill: #00a95c;
}
.brand-mark .bm-y {
  fill: #fcd116;
}
.brand-mark .bm-r {
  fill: #e5383b;
}
.brand-name {
  display: flex;
  gap: 8px;
  align-items: center;
  font-weight: 800;
  letter-spacing: 0.04em;
}
.brand-tag {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--ok-text);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0;
}
.tabs {
  display: flex;
  gap: 4px;
  margin-inline-start: 12px;
}
.tabs a {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 0 12px;
  border-top: 3px solid transparent;
  border-bottom: 3px solid transparent;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.tabs a:hover {
  color: var(--text);
}
.tabs a[aria-current='page'] {
  border-bottom-color: var(--accent);
  color: var(--text);
}
.tabs a[aria-current='page'] .icon {
  color: var(--accent);
}
.tabs a:focus-visible {
  outline-offset: -4px;
}
.logout {
  display: flex;
  align-items: center;
  margin: 0;
  margin-inline-start: auto;
}

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 20px;
}
.page-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
}

/* Totaux */
.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  padding: 14px 16px;
}
.stat-label {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}
.stat-value {
  font-size: 1.9rem;
  font-weight: 700;
  line-height: 1.1;
}
.stat-note {
  margin-top: auto;
  color: var(--muted);
  font-size: 0.8rem;
}
.dot {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}
.dot.ok {
  background: var(--accent);
}
.dot.bad {
  background: var(--red);
}
/* Jauge : part des reçus, sur un fond de la même teinte, plus clair. */
.meter {
  display: block;
  height: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--neutral-bg);
}
.meter > span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--muted);
  transition: width 0.3s ease-out;
}
.meter.ok {
  background: var(--ok-bg);
}
.meter.ok > span {
  background: var(--accent);
}
.meter.bad {
  background: var(--bad-bg);
}
.meter.bad > span {
  background: var(--red);
}

/* Recherche et filtres */
.filters {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 0 0 16px;
  padding: 12px;
}
.filters-bar {
  display: flex;
  gap: 8px;
}
.search-field {
  position: relative;
  display: flex;
  flex: 1;
  align-items: center;
  min-width: 0;
}
.search-field .icon {
  position: absolute;
  inset-inline-start: 12px;
  color: var(--muted);
  pointer-events: none;
}
.search-field input {
  padding-inline-start: 38px;
}
.filters-toggle {
  display: none;
}
.filters-toggle[aria-expanded='true'] {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.count {
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.75rem;
  font-weight: 700;
}
.filters-more {
  display: grid;
  grid-template-columns: minmax(200px, 1.4fr) repeat(4, minmax(140px, 1fr)) auto;
  gap: 12px;
  align-items: end;
}
.filters-more label,
.field > label,
.field > legend,
.field > .range-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}
.filters-more select {
  text-overflow: ellipsis;
}

/* Liste : résumé, actualisation, tableau */
.list-head {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  align-items: center;
  justify-content: space-between;
  min-height: 1.5em;
  margin: 0 0 10px;
}
.list-head .search-field {
  flex: 0 1 360px;
}
.status {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.status.error {
  color: var(--bad-text);
  font-weight: 600;
}
.live {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}
.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 2.4s ease-out infinite;
}
.live.stale .live-dot {
  background: var(--warn-text);
  animation: none;
}
@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgb(0 169 92 / 0.5);
  }
  70%,
  100% {
    box-shadow: 0 0 0 6px rgb(0 169 92 / 0);
  }
}

.table-wrap {
  overflow-x: auto;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th,
td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  text-align: start;
  vertical-align: middle;
}
th {
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}
thead th:first-child {
  border-start-start-radius: 12px;
}
thead th:last-child {
  border-start-end-radius: 12px;
}
tbody tr:hover {
  background: var(--row-hover);
}
tbody tr:last-child td {
  border-bottom: 0;
}
.clickable tr {
  cursor: pointer;
}
tr.inactive td {
  color: var(--muted);
}
td .sub {
  display: block;
  unicode-bidi: isolate;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.8rem;
}
.strong {
  font-weight: 600;
}
.num {
  text-align: end;
  font-variant-numeric: tabular-nums;
}
.nowrap {
  white-space: nowrap;
}
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}
.muted {
  color: var(--muted);
}
.warn-text {
  color: var(--warn-text);
  font-weight: 600;
}
/* Appareil : affiché seulement sur les grands écrans (il est aussi dans le détail du reçu). */
@media (max-width: 1279px) {
  .col-device,
  .data-table .col-device {
    display: none;
  }
}
.c-device {
  white-space: nowrap;
}
@media (min-width: 1100px) {
  .receipts .c-agent {
    min-width: 170px;
  }
}

/* Badges : état (fond teinté) et carnet (pastille de couleur, texte neutre) */
.badge {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge.ok,
.badge.bad,
.badge.warn {
  padding-inline-start: 8px;
}
.badge.ok::before,
.badge.bad::before,
.badge.warn::before {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  content: '';
}
.badge.ok {
  background: var(--ok-bg);
  color: var(--ok-text);
}
.badge.bad {
  background: var(--bad-bg);
  color: var(--bad-text);
}
.badge.warn {
  background: var(--warn-bg);
  color: var(--warn-text);
}
.badge.neutral {
  background: var(--neutral-bg);
  color: var(--neutral-text);
}
.badges {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.chip {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  padding: 1px 9px;
  padding-inline-start: 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
}
.chip::before {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--chip-color, var(--muted));
  content: '';
}
[data-carnet='LV'] {
  --chip-color: var(--carnet-lv);
}
[data-carnet='MV'] {
  --chip-color: var(--carnet-mv);
}
[data-carnet='SP'] {
  --chip-color: var(--carnet-sp);
}
[data-carnet='LICENCE'] {
  --chip-color: var(--carnet-licence);
}

.row-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  white-space: nowrap;
}

/* Pagination des reçus */
.pagination {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0 0;
}
.pages {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.pages .page-num {
  min-width: 38px;
  padding: 0 8px;
  font-variant-numeric: tabular-nums;
}
.pages .page-num[aria-current='page'] {
  cursor: default;
}
.page-gap {
  padding: 0 2px;
  color: var(--muted);
}
.page-of {
  display: none;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}
.page-size {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
}
.page-size select {
  width: auto;
  min-height: 34px;
}
/* Nouveaux reçus signalés hors de la première page. */
.fresh {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 0;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--ok-bg);
  color: var(--ok-text);
  font-size: 0.85rem;
  font-weight: 600;
}
.list-head {
  scroll-margin-top: 80px;
}

/* Liste vide */
.empty {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  padding: 48px 16px;
  border: 1px dashed var(--border-strong);
  border-radius: 12px;
  background: var(--surface);
  color: var(--muted);
  text-align: center;
}
.empty > .icon {
  width: 40px;
  height: 40px;
  margin-bottom: 4px;
  stroke-width: 1.5;
}
.empty p {
  max-width: 48ch;
  margin: 0;
}
.empty .empty-title {
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 700;
}
.empty button {
  margin-top: 10px;
}

/* Messages de confirmation */
.notice {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 16px;
  padding: 12px;
  padding-inline-start: 16px;
  border: 1px solid color-mix(in srgb, var(--ok-text) 25%, transparent);
  border-radius: 12px;
  background: var(--ok-bg);
  color: var(--ok-text);
  scroll-margin-top: 80px;
}
.notice > .icon {
  margin-top: 2px;
}
.notice-body {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.notice-body p {
  margin: 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}
.notice-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}
.notice .icon-button {
  width: 34px;
}
.credentials {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 4px 16px;
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.credentials dt {
  color: var(--muted);
}
.credentials dd {
  margin: 0;
  font-weight: 700;
  overflow-wrap: anywhere;
  user-select: all;
}
.notice-warning {
  font-size: 0.85rem;
}

/* Aide repliable */
.help-panel {
  margin: 0 0 16px;
  padding: 0;
}
.help-panel summary {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 12px 16px;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  list-style: none;
}
.help-panel summary::-webkit-details-marker {
  display: none;
}
.help-panel summary .icon {
  color: var(--accent);
}
.help-panel summary::after {
  margin-inline-start: auto;
  color: var(--muted);
  content: '+';
  font-size: 1.2rem;
  line-height: 1;
}
.help-panel[open] summary::after {
  content: '−';
}
.help-panel ul {
  margin: 0;
  padding: 0 16px 14px;
  padding-inline-start: 40px;
  color: var(--muted);
}
.help-panel li + li {
  margin-top: 6px;
}

/* Fenêtres de dialogue */
dialog {
  width: min(920px, calc(100vw - 32px));
  max-width: none;
  max-height: calc(100dvh - 48px);
  padding: 0;
  overflow: auto;
  overscroll-behavior: contain;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
dialog.form-dialog {
  width: min(680px, calc(100vw - 32px));
}
dialog::backdrop {
  background: var(--backdrop);
}
dialog[open] {
  animation: dialog-in 0.18s ease-out;
}
@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
}
.dialog-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  padding-inline-start: 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.dialog-head h2 {
  margin: 0;
  font-size: 1.1rem;
  overflow-wrap: anywhere;
}
.dialog-title {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}
.dialog-tools {
  display: flex;
  flex: none;
  gap: 6px;
  align-items: center;
}
.position {
  margin-inline-end: 4px;
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.dialog-body {
  padding: 20px;
}
.dialog-foot {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* Détail d'un reçu */
.detail-body {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 20px;
  align-items: start;
  padding: 20px;
}
.paper {
  justify-self: center;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  box-shadow: var(--shadow-sm);
}
.paper iframe {
  display: block;
  width: 260px;
  height: 640px;
  border: 0;
  background: #ffffff;
}
.detail-info h3 {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}
.detail-info .fields + h3 {
  margin-top: 20px;
}
.fields {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  margin: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.fields dt,
.fields dd {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.fields > :nth-last-child(-n + 2) {
  border-bottom: 0;
}
.fields dt {
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.85rem;
}
.fields dd {
  margin: 0;
  overflow-wrap: anywhere;
  unicode-bidi: plaintext;
}

/* Formulaires (utilisateurs, types de tickets) */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}
.form-grid .wide {
  grid-column: 1 / -1;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}
/* Groupe de cases : un fieldset en flex place mal sa légende, il reste en bloc. */
fieldset.field {
  display: block;
}
fieldset.field > * + * {
  margin-top: 8px;
}
.field > legend {
  width: 100%;
  padding: 0;
}
.field > legend.legend-row {
  flex-direction: row;
  flex-wrap: wrap;
  gap: 4px 12px;
  align-items: baseline;
  justify-content: space-between;
}
.legend-row span:last-child {
  display: flex;
  gap: 12px;
}
.help {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
}
.with-button {
  display: flex;
  gap: 6px;
}
.with-button input {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.range {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 8px;
  align-items: center;
}
.range > span {
  color: var(--muted);
}
.preview {
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.85rem;
}
.preview .mono {
  color: var(--text);
  font-weight: 600;
  overflow-wrap: anywhere;
}
.checks {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.check {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  min-height: 38px;
  padding: 0 12px;
  padding-inline-start: 10px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.check:hover {
  border-color: var(--muted);
}
.check:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.check:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.check input {
  width: 18px;
  height: 18px;
  min-height: 18px;
  margin: 0;
  accent-color: var(--accent);
}
.check .chip-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--chip-color, var(--muted));
}
.callout {
  display: flex;
  gap: 10px;
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--neutral-bg);
  color: var(--neutral-text);
  font-size: 0.85rem;
}
.callout .icon {
  margin-top: 1px;
}
.form-error {
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bad-bg);
  color: var(--bad-text);
  font-weight: 600;
}

/* Connexion */
.login-page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}
.login {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  padding: 32px 16px 12vh;
}
.login-card {
  display: flex;
  flex-direction: column;
  gap: 18px;
  width: min(400px, 100%);
  padding: 28px;
}
.login-card .brand {
  margin-bottom: 4px;
}
.login-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}
.login-card button {
  min-height: 44px;
}
.login-foot {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}
.error {
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bad-bg);
  color: var(--bad-text);
  font-weight: 600;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* Accueil : période */
.period {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  align-items: end;
  margin: 0 0 16px;
  padding: 12px;
}
.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface-2);
}
.segmented button {
  min-height: 36px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: none;
  color: var(--muted);
}
.segmented button:hover {
  border-color: transparent;
  background: var(--surface);
  color: var(--text);
}
.segmented button[aria-pressed='true'] {
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.period-dates {
  display: flex;
  gap: 12px;
}
.period-dates label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 150px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
}
.period-label {
  display: flex;
  flex: 1 1 260px;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  min-height: 40px;
  margin: 0;
  font-weight: 700;
}
.period-label .icon {
  color: var(--accent);
}
.period-label.error {
  color: var(--bad-text);
}
.period-label.error .icon {
  color: inherit;
}

/* En-têtes triables */
th .sort {
  gap: 4px;
  min-height: 0;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
}
th .sort:hover {
  background: none;
  color: var(--text);
}
th .sort::after {
  width: 1em;
  color: var(--border-strong);
  content: '↕';
}
th[aria-sort] {
  color: var(--text);
}
th[aria-sort='descending'] .sort::after {
  color: var(--accent);
  content: '↓';
}
th[aria-sort='ascending'] .sort::after {
  color: var(--accent);
  content: '↑';
}
th.num .sort {
  justify-content: flex-end;
}

/* Barre de montant : part du plus grand montant de la période. */
.bar {
  display: block;
  width: 100%;
  min-width: 90px;
  height: 4px;
  margin-top: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--ok-bg);
}
.bar > span {
  display: block;
  height: 100%;
  margin-inline-start: auto;
  border-radius: inherit;
  background: var(--accent);
}
.c-tickets .sub,
.c-amount .strong {
  white-space: nowrap;
}
.chip-count {
  color: var(--muted);
  font-weight: 600;
}
tr.idle td {
  color: var(--muted);
}
.row-link {
  display: inline-flex;
  gap: 2px;
  align-items: center;
  color: var(--ok-text);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.row-link:hover {
  text-decoration: underline;
}
.row-link .icon {
  width: 16px;
  height: 16px;
}
.live.idle .live-dot {
  background: var(--muted);
  animation: none;
}
.footnote {
  display: flex;
  gap: 10px;
  max-width: 100ch;
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}
.footnote .icon {
  margin-top: 1px;
}

/* Grands tableaux : sous 1100 px, chaque ligne devient une carte (grille de cartes). */
@media (max-width: 1099px) {
  .data-table,
  .data-table tbody {
    display: block;
  }
  .table-wrap.card {
    overflow: visible;
    border: 0;
    background: none;
    box-shadow: none;
  }
  .data-table thead {
    display: none;
  }
  .data-table tbody {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
    gap: 12px;
  }
  .data-table tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px 12px;
    align-content: start;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
  }
  .data-table td {
    display: block;
    min-width: 0;
    padding: 0;
    border: 0;
  }
  .data-table td:empty,
  .data-table td.na {
    display: none;
  }
  .data-table td[data-label]::before {
    color: var(--muted);
    content: attr(data-label) ': ';
  }
  .data-table td.num {
    text-align: start;
  }
  .data-table td .sub.inline {
    display: inline;
    margin: 0;
    margin-inline-start: 6px;
  }
  .clickable tr:hover {
    border-color: var(--accent);
    background: var(--surface);
  }
  .data-table .row-actions {
    justify-content: stretch;
    margin-top: 6px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
  }
  .data-table .c-actions {
    container-type: inline-size;
  }
  .data-table .row-actions button {
    flex: 1 1 auto;
  }

  /* Reçus */
  .receipts tbody {
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
  }
  .receipts tr {
    grid-template-areas:
      'num amount'
      'status type'
      'carrier carrier'
      'dest dest'
      'agent date';
  }
  .receipts .c-num {
    grid-area: num;
    font-weight: 700;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  .receipts .c-amount {
    grid-area: amount;
    font-weight: 700;
    text-align: end;
  }
  .receipts .c-status {
    grid-area: status;
  }
  .receipts .c-type {
    grid-area: type;
    text-align: end;
  }
  .receipts .c-carrier {
    grid-area: carrier;
  }
  .receipts .c-dest {
    grid-area: dest;
  }
  /* Pas de points de suspension : en droite-à-gauche, un nom latin serait coupé par le début. */
  .receipts .c-agent {
    grid-area: agent;
    color: var(--muted);
    font-size: 0.85rem;
  }
  .receipts .c-agent .sub {
    display: none;
  }
  .receipts .c-date {
    grid-area: date;
    color: var(--muted);
    font-size: 0.85rem;
    text-align: end;
  }
  .receipts .c-date .sub {
    display: inline;
    margin-inline-start: 4px;
    font-size: inherit;
  }
  .receipts .c-carrier,
  .receipts .c-dest {
    padding-top: 6px;
    border-top: 1px solid var(--border);
  }
  .receipts .c-dest {
    padding-top: 0;
    border-top: 0;
  }
  .receipts .c-agent,
  .receipts .c-date {
    padding-top: 6px;
  }

  /* Utilisateurs */
  .users tbody {
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
  }
  .users tr {
    grid-template-areas:
      'user status'
      'gare gare'
      'tickets tickets'
      'login receipts'
      'actions actions';
  }
  .users .c-user {
    grid-area: user;
    font-weight: 700;
  }
  .users .c-user .sub {
    font-weight: 400;
  }
  .users .c-status {
    grid-area: status;
  }
  .users .c-gare {
    grid-area: gare;
  }
  .users .c-tickets {
    grid-area: tickets;
    padding: 4px 0;
  }
  .users .c-login {
    grid-area: login;
    font-size: 0.85rem;
    white-space: normal;
  }
  .users td.c-receipts {
    grid-area: receipts;
    font-size: 0.85rem;
    text-align: end;
  }
  .users .c-actions {
    grid-area: actions;
  }

  /* Accueil */
  .summary tr {
    grid-template-areas:
      'user amount'
      'gare tickets'
      'detail detail'
      'last link';
  }
  .summary .c-user {
    grid-area: user;
    font-weight: 700;
  }
  .summary .c-user .sub {
    font-weight: 400;
  }
  .summary td.c-amount {
    grid-area: amount;
    min-width: 110px;
    text-align: end;
  }
  .summary .c-gare {
    grid-area: gare;
  }
  .summary td.c-tickets {
    grid-area: tickets;
    text-align: end;
  }
  .summary .c-detail {
    grid-area: detail;
  }
  .summary .c-last {
    grid-area: last;
    align-self: center;
    font-size: 0.85rem;
  }
  .summary .c-link {
    grid-area: link;
    text-align: end;
  }
  .summary .c-detail {
    margin-top: 4px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
  }
  .summary .c-last,
  .summary .c-link {
    padding-top: 6px;
  }
  /* « Dernier ticket : — » gardé en carte : la ligne du lien reste alignée. */
  .summary .c-last.na {
    display: block;
  }

  /* Types de tickets */
  .types tr {
    grid-template-areas:
      'label amount'
      'carnet carnet'
      'range range'
      'dest users'
      'actions actions';
  }
  .types .c-label {
    grid-area: label;
  }
  .types .c-amount {
    grid-area: amount;
    font-weight: 700;
    text-align: end;
  }
  .types .c-carnet {
    grid-area: carnet;
  }
  .types .c-range {
    grid-area: range;
  }
  .types .c-dest {
    grid-area: dest;
    font-size: 0.85rem;
  }
  .types .c-users {
    grid-area: users;
    font-size: 0.85rem;
    text-align: end;
  }
  .types .c-actions {
    grid-area: actions;
  }
}

/* Carte étroite : boutons d'action sans icône pour tenir sur une ligne. */
@container (max-width: 360px) {
  .row-actions button {
    padding: 0 8px;
  }
  .row-actions .icon {
    display: none;
  }
}

@media (max-width: 1023px) {
  .tab-long {
    display: none;
  }
  .tab-short {
    display: inline;
  }
  .filters-more {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 899px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* Tablettes et téléphones : marque et déconnexion en haut, sections sur toute la largeur en dessous. */
@media (max-width: 899px) {
  .app-header {
    position: static;
  }
  .app-bar {
    flex-wrap: wrap;
    gap: 0;
    min-height: 0;
    padding: 0;
    padding-inline: 12px 8px;
  }
  .brand {
    min-height: 52px;
  }
  .tabs {
    order: 3;
    width: calc(100% + 20px);
    margin: 0;
    margin-inline: -12px -8px;
    border-top: 1px solid var(--border);
  }
  .tabs a {
    flex: 1 1 0;
    justify-content: center;
    min-width: 0;
    padding: 10px 4px;
  }
  .logout-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }
  .logout button {
    width: 44px;
    padding: 0;
    border-color: transparent;
  }
}

/* Téléphones */
@media (max-width: 719px) {
  .brand-mark {
    width: 28px;
    height: 28px;
  }
  .tabs a {
    flex-direction: column;
    gap: 2px;
    padding: 8px 4px 6px;
    font-size: 0.8rem;
  }

  main {
    padding: 16px 12px 40px;
  }
  h1 {
    font-size: 1.3rem;
  }
  .page-head {
    margin-bottom: 16px;
  }
  .page-actions {
    width: 100%;
  }
  .page-actions > * {
    flex: 1 1 0;
    padding: 0 10px;
  }

  .stats {
    gap: 8px;
  }
  .stat {
    padding: 12px;
  }
  .stat-value {
    font-size: 1.5rem;
  }

  .period {
    align-items: stretch;
  }
  /* Cinq boutons sur deux lignes : jours (اليوم، أمس), puis périodes longues (الشهر، السنة، الكل). */
  .segmented {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 3px;
    width: 100%;
  }
  .segmented button {
    grid-column: span 2;
    min-width: 0;
    padding: 0 6px;
  }
  .segmented button:nth-child(-n + 2) {
    grid-column: span 3;
  }
  .period-dates {
    width: 100%;
  }
  .period-dates label {
    flex: 1;
    min-width: 0;
  }
  .period-label {
    flex-basis: 100%;
    justify-content: flex-start;
    min-height: 0;
  }

  .filters-toggle {
    display: inline-flex;
  }
  .filters:not(.open) .filters-more {
    display: none;
  }
  .filters-more {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .filters-more .user,
  .filters-more .reset {
    grid-column: 1 / -1;
  }
  .list-head .search-field {
    flex: 1 1 100%;
  }

  /* Pagination : précédente / « الصفحة n من m » / suivante, sur toute la largeur. */
  .pagination {
    flex-direction: column;
    align-items: stretch;
  }
  .pages {
    flex-wrap: nowrap;
    justify-content: space-between;
  }
  .pages .page-num,
  .page-gap {
    display: none;
  }
  .page-of {
    display: inline;
  }
  .page-size {
    justify-content: center;
  }

  .notice {
    flex-wrap: wrap;
  }
  .notice-actions {
    width: 100%;
  }
  .notice-actions #notice-copy {
    flex: 1;
  }

  /* Dialogues en feuille, depuis le bas de l'écran */
  dialog,
  dialog.form-dialog {
    width: 100%;
    max-height: calc(100dvh - 16px);
    margin: auto 0 0;
    border-width: 1px 0 0;
    border-radius: 16px 16px 0 0;
  }
  dialog[open] {
    animation-name: sheet-in;
  }
  .dialog-head {
    padding: 10px;
    padding-inline: 16px 8px;
  }
  .dialog-body,
  .detail-body {
    padding: 16px;
  }
  .dialog-foot {
    padding: 12px 16px max(12px, env(safe-area-inset-bottom));
  }
  .dialog-foot > * {
    flex: 1;
  }
  .detail-body {
    grid-template-columns: minmax(0, 1fr);
  }
  .paper iframe {
    width: min(280px, calc(100vw - 74px));
    height: 560px;
  }
  .position {
    display: none;
  }
  .dialog-head h2 {
    font-size: 1rem;
  }
  .title-prefix {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }
  .form-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .login-card {
    padding: 22px;
  }

  /* Évite le zoom automatique d'iOS sur les champs. */
  input,
  select {
    font-size: 16px;
  }
}

/* Très petits écrans : champs date l'un sous l'autre (sinon la date est coupée). */
@media (max-width: 380px) {
  .tabs a {
    font-size: 0.72rem;
  }
  .period-dates {
    flex-direction: column;
  }
  .filters-more {
    grid-template-columns: minmax(0, 1fr);
  }
}

@keyframes sheet-in {
  from {
    transform: translateY(40px);
    opacity: 0;
  }
}

/* Écrans tactiles : cibles d'au moins 44 px. */
@media (pointer: coarse) {
  button,
  .button,
  input,
  select {
    min-height: 44px;
  }
  button.small {
    min-height: 40px;
  }
  .icon-button {
    width: 44px;
  }
  button.link {
    min-height: 0;
  }
}

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

@media print {
  .app-header,
  .page-actions,
  .filters,
  .pagination,
  .live {
    display: none;
  }
}
