:root {
  --bg: #ffffff;
  --card: #fafafa;
  --primary: #3b82f6;
  --success: #10b981;
  --danger: #ef4444;
  --text: #111111;
  --text-2: #444444;
  --muted: #888888;
  --border: #e8e8e8;
  --radius: 6px;
  --sidebar-w: 240px;
  --sidebar-collapsed: 72px;
  --topbar-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: #2563eb; }

.layout {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  background: var(--bg);
}

.sidebar {
  width: var(--sidebar-w);
  background: #fff;
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  transition: transform .25s ease, width .2s ease;
  flex-shrink: 0;
  z-index: 200;
  padding-bottom: var(--safe-bottom);
}

.layout.collapsed .sidebar { width: var(--sidebar-collapsed); }
.layout.collapsed .sidebar .nav-text,
.layout.collapsed .sidebar .brand-sub,
.layout.collapsed .sidebar .menu-group-title { display: none; }
.layout.collapsed .sidebar .brand-title { font-size: 14px; }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(17, 17, 17, 0.35);
  z-index: 190;
}

.sidebar-close {
  display: none;
  position: absolute;
  top: 14px;
  right: 12px;
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text-2);
  line-height: 1;
  cursor: pointer;
}

.brand {
  padding: 20px 16px 16px;
  border-bottom: 1px solid var(--border);
  position: relative;
}
.brand-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.5px;
}
.brand-sub {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.menu { padding: 12px 8px 24px; }
.menu-group-title {
  padding: 12px 12px 6px;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.menu a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  margin: 2px 0;
  border-radius: var(--radius);
  color: var(--text-2);
  min-height: 42px;
}
.menu a:hover { background: var(--card); color: var(--text); }
.menu a.active {
  background: rgba(59, 130, 246, 0.1);
  color: var(--primary);
  font-weight: 600;
}
.menu .icon {
  width: 18px;
  text-align: center;
  flex-shrink: 0;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  min-height: var(--topbar-h);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 20px;
  padding-top: calc(8px + var(--safe-top));
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 90;
}

.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; }
.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-2);
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.topbar-user { white-space: nowrap; }
.topbar-user .role-tag { color: var(--muted); }

.btn-collapse {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: var(--radius);
  width: 40px;
  height: 40px;
  cursor: pointer;
  color: var(--text-2);
  flex-shrink: 0;
  font-size: 18px;
  line-height: 1;
}

.content {
  padding: 20px 24px calc(40px + var(--safe-bottom));
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

.breadcrumb-nav {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 16px;
  word-break: break-all;
}
.breadcrumb-nav a { color: var(--muted); }
.breadcrumb-nav .current { color: var(--text-2); }

.page-title {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 6px;
  word-break: break-word;
}
.page-desc {
  color: var(--muted);
  margin: 0 0 20px;
}

.card-panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 16px;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.stat-item h3 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
}
.stat-item p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.btn-primary, .btn.btn-primary {
  background: var(--primary) !important;
  border-color: var(--primary) !important;
}
.btn-success, .btn.btn-success {
  background: var(--success) !important;
  border-color: var(--success) !important;
}
.btn-danger, .btn.btn-danger {
  background: var(--danger) !important;
  border-color: var(--danger) !important;
}
.btn {
  border-radius: var(--radius);
}
.btn-outline-secondary {
  border-color: var(--border);
  color: var(--text-2);
}

.table {
  background: #fff;
  margin-bottom: 0;
}
.table thead th {
  color: var(--muted);
  font-weight: 600;
  border-bottom-color: var(--border);
  white-space: nowrap;
}
.table td, .table th {
  vertical-align: middle;
  border-color: var(--border);
}

.badge-status {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}
.badge-ok { background: rgba(16,185,129,.12); color: var(--success); }
.badge-fail { background: rgba(239,68,68,.12); color: var(--danger); }
.badge-unknown { background: #f1f1f1; color: var(--muted); }
.badge-na { background: #f5f5f5; color: var(--muted); }

.progress {
  height: 8px;
  background: #eee;
  border-radius: 999px;
  overflow: hidden;
}
.progress-bar {
  background: var(--primary);
  height: 100%;
}

.form-label { color: var(--text-2); font-weight: 500; }
.form-control, .form-select {
  border-radius: var(--radius);
  border-color: var(--border);
}
.form-control:focus, .form-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 .2rem rgba(59,130,246,.15);
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}
.filter-bar .form-group { margin: 0; min-width: 140px; }
.filter-bar .form-control, .filter-bar .form-select { min-width: 140px; }

.auth-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #ffffff 0%, #f7f9fc 100%);
  padding: 24px;
  padding-bottom: calc(24px + var(--safe-bottom));
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
}
.auth-card h1 {
  font-size: 26px;
  margin: 0 0 4px;
}
.auth-card .sub {
  color: var(--muted);
  margin-bottom: 24px;
}

.thumb {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #fff;
}

.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; word-break: break-all; }
.empty-tip { color: var(--muted); padding: 28px 0; text-align: center; }

.dot {
  width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 6px;
}
.dot-ok { background: var(--success); }
.dot-fail { background: var(--danger); }
.dot-unknown { background: #bbb; }

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
  max-width: 420px;
}
.search-box input {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  background: #fff;
}
.search-box .btn { flex-shrink: 0; }
.search-box .btn-search-icon { display: none; }

/* 页面标题行工具条 */
.page-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 8px;
}
.page-toolbar .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex-shrink: 0;
}

pre, textarea.mono {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ========== 平板 ========== */
@media (max-width: 1200px) {
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ========== 手机 / 窄屏（抽屉侧栏）========== */
@media (max-width: 991.98px) {
  .layout {
    display: block;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: min(86vw, 300px);
    max-width: 300px;
    height: 100vh;
    height: 100dvh;
    transform: translateX(-105%);
    box-shadow: none;
    border-right: 1px solid var(--border);
  }

  .layout.sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.08);
  }

  /* 手机端忽略桌面 collapsed 宽度逻辑 */
  .layout.collapsed .sidebar {
    width: min(86vw, 300px);
  }
  .layout.collapsed .sidebar .nav-text,
  .layout.collapsed .sidebar .brand-sub,
  .layout.collapsed .sidebar .menu-group-title {
    display: block;
  }
  .layout.collapsed .sidebar .brand-title {
    font-size: 18px;
  }

  .sidebar-overlay {
    display: block;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  .layout.sidebar-open .sidebar-overlay {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-close { display: block; }

  .main { width: 100%; }

  .topbar {
    padding: 8px 12px;
    padding-top: calc(8px + var(--safe-top));
  }

  .topbar-right {
    gap: 8px;
    font-size: 13px;
  }
  .topbar-user .role-tag { display: none; }
  .topbar-link-profile { display: none; }

  .search-box { max-width: none; }
  .search-box input {
    font-size: 16px; /* 避免 iOS 聚焦放大 */
  }
  .search-box .btn-search-text { display: none; }
  .search-box .btn-search-icon { display: inline; }

  .content {
    padding: 14px 12px calc(28px + var(--safe-bottom));
  }

  .page-title { font-size: 20px; }
  .page-desc { margin-bottom: 14px; font-size: 13px; }

  .card-panel {
    padding: 14px;
  }

  .stat-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }
  .stat-item h3 { font-size: 22px; }

  /* 标题 + 操作按钮自动换行 */
  .content > .d-flex.justify-content-between,
  .content > .d-flex.justify-content-between.align-items-start,
  .page-toolbar {
    flex-direction: column;
    align-items: stretch !important;
  }
  .content > .d-flex.justify-content-between .d-flex,
  .content > .d-flex.justify-content-between.align-items-start .d-flex,
  .page-toolbar .actions {
    width: 100%;
  }
  .content > .d-flex.justify-content-between .btn,
  .content > .d-flex.justify-content-between.align-items-start .btn,
  .page-toolbar .actions .btn {
    flex: 1 1 auto;
  }

  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }
  .filter-bar .form-group,
  .filter-bar .form-control,
  .filter-bar .form-select {
    width: 100%;
    min-width: 0;
  }
  .filter-bar .btn {
    width: 100%;
  }

  /* 表格横向滚动，避免撑破屏幕 */
  .table-responsive,
  .card-panel:has(> .table),
  .card-panel:has(> table),
  .card-panel.p-0 {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table {
    min-width: 560px;
    font-size: 13px;
  }
  .table td, .table th {
    padding: 0.55rem 0.65rem;
  }

  /* 表单控件加大触控，防 iOS zoom */
  .form-control, .form-select, .btn {
    min-height: 42px;
  }
  .form-control, .form-select, textarea.form-control {
    font-size: 16px !important;
  }
  .btn-sm {
    min-height: 36px;
  }

  .auth-card {
    padding: 24px 18px;
  }
  .auth-card h1 { font-size: 22px; }

  .pagination {
    flex-wrap: wrap;
    justify-content: center;
  }

  /* Bootstrap 栅格在手机上更紧凑 */
  .row.g-2, .row.g-3 {
    --bs-gutter-y: 0.75rem;
  }
}

@media (max-width: 575.98px) {
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .topbar-right a { padding: 4px 0; }
  .thumb { width: 64px; height: 64px; }
  .table { min-width: 520px; }
}

/* ========== 全局 Loading ========== */
.global-loading[hidden] { display: none !important; }
.global-loading {
  position: fixed;
  inset: 0;
  z-index: 2000;
}

.global-loading-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(59, 130, 246, 0.12);
  z-index: 2002;
  overflow: hidden;
}
.global-loading-bar-inner {
  height: 100%;
  width: 40%;
  background: var(--primary);
  border-radius: 0 2px 2px 0;
  animation: tavigh-bar 1.1s ease-in-out infinite;
}

.global-loading-mask {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  backdrop-filter: blur(1px);
}

.global-loading-card {
  width: min(320px, 100%);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 22px 22px;
  text-align: center;
  box-shadow: 0 8px 28px rgba(17, 17, 17, 0.06);
}

.global-loading-spinner {
  width: 36px;
  height: 36px;
  margin: 0 auto 14px;
  border: 3px solid #e8eefc;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: tavigh-spin 0.8s linear infinite;
}

.global-loading-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
}
.global-loading-text {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 16px;
  line-height: 1.5;
}

.global-loading-track {
  height: 6px;
  background: #f1f5f9;
  border-radius: 999px;
  overflow: hidden;
}
.global-loading-track-inner {
  height: 100%;
  width: 35%;
  background: linear-gradient(90deg, #93c5fd, var(--primary), #93c5fd);
  border-radius: 999px;
  animation: tavigh-progress 1.25s ease-in-out infinite;
}

body.tavigh-loading {
  overflow: hidden;
}
body.tavigh-loading .btn[type="submit"],
body.tavigh-loading button[type="submit"] {
  pointer-events: none;
}

@keyframes tavigh-spin {
  to { transform: rotate(360deg); }
}
@keyframes tavigh-bar {
  0% { transform: translateX(-100%); }
  50% { transform: translateX(120%); }
  100% { transform: translateX(280%); }
}
@keyframes tavigh-progress {
  0% { transform: translateX(-120%); }
  100% { transform: translateX(320%); }
}
