:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --border: #2d3a4d;
  --text: #e7edf4;
  --muted: #8b9cb3;
  --accent: #3d8bfd;
  --accent-dim: #2563c7;
  --danger: #f87171;
  --ok: #4ade80;
  --radius: 10px;
  font-family: "SF Pro Text", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  /* 刘海屏安全区 */
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: none;
  margin: 0;
  padding: 0.75rem 1.25rem;
  padding-left: max(1.25rem, env(safe-area-inset-left, 1.25rem));
  padding-right: max(1.25rem, env(safe-area-inset-right, 1.25rem));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  width: 100%;
}

/* 桌面端：无汉堡按钮，用户信息仍靠右 */
@media (min-width: 901px) {
  .topbar-inner {
    justify-content: flex-end;
  }
}

.sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 2.75rem;
  min-width: 2.75rem;
  padding: 0.35rem 0.65rem;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sidebar-toggle:hover {
  background: rgba(255, 255, 255, 0.06);
}
.sidebar-toggle-label {
  display: inline;
}
.sidebar-toggle-bars {
  display: block;
  width: 1.125rem;
  height: 2px;
  background: currentColor;
  box-shadow: 0 5px 0 currentColor, 0 -5px 0 currentColor;
  border-radius: 1px;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  flex-wrap: wrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 1.25rem;
}

.topbar-right-link {
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
}
.topbar-right-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

.brand {
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
}
.brand:hover {
  color: var(--accent);
  text-decoration: none;
}
.nav {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  flex-wrap: wrap;
}
.nav-user {
  color: var(--muted);
  font-size: 0.875rem;
}

.layout {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

.sidebar-backdrop {
  display: none;
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem 0;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
}

.sidebar-title {
  padding: 0 1.25rem 1rem;
  font-weight: 800;
  font-size: 1rem;
}

.sidebar-title a {
  color: var(--text);
  text-decoration: none;
}

.sidebar-title a:hover {
  color: var(--accent);
  text-decoration: none;
}

.sidebar-menu {
  display: flex;
  flex-direction: column;
}

.sidebar-link {
  padding: 0.75rem 1.25rem;
  min-height: 2.75rem;
  display: flex;
  align-items: center;
  color: var(--muted);
  text-decoration: none;
  font-weight: 650;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-link:hover {
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
}

.sidebar-link.active {
  background: rgba(61, 139, 253, 0.12);
  color: var(--text);
  border-left: 3px solid var(--accent);
  padding-left: calc(1.25rem - 3px);
}

.layout-main {
  flex: 1;
  min-width: 0;
}

.main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
  padding-left: max(1.25rem, env(safe-area-inset-left));
  padding-right: max(1.25rem, env(safe-area-inset-right));
  padding-bottom: max(3rem, calc(2rem + env(safe-area-inset-bottom)));
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.page-header-actions {
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
}

/* 页头多个 outline 按钮（如 新增 + 刷新）统一宽高，避免 button 与 a.btn 视觉不一致 */
.page-header-actions.equal-btns {
  align-items: center;
  gap: 0.75rem;
}
.page-header-actions.equal-btns .btn {
  box-sizing: border-box;
  width: 5.75rem;
  height: 2.375rem;
  min-height: 0;
  padding: 0 0.5rem;
  line-height: 1.2;
}
.page-header-actions.equal-btns button.btn {
  appearance: none;
  margin: 0;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  vertical-align: baseline;
}

.qr-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 9999;
}
.qr-modal-overlay.show {
  display: flex;
}
.qr-modal-content {
  width: 420px;
  max-width: calc(100vw - 2rem);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.05rem;
}
.qr-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}
.qr-modal-title {
  font-weight: 800;
}
.qr-modal-url {
  margin: 0.65rem 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  word-break: break-all;
  line-height: 1.45;
}

.qr-modal-body {
  display: flex;
  flex-direction: column;
  align-items: center;
}

#qr-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 240px;
  max-width: 100%;
}

.qr-modal-actions {
  margin-top: 0.85rem;
  display: flex;
  justify-content: center;
}

@media (max-width: 420px) {
  .qr-modal-content {
    width: 100%;
    padding: 0.9rem;
  }
  #qr-container {
    width: 210px;
  }
}

h1 {
  font-size: 1.5rem;
  font-weight: 600;
  margin: 0 0 1rem;
}

.flash {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
  border: 1px solid var(--border);
}
.flash.ok {
  border-color: #166534;
  background: rgba(74, 222, 128, 0.08);
}
.flash.err {
  border-color: #991b1b;
  background: rgba(248, 113, 113, 0.08);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}
th,
td {
  padding: 0.65rem 0.85rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
th {
  background: rgba(0, 0, 0, 0.2);
  color: var(--muted);
  font-weight: 600;
}
tr:last-child td {
  border-bottom: none;
}
.badge {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge.on {
  background: rgba(74, 222, 128, 0.15);
  color: var(--ok);
}
.badge.off {
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}
.badge.force {
  background: rgba(251, 191, 36, 0.15);
  color: #fbbf24;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.45rem 0.9rem;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  background: var(--accent);
  color: #fff;
  line-height: 1.25;
}
button.btn {
  font: inherit;
}
.btn:hover {
  background: var(--accent-dim);
  text-decoration: none;
  color: #fff;
}
.btn.secondary {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}
.btn.danger {
  background: #b91c1c;
  border-color: #b91c1c;
}
.btn.sm {
  padding: 0.25rem 0.55rem;
  font-size: 0.8125rem;
  min-height: 2rem;
}

.form-grid {
  display: grid;
  gap: 1rem;
  max-width: 640px;
}
label {
  display: block;
  font-size: 0.8125rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
input[type="text"],
input[type="password"],
input[type="url"],
input[type="number"],
select,
textarea {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.9375rem;
}
textarea {
  min-height: 120px;
  resize: vertical;
}
.check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.check input {
  width: auto;
}

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.75rem;
}
.login-card h1 {
  margin-top: 0;
  text-align: center;
}
.login-card .btn {
  width: 100%;
  margin-top: 0.5rem;
  text-align: center;
}

.row-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.row-actions form {
  display: inline-flex;
  align-items: center;
  margin: 0;
  padding: 0;
}
.row-actions .btn.sm {
  min-width: 3.5rem;
}

.mono {
  font-family: ui-monospace, monospace;
  font-size: 0.8125rem;
}

.hint {
  font-size: 0.8125rem;
  color: var(--muted);
  margin-top: 0.25rem;
}

/* —— 后台移动端：侧栏抽屉 + 顶栏菜单 —— */
@media (max-width: 900px) {
  .sidebar-toggle {
    display: inline-flex;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 998;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    -webkit-tap-highlight-color: transparent;
  }

  body.admin-sidebar-open .sidebar-backdrop {
    opacity: 1;
    visibility: visible;
  }

  .sidebar {
    position: fixed;
    z-index: 999;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(288px, 86vw);
    max-width: 100%;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: none;
    border-right: 1px solid var(--border);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  body.admin-sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.35);
  }

  body.admin-sidebar-open {
    overflow: hidden;
  }

  .layout-main {
    width: 100%;
  }

  .main {
    padding-top: 1rem;
  }

  h1 {
    font-size: 1.25rem;
  }

  .page-header {
    flex-direction: column;
    align-items: stretch;
  }

  .page-header-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .page-header-actions.equal-btns {
    flex-direction: column;
    align-items: stretch;
  }

  .page-header-actions.equal-btns .btn {
    width: 100%;
    height: auto;
    min-height: 2.75rem;
  }

  /* 避免 iOS 聚焦输入框时自动放大页面 */
  input[type="text"],
  input[type="password"],
  input[type="url"],
  input[type="number"],
  input[type="email"],
  input[type="tel"],
  select,
  textarea {
    font-size: 16px;
  }

  .table-wrap {
    margin-left: -0.25rem;
    margin-right: -0.25rem;
    border-radius: 8px;
  }

  table {
    font-size: 0.8125rem;
  }

  th,
  td {
    padding: 0.55rem 0.5rem;
  }

  .card {
    padding: 1rem;
  }

  .form-grid {
    max-width: none;
  }

  .row-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .row-actions .btn.sm {
    width: 100%;
    min-height: 2.5rem;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .topbar-inner {
    padding-top: max(0.65rem, env(safe-area-inset-top));
  }

  .nav-user {
    max-width: 42vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
