/**
 * Shared iOS bottom-sheet tokens + chrome.
 *
 * Stacking (intentional):
 *   --ios-sheet-z-drawer / -pane  create-drawer (below sheets so osConfirm stacks)
 *   --ios-sheet-z-nested          share sheet (under OS dialog / alerts)
 *   --ios-sheet-z                 default sheets + OS dialog
 */
:root {
  --ios-sheet-z: 6000;
  --ios-sheet-z-nested: 5900;
  --ios-sheet-z-drawer: 4800;
  --ios-sheet-z-drawer-pane: 4900;

  --ios-sheet-scrim: rgba(0, 0, 0, 0.3);
  --ios-sheet-scrim-blur: blur(10px) saturate(1.1);
  --ios-sheet-radius: 20px;
  --ios-sheet-shadow: 0 -8px 40px rgba(0, 0, 0, 0.18);
  --ios-sheet-duration: 0.38s;
  --ios-sheet-easing: cubic-bezier(0.32, 0.72, 0, 1);
  --ios-sheet-fade-duration: 0.28s;
  --ios-sheet-max-width: 520px;
  --ios-sheet-max-height: min(92dvh, 720px);
  --ios-sheet-grabber-w: 36px;
  --ios-sheet-grabber-h: 5px;

  --ios-sheet-bg: #ffffff;
  --ios-sheet-fg: #0f172a;
  --ios-sheet-muted: rgba(0, 0, 0, 0.55);
  --ios-sheet-border: rgba(0, 0, 0, 0.08);
  --ios-sheet-grabber: rgba(0, 0, 0, 0.18);
  --ios-sheet-close-bg: rgba(0, 0, 0, 0.06);
  --ios-sheet-close-hover: rgba(0, 0, 0, 0.1);
  --ios-sheet-link: #0a84ff;
}

html.ios-sheet-locked {
  overflow: hidden;
}

html.ios-sheet-locked body {
  overflow: hidden;
  overscroll-behavior: none;
}

.ios-sheet-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: var(--ios-sheet-z, 6000);
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  box-sizing: border-box;
  /* Lighter scrim so the frosted background blur reads as a slight blur
     rather than a flat dim. saturate() gives the iOS-style frosted look. */
  background: var(--ios-sheet-scrim);
  backdrop-filter: var(--ios-sheet-scrim-blur);
  -webkit-backdrop-filter: var(--ios-sheet-scrim-blur);
  -webkit-tap-highlight-color: transparent;
}

.ios-sheet-backdrop.open {
  display: flex;
}

/*
 * Keyboard: JS pins this backdrop to visualViewport. Cap the sheet to that
 * box so iOS does not scroll the overlay to keep the focused field on screen.
 */
.ios-sheet-backdrop.open.ios-sheet-keyboard .ios-sheet {
  height: 100%;
  max-height: 100%;
  min-height: 0;
  padding-bottom: 0;
}

.ios-sheet {
  width: 100%;
  max-width: var(--ios-sheet-max-width);
  max-height: var(--ios-sheet-max-height);
  display: flex;
  flex-direction: column;
  background: var(--ios-sheet-bg);
  color: var(--ios-sheet-fg);
  border: 1px solid var(--ios-sheet-border);
  border-bottom: none;
  border-radius: var(--ios-sheet-radius) var(--ios-sheet-radius) 0 0;
  box-shadow: var(--ios-sheet-shadow);
  transform: translateY(100%);
  opacity: 1;
  transition:
    transform var(--ios-sheet-duration) var(--ios-sheet-easing),
    opacity var(--ios-sheet-fade-duration) ease;
  overflow: hidden;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  touch-action: pan-y;
}

.ios-sheet--visible {
  transform: translateY(0);
}

.ios-sheet-grabber {
  flex-shrink: 0;
  position: relative;
  width: var(--ios-sheet-grabber-w);
  height: var(--ios-sheet-grabber-h);
  margin: 8px auto 4px;
  border-radius: 999px;
  background: var(--ios-sheet-grabber);
  touch-action: none;
}

.ios-sheet-grabber::before {
  content: '';
  position: absolute;
  left: -40vw;
  right: -40vw;
  top: -14px;
  bottom: -10px;
}

.ios-sheet-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 3rem;
  flex-shrink: 0;
  border-bottom: 1px solid var(--ios-sheet-border);
}

.ios-sheet-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  text-align: center;
  line-height: 1.3;
}

.ios-sheet-close {
  position: absolute;
  left: max(12px, env(safe-area-inset-left, 0px));
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: var(--ios-sheet-close-bg);
  color: var(--ios-sheet-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, color 0.15s;
}

.ios-sheet-close:hover {
  background: var(--ios-sheet-close-hover);
  color: var(--ios-sheet-fg);
}

.ios-sheet-close[hidden] {
  display: none !important;
}

.ios-sheet-header-trailing {
  position: absolute;
  right: max(12px, env(safe-area-inset-right, 0px));
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
}

.ios-sheet-header-trailing:not(:has(*)) {
  display: none;
}

.ios-sheet-header-action {
  margin: 0;
  padding: 0.35rem 0.2rem;
  border: none;
  background: transparent;
  color: var(--ios-sheet-link);
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.ios-sheet-header-action:hover,
.ios-sheet-header-action:focus-visible {
  opacity: 0.72;
}

.ios-sheet-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
  padding: 1rem 1.15rem 1.25rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ios-sheet-bg: #121722;
    --ios-sheet-fg: #e9eef6;
    --ios-sheet-muted: rgba(255, 255, 255, 0.55);
    --ios-sheet-border: rgba(255, 255, 255, 0.1);
    --ios-sheet-grabber: rgba(255, 255, 255, 0.28);
    --ios-sheet-close-bg: rgba(255, 255, 255, 0.08);
    --ios-sheet-close-hover: rgba(255, 255, 255, 0.14);
    --ios-sheet-link: #60a5fa;
  }
}

/* Every sheet is a bottom sheet on all viewports — no centered desktop modal fork. */

@media (prefers-reduced-motion: reduce) {
  .ios-sheet {
    transition: none;
  }
}
