/* ===== 代码增强插件 — 折叠样式 ===== */

/* 基础配色变量 */
:root {
  --ce-border: rgba(0, 0, 0, 0.32);
  --ce-btn-bg: rgba(0, 0, 0, 0.55);
  --ce-btn-bg-hover: rgba(0, 0, 0, 0.75);
}

/* 暗色主题：仅通过 data-theme 属性检测，避免 [class*="dark"] 宽泛匹配 */
[data-theme="dark"] {
  --ce-border: rgba(255, 255, 255, 0.18);
}

/* ============================================================
   代码块折叠
   - 折叠态：按钮 absolute 定位在容器底部
   - 展开态：按钮 sticky 定位在视口底部
   - 两种状态都水平居中（相对于 .ce-block 宽度中央）
   ============================================================ */

.ce-block {
  position: relative;
  margin: 16px 0;
  border-radius: 8px;
  border: 1px solid var(--ce-border);
  /* 默认不裁剪（兼容不支持 overflow:clip 的浏览器） */
  overflow: visible;
  max-width: 100%;
}

/* 渐进增强：支持 overflow:clip 的浏览器才应用 */
@supports (overflow: clip) {
  .ce-block {
    overflow: clip;
  }
}

/* 展开态移除裁剪，让 sticky 按钮完全可见 */
.ce-block[data-ce-state="expanded"] {
  overflow: visible;
}

/* 代码块主体 — 只保留折叠必需的属性，不覆盖主题样式 */
.ce-pre {
  overflow: hidden;
  overflow-x: auto;
  transition: max-height 0.35s ease;
  white-space: pre;
}

/* 折叠态：限制 pre 高度实现内容截断，允许横向滚动 */
.ce-block[data-ce-state="collapsed"] .ce-pre {
  max-height: var(--ce-fold-h, 320px);
  overflow-x: auto;
}

/* 展开态：pre 高度自然撑开，允许横向滚动 */
.ce-block[data-ce-state="expanded"] .ce-pre {
  max-height: none;
  overflow: visible;
  overflow-x: auto;
}

/* ============================================================
   长图折叠
   - .ce-img-wrap 用 inline-block 让宽度跟随图片
   ============================================================ */

.ce-img-wrap {
  position: relative;
  display: inline-block;
  max-width: 100%;
  transition: max-height 0.35s ease;
}

.ce-img-wrap img {
  display: block;
  max-width: 100%;
}

.ce-img-wrap[data-ce-img="folded"] {
  max-height: var(--ce-img-h, 400px);
  overflow: hidden;
}

.ce-img-wrap[data-ce-img="unfolded"] {
  max-height: none;
  overflow: visible;
}

/* ============================================================
   折叠按钮（代码块 + 图片共用基础样式）
   ============================================================ */

.ce-block .ce-fold-btn,
.ce-img-wrap .ce-img-btn {
  z-index: 2;
  display: flex;
  align-items: center;
  min-height: 36px;
  padding: 6px 16px;
  border-radius: 18px;
  font-size: 13px;
  cursor: pointer;
  border: none;
  color: #fff;
  background: var(--ce-btn-bg);
  transition: background 0.2s, opacity 0.2s;
  user-select: none;
  gap: 6px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* 渐进增强：支持 backdrop-filter 的浏览器才应用模糊效果 */
@supports (backdrop-filter: blur(4px)) or (-webkit-backdrop-filter: blur(4px)) {
  .ce-block .ce-fold-btn,
  .ce-img-wrap .ce-img-btn {
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
  }
}

.ce-block .ce-fold-btn:hover,
.ce-img-wrap .ce-img-btn:hover {
  background: var(--ce-btn-bg-hover);
}

/* ============================================================
   折叠态：absolute 定位在容器底部，水平居中
   ============================================================ */

.ce-block[data-ce-state="collapsed"] .ce-fold-btn,
.ce-img-wrap[data-ce-img="folded"] .ce-img-btn {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  animation: ce-pop-in 0.2s ease;
}

/* ============================================================
   展开态：sticky 定位在视口底部，水平居中
   使用 margin:auto 居中（比 left:50%+transform 更稳定）
   ============================================================ */

.ce-block[data-ce-state="expanded"] .ce-fold-btn,
.ce-img-wrap[data-ce-img="unfolded"] .ce-img-btn {
  position: -webkit-sticky;
  position: sticky;
  bottom: 8px;
  left: auto;
  right: auto;
  transform: none;
  display: flex;
  margin: 8px auto 0;
  width: fit-content;
}

/* ============================================================
   按钮文字 + 图标（通过 ::after 注入）
   ============================================================ */

.ce-block[data-ce-state="collapsed"] .ce-fold-btn-text::after {
  content: "展开代码";
}

.ce-block[data-ce-state="collapsed"] .ce-fold-btn-icon::after {
  content: "▼";
}

.ce-block[data-ce-state="expanded"] .ce-fold-btn-text::after {
  content: "折叠代码";
}

.ce-block[data-ce-state="expanded"] .ce-fold-btn-icon::after {
  content: "▲";
}

.ce-img-wrap[data-ce-img="folded"] .ce-img-btn-text::after {
  content: "展开图片";
}

.ce-img-wrap[data-ce-img="folded"] .ce-img-btn-icon::after {
  content: "▼";
}

.ce-img-wrap[data-ce-img="unfolded"] .ce-img-btn-text::after {
  content: "折叠图片";
}

.ce-img-wrap[data-ce-img="unfolded"] .ce-img-btn-icon::after {
  content: "▲";
}

/* ============================================================
   动效与辅助功能
   ============================================================ */
@keyframes ce-pop-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.ce-fold-btn:focus-visible,
.ce-img-btn:focus-visible {
  outline: 2px solid #60a0ff;
  outline-offset: 2px;
  border-radius: 10px;
}

@media (prefers-reduced-motion: reduce) {
  .ce-pre,
  .ce-img-wrap,
  .ce-fold-btn,
  .ce-img-btn {
    transition: none;
  }
  .ce-fold-btn,
  .ce-img-btn {
    animation: none;
  }
}

/* ============================================================
   响应式
   ============================================================ */
@media (max-width: 768px) {
  .ce-block .ce-fold-btn,
  .ce-img-wrap .ce-img-btn {
    padding: 5px 12px;
    font-size: 12px;
    min-height: 34px;
  }
}

@media (max-width: 480px) {
  .ce-block {
    margin: 10px 0;
    border-radius: 6px;
  }
  .ce-block .ce-fold-btn,
  .ce-img-wrap .ce-img-btn {
    padding: 4px 10px;
    font-size: 11px;
    min-height: 32px;
  }
}
