/* ============================================================
   图片画廊 — 完整重写
   设计原则：
   1. 移动端优先（设计 mobile 380px 起，desktop 完全是放大版）
   2. lightbox 用 CSS Grid，不要 flexbox（避开 min-width: auto 坑）
   3. 图片硬约束：max-width: 100vw, max-height: 100vh, object-fit: contain
   4. 单列布局避免复杂计算
   ============================================================ */

* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f6f8fb;
  --card-bg: #ffffff;
  --text: #1f2937;
  --text-secondary: #6b7280;
  --text-tertiary: #9ca3af;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --hover-bg: #f3f4f6;
  --btn-bg: #ffffff;
  --btn-border: #d1d5db;
  --btn-hover: #f3f4f6;
  --shadow: 0 1px 3px rgba(0,0,0,0.04), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-hover: 0 4px 12px rgba(0,0,0,0.08);
  --overlay: rgba(0,0,0,0.92);
}
[data-theme="dark"] {
  --bg: #0f1115;
  --card-bg: #161a21;
  --text: #e6e8eb;
  --text-secondary: #8b94a3;
  --text-tertiary: #6b7383;
  --border: #1f242b;
  --border-strong: #2a2f38;
  --hover-bg: #1c2028;
  --btn-bg: #1c2028;
  --btn-border: #2a2f38;
  --btn-hover: #252a34;
  --shadow: none;
  --shadow-hover: 0 4px 12px rgba(0,0,0,0.4);
}

html, body {
  width: 100%;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.2s, color 0.2s;
  /* 关键：禁止横向滚动 */
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh; /* 移动端动态视口高度 */
}

/* ============ Topbar ============ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}
.logo { font-size: 20px; }
.title { font-size: 15px; font-weight: 700; white-space: nowrap; }
.count { font-size: 12px; color: var(--text-secondary); white-space: nowrap; }
.topbar-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.icon-btn {
  width: 38px;
  height: 38px;
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}
.icon-btn:hover { background: var(--btn-hover); }
.icon-btn:active { background: var(--hover-bg); }

/* ============ Filter ============ */
.filter-bar {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  flex-shrink: 0;
}
.filter-bar::-webkit-scrollbar { display: none; }
.chip {
  padding: 5px 12px;
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  border-radius: 16px;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
  min-height: 30px;
  -webkit-tap-highlight-color: transparent;
}
.chip.active {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
  font-weight: 600;
}

/* ============ Grid ============ */
.grid {
  flex: 1;
  display: grid;
  /* 移动端默认 2 列（避免单图过大） */
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  padding: 8px;
  align-content: start;
  overflow-y: auto;
  /* 关键：max-width 100vw 防止超出 */
  max-width: 100vw;
}
.empty {
  grid-column: 1 / -1;
  padding: 60px 20px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
}

.card {
  position: relative;
  aspect-ratio: 1;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s, box-shadow 0.15s;
  touch-action: manipulation;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow-hover); }
.card:active { transform: scale(0.98); }
.card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  /* disable drag */
  -webkit-user-drag: none;
  user-select: none;
  pointer-events: none;
}
.card .badge {
  position: absolute;
  top: 5px;
  left: 5px;
  padding: 2px 6px;
  background: rgba(0,0,0,0.65);
  color: white;
  font-size: 10px;
  border-radius: 4px;
  text-transform: uppercase;
  backdrop-filter: blur(4px);
}
.card .badge.feishu { background: rgba(0,182,203,0.85); }
.card .badge.upload { background: rgba(77,140,255,0.85); }

/* ============ Lightbox ============ */
/* 关键设计：
   - 不用 flexbox（避开 min-width: auto 陷阱）
   - 用 CSS Grid + place-items: center 居中
   - 图像硬约束：max-width: 100%, max-height: 100%, object-fit: contain
   - 容器 overflow: hidden 兜底 */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: var(--overlay);
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 50px 12px;       /* 留出边界给按钮 */
  /* 关键：不要让 lightbox 内任何东西溢出 */
  contain: strict;
}
.lightbox[hidden] { display: none; }

.lightbox > img {
  /* 核心：硬约束在 viewport 内 */
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
  border-radius: 4px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  user-select: none;
  -webkit-user-drag: none;
  /* 防止图片撑破容器 */
  min-width: 0;
  min-height: 0;
}

/* 控制按钮 */
.lb-close, .lb-prev, .lb-next {
  position: absolute;
  background: rgba(255,255,255,0.18);
  color: white;
  border: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1001;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  backdrop-filter: blur(8px);
  line-height: 1;
  padding: 0;
}
.lb-close:active, .lb-prev:active, .lb-next:active {
  background: rgba(255,255,255,0.32);
}
.lb-close { top: 12px; right: 12px; }
.lb-prev { left: 8px; top: 50%; transform: translateY(-50%); }
.lb-next { right: 8px; top: 50%; transform: translateY(-50%); }

.lb-info {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.6);
  color: white;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 12px;
  text-align: center;
  max-width: calc(100vw - 24px);
  backdrop-filter: blur(8px);
  word-break: break-all;
  z-index: 1001;
}
.lb-info > div { margin: 2px 0; }
.lb-info button {
  background: rgba(255,255,255,0.2);
  color: white;
  border: none;
  padding: 4px 12px;
  border-radius: 4px;
  margin-top: 6px;
  cursor: pointer;
  font-size: 12px;
  min-height: 28px;
}

/* ============ Upload drag overlay ============ */
.upload-zone {
  position: fixed;
  inset: 0;
  background: rgba(77, 140, 255, 0.85);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.upload-zone[hidden] { display: none; }
.upload-inner {
  font-size: 20px;
  color: white;
  font-weight: 700;
  padding: 24px 32px;
  background: rgba(255,255,255,0.15);
  border: 3px dashed white;
  border-radius: 16px;
  text-align: center;
}

/* ============ SVG 图标 ============ */
.icon-btn svg {
  width: 20px;
  height: 20px;
  display: block;
  pointer-events: none;
}
.lb-close svg, .lb-prev svg, .lb-next svg {
  width: 22px;
  height: 22px;
  display: block;
  pointer-events: none;
}
.logo svg {
  width: 22px;
  height: 22px;
  display: block;
}

/* ============ Spinner ============ */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 删除按钮里的 svg + text 布局 */
#lbDelete {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 12px !important;
}
#lbDelete svg {
  width: 14px;
  height: 14px;
}

/* ============ Desktop (>= 600px) ============ */
@media (min-width: 600px) {
  .topbar { padding: 12px 20px; }
  .title { font-size: 18px; }
  .icon-btn { width: 40px; height: 40px; font-size: 18px; }
  .filter-bar { padding: 10px 20px; gap: 8px; }
  .chip { padding: 6px 14px; font-size: 13px; min-height: 32px; }
  .grid { grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 12px; }
  .card .badge { top: 6px; left: 6px; padding: 3px 8px; font-size: 11px; }
  .lightbox { padding: 60px 24px; }
  .lb-close { top: 16px; right: 16px; width: 48px; height: 48px; font-size: 26px; }
  .lb-prev { left: 16px; width: 48px; height: 48px; font-size: 26px; }
  .lb-next { right: 16px; width: 48px; height: 48px; font-size: 26px; }
  .lb-info { font-size: 13px; padding: 10px 16px; max-width: 600px; }
}

@media (min-width: 900px) {
  .grid { grid-template-columns: repeat(4, 1fr); gap: 10px; padding: 16px; }
}

@media (min-width: 1200px) {
  .grid { grid-template-columns: repeat(5, 1fr); gap: 12px; padding: 20px; max-width: 1400px; margin: 0 auto; }
}