/* ==========================================================================
   全站随机固定背景 + 可读性 —— 站点级扩展样式（不改 themes/）
   配套脚本：static/js/wusi-bg.js
   数据来源：data/remote_backgrounds.yml（远端图源）+ static/images/pool/bg/（本地兜底）

   这里解决的是「背景层看不见」的根因：主题在
     body { background: var(--color-background) }
     #wrap { position: absolute; z-index: 1; background: var(--color-background) }
   两层不透明底色，把任何垫在下面的背景都盖掉了。
   所以本文件把 html/body/#wrap/#container 的底色全部放开，只留下 html 的底色当没图时的兜底，
   再让 #wusi-bg（position:fixed）当真正的不滚动背景层。

   另外负责「文字压在背景上要看得清」：
   · 内容卡片（文章卡 / 正文卡 / 侧栏 widget）半透明 + 毛玻璃，背景能透出来，正文不受影响；
   · 文章末尾「上一篇/下一篇」在没有封面图时垫一层深色底，白字不再直接压在随机背景上；
   · 页脚通栏 / 头部标题的透明度与配色在 config/_default/params.yml 的 injector 里（同属站点级样式）。
   ========================================================================== */

/* 没图 / 图还没加载出来时的兜底底色（放在最底层，不会被盖住） */
html {
  background-color: var(--color-background);
}

body,
#container,
#wrap {
  background: transparent !important;
}

/* 固定背景层：position:fixed 不随页面滚动；inset:0 只占视口，
   不用 100vw（100vw 含滚动条宽度，是之前出现横向滚动条的原因） */
#wusi-bg {
  position: fixed;
  inset: 0;
  z-index: -1; /* 在 html 底色之上、#wrap（z-index:1）内容之下 */
  overflow: hidden;
  pointer-events: none; /* 不挡任何点击/选择 */
  background-color: var(--color-background);
}

/* 背景图直接是 <img>：一张图只请求一次（旧实现 new Image() 探测 + CSS background 会下载两遍），
   object-fit: cover 铺满视口，position:fixed 由父层 #wusi-bg 承担，所以不随页面滚动。 */
#wusi-bg img.wusi-bg-layer {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  opacity: 0;
  transition: opacity 0.8s ease;
  will-change: opacity;
  /* 暗色主题的 img { filter: brightness(70%) } 是给内容图用的，别作用到背景图，
     否则背景会暗两次（主题遮罩再叠一次） */
  filter: none !important;
}

#wusi-bg img.wusi-bg-layer.is-on {
  opacity: 1;
}

/* 兼容旧缓存里的 div 层（HTML 缓存 / 旧脚本） */
#wusi-bg .wusi-bg-layer:not(img) {
  position: absolute;
  inset: 0;
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  opacity: 0;
  transition: opacity 0.8s ease;
}

#wusi-bg .wusi-bg-layer:not(img).is-on {
  opacity: 1;
}

/* 背景压暗/提亮一层，保证亮色主题下页面留白处的文字也看得清。
   暗色主题下更狠一些，避免大图太刺眼。
   ⚠️ 头部标题文字颜色的采样阈值在 wusi-bg.js 里，改这里的数值要同步改。 */
#wusi-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.12);
}

html[data-theme="dark"] #wusi-bg::after {
  background: rgba(10, 12, 16, 0.45);
}

/* 鼠标悬停顶部大图区域时，JS 会逐帧改 #content 的 opacity（1 → 0.1 → 0 淡出，移出恢复）。
   这里给 .2s 过渡，移出时快速恢复，不至于生硬。 */
#content {
  transition: opacity 0.2s ease;
}

/* ==========================================================================
   内容卡片：半透明 + 毛玻璃（亮/暗两套）
   背景图能透出来，但毛玻璃把背景细节糊掉，正文仍然清晰；
   鼠标悬停在卡片上时再透一点，方便欣赏背景（过渡 0.35s）。
   ========================================================================== */
:root {
  --color-wrap: rgba(255, 255, 255, 0.9);
  --wusi-card-blur: 10px;
}

html[data-theme="dark"] {
  --wusi-dark-wrap: rgba(39, 43, 48, 0.92);
}

/* 暗色主题的卡片底色：
   主题自己的写法是 [data-theme="dark"]:root（两个选择器），
   这里必须用 html[data-theme="dark"]:root 才压得住它，否则卡片还是纯色 #272b30。 */
html[data-theme="dark"]:root {
  --color-wrap: var(--wusi-dark-wrap);
}

.post-wrap,
.article-inner,
.widget-wrap,
#article-nav,
.sidebar-top {
  -webkit-backdrop-filter: blur(var(--wusi-card-blur)) saturate(1.05);
  backdrop-filter: blur(var(--wusi-card-blur)) saturate(1.05);
}

.post-wrap,
.article-inner,
.widget-wrap {
  transition:
    background-color 0.35s ease,
    box-shadow 0.3s ease,
    transform 0.3s ease;
}

@media (hover: hover) and (pointer: fine) {
  /* 悬停更透一点（默认 .9 → .82），毛玻璃保证文字依旧清楚 */
  .post-wrap:hover,
  .widget-wrap:hover {
    background-color: rgba(255, 255, 255, 0.82);
  }
  .article-inner:hover {
    background-color: rgba(255, 255, 255, 0.87);
  }
  html[data-theme="dark"] .post-wrap:hover,
  html[data-theme="dark"] .widget-wrap:hover {
    background-color: rgba(39, 43, 48, 0.84);
  }
  html[data-theme="dark"] .article-inner:hover {
    background-color: rgba(39, 43, 48, 0.89);
  }
}

/* ==========================================================================
   文章末尾「上一篇 / 下一篇」(#article-nav)
   主题的设计是「封面图压暗 + 白字」，但封面图是 lazysizes 懒加载的：
   滚到页面底部时图还没加载 / 图挂了，白字就直接压在随机背景上 → 看不清。
   这里给整条 nav 垫一层深色底（有图时图盖在上面，看不出来），并加强文字阴影兜底。
   ========================================================================== */
#article-nav {
  background: rgba(18, 20, 24, 0.5);
  overflow: hidden;
}

#article-nav .article-nav-link-wrap {
  background: rgba(18, 20, 24, 0.16);
}

#article-nav .article-nav-caption,
#article-nav .article-nav-title {
  text-shadow:
    0 1px 3px rgba(0, 0, 0, 0.95),
    0 0 12px rgba(0, 0, 0, 0.75);
}

/* ==========================================================================
   移动端：背景层 / 卡片 / 页脚的少量适配
   （页脚通栏、BLOG 状态徽章的间距在 config/_default/params.yml 里；
     这里只补「卡片贴边」「毛玻璃降级」这类和背景层直接相关的东西）
   ========================================================================== */
@media (max-width: 767px) {
  /* 移动端 GPU 弱，毛玻璃半径收小一点，滚动更稳 */
  :root {
    --wusi-card-blur: 6px;
  }
  /* 背景层只占视口内容区，避免个别浏览器把 fixed 层算进文档宽度 */
  #wusi-bg {
    width: 100%;
    height: 100%;
  }
}

/* 不支持 backdrop-filter 的浏览器：把卡片调到接近不透明，保证正文可读 */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  :root {
    --color-wrap: rgba(255, 255, 255, 0.97);
  }
  html[data-theme="dark"]:root {
    --color-wrap: rgba(39, 43, 48, 0.98);
  }
  .post-wrap:hover,
  .article-inner:hover,
  .widget-wrap:hover {
    background-color: var(--color-wrap) !important;
  }
}

/* 打印：不打印背景层，白底黑字 */
@media print {
  #wusi-bg {
    display: none !important;
  }
  body,
  #wrap,
  #container {
    background: #fff !important;
  }
}

/* 主题把 banner 画在 #header 里的 <img> 上（可能被 <picture> 包着），
   默认这张图会随页面滚动、并且带一层白色渐变遮罩。
   普通页面：整屏背景由 #wusi-bg 承担，这张图要藏起来，否则会和背景层叠在一起；
   有 banner 的页面（文章页，html 上有 wusi-page-banner 类）：把这张图本身变成
   整屏固定背景，一图两用 —— 不用再额外下载一遍同一张图。 */
#header > picture {
  display: none !important;
}

html:not(.wusi-page-banner) #header img {
  display: none !important;
}

/* 有 banner 的页面：图片铺满视口、固定不滚动。
   ⚠️ 宽度必须用 100%（相对视口内容区），不能用 100vw —— 100vw 含滚动条宽度，
   会把页面撑出横向滚动条（之前踩过的坑）。 */
html.wusi-page-banner #header img {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  width: 100% !important;
  height: 100% !important;
  max-width: 100% !important;
  object-fit: cover;
  z-index: -1 !important;
  -webkit-mask: none !important;
  mask: none !important;
}

/* ==========================================================================
   点击背景 → 全屏查看当前这张背景图
   逻辑在 static/js/wusi-bg.js 末尾（事件委托：只有点到页面空白处才打开，
   点链接/按钮/正文/卡片都不会触发）。这里只负责查看层的样式。
   · #wusi-bg 本身 pointer-events: none，所以不能直接给它绑 click；
   · 遮罩用 fixed + inset:0，z-index 高于主题所有浮层（回到顶部 999）；
   · html.wusi-bg-viewing 时锁页面滚动、显示遮罩。
   ========================================================================== */
#wusi-bg-viewer {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(8, 10, 14, 0.93);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.22s ease,
    visibility 0.22s ease;
  cursor: zoom-out;
}

html.wusi-bg-viewing #wusi-bg-viewer {
  opacity: 1;
  visibility: visible;
}

/* 查看时锁滚动（html 和 body 都锁，各浏览器滚动容器不一致） */
html.wusi-bg-viewing,
html.wusi-bg-viewing body {
  overflow: hidden !important;
}

#wusi-bg-viewer img.wusi-bg-viewer-img {
  display: block;
  max-width: 100%;
  /* 底部给「滚轮缩放 · 点击空白处或按 Esc 关闭」那行提示留位置，避免压在图片上 */
  max-height: calc(100vh - 96px);
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 6px;
  box-shadow: 0 16px 60px rgba(0, 0, 0, 0.6);
  /* 滚轮缩放：transform-origin 取中心，JS 里按鼠标位置算平移，保证缩放锚点不漂 */
  transform-origin: center center;
  will-change: transform;
  cursor: zoom-in;
  touch-action: none;
  /* 主题暗色模式的 img { filter: brightness(70%) } 不要作用到查看图上 */
  filter: none !important;
}

/* 放大后可拖动：光标由 JS 切成 grab / grabbing（放在这里只是兜底） */
html.wusi-bg-zoomed #wusi-bg-viewer img.wusi-bg-viewer-img {
  cursor: grab;
}

/* 移动端提示文案更长，给底部多留一点空间，避免压到图片 */
@media (max-width: 767px) {
  #wusi-bg-viewer img.wusi-bg-viewer-img {
    max-height: calc(100vh - 120px);
  }
  #wusi-bg-viewer .wusi-bg-viewer-hint {
    font-size: 12px;
  }
}

#wusi-bg-viewer .wusi-bg-viewer-close {
  position: fixed;
  top: 14px;
  right: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

#wusi-bg-viewer .wusi-bg-viewer-close:hover,
#wusi-bg-viewer .wusi-bg-viewer-close:focus-visible {
  background: rgba(255, 255, 255, 0.3);
}

#wusi-bg-viewer .wusi-bg-viewer-hint {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 16px;
  margin: 0;
  padding: 0 16px;
  color: rgba(255, 255, 255, 0.72);
  font-size: 13px;
  line-height: 1.5;
  text-align: center;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  pointer-events: none;
}

/* 打印：查看层不打印 */
@media print {
  #wusi-bg-viewer {
    display: none !important;
  }
}

/* ==========================================================================
   顶部导航栏里的提示小字：「点击背景图可看全图」
   由 static/js/wusi-bg.js 在确认「当前页有背景图可看」后插入 #sub-nav 末尾
   （搜索图标后面），颜色是博丽灵梦红（#e60012）。窄屏导航太挤，直接隐藏。
   ========================================================================== */
#wusi-bg-hint {
  align-self: center;
  margin-left: 6px;
  padding: 0 4px 0 6px;
  color: var(--wusi-reimu-red, #e60012);
  font-size: 12px;
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.5px;
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
  opacity: 0.85;
}

html[data-theme="dark"] #wusi-bg-hint {
  opacity: 0.95;
}

/* 窄屏/中屏隐藏提示字：这段文案比较长，实测在 ~1230px 以下会顶到右上角的三角徽章，
   所以 ≥1230px 才显示（换图按钮本身仍保留，小图标不占地方） */
@media (max-width: 1229px) {
  #wusi-bg-hint {
    display: none;
  }
}

/* ==========================================================================
   导航栏「换一张背景图」按钮
   由 wusi-bg.js 在确认有背景图可看后插到 #sub-nav（深色模式 / 搜索图标那排）末尾。
   图标是刷新箭头，点一下换一张随机背景；加载中图标转圈，完成后自动停。
   ========================================================================== */
#wusi-bg-switch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  margin: 0 2px 0 6px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--wusi-reimu-red, #e60012);
  cursor: pointer;
  opacity: 0.8;
  transition:
    opacity 0.2s ease,
    background-color 0.2s ease,
    transform 0.3s ease;
}

#wusi-bg-switch svg {
  display: block;
  width: 16px;
  height: 16px;
}

#wusi-bg-switch:hover,
#wusi-bg-switch:focus-visible {
  opacity: 1;
  background: rgba(230, 0, 18, 0.1);
  transform: rotate(30deg);
}

#wusi-bg-switch:active {
  transform: rotate(30deg) scale(0.92);
}

#wusi-bg-switch.is-loading {
  pointer-events: none;
  opacity: 0.45;
}

#wusi-bg-switch.is-loading svg {
  animation: wusi-bg-spin 0.9s linear infinite;
}

@keyframes wusi-bg-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  #wusi-bg-switch.is-loading svg {
    animation: none;
  }
}
