/* ==========================================================================
   layout.css — header, split / stacked / mobile shell, panes, view host (Core)
   ========================================================================== */

/* ---------------------------------------------------------------------------
   Header (56 px, sticky)
   --------------------------------------------------------------------------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: var(--z-header);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: var(--header-h);
  padding: 0 var(--space-4) 0 var(--space-4);
  background: color-mix(in srgb, var(--bg) 98%, transparent);
  border-bottom: 1px solid var(--hairline);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  flex: none;
  height: 40px;
  padding: 0 var(--space-2) 0 var(--space-1);
  margin-right: var(--space-4);
  border-radius: var(--radius-sm);
  color: var(--text-1);
}

.brand__mark {
  flex: none;
}

.brand__name {
  font-size: var(--fs-16);
  line-height: var(--lh-16);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-tight);
  white-space: nowrap;
}

.header-search {
  position: relative;
  flex: 1 1 520px;
  max-width: 520px;
  min-width: 220px;
}

.site-header .header-search-btn,
.site-header .header-mobile-search {
  display: none;
}

.site-header__spacer {
  flex: 1 1 0;
  min-width: var(--space-2);
}

.header-slot {
  display: flex;
  align-items: center;
  flex: none;
}

.header-slot:empty {
  display: none;
}

/* ---------------------------------------------------------------------------
   Shell: split (default) = sticky player column + scrolling view host
   --------------------------------------------------------------------------- */
.shell {
  display: grid;
  grid-template-columns: var(--stage-w) minmax(0, 1fr);
  align-items: start;
  min-height: calc(100dvh - var(--header-h));
}

.stage {
  position: sticky;
  top: var(--header-h);
  z-index: var(--z-base);
  display: flex;
  flex-direction: column;
  height: calc(100dvh - var(--header-h));
  min-width: 0;
  overflow: hidden;
  border-right: 1px solid var(--hairline);
  background:
    radial-gradient(120% 60% at 20% 0%, rgb(var(--ambient) / 0.12), transparent 70%),
    radial-gradient(90% 50% at 90% 30%, rgb(var(--ambient) / 0.06), transparent 70%),
    var(--bg);
  transition: background var(--dur-slow) var(--ease);
}

.stage__screen-slot {
  flex: none;
  position: relative;
}

.stage__info {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--space-6) var(--space-6) var(--space-8);
  scrollbar-gutter: stable;
}

.main-pane {
  min-width: 0;
  min-height: calc(100dvh - var(--header-h));
}

.theater-bar:empty {
  display: none;
}

.view-host {
  position: relative;
  min-width: 0;
  outline: none;
}

.view-host__browse[hidden],
.view-host__page[hidden] {
  display: none !important;
}

.view {
  min-height: calc(100dvh - var(--header-h));
}

.view__header {
  position: sticky;
  top: var(--header-h);
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--filterbar-h);
  padding: 0 var(--space-6);
  background: color-mix(in srgb, var(--bg) 96%, transparent);
  -webkit-backdrop-filter: var(--blur-backdrop);
  backdrop-filter: var(--blur-backdrop);
  border-bottom: 1px solid var(--hairline);
}

.view__body {
  padding: var(--space-6);
}

/* ---------------------------------------------------------------------------
   Browse view: sticky filter bar, chips row, contextual bar, results
   --------------------------------------------------------------------------- */
.filterbar {
  position: sticky;
  top: var(--header-h);
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  gap: var(--space-1);
  height: var(--filterbar-h);
  padding: 0 var(--space-4) 0 var(--space-4);
  background: color-mix(in srgb, var(--bg) 96%, transparent);
  -webkit-backdrop-filter: var(--blur-backdrop);
  backdrop-filter: var(--blur-backdrop);
  border-bottom: 1px solid var(--hairline);
}

.filterbar__group {
  display: flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
  overflow: hidden;
}

.filterbar__spacer {
  flex: 1 1 auto;
  min-width: var(--space-2);
}

.filterbar__count {
  flex: none;
  padding: 0 var(--space-2);
  color: var(--text-3);
  font-size: var(--fs-12);
  line-height: var(--lh-12);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.filterbar__count strong {
  color: var(--text-2);
  font-weight: var(--fw-medium);
}

.filterbar--skeleton {
  gap: var(--space-2);
}

.filterbar .filterbar__mobile {
  display: none;
}

.browse-body {
  padding: var(--space-4) var(--space-6) var(--space-16);
}

.browse-chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-6) 0;
}

.browse-context {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
  padding: var(--space-3) var(--space-6) 0;
}

.browse-context__label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-2);
  font-size: var(--fs-12);
  line-height: var(--lh-12);
  font-weight: var(--fw-medium);
  margin-right: var(--space-1);
}

.browse-context__years {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
}

.browse-feedback {
  padding: var(--space-3) var(--space-6) 0;
  color: var(--text-2);
  font-size: var(--fs-14);
}

.browse-feedback strong {
  color: var(--text-1);
  font-weight: var(--fw-medium);
}

.browse-more {
  display: flex;
  justify-content: center;
  padding: var(--space-8) 0 0;
  color: var(--text-3);
  font-size: var(--fs-12);
}

/* ---------------------------------------------------------------------------
   Stacked layout (portrait desktop or chosen in Display)
   Player on top (sticky), info beside the video, browse below.
   --------------------------------------------------------------------------- */
body.layout-stacked {
  --stack-video-h: min(50vh, calc((100vw - 360px) * 0.5625));
}

body.layout-stacked .shell {
  grid-template-columns: minmax(0, 1fr);
}

body.layout-stacked .stage {
  top: var(--header-h);
  z-index: calc(var(--z-sticky) + 1);
  flex-direction: row;
  height: var(--stack-video-h);
  border-right: 0;
  border-bottom: 1px solid var(--hairline);
}

body.layout-stacked .stage__screen-slot {
  height: 100%;
  aspect-ratio: 16 / 9;
}

body.layout-stacked .stage__screen-slot .screen {
  height: 100%;
  width: auto;
  aspect-ratio: 16 / 9;
}

body.layout-stacked .stage__info {
  padding: var(--space-4) var(--space-6);
}

body.layout-stacked .filterbar,
body.layout-stacked .view__header {
  top: calc(var(--header-h) + var(--stack-video-h));
}

/* ---------------------------------------------------------------------------
   Theater connected: the stage collapses; the view host goes full width.
   F4 renders the "Now playing on Theater" bar into #theater-bar.
   --------------------------------------------------------------------------- */
body.is-theater .shell {
  grid-template-columns: minmax(0, 1fr);
}

body.is-theater .stage {
  display: none;
}

body.is-theater .filterbar,
body.is-theater .view__header {
  top: var(--header-h);
}

.m-hero,
.m-browse-bar,
.m-first-hint {
  display: none;
}

/* ---------------------------------------------------------------------------
   Mobile (≤ 899 px or phone landscape): idle hero, chips scroller, sheets, grid
   --------------------------------------------------------------------------- */
@media (max-width: 899px), (max-height: 500px) and (orientation: landscape) and (pointer: coarse) {
  html {
    scrollbar-gutter: auto;
  }

  .site-header {
    gap: var(--space-1);
    padding: 0 var(--space-2) 0 var(--space-3);
  }

  .site-header .brand,
  .site-header .btn {
    min-width: var(--hit, 44px);
    min-height: var(--hit, 44px);
  }

  .brand {
    margin-right: 0;
  }

  .brand__name {
    font-size: var(--fs-14);
  }

  .header-search {
    display: none;
  }

  .site-header .header-search-btn,
  .site-header .header-mobile-search {
    display: inline-flex;
  }

  #btn-display,
  #header-theater-slot {
    display: none;
  }

  .shell,
  body.layout-stacked .shell,
  body.is-theater .shell {
    display: block;
  }

  .stage__info {
    display: none;
  }

  .stage-bar .btn {
    min-width: var(--hit, 44px);
    min-height: var(--hit, 44px);
  }

  /* Hide desktop filter bar / chips / contextual bar on mobile */
  .filterbar,
  body.layout-stacked .filterbar,
  .browse-chips,
  .browse-context {
    display: none !important;
  }

  /* Mobile idle Hero (§6) */
  .m-hero {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-height: 320px;
    overflow: hidden;
    background: var(--bg);
  }

  body.has-dock .m-hero {
    display: none !important;
  }

  .m-hero__media {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 9;
    max-height: 220px;
    overflow: hidden;
    background: var(--bg);
    flex: none;
  }

  .m-hero__img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }

  .m-hero__img--poster-blur {
    filter: blur(24px) brightness(0.72);
    transform: scale(1.15);
  }

  .m-hero__scrim {
    position: absolute;
    inset: 0 0 -1px 0;
    pointer-events: none;
    background:
      linear-gradient(to bottom, rgba(10, 11, 15, 0.72) 0%, rgba(10, 11, 15, 0) 36%),
      linear-gradient(
        to bottom,
        rgba(10, 11, 15, 0) 15%,
        rgba(10, 11, 15, 0.52) 46%,
        rgba(10, 11, 15, 0.88) 72%,
        var(--bg) 90%,
        var(--bg) 100%
      );
  }

  .m-hero__info {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    margin-top: -58px;
    padding: 0 var(--page-x, var(--space-4)) var(--space-2);
  }

  .m-hero__title {
    font-size: var(--fs-28);
    line-height: 1.15;
    font-weight: var(--fw-semibold);
    color: var(--text-1);
    letter-spacing: var(--tracking-tight);
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
  }

  .m-hero__meta {
    font-size: var(--fs-14);
    line-height: var(--lh-14);
    color: var(--text-2);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-variant-numeric: tabular-nums;
  }

  .m-hero__sep {
    display: inline-block;
    padding: 0 0.2em;
  }

  .m-hero__actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-top: var(--space-1);
  }

  .m-hero__btn {
    height: var(--hit, 44px);
    min-height: var(--hit, 44px);
    min-width: var(--hit, 44px);
    padding: 0 var(--space-4);
    border-radius: var(--radius-md);
    font-size: var(--fs-14);
    font-weight: var(--fw-semibold);
  }

  /* Mobile chips row & quiet result caption (§6) */
  .m-browse-bar {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-2) 0 0;
  }

  .m-chips-wrap {
    position: relative;
    width: 100%;
    overflow: hidden;
  }

  .m-chips-wrap::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 32px;
    pointer-events: none;
    background: linear-gradient(to right, transparent, var(--bg) 90%);
  }

  .m-chips {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: var(--space-2);
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    padding: 2px calc(var(--page-x, var(--space-4)) + 16px) 2px var(--page-x, var(--space-4));
  }

  .m-chips::-webkit-scrollbar {
    display: none;
  }

  .m-chip {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex: none;
    height: 40px;
    min-height: 40px;
    min-width: var(--hit, 44px);
    padding: 0 var(--space-3);
    border-radius: var(--radius-full);
    font-size: var(--fs-14);
    font-weight: var(--fw-medium);
    white-space: nowrap;
  }

  .m-chip::before {
    content: '';
    position: absolute;
    inset: -2px 0;
  }

  .m-chip--filters.is-active,
  .m-chip--filters-active {
    border-color: var(--accent);
    background: var(--accent-soft);
    color: var(--text-1);
  }

  .m-chip--active {
    border-color: color-mix(in srgb, var(--accent) 55%, transparent);
    background: var(--accent-soft);
    color: var(--text-1);
    padding-right: var(--space-2);
  }

  .m-chip__x {
    display: inline-grid;
    place-items: center;
    width: 18px;
    height: 18px;
    border-radius: var(--radius-full);
    color: var(--text-2);
  }

  .m-chips-caption {
    padding: 0 var(--page-x, var(--space-4));
    color: var(--text-3);
    font-size: var(--fs-12);
    line-height: var(--lh-12);
    font-variant-numeric: tabular-nums;
  }

  .view__header,
  body.layout-stacked .view__header {
    position: static;
    padding: 0 var(--page-x, var(--space-4));
  }

  .view__body {
    padding: var(--space-4) var(--page-x, var(--space-4));
  }

  .browse-body {
    padding: var(--space-2) var(--page-x, var(--space-4)) calc(var(--tabbar-total, 56px) + var(--space-12));
  }

  .browse-feedback {
    padding: var(--space-2) var(--page-x, var(--space-4)) 0;
  }

  /* Filters sheet & Sort sheet (§6) */
  .dialog__header [data-dialog-close] {
    min-width: var(--hit, 44px);
    min-height: var(--hit, 44px);
  }

  .m-sheet-reset {
    height: var(--hit, 44px);
    min-height: var(--hit, 44px);
    min-width: var(--hit, 44px);
    padding: 0 var(--space-3);
    margin-left: auto;
    margin-right: var(--space-1);
    color: var(--accent);
    font-size: var(--fs-14);
    font-weight: var(--fw-medium);
  }

  .sheet-group {
    margin-bottom: var(--space-5);
  }

  .sheet-group:last-child {
    margin-bottom: var(--space-2);
  }

  .sheet-group__title {
    margin-bottom: var(--space-2);
    color: var(--text-2);
    font-size: var(--fs-14);
    line-height: var(--lh-14);
    font-weight: var(--fw-semibold);
  }

  .sheet-group__chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
  }

  .sheet-group__chips .chip {
    position: relative;
    height: 40px;
    min-height: 40px;
    min-width: var(--hit, 44px);
    padding: 0 var(--space-3);
  }

  .sheet-group__chips .chip::before {
    content: '';
    position: absolute;
    inset: -2px 0;
  }

  .sheet-group__years {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: var(--space-2);
    padding-top: var(--space-2);
    border-top: 1px solid var(--hairline);
  }

  .sheet-group__years .chip {
    position: relative;
    height: 40px;
    min-height: 40px;
    min-width: var(--hit, 44px);
    padding: 0 var(--space-3);
  }

  .sheet-group__years .chip::before {
    content: '';
    position: absolute;
    inset: -2px 0;
  }

  .m-segmented-full {
    display: flex;
    width: 100%;
    min-height: var(--hit, 44px);
    padding: 2px;
  }

  .m-segmented-full .segmented__item {
    position: relative;
    flex: 1 1 0;
    height: 40px;
    min-height: 40px;
    min-width: var(--hit, 44px);
  }

  .m-segmented-full .segmented__item::before {
    content: '';
    position: absolute;
    inset: -2px 0;
  }

  .dialog--m-filters .dialog__footer,
  .m-filters-footer {
    position: sticky;
    bottom: 0;
    z-index: 2;
    display: flex;
    width: 100%;
    padding: var(--space-3) var(--space-4) calc(var(--space-3) + var(--safe-b, 0px));
    background: var(--surface-2);
    border-top: 1px solid var(--hairline);
  }

  .dialog--m-filters .dialog__footer .btn,
  .m-sheet-apply {
    width: 100%;
    height: 48px;
    min-height: 48px;
    border-radius: var(--radius-md);
    font-size: var(--fs-16);
    font-weight: var(--fw-semibold);
  }

  .m-sort-list {
    display: flex;
    flex-direction: column;
    margin: calc(-1 * var(--space-2)) calc(-1 * var(--space-4));
  }

  .m-sort-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    width: 100%;
    min-height: 52px;
    padding: 0 var(--space-4);
    border-bottom: 1px solid var(--hairline);
    color: var(--text-1);
    font-size: var(--fs-16);
    line-height: var(--lh-16);
    font-weight: var(--fw-regular);
  }

  .m-sort-row:last-child {
    border-bottom: 0;
  }

  .m-sort-row.is-checked {
    color: var(--accent);
    font-weight: var(--fw-semibold);
  }

  .m-sort-row__check {
    display: inline-grid;
    place-items: center;
    width: 24px;
    height: 24px;
    color: var(--accent);
    flex: none;
  }

  /* First-run hint on mobile (§6) */
  .m-first-hint {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    margin: var(--space-2) var(--page-x, var(--space-4)) var(--space-1);
    padding: var(--space-1) var(--space-1) var(--space-1) var(--space-3);
    border-radius: var(--radius-md);
    background: var(--surface-1);
    border: 1px solid var(--hairline);
    color: var(--text-2);
    font-size: 0.8125rem;
    line-height: var(--lh-14);
  }

  .m-first-hint[hidden],
  body.has-dock .m-first-hint {
    display: none !important;
  }

  .m-first-hint__close {
    display: inline-grid;
    place-items: center;
    flex: none;
    width: var(--hit, 44px);
    height: var(--hit, 44px);
    min-width: var(--hit, 44px);
    min-height: var(--hit, 44px);
    border-radius: var(--radius-full);
    color: var(--text-2);
  }

  /* Empty state & error panel polish on mobile */
  .empty-state__actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-2);
  }

  .empty-state__actions .btn {
    min-height: var(--hit, 44px);
    min-width: var(--hit, 44px);
    padding: 0 var(--space-4);
  }
}

@media (min-width: 600px) and (max-width: 899px) {
  .m-hero {
    max-height: 360px;
  }

  .m-hero__media {
    aspect-ratio: 2.2 / 1;
    max-height: 270px;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .m-hero {
    max-height: min(56dvh, 160px);
  }

  .m-hero__media {
    aspect-ratio: auto;
    height: 100px;
    max-height: 100px;
  }

  .m-hero__info {
    margin-top: -52px;
    padding: 0 max(var(--page-x, var(--space-4)), var(--safe-r, 0px)) var(--space-1) max(var(--page-x, var(--space-4)), var(--safe-l, 0px));
    gap: 2px;
  }

  .m-hero__title {
    font-size: 1.375rem;
    line-height: 1.15;
    -webkit-line-clamp: 1;
    line-clamp: 1;
  }

  .m-hero__meta {
    font-size: var(--fs-12);
    line-height: var(--lh-12);
  }

  .m-hero__actions {
    margin-top: 4px;
  }

  .m-browse-bar {
    padding-top: var(--space-1);
    padding-left: max(var(--page-x, var(--space-4)), var(--safe-l, 0px));
    padding-right: max(var(--page-x, var(--space-4)), var(--safe-r, 0px));
  }

  .m-chips {
    padding-left: 0;
    padding-right: 16px;
  }

  .m-chips-caption {
    padding-left: 0;
  }

  .m-first-hint {
    margin: var(--space-1) max(var(--page-x, var(--space-4)), var(--safe-r, 0px)) 0 max(var(--page-x, var(--space-4)), var(--safe-l, 0px));
  }

  .browse-body {
    padding-top: var(--space-1);
    padding-left: max(var(--page-x, var(--space-4)), var(--safe-l, 0px));
    padding-right: max(var(--page-x, var(--space-4)), var(--safe-r, 0px));
  }
}
