/* D-057 / point 1 only. Load after the existing stylesheets.
   The controller restores moved nodes and hidden states outside mobile-terrain.
   All dimensions below are interface dimensions, never planning rules. */
@media (max-width: 880px) {
  body:is(.mobile-terrain, .mobile-journey) {
    --mobile-safe-top: env(safe-area-inset-top, 0px);
    --mobile-safe-right: env(safe-area-inset-right, 0px);
    --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
    --mobile-safe-left: env(safe-area-inset-left, 0px);
    --mobile-step-height: 44px;
    --mobile-search-height: 48px;
    --mobile-footer-height: calc(64px + var(--mobile-safe-bottom));
    --mobile-invite-height: 168px;
    margin: 0;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  body:is(.mobile-terrain, .mobile-journey) #root {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100vh;
    height: 100dvh;
    min-width: 0;
    overflow: hidden;
  }

  /* Intrinsic header height also accommodates the local-only town selector. */
  body:is(.mobile-terrain, .mobile-journey) .topbar {
    grid-row: 1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    grid-template-rows: minmax(44px, auto) auto;
    align-items: center;
    gap: 2px 8px;
    height: auto;
    min-height: calc(60px + var(--mobile-safe-top));
    padding: calc(4px + var(--mobile-safe-top))
      max(12px, var(--mobile-safe-right)) 4px
      max(12px, var(--mobile-safe-left));
    z-index: 1200;
  }

  body:is(.mobile-terrain, .mobile-journey) .topbar .brand {
    grid-column: 1;
    grid-row: 1;
    min-width: 0;
    gap: 8px;
  }

  body:is(.mobile-terrain, .mobile-journey) .brand h1 {
    min-width: 0;
    font-size: 17px;
    line-height: 1.15;
  }

  body:is(.mobile-terrain, .mobile-journey) .brand small {
    display: block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 10px;
    line-height: 1.25;
  }

  body:is(.mobile-terrain, .mobile-journey) .topbar .live-badge {
    grid-column: 1 / -1;
    grid-row: 2;
    order: initial;
    width: auto;
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 10px;
    line-height: 1.2;
  }

  body:is(.mobile-terrain, .mobile-journey) .topbar .local-territory-switch {
    grid-column: 2;
    grid-row: 1;
    order: initial;
    width: 92px;
    min-width: 0;
    max-width: 112px;
    height: 44px;
    min-height: 44px;
    padding: 5px 6px;
    font-size: 12px;
  }

  body:is(.mobile-terrain, .mobile-journey) #source-toggle {
    grid-column: 3;
    grid-row: 1;
    min-height: 44px;
    padding: 8px 10px;
    font-size: 13px;
  }

  body:is(.mobile-terrain, .mobile-journey) .topbar .spacer,
  body:is(.mobile-terrain, .mobile-journey) #map-visibility-toggle {
    display: none;
  }

  body.mobile-terrain #app,
  body.mobile-terrain #app.map-big,
  body.mobile-terrain #app.map-hidden {
    grid-row: 2;
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--mobile-step-height) minmax(0, 1fr);
    min-height: 0;
    height: auto;
    overflow: hidden;
  }

  body.mobile-terrain #main-panel {
    display: contents;
  }

  body:is(.mobile-terrain, .mobile-journey) #main-panel > .steps {
    grid-column: 1;
    grid-row: 1;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    min-height: var(--mobile-step-height);
    margin: 0;
    padding: 0 max(8px, var(--mobile-safe-right)) 0
      max(8px, var(--mobile-safe-left));
    border-top: 0;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
    z-index: 1100;
  }

  body:is(.mobile-terrain, .mobile-journey) .steps li {
    min-width: 0;
    width: auto;
  }

  body:is(.mobile-terrain, .mobile-journey) .steps button {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    width: 100%;
    gap: 5px;
    padding: 6px 2px;
  }

  body:is(.mobile-terrain, .mobile-journey) .steps button .l,
  body:is(.mobile-terrain, .mobile-journey) .steps button:not(.is-current) .l {
    display: inline;
    min-width: 0;
    font-size: 11px;
    line-height: 1.2;
  }

  body:is(.mobile-terrain, .mobile-journey) .steps .n {
    flex: 0 0 auto;
    min-width: 18px;
    width: auto;
    height: 18px;
    font-size: 11px;
  }

  body:is(.mobile-terrain, .mobile-journey) .steps .n::before {
    content: none;
  }

  /* Only the terrain panel is replaced. Project/result styles are untouched. */
  body.mobile-terrain #main-panel > #panel {
    display: none;
  }

  body.mobile-terrain #mapwrap {
    grid-column: 1;
    grid-row: 2;
    position: relative;
    display: block;
    order: initial;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    border: 0;
  }

  body.mobile-terrain #map {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
  }

  /* The real .field, including suggestions and dynamic error text, moves here. */
  body.mobile-terrain #mobile-search-slot {
    position: absolute;
    top: 12px;
    left: max(12px, var(--mobile-safe-left));
    right: calc(max(12px, var(--mobile-safe-right)) + 60px);
    min-width: 0;
    z-index: 1150;
  }

  body.mobile-terrain #mobile-search-slot > .field {
    display: grid;
    gap: 0;
    margin: 0;
    min-width: 0;
  }

  body.mobile-terrain #mobile-search-slot label[for="search"] {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  body.mobile-terrain #mobile-search-slot .search {
    position: relative;
    min-width: 0;
  }

  body.mobile-terrain #mobile-search-slot #search {
    display: block;
    width: 100%;
    min-width: 0;
    height: 48px;
    min-height: 48px;
    padding: 0 14px 0 40px;
    border: 1px solid var(--line-2);
    border-radius: 24px;
    background: var(--card);
    color: var(--ink);
    font-size: 16px;
    line-height: 1.25;
    box-shadow: 0 2px 10px rgb(15 26 23 / 14%);
  }

  body.mobile-terrain #mobile-search-slot .suggest {
    z-index: 1;
    max-height: 42vh;
    max-height: min(42dvh, 360px);
    overscroll-behavior: contain;
    border-radius: 12px;
    box-shadow: 0 4px 16px rgb(15 26 23 / 18%);
  }

  body.mobile-terrain #mobile-search-slot .suggest [role="option"] {
    min-height: 48px;
    overflow-wrap: anywhere;
  }

  body.mobile-terrain #search-status {
    max-height: 3.9em;
    margin: 4px 4px 0;
    padding: 4px 8px;
    overflow: auto;
    border-radius: 8px;
    background: rgb(255 255 255 / 95%);
    color: var(--ink-2);
    font-size: 12px;
    line-height: 1.3;
  }

  body.mobile-terrain #search-status:empty {
    display: none;
  }

  body.mobile-terrain #mobile-search-slot > #global-error {
    max-height: 25vh;
    max-height: min(25dvh, 160px);
    margin: 8px 0 0;
    overflow: auto;
    overscroll-behavior: contain;
    font-size: 12px;
    line-height: 1.4;
  }

  body.mobile-map-active #mobile-layer-toggle,
  body.mobile-map-active #mobile-legend-toggle {
    position: absolute;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    gap: 7px;
    border: 1px solid var(--line-2);
    border-radius: 24px;
    background: var(--card);
    color: var(--ink);
    box-shadow: 0 2px 10px rgb(15 26 23 / 14%);
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    z-index: 1100;
  }

  body.mobile-map-active #mobile-layer-toggle {
    top: 12px;
    right: max(12px, var(--mobile-safe-right));
    width: 48px;
    height: 48px;
    padding: 0;
  }

  body.mobile-map-active #mobile-legend-toggle {
    left: max(12px, var(--mobile-safe-left));
    bottom: calc(var(--mobile-invite-height) + var(--mobile-footer-height) + 20px);
    max-width: calc(100% - 88px);
    padding: 7px 14px;
  }

  body.mobile-map-active #mobile-layer-toggle svg,
  body.mobile-map-active #mobile-legend-toggle svg {
    flex: none;
  }

  body.mobile-map-active #mapwrap .leaflet-top.leaflet-left {
    top: auto;
    left: auto;
    right: max(12px, var(--mobile-safe-right));
    bottom: calc(var(--mobile-invite-height) + var(--mobile-footer-height) + 20px);
  }

  body.mobile-map-active #mapwrap .leaflet-control-zoom {
    margin: 0;
    background: var(--card);
    box-shadow: 0 2px 10px rgb(15 26 23 / 14%);
  }

  body.mobile-map-active.mobile-search-focused #mapwrap .leaflet-top.leaflet-left {
    top: calc(var(--mobile-search-height) + 24px);
    bottom: auto;
  }

  body.mobile-map-active #mapwrap .leaflet-control-zoom a {
    width: 44px;
    height: 44px;
    line-height: 44px;
  }

  body.mobile-map-active #mapwrap .leaflet-bottom.leaflet-right {
    right: max(4px, var(--mobile-safe-right));
    bottom: calc(var(--mobile-invite-height) + var(--mobile-footer-height) + 2px);
  }

  body.mobile-map-active #mapwrap .leaflet-control-attribution {
    margin: 0;
    max-width: calc(100vw - 24px);
    font-size: 9px;
    line-height: 1.3;
  }

  body.mobile-map-active #map-status {
    left: max(12px, var(--mobile-safe-left));
    right: auto;
    bottom: calc(var(--mobile-invite-height) + var(--mobile-footer-height) + 64px);
    max-width: calc(100% - 88px);
    padding: 3px 7px;
    font-size: 11px;
    line-height: 1.3;
    pointer-events: none;
    z-index: 1050;
  }

  body.mobile-terrain #mobile-map-invite {
    position: absolute;
    inset-inline: 0;
    bottom: var(--mobile-footer-height);
    display: grid;
    align-content: start;
    gap: 8px;
    max-height: min(32dvh, 230px);
    margin: 0;
    padding: 14px max(16px, var(--mobile-safe-right))
      calc(16px + var(--mobile-safe-bottom))
      max(16px, var(--mobile-safe-left));
    overflow: auto;
    overscroll-behavior: contain;
    border: 0;
    border-radius: 20px 20px 0 0;
    background: var(--panel);
    color: var(--ink);
    box-shadow: 0 -4px 20px rgb(15 26 23 / 14%);
    z-index: 1120;
  }

  body.mobile-terrain #mobile-map-invite::before {
    content: "";
    justify-self: center;
    width: 40px;
    height: 5px;
    margin-bottom: 2px;
    border-radius: 3px;
    background: var(--line);
  }

  body.mobile-terrain #mobile-map-invite h2,
  body.mobile-terrain #mobile-map-invite h3 {
    margin: 0;
    font-family: var(--font-display);
    font-size: 22px;
    line-height: 1.15;
  }

  body.mobile-terrain #mobile-map-invite p {
    margin: 0;
    font-size: 14px;
    line-height: 1.4;
  }

  body.mobile-terrain #main-panel > .panel-foot {
    position: absolute;
    inset-inline: 0;
    bottom: 0;
    display: block;
    min-height: calc(64px + var(--mobile-safe-bottom));
    padding: 8px max(16px, var(--mobile-safe-right))
      calc(8px + var(--mobile-safe-bottom)) max(16px, var(--mobile-safe-left));
    border: 0;
    background: var(--panel);
    z-index: 1130;
  }

  body.mobile-terrain:not(.mobile-has-selection) #main-panel > .panel-foot {
    display: none;
  }

  body.mobile-terrain.mobile-has-selection #mobile-map-invite,
  body.mobile-terrain.mobile-search-focused #mobile-map-invite {
    display: none;
  }

  body.mobile-terrain .panel-foot .actions {
    display: block;
  }

  body.mobile-terrain #footer-status {
    display: none;
  }

  body.mobile-terrain .panel-foot #next-step {
    display: flex;
    justify-content: center;
    width: 100%;
    min-height: 48px;
    padding: 10px 14px;
    border-radius: 10px;
    font-size: 16px;
    line-height: 1.3;
  }

  /* The modal uses the original controls: no duplicate inputs or fake filters. */
  body.mobile-map-active #mobile-layers-dialog {
    position: fixed;
    inset: auto 0 0;
    width: 100%;
    max-width: none;
    max-height: 84vh;
    max-height: min(84dvh, calc(100dvh - var(--mobile-safe-top) - 12px));
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 20px 20px 0 0;
    background: var(--panel);
    color: var(--ink);
    box-shadow: 0 -6px 24px rgb(15 26 23 / 20%);
    overflow: hidden;
  }

  body.mobile-map-active #mobile-layers-dialog:not([open]) {
    display: none;
  }

  body.mobile-map-active #mobile-layers-dialog[open] {
    display: flex;
    flex-direction: column;
  }

  body.mobile-map-active #mobile-layers-dialog::backdrop {
    background: rgb(15 26 23 / 38%);
  }

  body.mobile-map-active #mobile-layers-dialog .dialog-top {
    flex: none;
    align-items: center;
    gap: 12px;
    padding: 16px max(16px, var(--mobile-safe-right)) 12px
      max(16px, var(--mobile-safe-left));
  }

  body.mobile-map-active #mobile-layers-dialog .dialog-top h2 {
    font-size: 22px;
    line-height: 1.2;
  }

  body.mobile-map-active #mobile-layers-dialog .dialog-top .btn {
    flex: none;
    min-width: 44px;
    min-height: 44px;
    padding: 8px 12px;
    border-radius: 22px;
  }

  body.mobile-map-active #mobile-map-controls-slot {
    display: grid;
    align-content: start;
    flex: 1 1 auto;
    min-height: 0;
    gap: 18px;
    padding: 4px max(16px, var(--mobile-safe-right))
      calc(20px + var(--mobile-safe-bottom)) max(16px, var(--mobile-safe-left));
    overflow: auto;
    overscroll-behavior: contain;
  }

  body.mobile-map-active #mobile-map-controls-slot > .map-tools {
    position: static;
    inset: auto;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    width: 100%;
    margin: 0;
    gap: 12px;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-base-switch {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    overflow: hidden;
    border: 1px solid var(--ink);
    border-radius: 10px;
    box-shadow: none;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-base-switch button,
  body.mobile-map-active #mobile-map-controls-slot .map-tools > .btn {
    min-height: 44px;
    padding: 10px 12px;
    font-size: 15px;
    line-height: 1.3;
  }

  body.mobile-map-active #mobile-map-controls-slot #map-toggle {
    display: none;
  }

  body.mobile-map-active #mobile-map-controls-slot > .map-coverage {
    position: static;
    inset: auto;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    width: 100%;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0;
    border: 0;
    overflow: visible;
    gap: 12px;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-coverage-title {
    display: block;
    margin: 0;
    font-size: 13px;
  }

  body.mobile-map-active #mobile-map-controls-slot .zone-filters {
    grid-column: 1;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  body.mobile-map-active #mobile-map-controls-slot .zone-filters button {
    justify-content: start;
    min-height: 48px;
    padding: 8px 10px;
    border-radius: 10px;
    text-align: left;
    font-size: 14px;
  }

  body.mobile-map-active #mobile-map-controls-slot .zone-filters button[data-zone-filter="all"] {
    grid-column: 1 / -1;
  }

  body.mobile-map-active #mobile-map-controls-slot #map-coverage-key,
  body.mobile-map-active #mobile-map-controls-slot #map-scope-key,
  body.mobile-map-active #mobile-map-controls-slot #map-zone-feedback,
  body.mobile-map-active #mobile-map-controls-slot .plu-zones-key {
    grid-column: 1;
    margin: 0;
    font-size: 12px;
    line-height: 1.4;
  }

  body.mobile-map-active #mobile-map-controls-slot #map-coverage-key .legend-coverage-row:last-child {
    display: flex;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-legend {
    position: static;
    grid-column: 1;
    width: auto;
    max-width: none;
    margin: 0;
    padding-top: 10px;
    border: 0;
    border-top: 1px solid var(--line);
    font-size: 14px;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-legend summary {
    min-height: 44px;
    padding: 8px 0;
    font-size: 13px;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-legend ul {
    display: grid;
    gap: 0;
    margin: 0;
    padding: 0;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-legend li {
    min-height: 44px;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-legend label {
    display: flex;
    flex: 1;
    min-height: 44px;
    gap: 8px;
    padding: 7px 0;
    font-size: 16px;
    line-height: 1.3;
  }

  body.mobile-map-active #mobile-map-controls-slot .map-legend input {
    flex: none;
    width: 22px;
    height: 22px;
  }

  body.mobile-map-active #mobile-map-controls-slot .help {
    font-size: 12px;
    line-height: 1.4;
  }
}

/* D-057 / point 4 : formulaires tactiles, presentation commune uniquement.
   Champs a 16px, cibles a 44px, unites dans leur enveloppe visuelle.
   Les valeurs, leurs sources et les reponses declarees restent gerees par controls.js.
   Les deux blocs ajoutes restent limites aux largeurs de 880px au plus. */
@media (max-width: 880px) {
  /* Inclut les sélecteurs de provenance et la correction, pas seulement
     les mesures du formulaire principal. N'ajoute aucun inputmode. */
  body :is(#root, #correction-dialog, #work-type-dialog, #source-dialog,
      #document-dialog, #mobile-layers-dialog, #mobile-map-dialog)
    :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea) {
    min-height: 44px;
    font-size: 16px;
  }

  body :is(#root, #correction-dialog, #work-type-dialog, #source-dialog,
      #document-dialog, #mobile-layers-dialog, #mobile-map-dialog)
    :is(button, a.btn, summary) {
    min-width: 44px;
    min-height: 44px;
  }

  /* Les deux colonnes forment un unique champ visuel. Le suffixe reste
     du vrai texte DOM ; aucune unité n'est ajoutée à la valeur saisie. */
  body :is(#controls-form, #correction-dialog) .fact-number {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 0;
    min-width: 0;
    min-height: 47px;
    border: 1.5px solid var(--line-2);
    background: var(--card, #fff);
  }
  body :is(#controls-form, #correction-dialog) .fact-number > .input {
    min-width: 0;
    width: 100%;
    border: 0;
    background: transparent;
    padding-inline: 12px;
    font-size: 16px;
  }
  body :is(#controls-form, #correction-dialog) .fact-number > .input:focus {
    outline: 0;
  }
  body :is(#controls-form, #correction-dialog) .fact-number:focus-within {
    border-color: var(--ink);
    outline: 2px solid var(--ink);
    outline-offset: 0;
  }
  body :is(#controls-form, #correction-dialog)
    .fact-number:has(> .input[aria-invalid="true"]) {
    border-color: var(--coral);
  }
  body :is(#controls-form, #correction-dialog)
    .fact-number:has(> .input:disabled) {
    background: var(--soft);
  }
  body :is(#controls-form, #correction-dialog) .fact-number > span {
    min-width: 0;
    max-width: 8em;
    padding-inline: 8px 12px;
    font-size: 16px;
    line-height: 1.25;
    color: var(--ink-2);
    overflow-wrap: anywhere;
  }
  body :is(#controls-form, #correction-dialog) .fact-number > span:empty {
    display: none;
  }
}

@media (max-width: 880px) {
  body > .multizone-project-dialog :is(button, a.btn, summary) {
    min-width: 44px;
    min-height: 44px;
  }

  /* La petite carte de ce dialogue a ses propres commandes Leaflet.
     Elles ne descendent pas de #mapwrap et sont des liens sans .btn. */
  body > .multizone-project-dialog .leaflet-control-zoom a {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    line-height: 44px;
  }
}
