/*
 * Frame Picker v2 ("Välj ram"), the frame modal: inc/frame-picker-v2.php +
 * js/frame-picker-v2.js. Plain CSS like css/pdp2.css, served unminified via
 * assetUrl(); product-poster-v2.php loads it only when ?framepicker=1 is set.
 *
 * ≥ 768px: a centred modal, preview panel left, filter + card grid right.
 * < 768px: a bottom sheet. The card grid wraps and scrolls vertically, never
 * sideways. Everything is scoped under .fp2 / .fp2-frame.
 */

.fp2 {
  --fp2-text: #1a1a1a;
  --fp2-text-muted: #5f5f5f;
  --fp2-surface: #fff;
  --fp2-surface-warm: #f3eee6;
  --fp2-tile: #f6f5f2;
  --fp2-track: #f1f0ed;
  --fp2-border: #e6e5e1;
  --fp2-border-hover: #b9b8b3;
  --fp2-action: #141414;
  --fp2-focus: #1a5fd6;
  --fp2-shadow-frame: 0 0 0 1px rgb(0 0 0 / 0.08), 0 6px 16px rgb(0 0 0 / 0.16);

  box-sizing: border-box;
  width: min(960px, 100vw - 48px);
  height: min(640px, 100vh - 64px);
  max-width: none;
  max-height: none;
  padding: 0;
  border: 0;
  border-radius: 12px;
  overflow: hidden;
  background: var(--fp2-surface);
  color: var(--fp2-text);
  font-family: var(--text-font-family, "Nunito Sans", sans-serif);
}

.fp2 *,
.fp2 *::before,
.fp2 *::after {
  box-sizing: border-box;
}

.fp2[open] {
  display: grid;
  grid-template-columns: clamp(300px, 38%, 380px) 1fr;
}

.fp2::backdrop {
  background: rgb(20 20 20 / 0.45); /* literal: older browsers don't pass .fp2's vars to ::backdrop */
}

html:has(.fp2[open]) {
  overflow: hidden; /* page scroll lock (js/frame-picker-v2.js sets it too) */
}

.fp2 :focus {
  outline: none;
}

.fp2 :focus-visible {
  outline: 2px solid var(--fp2-focus);
  outline-offset: 2px;
}

.fp2__handle {
  display: none;
}

.fp2__sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ── Preview panel (left) ─────────────────────────────────────────────── */

.fp2__preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  min-width: 0;
  padding: 40px;
  background: var(--fp2-surface-warm);
}

.fp2__caption {
  margin: 0;
  text-align: center;
}

.fp2__caption strong {
  display: block;
  font-size: 17px;
  font-weight: 600;
  line-height: 24px;
}

.fp2__caption span {
  display: block;
  margin-top: 2px;
  font-size: 13px;
  line-height: 18px;
  color: var(--fp2-text-muted);
}

/* ── Framed poster: one component, large (preview) and small (cards) ─────
   True to scale: the poster sits straight in the frame (no mat), and the
   border is the frame profile's real share of the framed width, set by
   js/frame-picker-v2.js as --frame-share, with --motif-ratio the framed
   piece's ratio. --w is the component's width per size; a floor keeps the
   thinnest borders (big posters, small cards) visible. With a frame texture
   (--frame-img, the frame's front.png: an opaque band of 19px top/bottom and
   20px at the sides round a transparent window) it's the border-image;
   otherwise the flat --frame-color. The 1px hairline in the shadow keeps
   light frames visible. */

.fp2-frame {
  --w: 216px;
  --fw: max(1.5px, calc(var(--w) * var(--frame-share, 0.05)));
  position: relative;
  display: block;
  width: var(--w);
  aspect-ratio: var(--motif-ratio, 21 / 30);
  border: var(--fw) solid transparent; /* sizes the box; ::after draws the frame */
  padding: 0;
  background: none;
  box-shadow: var(--fp2-shadow-frame);
}

/* The frame, drawn on top of the poster. */
.fp2-frame::after {
  content: "";
  position: absolute;
  inset: calc(-1 * var(--fw));
  border: var(--fw) solid var(--frame-color, #1a1a1a);
  pointer-events: none;
}

.fp2-frame[data-img]::after {
  border-image-source: var(--frame-img);
  border-image-slice: 19 20;
  border-image-repeat: stretch;
}

.fp2-frame[data-none] {
  border: 0;
  padding: 0;
  box-shadow: 0 1px 4px rgb(0 0 0 / 0.2);
}

.fp2-frame[data-none]::after {
  content: none;
}

/* The poster runs 1px under the frame band on every side (as the page's own
   framed stage does), so pixel snapping at small sizes never leaves a gap
   between poster and frame. */
.fp2-frame img {
  position: absolute;
  top: -1px;
  left: -1px;
  display: block;
  width: calc(100% + 2px);
  height: calc(100% + 2px);
  object-fit: cover;
}

.fp2-frame[data-none] img {
  inset: 0;
  width: 100%;
  height: 100%;
}

/* Each size fills a box --h high and at most --wmax wide, at the framed
   piece's own ratio (--motif-ratio), so portrait, square and landscape
   posters all use their space. */
.fp2-frame--lg,
.fp2-frame--sm {
  --w: min(calc(var(--h) * var(--motif-ratio, 0.7)), var(--wmax));
}

.fp2-frame--lg {
  --h: 300px;
  --wmax: 260px;
  max-width: 100%;
}

.fp2-frame--sm {
  --h: 86px;
  --wmax: 96px;
}

/* ── Right side ───────────────────────────────────────────────────────── */

.fp2__side {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 24px 32px;
}

.fp2__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.fp2__title {
  margin: 0;
  font-family: inherit;
  font-size: 20px;
  font-weight: 600;
  line-height: 28px;
  letter-spacing: 0;
  text-transform: none;
  color: var(--fp2-text);
}

.fp2__close {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  margin-right: -10px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--fp2-text);
  cursor: pointer;
}

.fp2__close:hover {
  background: var(--fp2-track);
}

/* Material filter (segmented control; aria-pressed buttons, not tabs) */
.fp2__filter {
  display: flex;
  gap: 4px;
  align-self: flex-start;
  margin-top: 12px;
  padding: 4px;
  background: var(--fp2-track);
  border-radius: 999px;
}

.fp2__filter[hidden] {
  display: none;
}

.fp2__filter button {
  height: 36px;
  padding: 0 18px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--fp2-text-muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.fp2__filter button[aria-pressed="true"] {
  background: #fff;
  color: var(--fp2-text);
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.12);
}

/* Card grid: wraps, scrolls vertically, never sideways */
/* The scroller is a plain div round the fieldset: a fieldset that scrolls
   itself doesn't take touch scrolls that start in the gaps between cards on
   phones. overscroll-behavior keeps a swipe at either end from moving the page. */
.fp2__scroll {
  flex: 1;
  min-width: 0;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  margin: 16px -4px 0;
  padding: 4px;
}

.fp2__grid {
  min-width: 0;
  min-inline-size: 0;
  margin: 0;
  padding: 0;
  border: 0;
  display: grid;
  align-content: start;
  gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
}

.fp2__card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
  margin: 0;
  padding: 8px 8px 12px;
  border: 1px solid var(--fp2-border);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  transition: border-color 120ms ease-out, box-shadow 120ms ease-out, background-color 120ms ease-out;
}

.fp2__card[hidden] {
  display: none;
}

.fp2__card input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.fp2__card:hover {
  border-color: var(--fp2-border-hover);
}

.fp2__card:has(input:checked) {
  border-color: var(--fp2-text);
  box-shadow: 0 0 0 1px var(--fp2-text);
  background: var(--fp2-surface-warm);
}

.fp2__card:has(input:focus-visible) {
  outline: 2px solid var(--fp2-focus);
  outline-offset: 2px;
}

.fp2__tile {
  display: grid;
  place-items: center;
  height: 100px;
  background: var(--fp2-tile);
  border-radius: 6px;
}

.fp2__name {
  padding: 0 4px;
  font-size: 13px;
  font-weight: 600;
  line-height: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fp2__price {
  padding: 0 4px;
  font-size: 12px;
  line-height: 16px;
  color: var(--fp2-text-muted);
}

/* Footer */
.fp2__foot {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--fp2-border);
}

.fp2__cta {
  width: 100%;
  height: 52px;
  border: 0;
  border-radius: 999px;
  background: var(--fp2-action);
  color: #fff;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  cursor: pointer;
}

/* Motion */
@media (prefers-reduced-motion: no-preference) {
  .fp2[open] {
    animation: fp2-in 180ms ease-out;
  }

  .fp2[open]::backdrop {
    animation: fp2-fade 150ms ease-out;
  }
}

@keyframes fp2-in {
  from {
    opacity: 0;
    transform: scale(0.98);
  }
}

@keyframes fp2-fade {
  from {
    opacity: 0;
  }
}

/* ── Phone: bottom sheet ──────────────────────────────────────────────── */

@media (max-width: 767.98px) {
  /* Compact, so a full row of frames with their prices shows under the
     preview. Heights use svh (the visible height, without the browser's
     toolbars), with vh where svh isn't supported. */
  .fp2 {
    width: 100%;
    height: auto;
    max-height: 92vh;
    max-height: 92svh;
    margin: auto 0 0;
    border-radius: 16px 16px 0 0;
  }

  .fp2[open] {
    display: flex;
    flex-direction: column;
  }

  .fp2__handle {
    display: flex;
    justify-content: center;
    padding-top: 6px;
  }

  .fp2__handle span {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: #d4d3cf;
  }

  .fp2__side {
    display: contents; /* lets the preview sit between the header and the filter */
  }

  .fp2__head {
    order: 1;
    padding: 0 2px 0 12px;
  }

  .fp2__title {
    font-size: 16px;
    line-height: 22px;
  }

  .fp2__close {
    margin-right: 0;
  }

  /* Poster centred, its frame name and size underneath, as on desktop. */
  .fp2__preview {
    order: 2;
    flex: 0 0 auto;
    gap: 6px;
    margin: 0 12px;
    padding: 10px 12px 8px;
    border-radius: 10px;
  }

  .fp2__caption {
    min-width: 0;
    max-width: 100%;
  }

  .fp2__caption strong {
    font-size: 15px;
    line-height: 19px;
  }

  .fp2__caption span {
    margin-top: 0;
    font-size: 12px;
    line-height: 16px;
  }

  /* The preview takes the height left once the rest of the sheet (about
     420px: header, filter, a row of cards with a peek of the next, button)
     fits, 96–220px, and never outgrows the preview box's width. */
  .fp2-frame--lg {
    --h: clamp(96px, 92vh - 420px, 220px);
    --wmax: calc(100vw - 48px);
    flex: 0 0 auto;
  }

  @supports (height: 1svh) {
    .fp2-frame--lg {
      --h: clamp(96px, 92svh - 420px, 220px);
    }
  }

  .fp2__filter {
    order: 3;
    align-self: stretch;
    margin: 8px 12px 0;
    padding: 3px;
  }

  .fp2__filter button {
    flex: 1;
    padding: 0 10px;
    font-size: 12px;
  }

  .fp2__scroll {
    order: 4;
    flex: 1 1 auto;
    margin: 0;
    padding: 8px 12px 10px;
  }

  .fp2__grid {
    gap: 8px;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .fp2__card {
    gap: 6px;
    padding: 6px 6px 8px;
  }

  .fp2__tile {
    height: 80px;
  }

  .fp2__name {
    padding: 0 2px;
    font-size: 12px;
    line-height: 16px;
  }

  .fp2__price {
    padding: 0 2px;
    font-size: 11px;
    line-height: 14px;
  }

  .fp2-frame--sm {
    --h: 68px;
    /* The tile's width less 8px: 3 columns in 100vw minus 24px padding and
       2 × 8px gaps, less each card's 2 × 6px padding and 2 × 1px border.
       In vw, not %, as --w feeds the border width too. */
    --wmax: calc((100vw - 40px) / 3 - 22px);
  }

  .fp2__foot {
    order: 5;
    margin: 0;
    padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  }

  .fp2__cta {
    height: 48px;
    font-size: 13px;
  }

  @media (prefers-reduced-motion: no-preference) {
    .fp2[open] {
      animation: fp2-up 240ms ease-out;
    }
  }
}

@keyframes fp2-up {
  from {
    transform: translateY(100%);
  }
}
