/* Mobile planning controls are scoped below the desktop breakpoint. */
.mobile-more {
  display: none;
}

@media (max-width: 900px) {
  html,
  body,
  #app {
    height: 100dvh;
  }

  .topbar {
    min-height: calc(62px + env(safe-area-inset-top));
  }

  .search-box input {
    min-height: 44px;
    font-size: 16px;
  }

  .search-box .icon-button,
  .top-actions #account-button,
  .panel-tab,
  .dialog-close,
  .feature-dialog .dialog-actions button,
  .segmented-control button,
  .panel-view .text-button {
    min-width: 44px;
    min-height: 44px;
  }

  .leaflet-control-zoom a {
    width: 44px;
    height: 44px;
    line-height: 44px;
  }

  .workspace .tool-rail {
    left: 8px;
    right: 8px;
    bottom: calc(8px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 2px;
    width: auto;
    height: auto;
    max-height: min(55dvh, 380px);
    padding: 4px;
    overflow: visible;
    border-radius: 10px;
  }

  .tool-rail .tool-button,
  .tool-rail .mobile-more {
    min-width: 0;
    min-height: 52px;
    width: 100%;
    padding: 3px 1px;
    font-size: 9px;
    line-height: 1.1;
    text-align: center;
    white-space: normal;
    touch-action: manipulation;
  }

  .tool-rail .mobile-more {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .tool-rail .tool-icon {
    font-size: 19px;
  }

  .tool-rail:not(.is-expanded) .tool-button[data-tool="line"],
  .tool-rail:not(.is-expanded) .tool-button[data-tool="edit"],
  .tool-rail:not(.is-expanded) .tool-button[data-tool="move"],
  .tool-rail:not(.is-expanded) .tool-button[data-tool="delete"],
  .tool-rail:not(.is-expanded) .tool-button[data-tool="locate"] {
    display: none;
  }

  .tool-rail.is-expanded {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    padding: 6px;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .tool-rail .mobile-more[aria-expanded="true"] {
    background: var(--forest-800);
    color: white;
  }

  body:has(.tool-rail.is-expanded) .ap-foundation-launchers,
  body:has(#selection-actions:not([hidden])) .ap-foundation-launchers {
    display: none !important;
  }

  body:has(.tool-rail.is-expanded) #selection-actions:not([hidden]) {
    display: none !important;
  }

  .leaflet-control-attribution {
    max-width: min(70vw, 420px) !important;
  }

  body:has(.tool-rail.is-expanded) .leaflet-control-attribution {
    bottom: calc(205px + env(safe-area-inset-bottom));
  }

  body:has(#selection-actions:not([hidden])):not(:has(.tool-rail.is-expanded)) .leaflet-control-attribution {
    bottom: calc(238px + env(safe-area-inset-bottom));
  }

  .workspace .side-panel {
    top: auto;
    bottom: calc(76px + env(safe-area-inset-bottom));
    height: min(64dvh, 560px);
    max-height: calc(100% - 90px - env(safe-area-inset-bottom));
    border-radius: 12px 12px 8px 8px;
  }

  .workspace .panel-toggle {
    min-height: 44px;
  }

  .panel-handle,
  .panel-tab {
    min-height: 44px;
  }

  .workspace .mode-actions {
    left: 8px;
    right: 8px;
    bottom: calc(76px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px;
    max-width: none;
    padding: 8px;
  }

  .workspace.panel-open .mode-actions {
    bottom: calc(min(64dvh, 560px) + 84px + env(safe-area-inset-bottom));
  }

  body:has(.mode-actions:not([hidden])) .workspace.panel-open .side-panel {
    height: min(44dvh, 400px);
  }

  body:has(.mode-actions:not([hidden])) .workspace.panel-open .mode-actions {
    bottom: calc(min(44dvh, 400px) + 84px + env(safe-area-inset-bottom));
  }

  .mode-copy {
    align-self: center;
  }

  .mode-copy span {
    max-width: none;
    white-space: normal;
    overflow: visible;
  }

  .mode-measurement {
    min-width: 0;
    max-width: 42vw;
    white-space: normal;
  }

  .mode-actions button {
    min-width: 44px;
    min-height: 44px;
  }

  .mode-actions .finish-mode {
    grid-column: 1;
  }

  .mode-actions .cancel-mode {
    grid-column: 2;
  }

  .mode-actions .finish-mode[hidden] + .cancel-mode {
    grid-column: 1 / -1;
  }

  .workspace #selection-actions {
    position: absolute;
    z-index: 790;
    left: 8px;
    right: 8px;
    bottom: calc(76px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    align-items: stretch;
    gap: 6px;
    max-width: none;
    padding: 8px;
    overflow: visible;
    transform: none;
    white-space: normal;
  }

  #selection-actions strong {
    grid-column: 1 / -1;
    min-width: 0;
    padding: 0 4px 2px;
    font-size: 12px;
    line-height: 1.3;
    overflow-wrap: anywhere;
  }

  #selection-actions button {
    min-width: 0;
    min-height: 50px;
    width: 100%;
    padding: 5px 2px;
    font-size: 11px;
    line-height: 1.15;
    white-space: normal;
  }

  #selection-actions button.is-active {
    border-color: var(--lime);
    background: rgba(168, 207, 69, 0.18);
  }

  #selection-actions .danger-action {
    grid-column: 4;
    margin-left: 0;
    border-color: rgba(255, 187, 181, 0.4);
  }

  body:has(.mode-actions:not([hidden])) #selection-actions:not([hidden]) {
    bottom: calc(184px + env(safe-area-inset-bottom));
  }

  body:has(.mode-actions:not([hidden])):has(#selection-actions:not([hidden])) .leaflet-control-attribution {
    bottom: calc(346px + env(safe-area-inset-bottom));
  }

  body:has(.workspace.panel-open) .ap-foundation-launchers {
    display: none !important;
  }

  .ap-project-context {
    max-width: calc(100% - 110px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .panel-view .plan-item {
    grid-template-columns: 10px minmax(0, 1fr);
    gap: 2px 10px;
  }

  .panel-view .plan-main button {
    min-height: 44px;
    font-size: 12px;
  }

  .panel-view .plan-actions {
    grid-column: 2;
    justify-content: flex-end;
    gap: 4px;
  }

  .panel-view .plan-action {
    min-width: 44px;
    height: 44px;
    padding: 0 9px;
    font-size: 16px;
  }

  .panel-view .plan-action.danger {
    margin-left: 8px;
  }

  .feature-dialog {
    max-height: calc(100dvh - 16px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow-y: auto;
  }

  .feature-dialog form {
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }

  #fence-planner-dialog {
    position: fixed;
    inset: auto 0 0;
    width: 100%;
    max-width: none;
    max-height: calc(100dvh - 12px - env(safe-area-inset-top));
    margin: 0;
    padding: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    border: 0;
    border-radius: 14px 14px 0 0;
  }

  #fence-planner-dialog form {
    padding: 16px 16px calc(20px + env(safe-area-inset-bottom));
  }

  #fence-planner-dialog :is(button, select, input:not([type="radio"]):not([type="checkbox"]):not([type="hidden"])) {
    min-height: 44px;
  }

  #fence-planner-dialog :is([data-fence-shape], .fence-shape-option) {
    min-height: 64px;
  }

  #map .leaflet-marker-pane .marker-icon {
    width: 44px !important;
    height: 44px !important;
    margin: -22px 0 0 -22px !important;
    border: 0;
    background: transparent;
    touch-action: none;
  }

  #map .leaflet-marker-pane .marker-icon::before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 14px;
    height: 14px;
    transform: translate(-50%, -50%);
    border: 2px solid #297854;
    border-radius: 50%;
    background: white;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.55);
    pointer-events: none;
  }

  #map .leaflet-marker-icon.acreplan-plan-marker {
    width: 44px !important;
    height: 44px !important;
    margin-left: -22px !important;
    margin-top: -44px !important;
    object-fit: contain;
    touch-action: none;
  }

  #map .leaflet-marker-pane .marker-icon-middle::before {
    width: 10px;
    height: 10px;
    border-color: #6a8e73;
  }

  #map .is-selected.leaflet-interactive,
  #map .is-selected .leaflet-interactive {
    filter: drop-shadow(0 0 4px rgba(255, 245, 142, 0.95));
  }

  #map .is-selected.leaflet-marker-icon {
    filter: drop-shadow(0 0 5px rgba(255, 245, 142, 0.95));
  }

  .ap-foundation-launchers button:focus-visible,
  .tool-button:focus-visible,
  .mobile-more:focus-visible,
  #selection-actions button:focus-visible {
    outline: 3px solid #a8cf45;
    outline-offset: 2px;
  }
}

@media (max-width: 520px) {
  .topbar {
    grid-template-columns: 44px minmax(0, 1fr) 44px;
  }

  .summary-grid strong {
    overflow-wrap: anywhere;
  }

  #fence-planner-dialog :is(.fence-shape-grid, .fence-presets) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 900px) and (orientation: landscape) and (max-height: 540px) {
  .workspace .side-panel {
    bottom: calc(66px + env(safe-area-inset-bottom));
    height: min(58dvh, 300px);
  }

  .workspace .tool-rail {
    bottom: calc(4px + env(safe-area-inset-bottom));
  }

  .workspace .tool-rail.is-expanded {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  body:has(.tool-rail.is-expanded) .leaflet-control-attribution {
    bottom: calc(137px + env(safe-area-inset-bottom));
  }

  .workspace .mode-actions,
  .workspace #selection-actions {
    bottom: calc(66px + env(safe-area-inset-bottom));
  }

  .workspace #selection-actions {
    bottom: calc(74px + env(safe-area-inset-bottom));
  }

  .workspace.panel-open .mode-actions {
    bottom: calc(min(58dvh, 300px) + 72px + env(safe-area-inset-bottom));
  }

  body:has(.mode-actions:not([hidden])) .workspace.panel-open .side-panel {
    height: min(36dvh, 180px);
  }

  body:has(.mode-actions:not([hidden])) .workspace.panel-open .mode-actions {
    bottom: calc(min(36dvh, 180px) + 72px + env(safe-area-inset-bottom));
  }

  body:has(.mode-actions:not([hidden])) #selection-actions:not([hidden]) {
    bottom: calc(166px + env(safe-area-inset-bottom));
  }

  #fence-planner-dialog {
    width: min(640px, calc(100% - 20px));
    max-height: calc(100dvh - 12px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    margin-inline: auto;
    border-radius: 12px 12px 0 0;
  }
}

@media (min-width: 600px) and (max-width: 900px) and (orientation: landscape) and (max-height: 540px) {
  .workspace #selection-actions {
    grid-template-columns: minmax(108px, 1.35fr) repeat(7, minmax(0, 1fr));
    align-items: center;
    gap: 4px;
    padding: 6px;
  }

  #selection-actions strong {
    grid-column: auto;
    padding: 0 4px;
  }

  #selection-actions button {
    min-height: 48px;
  }

  #selection-actions .danger-action {
    grid-column: auto;
  }

  body:has(#selection-actions:not([hidden])):not(:has(.tool-rail.is-expanded)) .leaflet-control-attribution {
    bottom: calc(145px + env(safe-area-inset-bottom));
  }

  body:has(.mode-actions:not([hidden])):has(#selection-actions:not([hidden])) .leaflet-control-attribution {
    bottom: calc(235px + env(safe-area-inset-bottom));
  }
}
