:root {
  --blue-50: #eff6ff;
  --blue-100: #dbeafe;
  --blue-200: #bfdbfe;
  --blue-500: #3b82f6;
  --blue-600: #2563eb;
  --blue-700: #1d4ed8;
  --blue-900: #1e3a8a;
  --bg: #f3f7ff;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --danger: #ef4444;
  --success: #10b981;
  --warn: #f59e0b;
  --sidebar-w: 248px;
  --sidebar-collapsed-w: 76px;
  --topbar-h: 60px;
  --shadow-sm: 0 1px 3px rgba(37, 99, 235, 0.06);
  --shadow-md: 0 8px 24px rgba(37, 99, 235, 0.08);
  --radius: 14px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }

/* ========== App Shell ========== */
.app-shell {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  backdrop-filter: blur(2px);
  z-index: 90;
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.28s ease, width 0.28s ease;
  box-shadow: var(--shadow-sm);
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--blue-500), var(--blue-700));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.25);
}

.brand-text {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--blue-900);
  white-space: nowrap;
}

.sidebar-close {
  display: none;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: var(--blue-50);
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
}

.sidebar-status {
  margin: 0 12px 14px;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 0.82rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

.sidebar-status.status-on { background: #ecfdf5; color: #047857; }
.sidebar-status.status-off { background: #fef2f2; color: #b91c1c; }
.sidebar-status.status-warn { background: #fffbeb; color: #b45309; }

.sidebar-nav {
  flex: 1;
  padding: 4px 10px;
  overflow-y: auto;
}

.sidebar-foot {
  padding: 10px 10px 16px;
  border-top: 1px solid var(--border);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  color: var(--muted);
  font-size: 0.94rem;
  font-weight: 500;
  margin-bottom: 4px;
  transition: background 0.2s, color 0.2s;
}

.nav-item:hover {
  background: var(--blue-50);
  color: var(--blue-700);
}

.nav-item.active {
  background: linear-gradient(135deg, var(--blue-600), var(--blue-500));
  color: #fff;
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.28);
}

.nav-icon {
  width: 22px;
  text-align: center;
  flex-shrink: 0;
  font-size: 1rem;
}

.nav-label { white-space: nowrap; }

.nav-logout:hover {
  background: #fef2f2;
  color: #b91c1c;
}

.main-wrap {
  flex: 1;
  margin-left: var(--sidebar-w);
  min-width: 0;
  display: flex;
  flex-direction: column;
  transition: margin-left 0.28s ease;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--topbar-h);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
}

.topbar-toggle {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  flex-shrink: 0;
}

.topbar-toggle span {
  display: block;
  width: 16px;
  height: 2px;
  background: var(--blue-700);
  border-radius: 2px;
}

.topbar-title h1 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
}

.topbar-sub {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 70vw;
}

.main-content {
  flex: 1;
  padding: 16px;
  max-width: 960px;
}

/* Desktop collapsed sidebar */
.app-shell.sidebar-collapsed .sidebar {
  width: var(--sidebar-collapsed-w);
}

.app-shell.sidebar-collapsed .brand-text,
.app-shell.sidebar-collapsed .nav-label,
.app-shell.sidebar-collapsed .status-text,
.app-shell.sidebar-collapsed .sidebar-close {
  display: none;
}

.app-shell.sidebar-collapsed .sidebar-status {
  justify-content: center;
  padding: 10px;
}

.app-shell.sidebar-collapsed .nav-item {
  justify-content: center;
  padding: 12px 8px;
}

.app-shell.sidebar-collapsed .main-wrap {
  margin-left: var(--sidebar-collapsed-w);
}

/* ========== Cards & Forms ========== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  margin-bottom: 16px;
  overflow: hidden;
}

.card-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--blue-900);
  background: linear-gradient(180deg, #fff, var(--blue-50));
}

.card-body { padding: 16px; }

.card-tag {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--blue-600);
  background: var(--blue-50);
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 8px;
}

.form-group { margin-bottom: 16px; }

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  font-size: 0.88rem;
  color: #334155;
}

.form-control {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 16px;
  background: #fff;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-control:focus {
  outline: none;
  border-color: var(--blue-500);
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.12);
}

textarea.form-control { resize: vertical; min-height: 100px; }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 6px;
  line-height: 1.5;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: transform 0.15s, box-shadow 0.15s;
}

.btn:active { transform: scale(0.98); }

.btn-primary {
  background: linear-gradient(135deg, var(--blue-600), var(--blue-500));
  color: #fff;
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.25);
}

.btn-primary:hover {
  box-shadow: 0 6px 18px rgba(37, 99, 235, 0.32);
}

.btn-block { width: 100%; }

/* ========== Stats ========== */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: linear-gradient(135deg, #ffffff 0%, var(--blue-50) 100%);
  border: 1px solid var(--blue-100);
  padding: 18px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.stat-label {
  color: var(--muted);
  font-size: 0.82rem;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 2rem;
  font-weight: 800;
  color: var(--blue-600);
  line-height: 1.1;
}

/* ========== Table ========== */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table { width: 100%; border-collapse: collapse; min-width: 280px; }

th, td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.82rem;
  background: var(--blue-50);
}

tbody tr:hover { background: #fafcff; }

/* ========== Alerts ========== */
.alert {
  padding: 12px 14px;
  border-radius: 12px;
  margin-bottom: 14px;
  font-size: 0.9rem;
  border: 1px solid transparent;
}

.alert-success { background: #ecfdf5; color: #065f46; border-color: #a7f3d0; }
.alert-error { background: #fef2f2; color: #991b1b; border-color: #fecaca; }

/* ========== Toggle ========== */
.access-toggle-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px;
}

.access-toggle-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--blue-900);
  margin-bottom: 4px;
}

.access-toggle-desc {
  color: var(--muted);
  font-size: 0.84rem;
  margin-bottom: 10px;
}

.access-toggle-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.switch-label {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
}

.status-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
}

.status-badge.status-on, .status-on { background: #d1fae5; color: #065f46; }
.status-badge.status-off, .status-off { background: #fee2e2; color: #991b1b; }
.status-badge.status-warn, .status-warn { background: #fef3c7; color: #92400e; }

.switch {
  position: relative;
  display: inline-block;
  width: 54px;
  height: 30px;
  cursor: pointer;
}

.switch input { opacity: 0; width: 0; height: 0; }

.slider {
  position: absolute;
  inset: 0;
  background: #cbd5e1;
  border-radius: 30px;
  transition: 0.25s;
}

.slider:before {
  content: "";
  position: absolute;
  height: 24px;
  width: 24px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.25s;
  box-shadow: 0 1px 4px rgba(0,0,0,.15);
}

.switch input:checked + .slider { background: var(--blue-500); }
.switch input:checked + .slider:before { transform: translateX(24px); }

/* ========== Login ========== */
.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 16px;
  background:
    radial-gradient(circle at 10% 20%, rgba(59, 130, 246, 0.18), transparent 40%),
    radial-gradient(circle at 90% 80%, rgba(37, 99, 235, 0.12), transparent 35%),
    linear-gradient(160deg, #eff6ff 0%, #dbeafe 45%, #f8fbff 100%);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: var(--shadow-md);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.login-brand .brand-icon {
  width: 44px;
  height: 44px;
  font-size: 18px;
}

.login-card h1 {
  font-size: 1.35rem;
  color: var(--blue-900);
}

.text-muted {
  color: var(--muted);
  font-size: 0.88rem;
  margin-bottom: 22px;
  line-height: 1.6;
}

.login-note {
  margin-top: 16px;
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

/* ========== Mobile ========== */
@media (max-width: 767px) {
  .sidebar {
    transform: translateX(-100%);
    width: min(84vw, 280px);
    box-shadow: var(--shadow-md);
  }

  .app-shell.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .app-shell.sidebar-open .sidebar-overlay {
    display: block;
  }

  .sidebar-close { display: flex; align-items: center; justify-content: center; }

  .main-wrap { margin-left: 0; }

  .app-shell.sidebar-collapsed .main-wrap { margin-left: 0; }

  .main-content { padding: 14px 12px 24px; }

  .topbar { padding: 0 12px; }

  .topbar-sub { max-width: 55vw; }

  .form-grid { grid-template-columns: 1fr; }

  .access-toggle-card {
    flex-direction: column;
    align-items: stretch;
  }

  .access-toggle-form {
    flex-direction: row;
    justify-content: space-between;
    padding-top: 8px;
    border-top: 1px dashed var(--border);
  }

  .stat-value { font-size: 1.75rem; }
}

@media (min-width: 768px) {
  .main-content { padding: 20px 24px 32px; }
}

@media (min-width: 1024px) {
  .main-content { padding: 24px 32px 40px; }
}
