/* ページ左の目次（サイドバー）のデザイン。toc.js が、目次を作る。
   使いかた：style.css のあとに読み込む（<link rel="stylesheet" href="toc.css">）。色は、サイトの紫に合わせてある */

html {
    scroll-behavior: smooth;   /* 目次を押したとき、なめらかに動く */
}

/* 移動したとき、上のメニューバーの下に、見出しが隠れないようにする */
.content h1[id],
.content h2[id],
.content h3[id] {
    scroll-margin-top: 90px;
}

/* 参考文献：本文の [1] などから文献一覧へ飛ぶとき、文献一覧の「↩ Back to the text」から本文へ戻るとき、
   飛んだ先（引用の位置と、文献）が、上のメニューバーの下に隠れないようにする。
   メニューバーの実際の高さ（画面の幅で、1段にも2段にもなる）に、80px の間を足した値。本文へ戻る先は、その文献が最初に出てくる引用の位置。Simulation には、かけない（toc.js が、ref-margin というクラスを付けたページだけ） */
html.ref-margin [id^="cite-"],
html.ref-margin [id^="ref-"] {
    scroll-margin-top: calc(var(--navbar-h, 100px) + 80px);   /* --navbar-h は toc.js が、メニューバーの実際の高さに合わせて入れる */
}

/* Experimental Details のように、ボックス（section）自体に id があるページ：飛んだとき、ボックスの上の線が、メニューバーの下に来る（見出しが、隠れない） */
.content .content-section.exp-box[id] {
    scroll-margin-top: 60px;
}

/* ---- 目次から飛んだ先の見出し：背景が、黄色っぽく光って、消える ----
   toc.js が、移動が止まったときに、見出しに toc-flash をつける（2.4秒で消える）。枠や影は、つけない（背景の色だけ）。
   背景の色は、0%の行の rgba(255, 214, 64, 0.5) の、最後の数字（0.5）が濃さ。大きいほど、濃く光る。文字の色は、color の行 */
.content .toc-flash {
    border-radius: 6px;
    animation: toc-flash 2.4s ease-out;
}

.content .toc-flashed {
    animation: none;
}

/* 背景は、黄色っぽく光って消える。文字は、文字そのものの色が、濃いオレンジ色（サイトの jump-link と同じ色）になって、しばらく（0%〜55%）そのまま、そのあと、元の色に戻る。
   文字のまわりの、ぼかし（text-shadow）は、つけない（文字の上に、薄いオレンジが乗っているように見えるため）。
   100%の行に、color を書かないと、元の色に戻る。55%の数字を大きくすると、オレンジが、長く続く */
@keyframes toc-flash {
    0% {
        background-color: rgba(255, 214, 64, 0.5);
        color: #b85408;
    }
    55% {
        background-color: rgba(255, 214, 64, 0.35);
        color: #b85408;
    }
    100% {
        background-color: rgba(255, 214, 64, 0);
    }
}

/* ---- 左に目次、右に本文 ---- */
.toc-layout {
    display: grid;
    grid-template-columns: 230px minmax(0, 1fr);
    gap: 48px;
    align-items: start;
    max-width: 1320px;
    margin: 0 auto;
}

.toc-main {
    min-width: 0;   /* 中の動画・図が広くても、横にはみ出さない */
}

/* 「画面いっぱいに広げる」図（.media-wide）は、画面の中央を基準に広がるため、目次があると、右にはみ出す。本文の列の幅に、収める */
.toc-main .section-text .media-wide {
    left: 0;
    width: 100%;
    transform: none;
}

/* スクロールしても、目次は左についてくる */
.toc-side {
    position: sticky;
    top: 62px;                           /* スクロールしたとき、上のメニューバー（高さ60px）の、すぐ下に目次が止まる。数字を大きくすると、すき間が広がる */
    max-height: calc(100vh - 82px);      /* top の数字より、20px大きい値を引く（下に、少し余白を残す） */
    overflow-y: auto;
    padding: 6px 4px 20px 0;
    scrollbar-width: thin;
}

.toc ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

.toc a {
    display: block;
    padding: 8px 14px;
    border-radius: 6px;
    color: #6f6a7c;
    font-size: 15px;
    line-height: 1.35;
    text-decoration: none;
    transition: background-color 0.15s ease, color 0.15s ease;
}

/* 大きな項目（ページのタイトル・大見出し）：太字にして、少し大きく。左は、折りたたみの「›」のための、あき */
.toc .toc-l1 > .toc-row > a {
    padding-left: 32px;
    color: #3f3657;           /* 太字の項目は、小さな項目（#6f6a7c）より、濃い色に */
    font-size: 17px;
    font-weight: 700;
}

.toc .toc-l2 a {
    padding-left: 46px;
    font-size: 13.5px;
}

.toc a:hover {
    background: #f0eaf8;
    color: #2b1055;
}

/* いま読んでいる項目：サイトの紫で、塗りつぶす */
.toc a.is-active,
.toc .toc-l1 > .toc-row > a.is-active {
    background: #2b1055;
    color: #ffffff;           /* 大きな項目の「濃い色」より、優先する（紫の上で読めるように） */
    font-weight: 700;
}

/* 小見出しを読んでいるとき、その親の大見出しは、紫の文字で示す */
.toc .is-parent > .toc-row > a {
    color: #2b1055;
    font-weight: 700;
}

/* ---- 折りたたみ：大きな項目の左の「›」（開いているときは、下向き） ---- */
.toc-row {
    position: relative;
}

.toc-fold {
    position: absolute;
    left: 5px;
    top: 50%;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    margin-top: -12px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: #6f6a7c;
    cursor: pointer;
}

.toc-fold svg {
    transform: rotate(90deg);   /* 開いているとき：下向き */
    transition: transform 0.25s ease;   /* 向きが、くるっと変わる */
}

.toc-l1.is-folded > .toc-row .toc-fold svg {
    transform: none;            /* 閉じているとき：右向き */
}

.toc-fold:hover {
    background: #e2d8f2;
    color: #2b1055;
}

.toc-fold:focus-visible {
    outline: 3px solid #8650bd;
    outline-offset: 1px;
}

.toc-row.is-current .toc-fold {
    color: #ffffff;            /* 読んでいる項目（紫の塗りつぶし）の上でも、見えるように */
}

.toc-row.is-current .toc-fold:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* 小見出しの折りたたみ：高さが、0 までなめらかに縮む（開くときは、逆）。かくれている間は、キーボードでも選べない */
.toc-sub-wrap {
    display: grid;
    grid-template-rows: 1fr;
    transition: grid-template-rows 0.28s ease;
}

.toc-sub {
    min-height: 0;
    overflow: hidden;
    opacity: 1;
    visibility: visible;
    transition: opacity 0.2s ease;
}

.toc-l1.is-folded > .toc-sub-wrap {
    grid-template-rows: 0fr;
}

.toc-l1.is-folded > .toc-sub-wrap > .toc-sub {
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0s 0.28s;   /* 縮み終わってから、かくす */
}

/* ---- 目次のいちばん上の行：左に「‹」（閉じる）、右に「⋮」（すべて開く／すべて閉じる） ---- */
.toc-tools {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 2px;
    padding-top: 6px;
}

/* 「‹」：すぐ下の、項目の左の矢印（›）（左から5px、幅24px）と、同じ縦の線にそろう（左から3px、幅28px） */
.toc-close {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    margin-left: -3px;                 /* 中心の位置（左から17px）は、そのまま。大きくした分だけ、左へ出す */
    padding: 0;
    border: 2.5px solid transparent;   /* ふだんは、枠なし。カーソルを重ねたときだけ、枠が出る */
    border-radius: 10px;               /* 角の丸い四角（円にしたいときは 50%） */
    background: transparent;
    color: #2b1055;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.toc-close:hover,
.toc-close:focus-visible {
    border-color: #2b1055;
    background: #f0eaf8;
    outline: none;
}

.toc-close svg {
    width: 22px;
    height: 22px;
}

/* 説明の文章（「Hide contents」「Back to top」など）：カーソルを重ねたとき（キーボードで選んだときも）だけ、ボタンの右に、吹き出しで出る。
   ::after が、吹き出しの四角、::before が、ボタンを指す「しっぽ」（三角）。「‹」・「↑」・閉じているときの「開く」ボタンで、同じ形 */
.toc-close::before,
.toc-top::before,
.toc-collapse::before {
    content: "";
    position: absolute;
    left: calc(100% + 2px);
    top: 50%;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 6px 6px 6px 0;
    border-color: transparent #1c1c24 transparent transparent;   /* 左向きの三角（吹き出しと同じ色） */
    pointer-events: none;
    opacity: 0;
    transform: translateY(-50%);
    transition: opacity 0.15s ease;
}

.toc-close:hover::before,
.toc-close:focus-visible::before,
.toc-top:hover::before,
.toc-top:focus-visible::before,
.toc-collapse:hover::before,
.toc-collapse:focus-visible::before {
    opacity: 1;
}

.toc-close::after,
.toc-top::after {
    content: attr(data-tip);
    position: absolute;
    left: calc(100% + 8px);
    top: 50%;
    padding: 6px 12px;
    border-radius: 8px;
    background: #1c1c24;
    color: #ffffff;
    font: 600 13px/1.3 "Helvetica Neue", Arial, sans-serif;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transform: translateY(-50%);
    transition: opacity 0.15s ease;
}

.toc-close:hover::after,
.toc-close:focus-visible::after,
.toc-top:hover::after,
.toc-top:focus-visible::after {
    opacity: 1;
}

.toc-menu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: 1.5px solid transparent;
    border-radius: 50%;
    background: transparent;
    color: #4a4560;
    cursor: pointer;
}

.toc-menu-btn:hover,
.toc-menu-btn[aria-expanded="true"] {
    background: #f0eaf8;
    color: #2b1055;
}

.toc-menu-btn:focus-visible {
    border-color: #8650bd;
    outline: none;
}

.toc-menu {
    position: absolute;
    top: 100%;
    right: 0;
    z-index: 5;
    min-width: 176px;
    padding: 6px;
    border: 1px solid #e3dff0;
    border-radius: 10px;
    background: #ffffff;
    box-shadow: 0 6px 20px rgba(43, 16, 85, 0.18);
}

.toc-menu[hidden] {
    display: none;
}

.toc-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 9px 12px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: #2b2540;
    font: 500 14px/1.2 "Helvetica Neue", Arial, sans-serif;
    text-align: left;
    cursor: pointer;
}

.toc-menu-item:hover,
.toc-menu-item:focus-visible {
    background: #f0eaf8;
    color: #2b1055;
    outline: none;
}

/* いちばん下の「↑」：ページの先頭へ */
.toc-top {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    margin: 22px 0 0 4px;
    border: 2px solid transparent;   /* ふだんは、枠なし（「↑」だけ）。カーソルを重ねたときだけ、枠が出る */
    border-radius: 8px;
    color: #2b1055;
    background: transparent;
    transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.toc-top:hover,
.toc-top:focus-visible {
    border-color: #2b1055;
    background: #f0eaf8;
    color: #2b1055;
}

/* ---- 広い画面：目次を閉じる／開くボタン（1つのボタンで切り替える） ---- */
.toc-collapse {
    position: fixed;
    left: 6px;
    top: 74px;
    z-index: 900;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    padding: 0;
    border: 1.5px solid transparent;   /* ふだんは、枠なし（「‹」だけ）。カーソルを重ねたときだけ、枠が出る */
    border-radius: 50%;
    background: transparent;
    color: #2b1055;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.toc-collapse:hover,
.toc-collapse:focus-visible {
    border-color: #2b1055;
    background: #f0eaf8;
    color: #2b1055;
}

.toc-collapse:focus-visible {
    outline: 3px solid #8650bd;
    outline-offset: 2px;
}

/* 閉じているとき：「開く」ボタンは、文字なしの、アイコンだけ（点つきリストの絵）。
   形は「D」：画面の左はしから出ていて、上と下は直線、右側だけが丸い。うすい紫の塗り＋紫の太い枠で、影はつけない（平らな見た目） */
.toc-collapse.is-collapsed {
    left: 0;
    top: 74px;
    width: 50px;
    height: 56px;
    padding: 0;
    border: 3px solid #8650bd;
    border-left: none;                 /* 左は、画面のはしなので、枠をつけない */
    border-radius: 0 28px 28px 0;      /* 左上・左下は、直角（直線）。右だけ丸い。丸みは、28pxを小さくすると、角ばる */
    background: #e6dff3;
    color: #2b1055;
    box-shadow: none;
}

.toc-collapse.is-collapsed svg {
    width: 22px;
    height: 22px;
}

.toc-collapse.is-collapsed:hover,
.toc-collapse.is-collapsed:focus-visible {
    background: #d3c6ec;
    border-color: #2b1055;
    color: #2b1055;
}

/* 説明の文章（「Show contents」など）：カーソルを重ねたとき（キーボードで選んだときも）だけ、ボタンの右に出る */
.toc-collapse::after {
    content: attr(data-tip);
    position: absolute;
    left: calc(100% + 8px);
    top: 50%;
    padding: 6px 12px;
    border-radius: 8px;
    background: #1c1c24;
    color: #ffffff;
    font: 600 13px/1.3 "Helvetica Neue", Arial, sans-serif;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transform: translateY(-50%);
    transition: opacity 0.15s ease;
}

.toc-collapse:hover::after,
.toc-collapse:focus-visible::after {
    opacity: 1;
}

/* 画面に固定した「開く」ボタンは、閉じているときだけ出す（開いているときは、目次の中の「‹」を使う） */
.toc-collapse:not(.is-collapsed) {
    display: none;
}

/* 閉じているとき：目次を、左へ滑らせて消し、本文を、横いっぱいに広げる（広い画面だけ。どちらも、なめらかに動く）。
   目次の幅は、動かさずに、そのまま左へ出ていく（文字が折り返したりしないように） */
@media (min-width: 1000px) {
    .toc-layout {
        transition: grid-template-columns 0.3s ease, gap 0.3s ease;
    }

    /* 目次を、左へ28px寄せて、画面の左はしの、むだな空きをなくす（右はしの位置は、そのまま。幅を、28px広げる） */
    .toc-side {
        width: 261px;
        margin-left: -31px;      /* 28px寄せる＋「‹」の枠が切れないための3px。中身は、padding-left の3pxで、元の位置に戻す */
        margin-top: -32px;       /* 上の帯と目次のあいだの空白（帯の下の余白40px）をつめる。数字を大きく（-40pxなど）すると、もっとくっつく。本文の位置は、変わらない */
        padding-left: 3px;
        box-sizing: border-box;
        transition: transform 0.3s ease, opacity 0.25s ease;
    }

    /* いちばん上の行（「‹」「⋮」）は、目次の中だけがスクロールされても、上に残る */
    .toc-side {
        padding-top: 0;
    }

    .toc-tools {
        position: sticky;
        top: 0;
        z-index: 3;
        background: #fafafa;     /* ページの背景色（style.css の body と同じ） */
    }

    .toc-layout.is-collapsed {
        grid-template-columns: 0px minmax(0, 1fr);
        gap: 0;
    }

    .toc-layout.is-collapsed .toc-side {
        transform: translateX(-320px);
        opacity: 0;
        visibility: hidden;          /* 消えたあとは、キーボードでも選べない */
        pointer-events: none;
        transition: transform 0.3s ease, opacity 0.25s ease, visibility 0s 0.3s;
    }
}

/* 狭い画面用のボタン・背景は、広い画面では出さない */
.toc-toggle,
.toc-backdrop {
    display: none;
}

/* ---- 狭い画面（スマホ・小さいタブレット）：「Contents」ボタンで、横から開く ---- */
@media (max-width: 999px) {
    .toc-layout {
        display: block;
    }

    .toc-collapse {
        display: none;   /* 狭い画面は、「Contents」ボタンを使う */
    }

    .toc-side {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        z-index: 1300;
        box-sizing: border-box;
        width: min(82vw, 320px);
        max-height: none;
        padding: 76px 18px 28px 18px;
        background: #ffffff;
        box-shadow: 4px 0 24px rgba(43, 16, 85, 0.25);
        transform: translateX(-105%);
        transition: transform 0.25s ease;
    }

    .toc-side.is-open {
        transform: translateX(0);
    }

    .toc-toggle {
        position: fixed;
        left: 14px;
        bottom: 16px;
        z-index: 1200;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 11px 16px;
        border: none;
        border-radius: 999px;
        background: #2b1055;
        color: #ffffff;
        font-size: 14px;
        font-weight: 700;
        box-shadow: 0 6px 16px rgba(43, 16, 85, 0.35);
        cursor: pointer;
    }

    .toc-backdrop {
        position: fixed;
        inset: 0;
        z-index: 1250;
        display: block;
        background: rgba(20, 8, 40, 0.45);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
    }

    .toc-backdrop.is-open {
        opacity: 1;
        pointer-events: auto;
    }

    .content h1[id],
    .content h2[id],
    .content h3[id] {
        scroll-margin-top: 120px;   /* スマホは、メニューバーが2段になって、高いため */
    }

    .content .content-section.exp-box[id] {
        scroll-margin-top: 90px;
    }
}

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    .content .toc-flash {
        animation: none;   /* 動きを減らす設定の人には、光らせない */
    }

    .toc-layout,
    .toc-side,
    .toc-layout.is-collapsed .toc-side,
    .toc-backdrop,
    .toc a,
    .toc-sub-wrap,
    .toc-sub,
    .toc-l1.is-folded > .toc-sub-wrap > .toc-sub,
    .toc-fold svg {
        transition: none;
    }
}

/* 目次の一番下の紫のボタン（<main> の data-toc-link-href で出す。toc.js）。「↑」の右となりに並ぶ。目次の中身なので、目次といっしょに動く */
.toc-bottom-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin: 22px 0 0 4px;
}

.toc-bottom-row .toc-top {
    margin: 0;
    flex: none;
}

.toc-side a.toc-action {
    display: inline-block;
    padding: 9px 14px;
    border-radius: 999px;
    background: #2b1055;
    color: #ffffff;
    font-family: 'Helvetica Neue', Arial, sans-serif;
    font-size: 13px;
    font-weight: 700;
    text-decoration: none;
    white-space: nowrap;
    box-shadow: 0 4px 12px rgba(43, 16, 85, 0.3);
}

.toc-side a.toc-action:hover,
.toc-side a.toc-action:focus-visible {
    background: #4a2a85;
    color: #ffffff;
}
