/**
 * Front styles for buddica/service-directory.
 *
 * @package BUDDICA\Blocks
 */

@scope (.scoped.service-directory) to (.scoped) {
  @container style(--screen: --compact) {
    buddica-service-filter[data-state="floating"] ._surface {
      position: fixed;
      inset-inline: calc(20 * var(--px));
      inset-block-end: calc(20 * var(--px) + env(safe-area-inset-bottom));
    }

    /* ホストが isolation: isolate で重なり文脈を作るため、z-index はホストへ指定する */
    buddica-service-filter:is([data-state="floating"], [data-state="docked"]) {
      position: relative;
      z-index: calc(var(--wp--custom--z--footer) + 1);
    }

    /* --_dock-* は service-filter.ts がホスト基準の補正量を px で書き込む */
    buddica-service-filter[data-state="docked"] ._surface {
      position: absolute;
      inset-inline: calc(var(--_dock-inline-start) + 20 * var(--px)) calc(var(--_dock-inline-end) + 20 * var(--px));
      inset-block-end: calc(var(--_dock-block-end) + 20 * var(--px));
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    @container style(--screen: --spacious) {
      ._list::before {
        transition:
          inset 0.6s var(--wp--custom--ease--quart-in-out),
          width 0.6s var(--wp--custom--ease--quart-in-out);
      }

      ._text {
        transition:
          opacity var(--wp--custom--duration--slow) ease,
          color var(--wp--custom--duration--slow) var(--wp--custom--duration--slow) ease;
      }

      ._dot {
        transition:
          border-color var(--wp--custom--duration--slow) ease var(--wp--custom--duration--slow),
          background-color var(--wp--custom--duration--slow) ease var(--wp--custom--duration--slow);
      }
    }

    @container style(--screen: --compact) {
      ._tag[aria-pressed="true"] {
        transition:
          background-color var(--wp--custom--duration--slow) ease,
          box-shadow var(--wp--custom--duration--slow) ease;
      }

      ._text {
        transition: color var(--wp--custom--duration--slow) ease;
      }

      ._dot {
        transition:
          border-color var(--wp--custom--duration--slow) ease,
          background-color var(--wp--custom--duration--slow) ease;
      }
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    ._item {
      opacity: 0;
      translate: 0 calc(40 * var(--px));
      transition-duration: 0.8s;
      transition-property: opacity, translate;
      transition-timing-function: var(--wp--custom--ease--quart-out);
    }

    ._item.is-inView {
      opacity: 1;
      translate: none;
    }

    ._item:hover ._arrow::before {
      animation: --service-directory-arrow-out var(--wp--custom--duration--slow) var(--wp--custom--ease--quad-out)
        forwards;
    }

    ._item:hover ._arrow::after {
      animation: --service-directory-arrow-in var(--wp--custom--duration--slow) var(--wp--custom--ease--quad-out)
        forwards;
    }
  }

  /* 一覧のみ表示のカード（リンクなし）は操作できないため、ホバー演出を付けない。 */
  @media (any-hover: hover) {
    ._item:has(:any-link):hover ._thumbnail img {
      transform: scale(var(--wp--custom--scale--hover));
    }

    ._item:hover ._arrow {
      --_background: #4b1800;
    }
  }

  :scope {
    display: block flex;
    flex-direction: column;
    row-gap: calc(50 * var(--px));
    color: var(--wp--preset--color--ink);
  }

  buddica-service-filter {
    display: block flex;
    justify-content: center;
    inline-size: 100%;
    isolation: isolate;

    @container style(--screen: --compact) {
      min-block-size: calc(8 * var(--px) * 2 + 11 * var(--rem) * 1.9);
    }

    @container style(--screen: --spacious) {
      min-block-size: calc(7 * var(--px) * 2 + 12 * var(--rem) * 1.9);
    }
  }

  ._surface {
    display: block flex;
    align-items: center;
    border: 1px solid rgb(255 255 255 / 10%);
    border-radius: var(--wp--custom--rounded--full);
    background-color: #333;
    color: var(--wp--preset--color--white);

    @container style(--screen: --compact) {
      padding-block: calc(8 * var(--px));
      padding-inline: calc(15 * var(--px)) calc(20 * var(--px));
      backdrop-filter: blur(5px);
    }

    @container style(--screen: --spacious) {
      gap: calc(10 * var(--px));
      inline-size: fit-content;
      padding-block: calc(7 * var(--px));
      padding-inline: calc(20 * var(--px));
      backdrop-filter: blur(10px);
    }
  }

  ._label {
    flex-shrink: 0;
    margin: 0;
    font-family: var(--wp--preset--font-family--sans-en);
    font-weight: var(--wp--custom--font-weight--medium);
    line-height: 0.9;

    @container style(--screen: --compact) {
      font-size: calc(11 * var(--rem));
      letter-spacing: calc(-0.11 / 11 * 1em);
    }

    @container style(--screen: --spacious) {
      font-size: calc(12 * var(--rem));
      letter-spacing: calc(-0.12 / 12 * 1em);
    }
  }

  ._list {
    position: relative;
    display: block flex;
    align-items: center;

    @container style(--screen: --compact) {
      gap: calc(6 * var(--px));
      padding-inline-start: calc(10 * var(--px));
    }

    @container style(--screen: --spacious) {
      gap: calc(2 * var(--px));
    }

    &::before {
      content: "";
      position: absolute;
      inset: anchor(--tag start);
      display: block flow;
      inline-size: anchor-size(--tag inline);
      block-size: anchor-size(--tag block);
      border-radius: var(--wp--custom--rounded--full);
      box-shadow: 0 0 5px rgb(0 0 0 / 5%);
      background-color: var(--wp--preset--color--white);
      pointer-events: none;
    }
  }

  ._container {
    @container style(--screen: --compact) {
      overflow-x: auto;
      overscroll-behavior-inline: contain;
      -webkit-mask-image: linear-gradient(
        to right,
        transparent,
        #000 calc(10 * var(--px)),
        #000 calc(100% - 20 * var(--px)),
        transparent
      );
              mask-image: linear-gradient(
        to right,
        transparent,
        #000 calc(10 * var(--px)),
        #000 calc(100% - 20 * var(--px)),
        transparent
      );
      margin-block: -6px;
      padding-block: 6px;
      scrollbar-width: none;

      &::-webkit-scrollbar {
        display: none;
      }
    }
  }

  ._button {
    flex-shrink: 0;

    @container style(--screen: --compact) {
      &:last-child {
        padding-inline-end: calc(15 * var(--px));
      }
    }
  }

  ._tag {
    position: relative;
    display: block flex;
    gap: calc(5 * var(--px));
    align-items: center;
    border: none;
    border-radius: var(--wp--custom--rounded--full);
    background-color: transparent;
    color: currentColor;
    font-family: var(--wp--preset--font-family--sans-jp);
    font-weight: var(--wp--custom--font-weight--semibold);
    line-height: 1.9;
    text-transform: capitalize;
    cursor: pointer;

    @container style(--screen: --compact) {
      padding-block: calc(3 * var(--px)) calc(2 * var(--px));
      padding-inline: calc(8 * var(--px)) calc(10 * var(--px));
      font-size: calc(11 * var(--rem));
    }

    @container style(--screen: --spacious) {
      padding-block: calc(3 * var(--px));
      padding-inline: calc(10 * var(--px)) calc(14 * var(--px));
      font-size: calc(12 * var(--rem));
    }

    &[aria-pressed="true"] {
      @supports not (anchor-name: --a) {
        box-shadow: 0 0 5px rgb(0 0 0 / 5%);
        background-color: var(--wp--preset--color--white);
      }

      @container style(--screen: --compact) {
        box-shadow: 0 0 5px rgb(0 0 0 / 5%);
        background-color: var(--wp--preset--color--white);
      }

      @container style(--screen: --spacious) {
        anchor-name: --tag;
      }
    }

    &[aria-pressed="true"] ._dot {
      border-color: var(--wp--preset--color--keycolor);
      background-color: var(--wp--preset--color--keycolor);
    }

    &[aria-pressed="true"] ._text {
      color: var(--wp--preset--color--ink);
    }

    &:focus-visible {
      outline: 2px solid var(--wp--preset--color--keycolor);
      outline-offset: 2px;
    }
  }

  ._tag:hover:not([aria-pressed="true"]) ._text {
    @media (any-hover: hover) {
      opacity: 0.5;
    }
  }

  ._dot {
    flex-shrink: 0;
    inline-size: calc(10 * var(--px));
    aspect-ratio: 1;
    border: 1px solid rgb(255 255 255 / 40%);
    border-radius: 50%;
    background-color: transparent;
  }

  /* JS未定義・無効時はタグバーを隠し、全カードを表示したままにする
     （押せないボタンを見せない。フィルタ機構全体がprogressive
     enhancementのため、カード側の`hidden`切替はJSが動いたときだけ発生し、
     JS未実行なら`_grid`の子要素に`hidden`は一切付かない）。 */
  buddica-service-filter:not(:defined) {
    display: none;
  }

  /* ==----------------------------------------------------
   * カードグリッド
   * ---------------------------------------------------- */

  ._grid {
    display: block grid;

    @container style(--screen: --compact) {
      row-gap: calc(15 * var(--px));
    }

    @container style(--screen: --spacious) {
      grid-template-columns: repeat(2, 1fr);
      gap: calc(30 * var(--px));
    }
  }

  ._item {
    position: relative;
    display: block flex;
    flex-direction: column;
    border: 1px solid oklch(from var(--wp--preset--color--ink) l c h / 10%);
    border-radius: var(--wp--custom--rounded--sm);
    isolation: isolate;

    @container style(--screen: --compact) {
      gap: calc(20 * var(--px));
      padding: calc(20 * var(--px));
    }

    @container style(--screen: --spacious) {
      gap: calc(25 * var(--px));
      padding: calc(30 * var(--px));
    }

    &[hidden] {
      display: none;
    }
  }

  ._thumbnail {
    overflow: clip;
    border-radius: var(--wp--custom--rounded--sm);
    background-color: var(--wp--preset--color--surface-muted);

    & img {
      display: block flow;
      inline-size: 100%;
      aspect-ratio: 3 / 2;
      object-fit: cover;

      @media (prefers-reduced-motion: no-preference) {
        transition-duration: var(--wp--custom--duration--slow);
        transition-property: transform;
        transition-timing-function: var(--wp--custom--ease--quad-out);
      }
    }
  }

  ._body {
    display: block flex;
    flex-direction: column;

    @container style(--screen: --compact) {
      gap: calc(15 * var(--px));
    }

    @container style(--screen: --spacious) {
      gap: calc(20 * var(--px));
    }
  }

  ._head {
    display: block flex;
    gap: calc(10 * var(--px));
    justify-content: space-between;
    align-items: center;
  }

  ._head-brand {
    display: block flex;
    min-inline-size: 0;

    @container style(--screen: --compact) {
      gap: calc(10 * var(--px));
    }

    @container style(--screen: --spacious) {
      gap: calc(15 * var(--px));
    }
  }

  ._logo {
    flex-shrink: 0;
    overflow: clip;

    @container style(--screen: --compact) {
      inline-size: calc(40 * var(--px));
      block-size: calc(40 * var(--px));
    }

    @container style(--screen: --spacious) {
      inline-size: calc(43 * var(--px));
      block-size: calc(43 * var(--px));
    }

    & img {
      display: block flow;
      inline-size: 100%;
      block-size: 100%;
      object-fit: contain;
    }
  }

  ._name {
    display: block flex;
    align-items: center;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0;
    padding-inline-start: calc(15 * var(--px));
    border-inline-start: rgb(4 4 4 / 10%) 1px solid;
    font-family: var(--wp--preset--font-family--sans-en);
    font-weight: var(--wp--custom--font-weight--medium);
    line-height: 0.9;
    letter-spacing: var(--wp--custom--tracking--tight);
    white-space: nowrap;

    @container style(--screen: --compact) {
      font-size: calc(18 * var(--rem));
    }

    @container style(--screen: --spacious) {
      font-size: calc(25 * var(--rem));
    }
  }

  /* 一覧のみ表示のブランドはリンクでなく `<span>` になる。 */
  ._name > * {
    overflow-inline: clip;
    text-overflow: ellipsis;
    white-space: nowrap;

    @supports not (overflow-inline: clip) {
      overflow-x: clip;
    }
  }

  ._name a {
    color: currentColor;
    text-decoration: none;

    /* リンクをカード全体へ広げる擬似要素。 */
    &::after {
      content: "";
      position: absolute;
      inset: 0;
    }

    &:focus-visible {
      outline: 2px solid var(--wp--preset--color--keycolor);
      outline-offset: 2px;
    }
  }

  ._arrow {
    --_background: var(--wp--preset--color--keycolor);

    display: block grid;
    flex-shrink: 0;
    place-items: center;
    overflow: clip;
    inline-size: calc(28 * var(--px));
    block-size: calc(28 * var(--px));
    border-radius: var(--wp--custom--rounded--sm);
    background-color: var(--_background);

    @media (prefers-reduced-motion: no-preference) {
      transition-duration: var(--wp--custom--duration--slow);
      transition-property: background-color;
      transition-timing-function: var(--wp--custom--ease--quad-out);
    }

    &::before,
    &::after {
      content: "";
      display: block flow;
      grid-area: 1 / 1;
      -webkit-mask-image: var(--arrow--action);
              mask-image: var(--arrow--action);
      -webkit-mask-repeat: no-repeat;
              mask-repeat: no-repeat;
      -webkit-mask-size: contain;
              mask-size: contain;
      inline-size: calc(14 * var(--px));
      block-size: calc(14 * var(--px));
      background-color: var(--wp--preset--color--white);
    }

    &::after {
      translate: -150% 0;
    }
  }

  ._description {
    margin: 0;
    color: var(--wp--preset--color--ink);
    font-weight: var(--wp--custom--font-weight--medium);
    line-height: 1.9;

    @container style(--screen: --compact) {
      font-size: calc(12 * var(--rem));
    }

    @container style(--screen: --spacious) {
      font-size: calc(13 * var(--rem));
    }
  }

  ._empty {
    margin: 0;
    color: oklch(from var(--wp--preset--color--ink) l c h / 60%);
    text-align: center;
  }
}

@keyframes --service-directory-arrow-out {
  to {
    translate: 150% 0;
  }
}

@keyframes --service-directory-arrow-in {
  to {
    translate: 0 0;
  }
}

