/* 网关控制台换肤（docs/11 §8、ADR-0021）：gateway-edge 在 base.css、brand.css 之后注入。
   针对 new-api v1.0.0-rc.51 的前端（shadcn / Tailwind v4，主题变量在 :root 与 html.dark 上，用户主题预设写在 <body> 的 data-theme-* 属性上）。
   做下面三件事，另有第 4、5 节的窄屏修正（列表页标题行不被挤没、时间范围按钮不截断；手机上把可点控件放大到 40 px、令牌名可折行），见文末：
   1. 把网关的主题变量映射到 --oem-* 令牌：主色及其派生、ring、侧栏、圆角、字体；浅色模式的文字与边框色也对齐平台。
      关键变量带 !important：用户在网关里选的主题预设（body[data-theme-preset] 等）和运行期插入的样式都盖不过它，品牌色以平台为准；
   2. 隐藏版本号与「可更新」徽标（只对网关管理员显示，客户不需要）；
   3. 被平台内嵌时（gateway.js 给 <html> 加 data-oem-embed="1"）隐藏网关自己的顶栏、左侧栏，去掉偏移，让内容占满 iframe。
   升级网关前端时按 docs/11 §8 的选择器清单复核（data-slot 属性、@container/system-brand、公共页顶栏）。 */

/* ---------- 1. 主题变量 ---------- */
:root:root,
html body {
  --primary: var(--oem-primary) !important;
  --primary-foreground: #fff !important;
  --ring: color-mix(in srgb, var(--oem-primary) 60%, #fff) !important;
  --secondary-foreground: color-mix(in srgb, var(--oem-primary) 75%, #000) !important;
  --info: var(--oem-primary) !important;
  --chart-1: var(--oem-primary) !important;
  --overview-accent-1: var(--oem-primary) !important;
  --sidebar-primary: var(--oem-primary) !important;
  --sidebar-primary-foreground: #fff !important;
  --sidebar-ring: color-mix(in srgb, var(--oem-primary) 60%, #fff) !important;
  --radius: var(--oem-radius) !important;
  --font-ui-sans: var(--oem-font-sans) !important;
  --font-body: var(--oem-font-sans) !important;
  --font-inter: var(--oem-font-sans) !important;
  --font-manrope: var(--oem-font-sans) !important;
  --font-public-sans: var(--oem-font-sans) !important;
  --font-mono: var(--oem-font-mono) !important;
}

/* 浅色：文字、边框、页面底色对齐平台；主色浅底（选中项、悬停）按平台的 light-9 派生 */
html:not(.dark),
html:not(.dark) body {
  --foreground: var(--oem-text-1) !important;
  --card-foreground: var(--oem-text-1) !important;
  --popover-foreground: var(--oem-text-1) !important;
  --accent-foreground: var(--oem-text-1) !important;
  --muted-foreground: var(--oem-text-2) !important;
  --border: var(--oem-card-border) !important;
  --accent: color-mix(in srgb, var(--oem-primary) 10%, #fff) !important;
  --sidebar: var(--oem-page-bg) !important;
  --sidebar-foreground: var(--oem-text-1) !important;
  --sidebar-border: var(--oem-card-border) !important;
  --sidebar-accent: color-mix(in srgb, var(--oem-primary) 10%, #fff) !important;
  --sidebar-accent-foreground: var(--oem-primary) !important;
}

/* 深色：只保证可读，主色略提亮，选中浅底按深色背景混合 */
html.dark,
html.dark body {
  --primary: color-mix(in srgb, var(--oem-primary) 80%, #fff) !important;
  --accent: color-mix(in srgb, var(--oem-primary) 22%, var(--background)) !important;
  --sidebar-accent: color-mix(in srgb, var(--oem-primary) 22%, var(--background)) !important;
  --sidebar-accent-foreground: #fff !important;
}

/* 左侧栏用平台的深色侧栏（变量只在侧栏内生效，外框与顶栏保持浅色）；桌面是外层 [data-slot=sidebar]（文字色设在它上面），手机是抽屉 */
[data-slot="sidebar"] {
  --sidebar: var(--oem-sidebar-bg) !important;
  --sidebar-foreground: var(--oem-sidebar-text) !important;
  --sidebar-accent: var(--oem-primary) !important;
  --sidebar-accent-foreground: #fff !important;
  --sidebar-border: rgba(255, 255, 255, .08) !important;
  --muted: rgba(255, 255, 255, .06) !important;
  --muted-foreground: rgba(255, 255, 255, .45) !important;
  --foreground: #fff !important;
}

/* 桌面侧栏与右侧内容卡片同样的圆角（inset 变体的内容区是 rounded-xl = radius × 1.4） */
[data-slot="sidebar"]:not([data-mobile="true"]) [data-slot="sidebar-inner"] {
  border-radius: calc(var(--radius) * 1.4);
}

/* 顶栏：平台式白底 + 下沿细线（深色模式下跟随网关自己的背景色） */
[data-slot="sidebar-wrapper"] > header {
  background: var(--background);
  border-bottom: 1px solid var(--border);
}

/* ---------- 2. 版本号与「可更新」 ---------- */
/* 顶栏（控制台页与公共页）的品牌区里紧跟 Logo 的那个按钮就是版本 / 更新入口，只对网关管理员渲染 */
[class*="@container/system-brand"] > button[data-slot="dialog-trigger"] {
  display: none !important;
}

/* ---------- 3. 被平台内嵌 ---------- */
html[data-oem-embed="1"] {
  --app-header-height: 0px !important;
}
/* 控制台页的顶栏、左侧栏（含占位 gap 与拖拽条）；公共页（模型广场等）的悬浮顶栏 */
html[data-oem-embed="1"] [data-slot="sidebar-wrapper"] > header,
html[data-oem-embed="1"] [data-slot="sidebar"]:not([data-mobile="true"]),
html[data-oem-embed="1"] [data-slot="sidebar-rail"],
html[data-oem-embed="1"] header.pointer-events-none.fixed {
  display: none !important;
}
/* 内容区去掉 inset 变体的外边距、圆角与阴影，占满 iframe */
html[data-oem-embed="1"] [data-slot="sidebar-wrapper"] {
  background: var(--background) !important;
}
html[data-oem-embed="1"] [data-slot="sidebar-inset"] {
  margin: 0 !important;
  border-radius: 0 !important;
  box-shadow: none !important;
}
/* 公共页（模型广场等）紧跟悬浮顶栏的正文给顶栏留了 4–5rem，内嵌时收回 */
html[data-oem-embed="1"] header.pointer-events-none.fixed + * {
  padding-top: 1rem !important;
}

/* ---------- 4. 列表页标题行与日志页工具栏 ---------- */
/* 标题区是 min-w-0 flex-1、操作按钮区 shrink-0，按钮多时（渠道页约 640–770 px 宽）标题被压成一个字宽也不折行；
   改为标题按内容宽、不收缩，放不下时操作按钮整体折到下一行 */
div.flex-wrap > div.min-w-0.flex-1:has(> h2.truncate) {
  flex: 1 0 auto;
  max-width: 100%;
}
/* 日志页的时间范围按钮：筛选网格只给它两列宽，1024 宽左右（Firefox、WebKit 字宽略大）秒数被省略；
   改为按内容宽、最宽不超过所在格，放不下时在空格处折成两行（按钮随之变高），不省略 */
button[data-slot="popover-trigger"]:has(> span.hidden.truncate) {
  width: auto;
  max-width: 100%;
  height: auto;
  min-height: 2rem;
}
button[data-slot="popover-trigger"] > span.hidden.truncate {
  white-space: normal;
  text-overflow: clip;
  text-align: left;
}

/* ---------- 5. 手机触控（≤ 640px） ---------- */
/* 可点控件不小于 40 × 40 px（网关默认 24–36 px）。网关的 Tailwind 样式都在 @layer 里，这里不分层，不用 !important 也盖得过；
   按 data-slot（shadcn 组件名）统一放大，不依赖 Tailwind 类名。勾选框、开关只用透明 ::after 扩点击区域，不改外观 */
@media (max-width: 640px) {
  button[data-slot="button"],
  a[data-slot="button"],
  button[data-slot$="-trigger"],
  [data-slot="toggle-group-item"],
  [data-slot="input-group-button"],
  [data-slot="numeric-spinner-value"],
  [class*="group/spinner"] button {
    min-height: 40px;
    min-width: 40px;
  }
  [data-slot="input"],
  [data-slot="form-control"],
  [data-slot="input-group"],
  [data-slot="input-group-control"],
  [data-slot="select-trigger"] {
    min-height: 40px;
  }
  [data-slot="tabs-list"] {
    height: auto;
  }
  [data-slot="tabs-trigger"] {
    min-height: 40px;
  }
  [data-slot="checkbox"],
  [data-slot="switch"] {
    position: relative;
  }
  [data-slot="checkbox"]::after,
  [data-slot="switch"]::after {
    content: "";
    position: absolute;
    inset: -12px;
  }
  /* 用量日志、渠道卡片里的分组 / 价格按钮（没有 data-slot，只能按 Tailwind 的 group 标记类选） */
  button.group.flex {
    min-height: 40px;
  }
  /* 顶栏、公共页、登录页左上角的品牌链接 */
  [class*="@container/system-brand"] a,
  #root > div.relative > a.absolute {
    min-height: 40px;
  }
  /* 用量日志卡片里的令牌名按钮：网关限宽单行省略且没有悬停提示，手机上看不到完整令牌名；改为可折行显示全名 */
  button[data-slot="button"]:has(> span.min-w-0.truncate.leading-normal) {
    height: auto;
    max-width: 100%;
  }
  button[data-slot="button"] > span.min-w-0.truncate.leading-normal {
    white-space: normal;
    overflow-wrap: anywhere;
    text-overflow: clip;
    text-align: left;
  }
  /* 登录表单里定位在标签行尾的小链接（「忘记密码？」）：透明 ::after 扩点击区域，不改排版 */
  form a.absolute::after {
    content: "";
    position: absolute;
    inset: -10px -6px;
  }
}
