/*
  One safe-area treatment for PitLane, Garage, Admin and the static pages.

  viewport-fit=cover (and the PWA standalone status bar) lets the layout run
  under the Dynamic Island, notch and home indicator. env() is 0 until that
  meta is set. constant() covers iOS 11.0–11.1, which had the insets but not
  env(). Components read the variables so a test page can set them when the
  browser cannot emulate env().

  Header background paints through the padding, so it still sits behind the
  status bar. Buttons live in the content box, below the inset. Do not put a
  fixed height (h-14) on .safe-area-header itself: with border-box that height
  eats the padding and the buttons land back under the island. Put the row
  height on an inner element, or let this min-height grow.
*/

:root {
  --safe-top: 0px;
  --safe-right: 0px;
  --safe-bottom: 0px;
  --safe-left: 0px;
}

@supports (top: constant(safe-area-inset-top)) {
  :root {
    --safe-top: constant(safe-area-inset-top);
    --safe-right: constant(safe-area-inset-right);
    --safe-bottom: constant(safe-area-inset-bottom);
    --safe-left: constant(safe-area-inset-left);
  }
}

@supports (top: env(safe-area-inset-top)) {
  :root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
  }
}

.safe-area-header {
  box-sizing: border-box;
  /* Beat a Tailwind h-14 on the same element. Utilities live in a layer;
     this file does not, so height:auto wins and the bar can grow. */
  height: auto;
  min-height: calc(3.5rem + var(--safe-top, 0px));
  padding-top: constant(safe-area-inset-top);
  padding-top: var(--safe-top, 0px);
  /* Side insets add to the header's own px, instead of replacing it.
     The background still paints into the border, behind the landscape notch. */
  border-left: var(--safe-left, 0px) solid transparent;
  border-right: var(--safe-right, 0px) solid transparent;
  background-clip: border-box;
}

/* Phone shells with no top bar. Stays stuck so page content cannot scroll
   back under the island, and the page colour fills the status-bar band. */
.safe-area-topbar {
  position: sticky;
  top: 0;
  z-index: 35;
  height: var(--safe-top, 0px);
  background-color: rgb(var(--bg));
}

@media (min-width: 1024px) {
  .safe-area-topbar {
    display: none;
  }
}

.safe-area-footer {
  box-sizing: border-box;
  padding-bottom: calc(1.5rem + constant(safe-area-inset-bottom));
  padding-bottom: calc(1.5rem + var(--safe-bottom, 0px));
  padding-left: var(--safe-left, 0px);
  padding-right: var(--safe-right, 0px);
}

/* Bottom chrome that already has its own height. Only the insets. */
.safe-area-bottom {
  box-sizing: border-box;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: var(--safe-bottom, 0px);
  padding-left: var(--safe-left, 0px);
  padding-right: var(--safe-right, 0px);
}

/* Dialogs and sheets. Padding is on the fixed overlay, so the card cannot
   enter the island or the home indicator. The scrim still covers that band. */
.safe-overlay {
  box-sizing: border-box;
  padding-top: max(1rem, constant(safe-area-inset-top));
  padding-right: max(1rem, constant(safe-area-inset-right));
  padding-bottom: max(1rem, constant(safe-area-inset-bottom));
  padding-left: max(1rem, constant(safe-area-inset-left));
  padding-top: max(1rem, var(--safe-top, 0px));
  padding-right: max(1rem, var(--safe-right, 0px));
  padding-bottom: max(1rem, var(--safe-bottom, 0px));
  padding-left: max(1rem, var(--safe-left, 0px));
}

/* Extra side inset that does not replace an existing px-* utility. */
.safe-area-inline {
  border-left: var(--safe-left, 0px) solid transparent;
  border-right: var(--safe-right, 0px) solid transparent;
  background-clip: border-box;
}

.safe-area-toast {
  top: calc(var(--safe-top, 0px) + 1rem);
  right: calc(var(--safe-right, 0px) + 1rem);
  left: auto;
  max-width: calc(100vw - 2rem - var(--safe-left, 0px) - var(--safe-right, 0px));
}

/* A 92vh sheet plus the overlay inset is taller than the screen. Cap it
   to the space under the island and above the home indicator. */
.safe-overlay .max-h-\[92vh\],
.safe-overlay .max-h-\[85vh\] {
  max-height: calc(100dvh - max(1rem, var(--safe-top, 0px)) - max(1rem, var(--safe-bottom, 0px)));
}

.back-to-top {
  right: max(1rem, var(--safe-right, 0px));
}

.safe-area-toast-bottom {
  bottom: calc(5rem + var(--safe-bottom, 0px));
  left: max(0.75rem, var(--safe-left, 0px));
  right: max(0.75rem, var(--safe-right, 0px));
}

@media (min-width: 1024px) {
  .safe-area-toast-bottom {
    bottom: 1rem;
    left: auto;
    right: 1rem;
  }
}

.shell-pad-pitlane {
  padding-bottom: calc(6rem + var(--safe-bottom, 0px));
}

.shell-pad-garage {
  padding-bottom: calc(7rem + var(--safe-bottom, 0px));
}

.above-bottom-nav {
  padding-bottom: calc(7rem + var(--safe-bottom, 0px));
}

@media (min-width: 1024px) {
  .shell-pad-pitlane,
  .shell-pad-garage {
    padding-bottom: 2rem;
  }
}

/* Header icon buttons. 44px is the iOS minimum. Tabs and full-width rows
   keep their own layout; they already use a 48px row. */
.safe-area-header :is(a, button):not([role="tab"]),
.safe-toolbar :is(a, button):not([role="tab"]) {
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-width: 2.75rem;
  min-height: 2.75rem;
}

/* Static documents. The sticky band keeps the page colour behind the status
   bar; the first real control starts below it. */
.safe-page {
  padding-right: var(--safe-right, 0px);
  padding-bottom: var(--safe-bottom, 0px);
  padding-left: var(--safe-left, 0px);
}

.safe-page::before {
  content: "";
  display: block;
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--safe-top, 0px);
  background: inherit;
  pointer-events: none;
}

/* Short centered pages (login, thank-you). A ::before flex/grid item would
   sit beside the card, so the inset is padding instead. */
.safe-page-pad {
  box-sizing: border-box;
  padding-top: max(1.5rem, constant(safe-area-inset-top));
  padding-right: max(1rem, constant(safe-area-inset-right));
  padding-bottom: max(1.5rem, constant(safe-area-inset-bottom));
  padding-left: max(1rem, constant(safe-area-inset-left));
  padding-top: max(1.5rem, var(--safe-top, 0px));
  padding-right: max(1rem, var(--safe-right, 0px));
  padding-bottom: max(1.5rem, var(--safe-bottom, 0px));
  padding-left: max(1rem, var(--safe-left, 0px));
}

/* Legal pages: the titlebar is the header and carries the top inset. */
.safe-page-sides {
  box-sizing: border-box;
  padding-right: var(--safe-right, 0px);
  padding-bottom: var(--safe-bottom, 0px);
  padding-left: var(--safe-left, 0px);
}
