/* ============================================================================
 * QQboot UI · 桌面端布局层（Desktop Layout）
 * ----------------------------------------------------------------------------
 * 只负责骨架：侧栏 / 顶栏 / 内容区 / 弹窗定位。
 * 组件样式在 components.css，令牌在 tokens.css。
 * ========================================================================== */

/* ============================================================================
 * 1. 应用骨架
 * ========================================================================== */
.app {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ============================================================================
 * 2. 侧边栏
 * ========================================================================== */
.app-sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: var(--z-sticky);
  display: flex;
  flex-direction: column;
  width: var(--sidebar-width);
  background: var(--card);
  border-right: 1px solid var(--border);
  transition: width var(--duration-slow) var(--ease-out),
              transform var(--duration-slow) var(--ease-out);
}

.sidebar__brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--topbar-height);
  padding: 0 var(--space-4);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  overflow: hidden;
}
.sidebar__logo {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 32px; height: 32px;
  border-radius: var(--radius-md);
  background: var(--primary);
  color: var(--primary-foreground);
  font-weight: var(--weight-bold);
  font-size: var(--text-md);
  letter-spacing: -.02em;
}
.sidebar__name {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  white-space: nowrap;
}
.sidebar__env {
  margin-left: auto;
  padding: 1px var(--space-2);
  border: 1px solid var(--primary-border);
  border-radius: var(--radius-xs);
  background: var(--primary-soft);
  color: var(--primary-soft-foreground);
  font-size: var(--text-2xs);
  font-weight: var(--weight-medium);
  white-space: nowrap;
}

.sidebar__scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--space-3) var(--space-3) var(--space-4);
}

.nav-group + .nav-group { margin-top: var(--space-5); }
.nav-group__label {
  padding: 0 var(--space-2) var(--space-2);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--muted-foreground);
  white-space: nowrap;
}

.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: 38px;
  padding: 0 var(--space-3);
  margin-bottom: 2px;
  border-radius: var(--radius-md);
  color: var(--muted-foreground);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}
.nav-item:hover {
  background: var(--muted);
  color: var(--foreground);
  text-decoration: none;
}
.nav-item[aria-current="page"] {
  background: var(--primary-soft);
  color: var(--primary-soft-foreground);
  font-weight: var(--weight-semibold);
}
.nav-item[aria-current="page"]::before {
  content: "";
  position: absolute;
  left: calc(var(--space-3) * -1 + 1px);
  top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 18px;
  background: var(--primary);
  border-radius: 0 var(--radius-pill) var(--radius-pill) 0;
}
.nav-item__icon {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 18px; height: 18px;
}
.nav-item__icon svg { width: 100%; height: 100%; }
.nav-item__text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.nav-item__badge {
  flex: 0 0 auto;
  min-width: 20px;
  height: 18px;
  padding: 0 5px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-pill);
  background: var(--destructive-soft);
  color: var(--destructive);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  font-variant-numeric: tabular-nums;
}
.nav-item[aria-current="page"] .nav-item__badge {
  background: var(--destructive);
  color: var(--destructive-foreground);
}

/* 侧栏底部用户区 */
.sidebar__footer {
  flex: 0 0 auto;
  padding: var(--space-3);
  border-top: 1px solid var(--border);
}
.sidebar__user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  padding: var(--space-2);
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  text-align: left;
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out);
}
.sidebar__user:hover { background: var(--muted); }
.sidebar__user-meta { min-width: 0; flex: 1; }
.sidebar__user-name {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar__user-role {
  font-size: var(--text-2xs);
  color: var(--muted-foreground);
}

/* 折叠态 */
.app--collapsed .app-sidebar { width: var(--sidebar-width-collapsed); }
.app--collapsed .app-main { margin-left: var(--sidebar-width-collapsed); }
.app--collapsed .sidebar__name,
.app--collapsed .sidebar__env,
.app--collapsed .nav-group__label,
.app--collapsed .nav-item__text,
.app--collapsed .nav-item__badge,
.app--collapsed .sidebar__user-meta { display: none; }
.app--collapsed .sidebar__brand { justify-content: center; padding: 0; }
.app--collapsed .nav-item { justify-content: center; padding: 0; }
.app--collapsed .sidebar__user { justify-content: center; }
.app--collapsed .nav-group__label {
  display: block;
  height: 1px;
  margin: var(--space-3) var(--space-2);
  padding: 0;
  overflow: hidden;
  background: var(--border);
  font-size: 0;
}

/* ============================================================================
 * 3. 主区域与顶栏
 * ========================================================================== */
.app-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  margin-left: var(--sidebar-width);
  transition: margin-left var(--duration-slow) var(--ease-out);
}

.app-topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--topbar-height);
  padding: 0 var(--space-6);
  background: var(--glass-bg-strong);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  border-bottom: 1px solid var(--border);
}

.topbar__toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 32px; height: 32px;
  margin-left: calc(var(--space-2) * -1);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted-foreground);
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}
.topbar__toggle:hover { background: var(--muted); color: var(--foreground); }
.topbar__toggle svg { width: 17px; height: 17px; }

/* 面包屑 */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  font-size: var(--text-sm);
  color: var(--muted-foreground);
}
.breadcrumb a { color: var(--muted-foreground); }
.breadcrumb a:hover { color: var(--foreground); text-decoration: none; }
/* 分隔符用伪元素绘制，不用文字：避免「正文用边框色」的对比度问题，
   屏幕阅读器也不会读到多余的斜杠（外层已 aria-hidden） */
.breadcrumb__sep {
  flex: 0 0 auto;
  width: 1px;
  height: 12px;
  background: var(--border-strong);
  transform: rotate(18deg);
}
.breadcrumb__current {
  color: var(--foreground);
  font-weight: var(--weight-medium);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar__spacer { flex: 1; }

.topbar__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: 0 0 auto;
}

/* 顶栏搜索：宽屏显示，窄屏收起成图标 */
.topbar__search {
  position: relative;
  width: 240px;
  flex: 0 0 auto;
}
.topbar__search svg {
  position: absolute;
  left: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  width: 15px; height: 15px;
  color: var(--subtle-foreground);
  pointer-events: none;
}
.topbar__search .input {
  height: 34px;
  min-height: 34px;
  padding-left: var(--space-8);
  background: var(--muted);
  border-color: transparent;
  font-size: var(--text-sm);
}
.topbar__search .input:focus { background: var(--card); border-color: var(--ring); }

.kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-xs);
  background: var(--card);
  color: var(--muted-foreground);
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  line-height: 1;
}

/* 通知按钮的未读点 */
.icon-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 34px; height: 34px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--muted-foreground);
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out);
}
.icon-btn:hover { background: var(--muted); color: var(--foreground); }
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn__dot {
  position: absolute;
  top: 7px; right: 8px;
  width: 7px; height: 7px;
  border: 1.5px solid var(--card);
  border-radius: 50%;
  background: var(--destructive);
}

/* ============================================================================
 * 4. 内容区
 * ========================================================================== */
.app-content {
  flex: 1;
  width: 100%;
  max-width: var(--container-max);
  margin: 0 auto;
  padding: var(--space-6);
}

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}
.page-head__main { min-width: 0; }
.page-head__title {
  font-size: var(--text-xl);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
}
.page-head__desc {
  margin-top: var(--space-1);
  font-size: var(--text-sm);
  color: var(--muted-foreground);
  line-height: var(--leading-snug);
}
.page-head__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: 0 0 auto;
}

/* 统计卡栅格 */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

/* 两栏布局：主内容 + 侧栏 */
.layout-split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: var(--space-5);
  align-items: start;
}

/* 设置页：左分类导航 + 右表单 */
.settings-layout {
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr);
  gap: var(--space-5);
  align-items: start;
}
.settings-nav {
  position: sticky;
  top: calc(var(--topbar-height) + var(--space-6));
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-2);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
}
.settings-nav__item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: 36px;
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted-foreground);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out);
}
.settings-nav__item:hover { background: var(--muted); color: var(--foreground); }
.settings-nav__item[aria-selected="true"] {
  background: var(--primary-soft);
  color: var(--primary-soft-foreground);
  font-weight: var(--weight-semibold);
}
.settings-nav__item svg { width: 15px; height: 15px; flex: 0 0 auto; }

/* 工具栏：搜索 + 筛选 + 操作 */
.toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}
.toolbar__search { width: 260px; flex: 0 0 auto; }
.toolbar__spacer { flex: 1; }

/* ============================================================================
 * 5. 主题切换按钮
 * ========================================================================== */
.theme-toggle .icon-sun,
.theme-toggle .icon-moon { display: none; }
[data-theme="light"] .theme-toggle .icon-moon,
:root:not([data-theme="dark"]) .theme-toggle .icon-moon { display: block; }
[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

/* ============================================================================
 * 6. 响应式
 * ========================================================================== */

/* 小桌面：统计卡 2 列 */
@media (max-width: 1279px) {
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .layout-split { grid-template-columns: minmax(0, 1fr); }
  .topbar__search { width: 180px; }
}

/* 平板：侧栏折叠为图标栏 */
@media (max-width: 1023px) {
  .app-sidebar { width: var(--sidebar-width-collapsed); }
  .app-main { margin-left: var(--sidebar-width-collapsed); }
  .sidebar__name,
  .sidebar__env,
  .nav-group__label,
  .nav-item__text,
  .nav-item__badge,
  .sidebar__user-meta { display: none; }
  .sidebar__brand { justify-content: center; padding: 0; }
  .nav-item { justify-content: center; padding: 0; }
  .sidebar__user { justify-content: center; }
  .topbar__search { display: none; }
  .settings-layout { grid-template-columns: minmax(0, 1fr); }
  .settings-nav {
    position: static;
    flex-direction: row;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .settings-nav::-webkit-scrollbar { display: none; }
  .app-content { padding: var(--space-5); }
}

/* 手机宽度：桌面模板退化为抽屉导航（手机端应使用 mobile/ 模板） */
@media (max-width: 767px) {
  .app-sidebar {
    /* 82vw：390px 屏幕上约 320px，既容得下中文又不至于盖住大半个页面 */
    width: min(82vw, 280px);
    transform: translateX(-100%);
    box-shadow: var(--shadow-xl);
  }
  /* 关键修复：1023px 断点把侧栏改成了纯图标态（文字 display:none），
     这里宽度又回到 248px，若不恢复文字就会变成「又宽又空」的一条。
     手机抽屉必须显示完整文字。 */
  .sidebar__name,
  .sidebar__env,
  .nav-group__label,
  .nav-item__text,
  .nav-item__badge,
  .sidebar__user-meta { display: revert; }
  .sidebar__brand { justify-content: flex-start; padding: 0 var(--space-4); }
  .nav-item { justify-content: flex-start; padding: 0 var(--space-3); }
  .nav-item__text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
  .sidebar__user { justify-content: flex-start; }
  /* 用户区恢复上下两行（名字 + 邮箱），否则会被挤成一行 */
  .sidebar__user-meta {
    display: flex;
    flex-direction: column;
    min-width: 0;
  }
  .sidebar__user-name,
  .sidebar__user-role {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .nav-group__label {
    display: block;
    height: auto;
    margin: 0;
    padding: 0 var(--space-2) var(--space-2);
    background: transparent;
    font-size: var(--text-2xs);
  }
  .app--drawer-open .app-sidebar { transform: translateX(0); }
  .app-main { margin-left: 0; }
  .app-content { padding: var(--space-4); }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
  .page-head { flex-direction: column; align-items: stretch; }
  .page-head__actions { justify-content: flex-start; flex-wrap: wrap; }
  .app-topbar {
    padding: 0 var(--space-3);
    gap: var(--space-2);
  }
  /* 面包屑在窄屏只留当前页，且禁止换行（否则会被挤成三行） */
  .breadcrumb { min-width: 0; flex: 1; overflow: hidden; }
  .breadcrumb a,
  .breadcrumb__sep { display: none; }
  .breadcrumb__current {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: var(--text-sm);
  }
  /* 顶栏右侧「站点设置」文字按钮在窄屏只留图标 */
  .topbar__actions .btn--outline {
    width: 34px;
    padding: 0;
    font-size: 0;
    gap: 0;
  }
  .topbar__actions .btn--outline .btn__icon { font-size: initial; }
  .topbar__actions .btn--outline .btn__icon svg { width: 16px; height: 16px; }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .toolbar__search { width: 100%; }
}

/* 极窄 */
@media (max-width: 359px) {
  .app-content { padding: var(--space-3); }
  .stat-grid { grid-template-columns: minmax(0, 1fr); }
}

/* 抽屉遮罩（仅手机宽度使用） */
.drawer-mask {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-sticky) - 1);
  background: rgba(15, 21, 32, .45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--duration-base) var(--ease-out),
              visibility var(--duration-base);
}
.app--drawer-open .drawer-mask { opacity: 1; visibility: visible; }

@media (min-width: 768px) {
  .drawer-mask { display: none; }
}
