/* ============================================================
   API 设置 —— 黑白毛玻璃
   材质规则：
   · 容器 = 半透明白 + backdrop-filter，靠背景透过来产生层次，不用实色填充
   · 边缘 = 低对比度（白色高光在上，极淡的黑在下），不画 1px 硬描边
   · 控件 = 胶囊（999px），大圆角，柔和大扩散阴影
   · 重要按钮 = 纯黑 + 像素切角（clip-path 台阶），是画面里唯一的高对比块
   · 导航不是实色白条：内容从底下穿过去，用 mask 渐隐 + 毛玻璃透出
   ============================================================ */

:root {
  --safe-top: max(env(safe-area-inset-top, 0px), var(--safe-top-fallback, 0px));
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;

  /* 单色系统：全部由黑的透明度派生，杜绝彩色混进来 */
  --ink: #17171a;
  --ink-2: rgb(23 23 26 / 62%);
  --ink-3: rgb(23 23 26 / 42%);
  --ink-4: rgb(23 23 26 / 26%);

  /* 大面板：白纱只有 26%，剩下的可读性靠 brightness 把壁纸的黑区提亮、
     白区压平来换 —— 这样壁纸的形状仍然透得过来，不会糊成一片白。 */
  --glass: rgb(255 255 255 / 26%);
  --glass-blur: saturate(180%) brightness(1.32) blur(34px);
  /* 小控件维持原来的白度，保证输入框里的文字始终踏实 */
  --glass-strong: rgb(255 255 255 / 62%);
  --glass-sunken: rgb(255 255 255 / 26%);
  --glass-ctrl-blur: saturate(150%) brightness(1.1) blur(14px);

  /* 低对比度边缘：顶部一道白高光，整体一圈极淡的黑 */
  --edge: inset 0 0 0 1px rgb(255 255 255 / 42%), inset 0 1px 0 0 rgb(255 255 255 / 72%), inset 0 0 0 1.5px rgb(23 23 26 / 5%);
  --lift: 0 1px 2px rgb(23 23 26 / 4%), 0 8px 20px -8px rgb(23 23 26 / 12%);
  --lift-lg: 0 2px 4px rgb(23 23 26 / 4%), 0 18px 40px -14px rgb(23 23 26 / 16%);

  --r-card: 28px;
  --r-sub: 22px;
  --r-pill: 999px;

  /* 4px 台阶的像素切角，跟主页那套点阵图标同一种语言 */
  --pixel-cut: polygon(
    0 8px, 4px 8px, 4px 4px, 8px 4px, 8px 0,
    calc(100% - 8px) 0, calc(100% - 8px) 4px, calc(100% - 4px) 4px, calc(100% - 4px) 8px, 100% 8px,
    100% calc(100% - 8px), calc(100% - 4px) calc(100% - 8px), calc(100% - 4px) calc(100% - 4px), calc(100% - 8px) calc(100% - 4px), calc(100% - 8px) 100%,
    8px 100%, 8px calc(100% - 4px), 4px calc(100% - 4px), 4px calc(100% - 8px), 0 calc(100% - 8px)
  );
  /* 小一号（3px 台阶），给 tab / choice 这类矮控件 */
  --pixel-cut-sm: polygon(
    0 6px, 3px 6px, 3px 3px, 6px 3px, 6px 0,
    calc(100% - 6px) 0, calc(100% - 6px) 3px, calc(100% - 3px) 3px, calc(100% - 3px) 6px, 100% 6px,
    100% calc(100% - 6px), calc(100% - 3px) calc(100% - 6px), calc(100% - 3px) calc(100% - 3px), calc(100% - 6px) calc(100% - 3px), calc(100% - 6px) 100%,
    6px 100%, 6px calc(100% - 3px), 3px calc(100% - 3px), 3px calc(100% - 6px), 0 calc(100% - 6px)
  );
  /* 扫描线：叠在黑块上的一层极淡横纹，凑近才看得见 */
  --pixel-scan: repeating-linear-gradient(0deg, transparent 0 2px, rgb(255 255 255 / 5%) 2px 3px);

  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; margin: 0; }
body { min-height: 100vh; display: grid; place-items: center; overflow: hidden; background: #ececed; }
button, input, select, textarea { font: inherit; color: inherit; }
button { -webkit-tap-highlight-color: transparent; }

/* 玻璃需要背景才成立：一层单色的柔光底，给 blur 提供可折射的内容 */
.api-phone {
  position: relative;
  width: min(100vw, calc(100vh * 393 / 852), 393px);
  aspect-ratio: 393 / 852;
  overflow: hidden;
  background-color: #e9e9eb;
  background-image: var(--page-wallpaper, url("参考图/默认背景图背景图.jpg"));
  background-size: cover;
  background-position: center;
  box-shadow: 0 24px 70px rgb(23 23 26 / 12%);
}

.api-status { display: none !important; }

/* ---------- 顶部：不是固定白条 ---------- */
/* 背景只有 22% 白 + 强 blur，内容从底下滚过去时看得见轮廓在动。
   底边不画线，用 mask 让整条向下渐隐，融进内容里。 */
.api-toolbar {
  position: absolute;
  top: 0; left: 0;
  z-index: 3;
  width: 100%;
  height: calc(64px + var(--safe-top));
  padding-top: var(--safe-top);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: linear-gradient(180deg, rgb(255 255 255 / 60%), rgb(255 255 255 / 22%));
  -webkit-backdrop-filter: saturate(160%) blur(20px);
  backdrop-filter: saturate(160%) blur(20px);
  -webkit-mask-image: linear-gradient(180deg, #000 0, #000 62%, transparent 100%);
  mask-image: linear-gradient(180deg, #000 0, #000 62%, transparent 100%);
}
.api-toolbar h1 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: .2px; line-height: 1; }
.api-back {
  position: absolute;
  left: 14px;
  top: calc(var(--safe-top) + 32px);
  width: 36px; height: 36px;
  padding: 0 0 4px;
  transform: translateY(-50%);
  display: grid; place-items: center;
  border: 0;
  border-radius: var(--r-pill);
  background: var(--glass-strong);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  box-shadow: var(--edge), var(--lift);
  font-family: Arial, sans-serif;
  font-size: 26px; font-weight: 300; line-height: 1;
  cursor: pointer;
}
.api-back:active { background: var(--glass); }
.api-back:focus-visible { outline: 2px solid var(--ink-4); outline-offset: 3px; }

/* ---------- 分类：胶囊分段控件，浮在内容上 ---------- */
.api-tabs {
  position: absolute;
  top: calc(58px + var(--safe-top));
  left: 12px; right: 12px;
  z-index: 3;
  height: 44px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 3px;
  padding: 4px;
  border: 0;
  border-radius: var(--r-pill);
  background: var(--glass);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  box-shadow: var(--edge), var(--lift);
}
.api-tabs button {
  border: 0;
  border-radius: var(--r-pill);
  color: var(--ink-2);
  background: transparent;
  font-size: 13.5px;
  font-weight: 500;
  letter-spacing: .2px;
  cursor: pointer;
  transition: color .18s ease, background .18s ease;
}
.api-tabs button:active { background: rgb(255 255 255 / 45%); }
/* 选中态：像素切角的黑块。box-shadow 会被 clip-path 裁掉，
   所以投影一律用 drop-shadow —— 它跟着裁完的形状走。 */
.api-tabs button.active {
  color: #fff;
  background: var(--ink);
  background-image: var(--pixel-scan);
  border-radius: 0;
  clip-path: var(--pixel-cut-sm);
  filter: drop-shadow(2px 2px 0 rgb(23 23 26 / 20%));
  font-weight: 550;
}

/* ---------- 滚动区：从导航底下穿过去 ---------- */
/* inset 顶部为 0（不是给导航让位），靠 padding-top 把首屏内容推下来，
   于是滚动时内容确实穿过毛玻璃。上下各一段 mask 渐隐。 */
.api-form {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: calc(118px + var(--safe-top)) 16px calc(40px + env(safe-area-inset-bottom, 0px));
  -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 calc(112px + var(--safe-top)), #000 calc(100% - 28px), transparent 100%);
  mask-image: linear-gradient(180deg, transparent 0, #000 calc(112px + var(--safe-top)), #000 calc(100% - 28px), transparent 100%);
  scrollbar-width: none;
}
.api-form::-webkit-scrollbar { display: none; }

/* 唯二直接压在壁纸上的文字。壁纸本身是高对比黑白，
   同一句话在白区读得清、滑到黑区就没了，所以各自垫一小片胶囊玻璃。 */
.category-hint, .api-field strong {
  display: inline-block;
  padding: 4px 13px;
  border-radius: var(--r-pill);
  background: rgb(255 255 255 / 52%);
  -webkit-backdrop-filter: saturate(150%) brightness(1.1) blur(12px);
  backdrop-filter: saturate(150%) brightness(1.1) blur(12px);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 40%);
}
.category-hint { margin: 2px 0 16px; color: var(--ink-2); font-size: 13px; line-height: 19px; }
.api-field strong { margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--ink); }
/* 卡片里的标签本来就有卡片当底，不需要再叠一层 */
.api-card .api-field strong {
  display: block;
  padding: 0 0 0 4px;
  border-radius: 0;
  background: none;
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  box-shadow: none;
  color: var(--ink-2);
}

/* ---------- 卡片 ---------- */
.api-card {
  margin: 0 0 16px;
  padding: 22px 20px;
  border: 0;
  border-radius: var(--r-card);
  background: var(--glass);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  box-shadow: var(--edge), var(--lift-lg);
}
.api-card h2 { margin: 0 0 7px; font-size: 19px; font-weight: 620; letter-spacing: -.2px; }
.api-card h3 { margin: 22px 0 10px; font-size: 15.5px; font-weight: 600; }
.api-card p, .api-card small { color: var(--ink-2); font-size: 13px; line-height: 1.6; }
.api-card h3 small { float: right; color: var(--ink-3); font-size: 12px; font-weight: 500; }

/* 内层卡片比外层"更凹"：白度更低，看起来是嵌进玻璃里的 */
.sub-card {
  position: relative;
  margin-top: 16px;
  padding: 18px 16px;
  border: 0;
  border-radius: var(--r-sub);
  background: var(--glass-sunken);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 34%), inset 0 1px 0 0 rgb(255 255 255 / 55%);
}
.sub-card strong, .sub-card b { display: block; font-size: 15px; font-weight: 600; }
.sub-card small { display: block; margin-top: 8px; }

/* ---------- 胶囊控件 ---------- */
.soft-button, .choice, .wide-input, .sub-card select,
.api-field input, .api-field select,
.character-api-card input, .character-api-card select,
.voice-tags button, .range-label output {
  min-height: 44px;
  border: 0;
  border-radius: var(--r-pill);
  color: var(--ink);
  background: var(--glass-strong);
  -webkit-backdrop-filter: var(--glass-ctrl-blur);
  backdrop-filter: var(--glass-ctrl-blur);
  box-shadow: var(--edge), 0 1px 2px rgb(23 23 26 / 4%);
  font-size: 14px;
  font-weight: 500;
}
.soft-button {
  margin-top: 12px;
  padding: 0 22px;
  border-radius: 0;
  clip-path: var(--pixel-cut-sm);
  box-shadow: none;
  letter-spacing: .5px;
  cursor: pointer;
}
.soft-button:active, .choice:active, .voice-tags button:active { background: var(--glass); }

.choice-row { display: flex; gap: 8px; flex-wrap: wrap; }
.choice { flex: 1; padding: 9px 12px; cursor: pointer; text-align: center; }
.choice small { display: block; color: var(--ink-3); font-size: 11.5px; font-weight: 400; }
/* 选中的胶囊反相成像素黑块 */
.choice.active {
  color: #fff;
  background: var(--ink);
  background-image: var(--pixel-scan);
  border-radius: 0;
  clip-path: var(--pixel-cut-sm);
  box-shadow: none;
  filter: drop-shadow(2px 2px 0 rgb(23 23 26 / 20%));
}
.choice.active small { color: rgb(255 255 255 / 62%); }

.wide-input { width: 100%; padding: 0 18px; }
.voice-tags { display: flex; flex-wrap: wrap; gap: 8px; }
.voice-endpoints { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.voice-endpoints .choice { min-width: 0; min-height: 64px; overflow-wrap: anywhere; }
.voice-endpoints .choice:last-child { grid-column: 1 / -1; }
.voice-field { display: grid; gap: 8px; margin-top: 16px; font-size: 13px; }
.voice-field input { min-width: 0; }
#voice-provider-hint { display: block; margin-top: 12px; overflow-wrap: anywhere; }
#voice-group-fields[hidden] { display: none; }
.voice-tags button { min-height: 38px; padding: 0 15px; font-size: 13px; font-weight: 500; cursor: pointer; }

/* 提示块：原来是黄底，改成同一套玻璃，只靠左侧一道黑条区分 */
.note, .billing {
  margin-top: 12px;
  padding: 13px 15px;
  border: 0;
  border-left: 3px solid var(--ink-4);
  border-radius: 6px 16px 16px 6px;
  color: var(--ink-2);
  background: var(--glass-sunken);
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 30%);
  font-size: 12.5px;
  line-height: 1.6;
}
.billing { margin-top: 16px; }

/* ---------- 开关 ---------- */
.switch { position: absolute; top: 18px; right: 16px; }
.switch input { display: none; }
.switch i {
  display: block;
  width: 52px; height: 31px;
  border-radius: var(--r-pill);
  background: var(--glass-sunken);
  box-shadow: inset 0 0 0 1px rgb(23 23 26 / 8%), inset 0 1px 3px rgb(23 23 26 / 8%);
  transition: background .22s ease;
}
.switch i::after {
  content: "";
  display: block;
  width: 25px; height: 25px;
  margin: 3px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(23 23 26 / 22%);
  transition: transform .22s cubic-bezier(.32,.72,0,1);
}
.switch input:checked + i { background: var(--ink); box-shadow: none; }
.switch input:checked + i::after { transform: translateX(21px); }

/* ---------- 滑块 ---------- */
.range-label { display: grid; grid-template-columns: 1fr 62px; gap: 10px; align-items: center; color: var(--ink-2); font-size: 14px; }
.range-label output { display: grid; place-items: center; min-height: 38px; font-variant-numeric: tabular-nums; }
.range-label input { grid-column: 1 / -1; width: 100%; height: 28px; appearance: none; background: transparent; }
.range-label input::-webkit-slider-runnable-track { height: 6px; border-radius: var(--r-pill); background: rgb(23 23 26 / 12%); }
.range-label input::-webkit-slider-thumb { appearance: none; width: 24px; height: 24px; margin-top: -9px; border-radius: 50%; background: var(--ink); box-shadow: 0 2px 6px rgb(23 23 26 / 35%); }
.range-label input::-moz-range-track { height: 6px; border-radius: var(--r-pill); background: rgb(23 23 26 / 12%); }
.range-label input::-moz-range-thumb { width: 24px; height: 24px; border: 0; border-radius: 50%; background: var(--ink); }

/* ---------- 多行输入 ---------- */
.preference {
  width: 100%;
  min-height: 140px;
  resize: vertical;
  margin-top: 4px;
  padding: 15px 17px;
  border: 0;
  border-radius: var(--r-sub);
  background: var(--glass-strong);
  box-shadow: var(--edge), 0 1px 2px rgb(23 23 26 / 4%);
  font: inherit;
  font-size: 14px;
  line-height: 1.65;
}
.count { float: right; color: var(--ink-3); }

/* ---------- 表单字段 ---------- */
.api-field { display: block; margin: 0 0 15px; }

.api-field input, .api-field select { width: 100%; height: 46px; padding: 0 18px; outline: 0; font-size: 16px; font-weight: 400; }
.character-api-card > label, .character-api-card > div > label { display: grid; gap: 7px; margin-top: 13px; color: var(--ink-3); font-size: 12px; font-weight: 600; }
.character-api-card input, .character-api-card select { width: 100%; height: 42px; padding: 0 16px; outline: 0; font-size: 16px; font-weight: 400; }
.sub-card select { width: 100%; padding: 0 16px; }

input:focus, select:focus, textarea:focus {
  outline: 0;
  box-shadow: inset 0 0 0 1.5px var(--ink-4), 0 0 0 4px rgb(23 23 26 / 6%);
}
input::placeholder, textarea::placeholder { color: var(--ink-4); }

.select-wrap { position: relative; display: block; }
.select-wrap::after {
  content: "";
  position: absolute;
  top: 50%; right: 20px;
  width: 8px; height: 8px;
  margin-top: -6px;
  transform: rotate(45deg);
  border-right: 1.6px solid var(--ink-2);
  border-bottom: 1.6px solid var(--ink-2);
  pointer-events: none;
}
.select-wrap select { appearance: none; padding-right: 46px; }

/* .character-api-card / .sub-card 里的 select 没有 .select-wrap 包裹，
   select 又挂不了伪元素，所以用同一个形状的 SVG 背景补上箭头，
   免得这几个控件露出系统原生的三角，跟别处对不上。 */
.character-api-card select, .sub-card select {
  appearance: none;
  padding-right: 42px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5l5-5' fill='none' stroke='%2317171a' stroke-opacity='.62' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 18px center;
}

/* ---------- 重要按钮：黑色像素 ---------- */
/* 唯一的实心高对比块。clip-path 切出 4px 台阶的像素角，
   跟主页那套点阵图标是同一种语言。注意：clip-path 会裁掉 box-shadow，
   所以投影画在外面的 .pixel-shadow 包裹层上 —— 这里改用 filter 代替。 */
.load-models, .save-api {
  width: 100%;
  height: 50px;
  border: 0;
  border-radius: 0;
  color: #fff;
  background: var(--ink);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 1.5px;
  cursor: pointer;
  background-image: var(--pixel-scan);
  clip-path: var(--pixel-cut);
  /* 第一层是硬边的像素位移投影（blur 0），第二层才是柔和的环境光 */
  filter: drop-shadow(4px 4px 0 rgb(23 23 26 / 26%)) drop-shadow(0 8px 18px rgb(23 23 26 / 22%));
  transition: transform .1s ease, filter .1s ease;
}
.load-models { margin: 4px 0 14px; background: rgb(23 23 26 / 82%); }
.save-api { margin-top: 4px; }
/* 按下：块往投影的位置挪 4px，硬投影同时收掉，是像素按钮的「压下去」 */
.load-models:active, .save-api:active {
  transform: translate(4px, 4px);
  filter: drop-shadow(0 0 0 rgb(23 23 26 / 0%)) drop-shadow(0 2px 6px rgb(23 23 26 / 20%));
}

.key-field, .model-field { margin-bottom: 18px; }

/* ---------- 分类切换 ---------- */
.category-card { display: none; }
.api-form[data-category="chat"] .category-card[data-panel="chat"],
.api-form[data-category="image"] .category-card[data-panel="image"],
.api-form[data-category="game"] .category-card[data-panel="game"],
.api-form[data-category="voice"] .category-card[data-panel="voice"],
.api-form[data-category="social"] .category-card[data-panel="social"] { display: block; }
.api-form[data-category="game"] .api-field,
.api-form[data-category="game"] > .load-models,
.api-form[data-category="game"] > .model-field,
.api-form[data-category="voice"] > .api-service-field,
.api-form[data-category="voice"] > .model-field { display: none; }

@media (max-width: 480px) {
  body { background: #ececed; }
  .api-phone { width: 100vw; height: 100vh; aspect-ratio: auto; box-shadow: none; }
}

/* 不支持 backdrop-filter 时（旧 Firefox / 部分安卓 WebView）玻璃会变成
   一片几乎透明的白，文字直接压在背景上。这里把白度补上换成实色回退。 */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  :root {
    --glass: rgb(255 255 255 / 86%);
    --glass-strong: rgb(255 255 255 / 94%);
    --glass-sunken: rgb(255 255 255 / 70%);
  }
  .api-toolbar, .api-tabs { background: rgb(255 255 255 / 90%); }
}

/* 数据备份卡片：和 .soft-button 同一套像素切角 */
#backup-card .soft-button, #backup-card .upload-button { width: 100%; height: 44px; display: grid; place-items: center; }
#backup-card .upload-button { margin-top: 10px; padding: 0 22px; border-radius: 0; clip-path: var(--pixel-cut-sm); color: var(--ink); background: var(--glass-strong); font-size: 14px; font-weight: 600; cursor: pointer; }
#backup-card .upload-button:active { background: var(--glass); }
#backup-status { display: block; min-height: 16px; margin-top: 10px; }
