/* ============================================================
 * ambient.css — 「檐下秘境」全站环境层 v1.0
 * 视觉来源：檐下圆满·秘境拾笺（古建筑 × 非遗 × 东方留白 × 心理疗愈）
 * 手法还原：真实古建影调 → 88~92% 白纱洗淡 → 青瓷底色 → 半透层次
 * 本项目以「零图片依赖」程序化复刻该语言：
 *   青瓷天光 / 宣纸肌理 / 檐影 / 窗棂（灯笼锦）/ 屋脊飞檐 / 青花微色 / 装裱细框
 * 铁律：环境层仅作氛围，永不覆盖、干扰、抢夺精油瓶与核心文字。
 * ============================================================ */

/* ---------- 令牌 ---------- */
:root {
  /* 洗淡后的古建线稿（浅色：墨；深色：月白）——由 html[data-theme=dark] 整体替换 */
  --amb-lat: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160' viewBox='0 0 160 160' fill='none' stroke='%232d3a38' stroke-opacity='.085' stroke-width='1'%3E%3Crect x='5' y='5' width='150' height='150'/%3E%3Cpath d='M5 55h150M5 105h150M55 5v150M105 5v150'/%3E%3Cpath d='M55 55l50 50M105 55l-50 50'/%3E%3C/svg%3E");
  --amb-ridge: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1200' height='120' viewBox='0 0 1200 120' preserveAspectRatio='none' fill='%232d3a38' fill-opacity='.05'%3E%3Cpath d='M0 74C50 86 90 94 150 97 300 70 450 40 600 32 750 40 900 70 1050 97 1110 94 1150 86 1200 74L1200 120L0 120Z'/%3E%3C/svg%3E");
  --amb-grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.055'/%3E%3C/svg%3E");

  /* 青瓷天光 */
  --amb-sky: rgba(198, 219, 213, .34);
  --amb-sky2: rgba(198, 219, 213, .14);
  --amb-halo: rgba(255, 255, 255, .46);
  /* 流动光带（浅色下用青瓷，纯白在宣纸底上不可见） */
  --amb-flow1: rgba(198, 219, 213, .30);
  --amb-flow2: rgba(198, 219, 213, .11);
  /* 檐影 */
  --amb-eave1: rgba(45, 58, 56, .055);
  --amb-eave2: rgba(45, 58, 56, .022);
  /* 青花微色 */
  --amb-blue: rgba(47, 90, 166, .032);
  /* 装裱细框 */
  --amb-frame-line: rgba(45, 58, 56, .07);
  --amb-frame-corner: rgba(45, 58, 56, .13);
  /* 四周微暗角（增加空间纵深） */
  --amb-vignette: rgba(45, 58, 56, .035);
}

html[data-theme="dark"] {
  --amb-lat: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160' viewBox='0 0 160 160' fill='none' stroke='%23cfded9' stroke-opacity='.085' stroke-width='1'%3E%3Crect x='5' y='5' width='150' height='150'/%3E%3Cpath d='M5 55h150M5 105h150M55 5v150M105 5v150'/%3E%3Cpath d='M55 55l50 50M105 55l-50 50'/%3E%3C/svg%3E");
  --amb-ridge: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1200' height='120' viewBox='0 0 1200 120' preserveAspectRatio='none' fill='%23cfded9' fill-opacity='.055'%3E%3Cpath d='M0 74C50 86 90 94 150 97 300 70 450 40 600 32 750 40 900 70 1050 97 1110 94 1150 86 1200 74L1200 120L0 120Z'/%3E%3C/svg%3E");

  --amb-sky: rgba(116, 152, 146, .17);
  --amb-sky2: rgba(116, 152, 146, .07);
  --amb-halo: rgba(160, 200, 192, .07);
  --amb-flow1: rgba(160, 200, 192, .055);
  --amb-flow2: rgba(160, 200, 192, .022);
  --amb-eave1: rgba(0, 0, 0, .34);
  --amb-eave2: rgba(0, 0, 0, .14);
  --amb-blue: rgba(120, 170, 220, .045);
  --amb-frame-line: rgba(226, 238, 234, .07);
  --amb-frame-corner: rgba(226, 238, 234, .13);
  --amb-vignette: rgba(0, 0, 0, .17);
}

/* ---------- 容器 ----------
 * z-index:-1 → 永远绘制在页面内容之下，仅作背景氛围；
 * pointer-events:none → 永不拦截交互。
 */
.ambient {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  overflow: hidden;
  opacity: 1;
  transition: opacity .7s ease;
  contain: layout paint;
}
.ambient > i {
  position: absolute;
  inset: 0;
  display: block;
  pointer-events: none;
}

/* ---------- 一 · 青瓷天光：由上方庭院洒落，随呼吸明暗 ---------- */
.amb-wash {
  background:
    radial-gradient(ellipse 82% 54% at 50% -10%, var(--amb-halo) 0%, transparent 62%),
    linear-gradient(180deg, var(--amb-sky) 0%, var(--amb-sky2) 22%, transparent 46%);
  animation: ambBreathe 19s ease-in-out infinite;
}
/* 流动光带：极慢横移，模拟光穿过窗棂后缓缓游走 */
.amb-wash::after {
  content: '';
  position: absolute;
  inset: -20% -40%;
  background: linear-gradient(104deg,
      transparent 34%,
      var(--amb-flow1) 46%,
      var(--amb-flow2) 54%,
      transparent 66%);
  animation: ambFlow 46s ease-in-out infinite;
}

/* ---------- 二 · 宣纸肌理 + 四周微暗角（空间纵深） ---------- */
.amb-paper {
  background-image:
    radial-gradient(ellipse 92% 84% at 50% 48%, transparent 58%, var(--amb-vignette) 100%),
    var(--amb-grain);
  background-size: 100% 100%, 200px 200px;
  background-repeat: no-repeat, repeat;
}

/* ---------- 三 · 檐影：身在此檐下，顶部投下柔和暗影 ---------- */
.amb-eave {
  inset: 0 0 auto 0;
  height: 260px;
  background:
    linear-gradient(180deg, var(--amb-eave1) 0px, var(--amb-eave2) 52px, transparent 190px),
    linear-gradient(172deg, var(--amb-eave2) 0px, transparent 120px);
  animation: ambEave 26s ease-in-out infinite;
}

/* ---------- 四 · 窗棂（灯笼锦）：隐于角落，只露一角 ---------- */
.amb-lat {
  background-image: var(--amb-lat);
  background-repeat: no-repeat;
}
.amb-lat-tr {
  background-position: right -46px top 96px;
  background-size: 208px 208px;
  animation: ambDrift 58s ease-in-out infinite;
}
.amb-lat-bl {
  background-position: left -52px bottom 46px;
  background-size: 164px 164px;
  animation: ambDrift2 72s ease-in-out infinite;
}

/* ---------- 五 · 屋脊飞檐：底部远檐轮廓，两端轻翘 ---------- */
.amb-ridge {
  inset: auto 0 0 0;
  height: 150px;
  background-image: var(--amb-ridge);
  background-repeat: no-repeat;
  background-position: bottom center;
  background-size: 116% 96px;
  animation: ambRidge 44s ease-in-out infinite;
}

/* ---------- 六 · 青花微色：非遗青花的一点点冷调 ---------- */
.amb-blue {
  background:
    radial-gradient(ellipse 44% 34% at 86% 82%, var(--amb-blue) 0%, transparent 64%),
    radial-gradient(ellipse 34% 26% at 12% 24%, var(--amb-blue) 0%, transparent 62%);
  animation: ambBreathe 33s ease-in-out infinite reverse;
}

/* ---------- 七 · 装裱细框：一根极淡细线 + 四角角标 ---------- */
.amb-frame {
  inset: 15px;
  border: 1px solid var(--amb-frame-line);
  border-radius: 3px;
  animation: ambFrame 31s ease-in-out infinite;
}
.amb-frame::before,
.amb-frame::after {
  content: '';
  position: absolute;
  left: -1px;
  right: -1px;
  height: 22px;
  background-repeat: no-repeat;
}
.amb-frame::before {
  top: -1px;
  background-image:
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner)),
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner)),
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner)),
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner));
  background-size: 1px 22px, 22px 1px, 1px 22px, 22px 1px;
  background-position: left top, left top, right top, right top;
}
.amb-frame::after {
  bottom: -1px;
  background-image:
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner)),
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner)),
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner)),
    linear-gradient(var(--amb-frame-corner), var(--amb-frame-corner));
  background-size: 1px 22px, 22px 1px, 1px 22px, 22px 1px;
  background-position: left bottom, left bottom, right bottom, right bottom;
}

/* ---------- 动效 ---------- */
@keyframes ambBreathe {
  0%, 100% { opacity: .78; }
  50%      { opacity: 1; }
}
@keyframes ambEave {
  0%, 100% { opacity: .86; transform: translate3d(0, 0, 0); }
  50%      { opacity: 1;   transform: translate3d(0, 3px, 0); }
}
@keyframes ambDrift {
  0%, 100% { transform: translate3d(0, 0, 0); opacity: .85; }
  50%      { transform: translate3d(-7px, 5px, 0); opacity: 1; }
}
@keyframes ambDrift2 {
  0%, 100% { transform: translate3d(0, 0, 0); opacity: 1; }
  50%      { transform: translate3d(6px, -4px, 0); opacity: .82; }
}
@keyframes ambRidge {
  0%, 100% { transform: translate3d(0, 0, 0) scale(1); opacity: .88; }
  50%      { transform: translate3d(0, -4px, 0) scale(1.006); opacity: 1; }
}
@keyframes ambFrame {
  0%, 100% { opacity: .72; }
  50%      { opacity: 1; }
}
@keyframes ambFlow {
  0%, 100% { transform: translate3d(-9%, 0, 0); opacity: .55; }
  50%      { transform: translate3d(9%, 0, 0);  opacity: 1; }
}

/* ---------- 沉浸页避让：五境等自有光影空间，环境层退场 ---------- */
body.amb-off .ambient { opacity: 0; }

/* ---------- 移动端：收窄尺度，保持留白 ---------- */
@media (max-width: 640px) {
  .amb-frame { inset: 9px; }
  .amb-lat-tr { background-position: right -70px top 78px; background-size: 150px 150px; }
  .amb-lat-bl { background-position: left -74px bottom 30px; background-size: 120px 120px; }
  .amb-ridge { height: 110px; background-size: 150% 62px; }
  .amb-eave { height: 180px; }
}

/* ---------- 无障碍：尊重减少动效偏好，仅保留静态氛围 ---------- */
@media (prefers-reduced-motion: reduce) {
  .amb-wash, .amb-wash::after, .amb-eave,
  .amb-lat-tr, .amb-lat-bl, .amb-ridge, .amb-blue, .amb-frame {
    animation: none !important;
  }
  .amb-wash { opacity: .92; }
  .amb-wash::after { opacity: .7; }
}

/* ---------- 打印：环境层不参与 ---------- */
@media print {
  .ambient { display: none !important; }
}
