:root {
  color-scheme: light;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --sidebar: #0f172a;
  --sidebar-hover: #1e293b;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --radius: 12px;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); min-height: 100vh; }

.hidden { display: none !important; }

/* Login */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a5f 50%, #0f172a 100%);
}
.login-card {
  width: min(440px, 100%);
  background: var(--surface);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.35);
}
.login-card h1 { margin: 0 0 8px; font-size: 1.35rem; }
.login-card .lead { color: var(--muted); font-size: 0.9rem; margin-bottom: 24px; line-height: 1.5; }
.login-hint {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 20px;
  line-height: 1.5;
}
.login-hint strong { color: var(--text); }

/* App shell */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px;
  background: var(--sidebar);
  color: #e2e8f0;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}
.sidebar-brand {
  padding: 20px 16px 12px;
  border-bottom: 1px solid #334155;
}
.sidebar-brand h2 { margin: 0; font-size: 1rem; color: #fff; }
.sidebar-brand p { margin: 4px 0 0; font-size: 0.75rem; color: #94a3b8; }
.nav { flex: 1; padding: 12px 8px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  color: #cbd5e1;
  cursor: pointer;
  font-size: 0.9rem;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-icon { width: 20px; text-align: center; opacity: 0.9; }
.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid #334155;
  font-size: 0.75rem;
  color: #94a3b8;
}
.sidebar-footer button {
  background: none;
  border: 1px solid #475569;
  color: #cbd5e1;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  margin-top: 8px;
}

.main { flex: 1; min-width: 0; padding: 24px 28px 48px; }
.page-header { margin-bottom: 24px; }
.page-header h1 { margin: 0 0 6px; font-size: 1.5rem; }
.page-header p { margin: 0; color: var(--muted); font-size: 0.92rem; }

/* Cards & layout */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}
.card h3 { margin: 0 0 16px; font-size: 1rem; }
.grid-2 { display: grid; gap: 16px; grid-template-columns: repeat(2, 1fr); }
.grid-4 { display: grid; gap: 12px; grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; }
  .nav { display: flex; flex-wrap: wrap; gap: 4px; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.stat-card .label { font-size: 0.8rem; color: var(--muted); margin-bottom: 4px; }
.stat-card .value { font-size: 1.75rem; font-weight: 700; line-height: 1.2; }
.stat-card .hint { font-size: 0.75rem; color: var(--muted); margin-top: 4px; }
.stat-card.primary .value { color: var(--primary); }
.stat-card.success .value { color: var(--success); }
.stat-card.warning .value { color: var(--warning); }
.stat-card.danger .value { color: var(--danger); }
.stat-card.stat-card-alert {
  border-color: var(--warning);
  box-shadow: 0 0 0 2px rgba(217, 119, 6, 0.15);
}
.dash-stat-link { cursor: pointer; transition: transform 0.12s ease, box-shadow 0.12s ease; }
.dash-stat-link:hover { transform: translateY(-1px); box-shadow: var(--shadow); }

/* Forms */
label { display: block; font-size: 0.82rem; margin-bottom: 6px; color: #334155; font-weight: 500; }
input, select, textarea, button.btn {
  font: inherit;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(37, 99, 235, 0.25);
  border-color: var(--primary);
}
.form-row {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  align-items: end;
}
.form-row .btn-wrap { display: flex; gap: 8px; align-items: end; }

button.btn {
  background: var(--primary);
  color: #fff;
  border: none;
  cursor: pointer;
  font-weight: 500;
  width: auto;
  min-width: 100px;
}
button.btn:hover { background: var(--primary-hover); }
button.btn:disabled { opacity: 0.55; cursor: not-allowed; }
button.btn.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
button.btn.danger { background: var(--danger); }
button.btn.sm { padding: 6px 10px; font-size: 0.82rem; min-width: auto; }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.02em; }
tr:hover td { background: #f8fafc; }
.empty-row td { color: var(--muted); text-align: center; padding: 24px; }

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-unused { background: #f1f5f9; color: #475569; }
.badge-active { background: #d1fae5; color: #047857; }
.badge-expired { background: #fef3c7; color: #b45309; }
.badge-frozen { background: #fee2e2; color: #b91c1c; }
.badge-online { background: #dbeafe; color: #1d4ed8; }

/* Keys output */
.keys-box {
  background: #0f172a;
  color: #e2e8f0;
  padding: 16px;
  border-radius: 8px;
  font-family: Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.6;
  white-space: pre-wrap;
  max-height: 280px;
  overflow: auto;
}
.keys-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

/* Steps / help */
.steps { list-style: none; padding: 0; margin: 0; }
.steps li {
  display: flex;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.steps li:last-child { border-bottom: none; }
.step-num {
  width: 28px; height: 28px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  flex-shrink: 0;
}
.step-body h4 { margin: 0 0 4px; font-size: 0.95rem; }
.step-body p { margin: 0; color: var(--muted); font-size: 0.88rem; line-height: 1.5; }

.callout {
  border-left: 4px solid var(--primary);
  background: #eff6ff;
  padding: 12px 16px;
  border-radius: 0 8px 8px 0;
  margin-bottom: 16px;
  font-size: 0.88rem;
  line-height: 1.5;
}
.callout.warning { border-color: var(--warning); background: #fffbeb; }

/* Toast */
.toast-host {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
}
.toast {
  padding: 12px 16px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 10px 25px rgba(0,0,0,0.1);
  font-size: 0.88rem;
  animation: slideIn 0.2s ease;
}
.toast.ok { border-color: #a7f3d0; background: #ecfdf5; }
.toast.err { border-color: #fecaca; background: #fef2f2; color: #b91c1c; }
@keyframes slideIn {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 12px;
  font-size: 0.85rem;
  color: var(--muted);
}
.pager button { margin-left: 8px; }

.remark-cell {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.82rem;
}

.card-key-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.card-key-plain {
  font-size: 0.82rem;
  word-break: break-all;
}
.card-key-cell .muted {
  color: var(--muted);
  font-size: 0.8rem;
}

.trend-chart {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 72px;
  padding-top: 8px;
}
.trend-bar-group {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 0;
}
.trend-bars {
  display: flex;
  gap: 2px;
  align-items: flex-end;
  height: 48px;
  width: 100%;
}
.trend-bar {
  flex: 1;
  min-height: 2px;
  border-radius: 2px 2px 0 0;
}
.trend-bar.issued { background: var(--primary); opacity: 0.85; }
.trend-bar.activated { background: var(--success); opacity: 0.85; }
.trend-label {
  font-size: 0.68rem;
  color: var(--muted);
  white-space: nowrap;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal.hidden { display: none; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
}
.modal-panel {
  position: relative;
  background: var(--card);
  border-radius: 12px;
  width: min(640px, 92vw);
  max-height: 85vh;
  overflow: auto;
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-body { padding: 16px 20px; }
.preview-box {
  margin-top: 12px;
  padding: 12px;
  background: var(--bg);
  border-radius: 8px;
  font-size: 0.88rem;
}
.preview-box.hidden { display: none; }
.system-info-panel {
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.6;
}
.audit-sensitive td { background: rgba(220, 53, 69, 0.06); }
.expiring-group-header td {
  background: var(--bg);
  font-weight: 600;
  font-size: 0.85rem;
}
.badge-disabled { background: #6c757d; color: #fff; }
.row-highlight td { background: rgba(13, 110, 253, 0.08); }
