:root {
    /* 动态变量，由 JS 更新 */
    --col-count: 5;
    /* --img-ratio: 2 / 3; */
    --gap-x: 10px;
    --gap-y: 10px;
    --padding-size: 20px;
}

/* 隐藏滚动条但保留功能 */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* --- 核心网格布局 --- */
#gallery-container {
    display: grid;
    align-items: start;
    grid-template-columns: repeat(var(--col-count), 1fr);
    column-gap: var(--gap-x);
    row-gap: var(--gap-y);
    padding: var(--padding-size);
    width: 100%;
    max-width: 1440px;
    margin: 0 auto;
}

/* 图片项基础样式 */
.img-cell {
    width: 100%;
    aspect-ratio: var(--img-ratio);
    position: relative;
    content-visibility: auto;
    contain-intrinsic-size: 300px;
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
    touch-action: manipulation;
}

.img-cell img {
    width: 100%;
    /* height: 100%;
     */
    height: auto;
    display: block;
    object-fit: contain;
    opacity: 0;
    transition: opacity 0.3s ease-in, filter 0.3s;
}

.img-cell img.loaded {
    opacity: 1;
}

#gallery-container[data-fit-mode="equal-height"] .img-cell img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.img-delete-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 5;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 999px;
    background: rgba(15, 23, 42, 0.78);
    color: #fff;
    font-size: 26px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transform: scale(0.92);
    transition: opacity 0.18s ease, transform 0.18s ease, background-color 0.18s ease;
}

.img-delete-btn:hover,
.img-delete-btn:focus-visible {
    background: rgba(220, 38, 38, 0.92);
    outline: none;
}

.img-cell.show-delete .img-delete-btn {
    opacity: 1;
    pointer-events: auto;
    transform: scale(1);
}

@media (hover: hover) and (pointer: fine) {
    .img-cell:hover .img-delete-btn,
    .img-delete-btn:focus-visible {
        opacity: 1;
        pointer-events: auto;
        transform: scale(1);
    }
}

@media (pointer: coarse), (max-width: 767px) {
    .img-cell {
        content-visibility: visible;
        contain-intrinsic-size: auto;
    }

    #advanced-panel input[type="range"] {
        -webkit-appearance: none;
        appearance: none;
        height: 32px;
        background: transparent;
    }

    #advanced-panel input[type="range"]::-webkit-slider-runnable-track {
        height: 6px;
        border-radius: 999px;
        background: #e5e7eb;
    }

    #advanced-panel input[type="range"]::-webkit-slider-thumb {
        -webkit-appearance: none;
        appearance: none;
        width: 24px;
        height: 24px;
        margin-top: -9px;
        border-radius: 999px;
        border: 0;
        background: #2563eb;
        box-shadow: 0 1px 4px rgba(15, 23, 42, 0.22);
    }

    #advanced-panel input[type="range"]::-moz-range-track {
        height: 6px;
        border-radius: 999px;
        background: #e5e7eb;
    }

    #advanced-panel input[type="range"]::-moz-range-thumb {
        width: 24px;
        height: 24px;
        border-radius: 999px;
        border: 0;
        background: #2563eb;
        box-shadow: 0 1px 4px rgba(15, 23, 42, 0.22);
    }
}

#advanced-panel input[type="range"] {
    touch-action: manipulation;
}

#advanced-panel input[type="number"],
#advanced-panel select {
    min-height: 40px;
}

#advanced-panel input[type="number"]:disabled {
    background: #f3f4f6;
    color: #9ca3af;
    cursor: not-allowed;
}

#advanced-panel {
    max-height: calc(100vh - 64px);
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

/* =========================================
   艺术风格系统
   ========================================= */

/* 1. 拍立得 (Polaroid) */
[data-style="polaroid"] .img-cell {
    background-color: #fff;
    padding: var(--polaroid-side, 10px) var(--polaroid-side, 10px) var(--polaroid-bottom, 45px) var(--polaroid-side, 10px);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    border-radius: 2px;
}

/* 2. 悬浮投影 (Shadow) */
[data-style="shadow"] .img-cell {
    border-radius: 12px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    background: transparent;
}

/* 3. 胶卷 (Film) */
[data-style="film"] .img-cell {
    background-color: #111;
    padding: var(--film-padding-y, 24px) 0;
    box-shadow: 0 0 0 1px #333;
    position: relative;
}

[data-style="film"] .img-cell::before,
[data-style="film"] .img-cell::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    height: var(--film-hole-height, 12px);
    background-image: radial-gradient(circle, #fff 40%, transparent 45%);
    background-size: var(--film-hole-gap, 16px) var(--film-hole-gap, 16px);
    background-position: center;
    background-repeat: repeat-x;
}

[data-style="film"] .img-cell::before {
    top: var(--film-hole-offset, 6px);
}

[data-style="film"] .img-cell::after {
    bottom: var(--film-hole-offset, 6px);
}

/* 4. 极简白框 (Clean) */
[data-style="clean"] .img-cell {
    border: 8px solid #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* 5. MacOS */
[data-style="macos"] .img-cell {
    background-color: #f1f5f9;
    padding: var(--mac-titlebar-height, 26px) 0 0 0;
    border-radius: 8px;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    position: relative;
}

[data-style="macos"] .img-cell::before {
    content: "";
    position: absolute;
    top: var(--mac-dot-top, 9px);
    left: var(--mac-dot-left, 12px);
    width: var(--mac-dot-size, 8px);
    height: var(--mac-dot-size, 8px);
    border-radius: 50%;
    background: #ff5f56;
    box-shadow: var(--mac-dot-gap, 14px) 0 0 #ffbd2e, calc(var(--mac-dot-gap, 14px) * 2) 0 0 #27c93f;
}

[data-style="macos"] .img-cell img {
    border-radius: 0 0 6px 6px;
}

/* 6. 幻灯片 (Slide) */
[data-style="slide"] .img-cell {
    background-color: #e2e8f0;
    padding: var(--slide-padding, 16px);
    border-radius: 4px;
    box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
}

[data-style="slide"] .img-cell::after {
    content: "Kodachrome";
    position: absolute;
    bottom: 4px;
    right: var(--slide-label-right, 16px);
    font-size: 9px;
    color: #94a3b8;
    font-family: monospace;
    letter-spacing: 0.5px;
}


/* --- 真正的邮票齿孔风格 (Stamp Concave Fix) --- */
[data-style="stamp"] .img-cell {
    background-color: white !important;
    /* 邮票的白色底纸 */
    border-radius: 0;
    border: none;

    /* 使用 drop-shadow 才能让阴影跟随“挖掉”后的形状 */
    filter: drop-shadow(2px 3px 5px rgba(0, 0, 0, 0.4));

    /* 设定齿孔的尺寸变量 */
    --hole-size: var(--stamp-hole-size, 5px);
    /* 孔的直径 */
    --hole-gap: var(--stamp-hole-gap, 16px);
    /* 孔与孔的中心间距 */

    /* 
       核心逻辑：
       Layer 1: 中心实心矩形，确保图片区域不被打孔。
       Layer 2: 边缘打孔层，使用径向渐变画圆，但颜色是“透明”，背景是“黑色”。
    */
    -webkit-mask-image:
        /* 1. 中心保护层：纯黑矩形，尺寸比容器小一个齿孔的宽度 */
        linear-gradient(#000 0 0),
        /* 2. 边缘挖孔层：画出带透明圆形的黑色方格 */
        radial-gradient(circle, transparent var(--hole-size), #000 calc(var(--hole-size) + 0.5px));

    -webkit-mask-size:
        /* 保护层尺寸：稍微收缩，留出边缘给齿孔 */
        calc(100% - var(--hole-gap)) calc(100% - var(--hole-gap)),
        /* 挖孔层尺寸：平铺整个格子 */
        var(--hole-gap) var(--hole-gap);

    -webkit-mask-position:
        center,
        /* 偏移半个间距，让圆心正好压在边线上 */
        calc(var(--hole-gap) / -2) calc(var(--hole-gap) / -2);

    -webkit-mask-repeat: no-repeat, round;

    /* 关键：告诉浏览器如何叠加这两层遮罩 */
    /* 在新版 Chrome 中 source-over 配合多层 mask 效果最稳 */
    -webkit-mask-composite: source-over;
    mask-composite: add;

    /* 必须有足够的内边距，否则齿孔会切到图片 */
    padding: var(--stamp-padding, 10px);
}

/* 邮票风格内部图片微调 */
[data-style="stamp"] .img-cell img {
    border-radius: 2px;
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* 8. 霓虹 (Neon) */
[data-style="neon"] .img-cell {
    padding: 2px;
    border: 1px solid #0ff;
    box-shadow: 0 0 8px #0ff, inset 0 0 5px #0ff;
    border-radius: 4px;
}

/* 9. 复古相角 (Corners) */
[data-style="corners"] .img-cell {
    background: #fff;
    padding: 6px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    position: relative;
}

[data-style="corners"] .img-cell::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(135deg, #d4af37 var(--corner-size, 20px), transparent var(--corner-edge, 20.5px)) top left,
        linear-gradient(225deg, #d4af37 var(--corner-size, 20px), transparent var(--corner-edge, 20.5px)) top right,
        linear-gradient(315deg, #d4af37 var(--corner-size, 20px), transparent var(--corner-edge, 20.5px)) bottom right,
        linear-gradient(45deg, #d4af37 var(--corner-size, 20px), transparent var(--corner-edge, 20.5px)) bottom left;
    background-repeat: no-repeat;
    background-size: 50% 50%;
}

/* --- 背景纹理 --- */
body[data-bg="dark"] {
    background-color: #0f172a;
    color: #94a3b8;
}

body[data-bg="grid"] {
    background-color: #f8fafc;
    background-image: linear-gradient(#e2e8f0 1px, transparent 1px), linear-gradient(90deg, #e2e8f0 1px, transparent 1px);
    background-size: 20px 20px;
}

body[data-bg="dots"] {
    background-color: #fff;
    background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
    background-size: 16px 16px;
}

body[data-bg="stamp-paper"] {
    background-color: #e0e0e0;
}

body[data-bg="wood"] {
    background-color: #d4b483;
    background-image: repeating-linear-gradient(45deg, rgba(0, 0, 0, 0.03) 0, rgba(0, 0, 0, 0.03) 2px, transparent 2px, transparent 4px);
}

body[data-bg="cork"] {
    background-color: #e3c08d;
    background-image: radial-gradient(rgba(139, 69, 19, 0.2) 1px, transparent 1px);
    background-size: 4px 4px;
}

/* --- 滤镜系统 --- */
#gallery-container[data-filter="bw"] img {
    filter: grayscale(100%);
}

#gallery-container[data-filter="fade"] img {
    filter: sepia(20%) saturate(70%) brightness(105%);
}

#gallery-container[data-filter="contrast"] img {
    filter: contrast(115%) saturate(110%);
}

#gallery-container[data-filter="warm"] img {
    filter: sepia(30%);
}

#gallery-container[data-filter="cool"] img {
    filter: saturate(80%) hue-rotate(10deg) brightness(95%);
}
/* 占位格基础样式 */
.img-cell.placeholder {
    height: 100%;
        /* 强制占位符填满 Grid 容器提供的整行高度 */
        min-height: 200px;
        /* 设置一个最小高度，防止在没有图片时缩得太小 */
    background-color: rgba(0, 0, 0, 0.05);
    /* 浅灰色背景 */
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px dashed #ccc;
    /* 虚线边框增加占位感 */
}

.img-cell.placeholder span {
    opacity: 0.1;
    font-size: 5rem;
    font-weight: bold;
    color: #999;
    font-family: monospace;
}

/* 霓虹风格下的占位格 */
[data-style="neon"] .img-cell.placeholder {
    background-color: rgba(255, 255, 255, 0.05);
    border-style: solid;
    opacity: 0.6;
}

/* 拍立得风格下的占位格 */
[data-style="polaroid"] .img-cell.placeholder {
    background-color: #f9f9f9;
    border: 1px solid #ddd;
}

/* =========================================
   布局模式系统
   ========================================= */

/* 竖向布局 (Vertical) */
#gallery-container[data-layout="vertical"] {
    display: flex !important;
    flex-wrap: wrap !important;
    align-items: flex-start !important;
    grid-template-columns: none !important;
}

#gallery-container[data-layout="vertical"] .img-cell {
    width: auto !important;
    aspect-ratio: auto !important;
    flex: 0 0 auto !important;
    /* height 由 JS 动态计算并内联设置 */
}

#gallery-container[data-layout="vertical"] .img-cell img {
    height: 100% !important;
    width: auto !important;
    object-fit: contain !important;
}
