:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e8eaed;
  --muted: #9aa1ad;
  --brand: #ff6b35;
  --ok: #35c46b;
  --err: #ff4d4f;
  /* 刘海屏/虚拟键安全区（非全面屏机型为 0） */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html {
  /* 禁止 iOS 横屏 / 系统字号设置把字体自动放大 */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent; /* 去掉 iOS 点按灰色方块 */
}

/* ---------------- 顶栏 ---------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px calc(16px + var(--safe-r)) 12px calc(16px + var(--safe-l));
  padding-top: calc(12px + var(--safe-t));
  background: rgba(15, 17, 21, .92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.brand { font-size: 17px; font-weight: 700; letter-spacing: .5px; white-space: nowrap; }
.brand::before { content: "●"; color: var(--brand); margin-right: 8px; }

.top-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

/* 实时通道状态：● 实时推送 / ○ 轮询 */
.rt-status {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.rt-status.ok { color: var(--ok); border-color: rgba(53, 196, 107, .5); }
.rt-status.warn { color: #ffd166; border-color: rgba(255, 209, 102, .4); }
.rt-status:empty { display: none; }

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px calc(16px + var(--safe-r)) calc(48px + var(--safe-b)) calc(16px + var(--safe-l));
}

/* ---------------- 基础控件 ---------------- */

button, .btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 14px;
  min-height: 44px;                 /* 触摸目标不小于 44px */
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  touch-action: manipulation;        /* 去掉双击缩放与点击延迟 */
  transition: background .15s, border-color .15s, transform .1s;
}
button:active, .btn:active { background: #262c37; border-color: var(--brand); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
button.primary:active { background: #e85c28; }

@media (hover: hover) {
  button:hover, .btn:hover { border-color: var(--brand); }
}

input, textarea, select {
  width: 100%;
  padding: 11px 12px;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 16px;                   /* 低于 16px 时 iOS 聚焦会放大整页 */
  font-family: inherit;
  outline: none;
  touch-action: manipulation;
}
textarea { min-height: 68px; resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--brand); }

/* ---------------- 卡片与表单 ---------------- */

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 14px;
}

.card h2 { margin: 0 0 12px; font-size: 16px; }
.card .hint { color: var(--muted); font-size: 13px; margin-top: 8px; }

.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row > * { flex: 1 1 160px; }
.row > button { flex: 0 0 auto; }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }

/* ---------------- 系统配置（server.json） ---------------- */

.cfg-group {
  margin: 18px 0 10px;
  padding-bottom: 6px;
  font-size: 14px;
  color: var(--text);
  border-bottom: 1px solid rgba(255, 255, 255, .08);
}
.badge-restart {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  font-size: 11px;
  border-radius: 4px;
  color: #ffb86b;
  border: 1px solid rgba(255, 184, 107, .45);
  background: rgba(255, 184, 107, .10);
}
input.readonly {
  color: var(--muted);
  background: rgba(255, 255, 255, .03);
  cursor: not-allowed;
}
/* 配置项多：桌面端两列排布（标题、按钮、说明占整行），手机端自动单列。
   注意必须限定 .active：否则 ID 选择器的 display 会压过面板隐藏规则 */
@media (min-width: 720px) {
  .admin-panels > #configCard.active { display: flex; flex-wrap: wrap; gap: 0 20px; align-items: flex-start; }
  #configCard .cfg-group { width: 100%; }
  #configCard .field { width: calc(50% - 10px); margin-bottom: 12px; }
  #configCard > h2, #configCard > p, #configCard .row, #configCard .preview-pair { width: 100%; }
}

/* ---------------- 照片墙 ---------------- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
}

.tile {
  position: relative;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border-radius: 10px;
  background: var(--panel-2);
  cursor: pointer;
  transition: transform .1s;
  touch-action: manipulation;
}
.tile:active { transform: scale(.97); }

.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }

.tile .badge {
  position: absolute;
  left: 6px;
  bottom: 6px;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, .55);
}

.tile .badge.ok { color: #7ee7a2; }
.tile .badge.pending { color: #ffd166; }
.tile .badge.fail { color: #ff8f8f; }
.tile .new-flag {
  position: absolute; right: 6px; top: 6px;
  background: var(--brand); color: #fff; font-size: 11px;
  border-radius: 6px; padding: 2px 6px;
}

/* ---------------- 上传列表 ---------------- */

.upload-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; border-bottom: 1px dashed var(--line);
}

.upload-item:last-child { border-bottom: none; }
.upload-item .name { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-item .pct { width: 62px; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }

.bar { height: 4px; border-radius: 3px; background: var(--panel-2); overflow: hidden; flex: 0 0 140px; }
.bar > i { display: block; height: 100%; width: 0; background: var(--brand); transition: width .2s; }

/* 移动端：真正的 file input 藏到屏幕外，用大块点按区触发。
   注意不要用 display:none —— iOS Safari 上对 display:none 的 file 控件
   调用 click() 有时不会弹出「拍照/相册」菜单。 */
.picker {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  margin: 0;
  padding: 0;
  border: 0;
  opacity: 0;
}

.picker-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 96px;
  padding: 14px;
  border: 1px dashed var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--muted);
  text-align: center;
  cursor: pointer;
  touch-action: manipulation;
}
.picker-area:active { border-color: var(--brand); }
.picker-area .big { font-size: 15px; font-weight: 600; color: var(--text); }
.picker-count { font-size: 13px; }

/* ---------------- 文本与代码 ---------------- */

/* 美颜实时预览：左原图 / 右效果 */
.preview-pair { display: flex; gap: 8px; margin-top: 10px; align-items: flex-start; }
.preview-pair img {
  width: 50%;
  min-width: 0;
  border-radius: 10px;
  background: var(--panel-2);
  display: block;
}
.preview-pair img:not([src]) { display: none; }
@media (max-width: 560px) {
  .preview-pair { flex-direction: column; }
  .preview-pair img { width: 100%; }
}

a { color: var(--brand); text-decoration: none; word-break: break-all; }
a:active { text-decoration: underline; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.ok-text { color: var(--ok); }
.err-text { color: var(--err); }

.code-box {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--panel-2); border: 1px dashed var(--brand);
  padding: 8px 14px; border-radius: 10px;
  font-size: 22px; font-weight: 700; letter-spacing: 4px;
  -webkit-user-select: text;
  user-select: text;                 /* 允许长按手动选择复制 */
}
/* 触摸设备没有 hover，用可见的“复制”字样替代 title 提示 */
.tap-copy { -webkit-touch-callout: default; cursor: pointer; }
.tap-copy::after { content: ""; }

@media (hover: none) {
  .tap-copy::after {
    content: "复制";
    font-size: 12px;
    font-weight: 400;
    letter-spacing: 0;
    color: var(--brand);
  }
}

/* ---------------- 提示条 ---------------- */

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b));
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  background: #000; color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: 14px; text-align: center; word-break: break-word;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 60;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------- 大图预览 ---------------- */

.lightbox {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .94);
  display: none; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  z-index: 50;
  /* 底部留足空间：手机浏览器（微信 / Safari）的底部工具栏约 50px 且会盖住视口底部，
     所以下载按钮不贴屏幕底，而是跟着布局流紧贴图片下方（见 .lb-actions）。 */
  padding: calc(52px + var(--safe-t)) calc(12px + var(--safe-r)) calc(76px + var(--safe-b)) calc(12px + var(--safe-l));
  overscroll-behavior: contain;
  touch-action: pan-y;
}
.lightbox.show { display: flex; }
/* 切换时淡入，轮播播放更顺眼；min-height:0 + flex 收缩：给下面的按钮留出位置 */
.lightbox img { max-width: 100%; max-height: 100%; min-height: 0; flex: 0 1 auto;
  border-radius: 8px; transition: opacity .25s ease; }

.lb-play {
  position: absolute; top: calc(6px + var(--safe-t)); left: calc(6px + var(--safe-l));
  min-height: 40px; padding: 0 14px;
  font-size: 13px; color: #fff;
  background: rgba(0, 0, 0, .45);
  border: 1px solid rgba(255, 255, 255, .18);
  border-radius: 20px;
}
.lb-play.on { background: var(--brand); border-color: var(--brand); font-weight: 600; }

.lb-close {
  position: absolute; top: calc(6px + var(--safe-t)); right: calc(6px + var(--safe-r));
  width: 44px; height: 44px; padding: 0;
  font-size: 28px; line-height: 1;
  background: rgba(0, 0, 0, .35); border: none; color: #fff; border-radius: 50%;
}
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 44px; height: 44px; padding: 0; min-height: 44px;
  display: flex; align-items: center; justify-content: center;
  font-size: 26px; line-height: 1;
  background: rgba(0, 0, 0, .4); border: 1px solid rgba(255, 255, 255, .16);
  color: #fff; border-radius: 50%;
}
.lb-prev { left: calc(4px + var(--safe-l)); }
.lb-next { right: calc(4px + var(--safe-r)); }
.lb-counter {
  position: absolute; top: calc(16px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  color: #fff; opacity: .75; font-size: 13px; font-variant-numeric: tabular-nums;
}
/* 当前预览图的真实分辨率（增强版会标注）：让"看到的"和"下载到的"一目了然 */
.lb-res {
  position: absolute; top: calc(36px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  color: #fff; opacity: .72; font-size: 12px; font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .5);
  pointer-events: none;
}
/* 两个下载入口并排：左「下载原图」（立即），右「下载 AI 增强」（生成期间显示进度）。
   注意：**不要绝对定位贴屏幕底** —— 手机浏览器底部工具栏会盖住它（曾表现为"移动端没有下载按钮"）。
   这里跟随纵向布局流，紧贴图片下方。 */
.lb-actions {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center;
  gap: 10px; flex: 0 0 auto; padding: 0 4px; max-width: 100%;
}
.lb-download {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px;                 /* 触摸目标：任何屏幕尺寸都不低于 44px */
  color: #fff; background: var(--brand); padding: 11px 18px; border-radius: 10px;
  text-decoration: none; font-size: 15px; white-space: nowrap;
}
.lb-download.ghost {
  background: rgba(0, 0, 0, .46); border: 1px solid rgba(255, 255, 255, .3);
}

/* 矮屏（横屏手机 / 小屏机）：收紧留白与按钮尺寸，
   保证「预览图 + 下载按钮」永远同时留在屏幕内（曾出现"有的手机看不到下载按钮"） */
@media (max-height: 560px) {
  .lightbox {
    padding: calc(36px + var(--safe-t)) calc(10px + var(--safe-r))
             calc(64px + var(--safe-b)) calc(10px + var(--safe-l));
    gap: 8px;
  }
  /* 只缩留白与字号，min-height:44px 依旧生效（触摸目标不缩水） */
  .lb-download { padding: 8px 14px; font-size: 14px; border-radius: 8px; }
  .lb-progress { width: min(280px, 70vw); }
}

/* AI 增强进度条：位于图片与两个按钮之间（同样跟随布局流，进度"看得见"且不会被工具栏盖住） */
.lb-progress {
  width: min(320px, 72vw); flex: 0 0 auto;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  color: #fff;
}
.lb-progress-track {
  width: 100%; height: 6px; border-radius: 99px;
  background: rgba(255, 255, 255, .22); overflow: hidden;
}
.lb-progress-bar {
  width: 0%; height: 100%; border-radius: 99px;
  background: var(--brand); transition: width .35s ease;
}
.lb-progress-text {
  font-size: 13px; opacity: .92; font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .55);
}
.lb-progress[hidden] { display: none; }

/* ---------------- 表格（桌面） ---------------- */

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 500; }
.table td.cell-ops { white-space: nowrap; }

.ops { display: flex; flex-wrap: wrap; gap: 6px; }
.ops button { padding: 7px 10px; min-height: 34px; font-size: 13px; }

/* 危险操作（删除相册）：红色，避免误点 */
button.danger {
  color: #ff6b6b;
  border-color: rgba(255, 107, 107, .45);
  background: rgba(255, 107, 107, .08);
}
button.danger:disabled { opacity: .55; }

/* 下载按钮：AI 增强进行中（呼吸提示，可再点一次下原图） */
.lb-download.busy {
  animation: dlBreath 1.4s ease-in-out infinite;
  border-color: var(--brand);
  color: var(--brand);
}
@keyframes dlBreath {
  0%, 100% { opacity: 1; }
  50% { opacity: .55; }
}

/* 管理端：推送到达时"照片数"闪一下，提示数据刚刚更新 */
@keyframes flashCount {
  from { background: rgba(255, 107, 53, .35); }
  to { background: transparent; }
}
td.flash { animation: flashCount 1.2s ease-out; }

/* ---------------- 管理端：侧边栏布局 ---------------- */

.admin-layout {
  display: grid;
  grid-template-columns: 208px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}
.admin-side {
  position: sticky;
  top: 80px;                 /* 顶栏高度 + 间距 */
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.admin-nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.admin-nav button {
  width: 100%;
  min-height: 42px;
  padding: 10px 12px;
  text-align: left;
  font-size: 14px;
  background: transparent;
  border-color: transparent;
}
.admin-nav button.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 600;
}
.admin-account {
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.admin-account .hint { margin: 0 0 8px; }
.admin-account .row { gap: 8px; }
.admin-account .row > button { flex: 1 1 auto; }
.admin-panels > .card { display: none; }
.admin-panels > .card.active { display: block; }

/* ============================================================
   手机端（H5）：表格转卡片、触摸目标加大、避免横向滚动
   ============================================================ */

@media (max-width: 760px) {
  main { padding: 12px calc(12px + var(--safe-r)) calc(40px + var(--safe-b)) calc(12px + var(--safe-l)); }
  .card { padding: 12px; border-radius: 10px; }
  .card h2 { font-size: 15px; }

  /* 后台：侧边栏变成顶部横向标签条（左右可滑） */
  .admin-layout { grid-template-columns: 1fr; gap: 10px; }
  .admin-side { position: static; gap: 8px; }
  .admin-nav {
    flex-direction: row;
    gap: 6px;
    padding: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .admin-nav button {
    flex: 0 0 auto;
    width: auto;
    min-height: 40px;
    padding: 8px 14px;
    white-space: nowrap;
  }
  .admin-account { padding: 8px 10px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 6px; }

  /* 后台相册表：一行变一张卡片，用 data-label 当字段名，彻底告别横向滚动 */
  .table { display: block; font-size: 14px; }
  .table thead { display: none; }
  .table tbody { display: block; }
  .table tr {
    display: block;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 10px;
  }
  .table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    width: 100%;
    padding: 5px 0;
    border-bottom: 0;
  }
  .table td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    color: var(--muted);
    font-size: 13px;
  }
  .table td:empty { display: none; }
  .table td > * { min-width: 0; }
  /* 相册名当作卡片标题，不显示字段名 */
  .table td.cell-title { font-size: 15px; font-weight: 600; }
  .table td.cell-title::before { content: none; }
  /* 操作区单独一行，按钮平分宽度，便于拇指点按 */
  .table td.cell-ops { display: block; padding-top: 8px; }
  .table td.cell-ops::before { display: block; margin-bottom: 6px; }
  .ops { gap: 6px; }
  .ops button { flex: 1 1 calc(50% - 3px); min-height: 40px; font-size: 14px; }

  .code-box { font-size: 20px; letter-spacing: 3px; padding: 7px 12px; }

  .upload-item { flex-wrap: wrap; row-gap: 4px; }
  .upload-item .name { flex: 1 1 100%; }
  .bar { flex: 1 1 auto; }
  .upload-item .pct { width: auto; }

  .lb-nav { display: none; }   /* 手机上左右滑动切换，箭头会挡照片 */

  .topbar { padding: 8px calc(12px + var(--safe-r)) 8px calc(12px + var(--safe-l)); padding-top: calc(8px + var(--safe-t)); }
  .brand { font-size: 16px; }
  .top-actions button { min-height: 38px; padding: 8px 12px; font-size: 14px; }
}

/* 小屏手机：按钮/输入独占一行，避免挤成两列 */
@media (max-width: 430px) {
  .row > * { flex: 1 1 100%; }
  .row > button { flex: 1 1 100%; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(94px, 1fr)); }
  .card h2 { font-size: 15px; }
}
