/* ============================================================
   gallery.css —— 图库页专用版式
   骨架（顶栏 / 页脚 / 颜色变量）全部来自 tokens.css + base.css，
   这里只管照片墙本身：筛选区、网格、灯箱。
   ============================================================ */

.page--gallery {
    /* 图库要比正文宽：正文 1040px，照片墙最多到 1320px */
    --wrap-max: 1320px;
    --masthead-h: 64px;
}

.page--gallery.wrap {
    max-width: var(--wrap-max);
    /* clip 比 hidden 更彻底：不产生滚动容器，同时把 scrollWidth 也收回来，
       否则筛选行的横向滚动区会把「页面能横向滚」这件事暴露出去 */
    overflow-x: clip;
    overflow-y: visible;
    width: 100%;
}

/* ================= 页面根：横向绝不溢出 =================
   筛选区行内有横向滚动条，个别元素会被内容顶宽；
   在这里一次性兜住，免得手机上整页能左右拖。 */
html,
body {
    max-width: 100%;
    overflow-x: hidden;
}

/* ================= 页头 ================= */
.page--gallery .page-title {
    font-family: var(--serif);
    font-size: clamp(36px, 5.4vw, 56px);
    font-weight: 400;
    letter-spacing: .06em;
    line-height: 1.1;
}

.page--gallery .page-meta b {
    color: var(--ink);
    font-weight: 400;
}

/* ================= 筛选区：三行，行内横向滚动，绝不折行 =================
   折行会让这块越来越高，把照片挤出屏幕；所以每行固定一行高，
   放不下的部分横向滚动，右侧渐隐提示「还有东西」。 */
.filters {
    position: sticky;
    top: var(--masthead-h);
    z-index: 9;
    background: var(--paper);
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    margin: 0 calc(-1 * var(--sp-5)) var(--sp-5);
    padding: 0 var(--sp-5);
    max-width: 100vw;
    overflow: hidden;
    transition: background-color .25s var(--ease);
}

.filters__row {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    min-height: 40px;
    padding: var(--sp-1) 0;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
}

.filters__row + .filters__row {
    border-top: 1px solid var(--line);
}

.filters__label {
    flex: 0 0 auto;
    width: 3.2em;
    font-family: var(--mono);
    font-size: var(--fs-meta);
    letter-spacing: .06em;
    color: var(--ink-soft);
}

.rail {
    flex: 1 1 0;
    min-width: 0;
    max-width: 100%;
    display: flex;
    align-items: center;
    gap: var(--sp-1);
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent 100%);
    padding-right: 28px;
}

.rail::-webkit-scrollbar {
    display: none;
}

.rail[data-noscroll="1"] {
    mask-image: none;
    padding-right: 0;
}

.chip {
    font: inherit;
    font-size: var(--fs-sm);
    line-height: 1.4;
    color: var(--ink-soft);
    background: none;
    border: 1px solid transparent;
    border-radius: var(--radius);
    padding: 4px 9px;
    cursor: pointer;
    white-space: nowrap;
    flex: 0 0 auto;
    transition: color .15s var(--ease), border-color .15s var(--ease), background-color .15s var(--ease);
}

.chip:hover {
    color: var(--ink);
}

.chip[aria-pressed="true"] {
    color: var(--ink);
    border-color: var(--line-strong);
    background: var(--paper-sunk);
}

.chip:disabled {
    opacity: .32;
    cursor: default;
}

.chip:disabled:hover {
    color: var(--ink-soft);
}

.chip__n {
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
    margin-left: 3px;
}

.chip[aria-pressed="true"] .chip__n {
    color: var(--ink);
}

.chip[data-kind="tag"][aria-pressed="true"] {
    color: var(--accent);
    border-color: color-mix(in srgb, var(--accent) 40%, transparent);
    background: var(--accent-wash);
}

.chip[data-kind="tag"][aria-pressed="true"] .chip__n {
    color: var(--accent);
}

/* 组合筛选摘要：没选东西时整行不出现，省高度 */
.summary {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
    padding: var(--sp-2) 0;
    border-top: 1px solid var(--line);
    min-height: 34px;
    flex-wrap: wrap;
}

.summary[hidden] {
    display: none;
}

.summary b {
    color: var(--ink);
    font-weight: 400;
}

.clear {
    font: inherit;
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
    background: none;
    border: 0;
    border-bottom: 1px solid var(--line-strong);
    padding: 2px 0 1px;
    cursor: pointer;
    margin-left: auto;
}

.clear:hover {
    color: var(--ink);
}

/* ================= 连续网格 ================= */
.grid {
    --gap: var(--sp-3);
    display: grid;
    /* 用 calc 显式算列宽：fr 在窄屏被内容撑开过，这里不给它机会 */
    grid-template-columns: repeat(3, calc((100% - 2 * var(--gap)) / 3));
    gap: var(--gap);
    /* 手机上只要有一个子孙把宽度撑开，整页就会横向溢出；
       把网格锁死在容器宽度内，配合 .shot 的 min-width:0 就不会跑出去 */
    width: 100%;
    max-width: 100%;
    min-width: 0;
    /* 不让内容撑出隐形轨道 */
    grid-auto-columns: 0;
}

.grid > .empty {
    grid-column: 1 / -1;
}

@media (min-width: 1280px) {
    .grid {
        grid-template-columns: repeat(4, calc((100% - 3 * var(--gap)) / 4));
    /* 不让内容撑出隐形轨道 */
    grid-auto-columns: 0;
}
}

.shot {
    margin: 0;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow: hidden;
    /* grid item 默认 min-width:auto：卡片里 nowrap 的标题会把格子顶宽，
       列宽跟着失控、整片网格超出视口，这里钉死内联尺寸计算 */
    contain: inline-size;
}

.shot__btn {
    display: block;
    width: 100%;
    padding: 0;
    border: 0;
    background: var(--paper-sunk);
    cursor: zoom-in;
    position: relative;
    overflow: hidden;
}

.shot__img {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 3 / 2;
    object-fit: cover;
    transition: transform .5s var(--ease), opacity .4s var(--ease);
}

/* 图还没到之前先占住位置，避免滚动时整页跳动 */
.shot__img[data-loading] {
    opacity: 0;
}

.shot__img[data-ready] {
    opacity: 1;
}

.shot__btn:hover .shot__img {
    transform: scale(1.03);
}

.shot__badge {
    position: absolute;
    left: 0;
    bottom: 0;
    max-width: 100%;
    padding: 3px 8px;
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: #f7f4ed;
    background: rgba(23, 20, 15, .62);
    opacity: 0;
    transition: opacity .2s var(--ease);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.shot__btn:hover .shot__badge,
.shot__btn:focus-visible .shot__badge {
    opacity: 1;
}

/* 卡片说明：第一行作品名，第二行「日期 / 城市」 */
.shot__meta {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding-top: 6px;
    min-width: 0;
}

.shot__title {
    font-family: var(--serif);
    font-size: var(--fs-sm);
    line-height: 1.35;
    color: var(--ink);
    /* 长作品名不能把格子顶宽：省略号截断 */
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    /* 长作品名用省略号，不许把格子撑开 */
    min-width: 0;
    display: block;
}

.shot__sub {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    min-width: 0;
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
    min-width: 0;
    min-width: 0;
}

.shot__place {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--ink-soft);
}

/* ================= 分批加载 ================= */
.sentinel {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-3);
    padding: var(--sp-6) 0 var(--sp-7);
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
}

.sentinel[hidden] {
    display: none;
}

.sentinel__bar {
    width: 120px;
    height: 2px;
    background: var(--line);
    position: relative;
    overflow: hidden;
}

.sentinel__bar i {
    position: absolute;
    inset: 0 auto 0 0;
    width: 40%;
    background: var(--line-strong);
    animation: sentinel-slide 1.1s ease-in-out infinite;
}

@keyframes sentinel-slide {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(250%); }
}

.empty {
    padding: var(--sp-8) var(--sp-4);
    text-align: center;
    color: var(--ink-soft);
    font-size: var(--fs-sm);
    border: 1px dashed var(--line);
}

/* ================= 灯箱 ================= */
dialog.lightbox {
    /* 显式居中：不管祖先有没有 transform，都不能让它跑到左上角。
       inset:0 + margin:auto 是让 <dialog> 居中的唯一可靠写法，
       光靠浏览器默认的居中在 main.wrap 里会被算歪。 */
    position: fixed;
    inset: 0;
    margin: auto;
    border: 0;
    padding: 0;
    background: var(--paper);
    color: var(--ink);
    max-width: min(1120px, 94vw);
    width: 100%;
    max-height: 94dvh;
    box-shadow: 0 24px 64px rgba(0, 0, 0, .28);
}

dialog.lightbox::backdrop {
    background: rgba(20, 18, 14, .78);
}

.lightbox__grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 300px;
}

.lightbox__stage {
    background: var(--paper-sunk);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    min-height: 320px;
    overflow: hidden;
}

.lightbox__stage img {
    display: block;
    max-width: 100%;
    max-height: 86dvh;
    object-fit: contain;
}

.lightbox__nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--line-strong);
    background: var(--paper);
    color: var(--ink);
    cursor: pointer;
    font-size: 16px;
    display: grid;
    place-items: center;
}

.lightbox__nav:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.lightbox__nav[data-dir="prev"] {
    left: var(--sp-3);
}

.lightbox__nav[data-dir="next"] {
    right: var(--sp-3);
}

.lightbox__side {
    padding: var(--sp-5);
    border-left: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    gap: var(--sp-3);
    overflow-y: auto;
    max-height: 94dvh;
}

.lightbox__close {
    align-self: flex-end;
    width: 32px;
    height: 32px;
    border: 1px solid var(--line);
    background: none;
    color: var(--ink);
    cursor: pointer;
    font-size: 13px;
    line-height: 1;
}

.lightbox__close:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.lightbox__idx {
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
}

.lightbox__where {
    font-family: var(--serif);
    font-size: 22px;
    font-weight: 400;
    margin: 0;
    line-height: 1.3;
}

.lightbox__when {
    margin: 0;
    font-family: var(--mono);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
}

.exif {
    margin: 0;
    display: grid;
    grid-template-columns: 58px minmax(0, 1fr);
    gap: 4px var(--sp-3);
    font-size: var(--fs-meta);
    border-top: 1px solid var(--line);
    padding-top: var(--sp-3);
}

.exif dt {
    color: var(--ink-soft);
}

.exif dd {
    margin: 0;
    font-family: var(--mono);
    overflow-wrap: anywhere;
}

.lightbox__tags {
    display: flex;
    gap: var(--sp-1);
    flex-wrap: wrap;
}

.lightbox__foot {
    margin: auto 0 0;
    padding-top: var(--sp-3);
    border-top: 1px solid var(--line);
    font-size: var(--fs-meta);
    color: var(--ink-soft);
    line-height: 1.6;
}

.lightbox__foot a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
}

/* ================= 窄屏 ================= */
@media (max-width: 900px) {
    .page--gallery {
        --masthead-h: 64px;
    }

    .filters {
        margin-left: calc(-1 * var(--sp-4));
        margin-right: calc(-1 * var(--sp-4));
        padding: 0 var(--sp-4);
    }

    .grid {
        gap: var(--sp-2);
    /* 不让内容撑出隐形轨道 */
    grid-auto-columns: 0;
}

    .lightbox__grid {
        grid-template-columns: 1fr;
    }

    /* 窄屏：灯箱几乎占满屏幕，左右各留一点缝 */
    dialog.lightbox {
        width: calc(100vw - 2 * var(--sp-3));
        max-width: none;
        max-height: calc(100dvh - 2 * var(--sp-4));
    }

    .lightbox__stage img {
        max-height: 52dvh;
    }

    .lightbox__side {
        border-left: 0;
        border-top: 1px solid var(--line);
        max-height: none;
    }
}

@media (max-width: 620px) {
    /* 手机上去掉筛选区的负外边距：它是为了贴着正文边缘画的，
       但在窄屏会和 padding 打架，让整页多出十几像素 */
    .filters {
        margin-left: 0;
        margin-right: 0;
        max-width: 100%;
    }

    .shot__title {
        font-size: var(--fs-meta);
    /* 长作品名用省略号，不许把格子撑开 */
    min-width: 0;
    display: block;
}

    .shot__sub {
        font-size: var(--fs-meta);
    min-width: 0;
}

    .page--gallery .page-lede {
        font-size: var(--fs-sm);
        line-height: 1.7;
        max-width: 100%;
    }

    .page--gallery .page-meta {
        gap: var(--sp-2) var(--sp-3);
    }

    .grid {
        --gap: 6px;
        grid-template-columns: repeat(2, calc((100% - var(--gap)) / 2));
        gap: var(--gap);
    /* 不让内容撑出隐形轨道 */
    grid-auto-columns: 0;
}

    .filters__label {
        width: 2.6em;
    }

    /* 手机上日期和城市还能挤下，就不藏了 */
    .shot__meta {
        gap: 0;
    }
}

@media (prefers-reduced-motion: reduce) {
    .shot__img,
    .shot__badge,
    .chip {
        transition: none;
    }

    .sentinel__bar i {
        animation: none;
    }
}