/* path: wp-content/themes/bedrebyttede/assets/css/components/surface-chrome.css */
@layer components {

  /* =========================================================
     Defaults for surface grid tracks (safe fallbacks)
  ========================================================= */
  :root {
    /* If not defined elsewhere, give sensible defaults */
    --bb-surface-grid-columns-idle: minmax(0, 1.6fr) minmax(0, 0.9fr);
    --bb-surface-grid-columns-active: minmax(0, 0.9fr) minmax(0, 2.2fr);
    --bb-surface-grid-transition: 240ms;
  }

  /* Optional: Safari-smooth column animation via typed custom prop.
     If you prefer the old track-list transition, you can remove this
     whole block AND the "--left" usage below. */
  @supports (grid-template-columns: subgrid) or (grid-template-columns: 1fr 1fr) {
    @property --left {
      syntax: '<percentage>';
      initial-value: 64%;
      inherits: false;
    }
  }

  /* ============ OVERLAY & CHROME ============ */
  #bb-global-surface[hidden] {
    display: none;
  }

  #bb-global-surface {
    position: fixed;
    inset: 0;
    z-index: 70;
    /* unified with modal/toast stack in tokens */
    display: grid;
    place-items: center;
    background: color-mix(in oklab, black, transparent 60%);
    backdrop-filter: saturate(120%) blur(6px);
    -webkit-backdrop-filter: saturate(120%) blur(6px);
    /* overscroll-behavior: contain; REMOVED to prevent scroll lock */
    scroll-behavior: smooth;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition:
      opacity 300ms var(--ease-out, ease-out),
      visibility 300ms;
    --bb-surface-scale: 0.96;
    --bb-surface-offset: 40px;
  }

  #bb-global-surface.is-active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    --bb-surface-scale: 1;
    --bb-surface-offset: 0px;
  }

  #bb-global-surface.is-closing {
    opacity: 0;
    visibility: visible;
    pointer-events: none;
    --bb-surface-scale: 0.96;
    --bb-surface-offset: 20px;
  }

  .bb-surface-chrome {
    background: var(--surface);
    width: min(1080px, 92vw);
    max-height: 92vh;
    border-radius: var(--radius-3);
    box-shadow: var(--shadow-3);
    display: grid;
    grid-template-rows: auto 1fr;
    outline: none;
    pointer-events: auto;
    position: relative;
    transition: transform 450ms cubic-bezier(0.32, 0.72, 0, 1);
    will-change: transform;
    touch-action: pan-y;
    transform: translateY(calc(var(--bb-sheet-y, 0px) + var(--bb-surface-offset, 0px))) scale(var(--bb-surface-scale, 1));
    padding: var(--space-4);
    /* Increased for 2025 breathability */
    overflow: hidden;
    /* Keep content isolated */
    contain: layout;
    /* Golden standard: isolate layout calculations */
  }

  .bb-surface-chrome.is-dragging {
    transition: none;
  }

  .bb-surface-actions {
    position: absolute;
    top: var(--space-7);
    right: var(--space-8);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: var(--space-2);
    z-index: 3;
    /* always above hero */
  }

  .bb-surface-actions>.bb-surface-action {
    position: static;
    inset: auto;
  }

  .bb-surface-action+.bb-surface-action {
    margin-block-start: 0;
  }

  .bb-surface-action {
    border: var(--border-1) solid var(--line);
    background: var(--surface);
    border-radius: var(--radius-round);
    width: 44px;
    height: 44px;
    /* Native mobile feel */
    padding: 0;
    display: grid;
    place-items: center;
    font-size: var(--fs-lg);
    line-height: 1;
    color: var(--text-2);
    cursor: pointer;
    box-shadow: var(--shadow-1);
    transition: all var(--dur-2) var(--ease-out, ease-out);
  }

  .bb-surface-action:hover,
  .bb-surface-action[aria-pressed="true"] {
    box-shadow: var(--shadow-2);
    color: #fff;
    background: var(--grad-move);
  }

  .bb-surface-close {
    font-size: var(--fs-xl);
  }

  .bb-surface-close:hover {
    color: #fff !important;
  }

  .bb-surface-expand {
    font-size: var(--fs-md);
  }

  .bb-surface-expand i {
    pointer-events: none;
  }

  /* drag affordance (mobile visible) */
  .bb-surface-drag {
    display: none;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    position: sticky;
    top: 0;
    z-index: 5;
    padding: 14px 0 10px;
    margin-top: -4px;
    background: transparent;
  }

  .bb-surface-handle {
    width: 48px;
    height: 4px;
    background: var(--line);
    border-radius: var(--radius-round);
    margin: 0 auto;
  }

  .bb-surface-header {
    margin: var(--space-0) var(--space-2);
  }

  /* The inner scroll container — make it stable and isolated */
  #bb-surface-body {
    padding: var(--space-2) 0;
    overflow-y: auto;
    overflow-x: hidden;
    /* CRITICAL: Prevent horizontal jiggle */
    background: transparent;
    display: flex;
    flex-direction: column;
    min-height: 0;
    /* Allow grid compression */
  }

  /* ============ Custom Scrollbar (generic "surface scroll skin") ============ */
  /* Applies to:
   - The global surface body when it has listing content
   - Any other scroll container marked with data-surface-scroll-skin
  */
  #bb-surface-body.has-listing-inner,
  [data-surface-scroll-skin] {
    scrollbar-width: thin;
    scrollbar-color: color-mix(in oklab, var(--text-3) 32%, transparent) transparent;
    scroll-behavior: smooth;
    scrollbar-gutter: stable both-edges;
    /* overflow-anchor: none; prevent bottom re-anchoring jumps */
    /* contain: paint;  REMOVED to prevent scroll clipping on mobile */
    -webkit-overflow-scrolling: touch;
    /* overscroll-behavior: contain; REMOVED to allow chaining */

    --bb-listing-scrollbar-thumb: color-mix(in oklab, var(--text-3) 32%, transparent);
    --bb-listing-scrollbar-thumb-hover: color-mix(in oklab, var(--text-3) 45%, transparent);
    --bb-listing-scrollbar-thumb-active: color-mix(in oklab, var(--text-2) 55%, transparent);
  }

  /* WebKit scrollbar styling for all skinned containers */
  #bb-surface-body.has-listing-inner::-webkit-scrollbar,
  [data-surface-scroll-skin]::-webkit-scrollbar {
    width: 12px;
    height: 12px;
  }

  #bb-surface-body.has-listing-inner::-webkit-scrollbar-track,
  [data-surface-scroll-skin]::-webkit-scrollbar-track {
    background: transparent;
  }

  #bb-surface-body.has-listing-inner::-webkit-scrollbar-thumb,
  [data-surface-scroll-skin]::-webkit-scrollbar-thumb {
    background-color: var(--bb-listing-scrollbar-thumb);
    border-radius: var(--radius-round);
    border: 3px solid transparent;
    background-clip: padding-box;
    box-shadow:
      0 0 0 1px color-mix(in oklab, var(--surface) 82%, transparent),
      0 12px 24px rgba(9, 23, 15, 0.18);
    transition:
      background-color var(--dur-2) var(--ease-in-out, ease-in-out),
      box-shadow var(--dur-2) var(--ease-in-out, ease-in-out);
  }

  #bb-surface-body.has-listing-inner::-webkit-scrollbar-thumb:hover,
  #bb-surface-body.has-listing-inner:hover::-webkit-scrollbar-thumb,
  [data-surface-scroll-skin]::-webkit-scrollbar-thumb:hover,
  [data-surface-scroll-skin]:hover::-webkit-scrollbar-thumb {
    background-color: var(--bb-listing-scrollbar-thumb-hover);
    box-shadow:
      0 0 0 1px color-mix(in oklab, var(--surface) 88%, transparent),
      0 16px 32px rgba(9, 23, 15, 0.24);
  }

  #bb-surface-body.has-listing-inner::-webkit-scrollbar-thumb:active,
  [data-surface-scroll-skin]::-webkit-scrollbar-thumb:active {
    background-color: var(--bb-listing-scrollbar-thumb-active);
    box-shadow:
      0 0 0 1px color-mix(in oklab, var(--surface) 92%, transparent),
      0 10px 24px rgba(9, 23, 15, 0.28);
  }

  /* ============ MOBILE DRAWER ============ */
  @media (max-width: 47.99rem) {
    #bb-global-surface {
      align-items: end;
    }

    .bb-surface-chrome {
      width: 100vw;
      max-height: 85vh;
      border-radius: var(--card-radius) var(--card-radius) 0 0;
      transform: translateY(calc(var(--bb-sheet-y, 0px) + var(--bb-surface-offset, 0px))) scale(var(--bb-surface-scale, 1));
      box-shadow: var(--shadow-3);
      position: relative;
      padding: var(--space-0);
    }

    .bb-surface-drag {
      display: block;
      padding: 10px 0 6px;
    }

    /* Single column + default stack: details above sidebar */
    .bb-surface-grid {
      grid-template-columns: minmax(0, 1fr) !important;
      --mobile-areas: "details" "sidebar";
      grid-template-areas: var(--mobile-areas) !important;
    }

    /* Alternate rows by wrapper, not the grid itself */
    .listing-inner:nth-of-type(even) .bb-surface-grid {
      --mobile-areas: "sidebar" "details";
    }

    /* Surface Harmony: If surface body has content, make sure it doesn't double-pad */
    #bb-surface-body {
      padding: var(--space-3);
    }

    .bb-surface-close {
      display: none;
    }

    /* Large, invisible hit area that sits ~8px above the handle and overlaps it */
    .bb-surface-grabzone {
      position: absolute;
      top: -8px;
      left: 0;
      right: 0;
      height: 48px;
      z-index: 2;
      cursor: grab;
      touch-action: none;
      -webkit-tap-highlight-color: transparent;
    }

    .bb-surface-grabzone:active {
      cursor: grabbing;
    }
  }

  /* Lock page behind the surface; iOS friendly */
  body.bb-surface-open {
    overflow: hidden;
    overscroll-behavior: none;
    scrollbar-gutter: stable both-edges;
  }

  html.bb-lock {
    overflow: hidden;
  }
}
