:root {
  color-scheme: light;
  --bg: #f0f2f7;
  --sidebar-bg: #0f1b3d;
  --sidebar-text: #94a3c8;
  --sidebar-hover: rgba(255,255,255,0.08);
  --sidebar-active: rgba(99,102,241,0.25);
  --paper: #fff;
  --text: #1e293b;
  --text-secondary: #64748b;
  --muted: #94a3b8;
  --line: #e2e8f0;
  --accent: #6366f1;
  --accent-dark: #4f46e5;
  --accent-light: #eef2ff;
  --accent-soft: #e0e7ff;
  --warning: #f59e0b;
  --warning-bg: #fffbeb;
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --success: #22c55e;
  --success-bg: #f0fdf4;
  --info: #3b82f6;
  --info-bg: #eff6ff;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.05);
  --shadow: 0 4px 16px rgba(0,0,0,0.06);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.08);
  --radius: 14px;
  --radius-sm: 10px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.5 'Inter', -apple-system, 'PingFang SC', 'Microsoft YaHei', sans-serif; }
a { color: inherit; }
button, input, select, textarea { font: inherit; }
button, .button { cursor: pointer; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ====== Shell layout ====== */
.app-shell { min-height: 100vh; display: flex; }

/* ====== Sidebar ====== */
.sidebar {
  width: 240px;
  background: var(--sidebar-bg);
  padding: 28px 16px;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 100;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  margin-bottom: 36px;
  text-decoration: none;
}
.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  font-weight: 700;
  flex-shrink: 0;
}
.brand-text {
  color: white;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-section {
  padding: 16px 12px 8px;
  font-size: 11px;
  font-weight: 600;
  color: rgba(255,255,255,0.3);
  text-transform: uppercase;
  letter-spacing: 1.2px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  color: var(--sidebar-text);
  text-decoration: none;
  background: transparent;
  border: 0;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 14px;
  width: 100%;
  text-align: left;
  transition: all 0.15s;
}
.nav-item:hover {
  background: var(--sidebar-hover);
  color: white;
}
.nav-item.active {
  background: var(--sidebar-active);
  color: white;
}
.nav-item .nav-icon {
  font-size: 17px;
  width: 22px;
  text-align: center;
  flex-shrink: 0;
}
.logout-form { margin-top: auto; padding-top: 16px; border-top: 1px solid rgba(255,255,255,0.1); }

/* ====== App main ====== */
.app-main {
  margin-left: 240px;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* ====== Topbar ====== */
.topbar {
  height: 64px;
  padding: 0 32px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-sm);
}
.topbar-title {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.2px;
}
.admin-name {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-secondary);
  font-size: 13px;
}
.admin-avatar {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--accent-light);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  font-weight: 600;
  font-size: 13px;
}
.subtitle { color: var(--muted); font-size: 13px; }

/* ====== Content container ====== */
.container {
  width: min(1200px, calc(100% - 48px));
  margin: 28px auto 48px;
}

/* ====== Typography ====== */
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 6px; font-size: 22px; line-height: 1.25; font-weight: 700; letter-spacing: -0.3px; }
h2 { font-size: 17px; line-height: 1.3; font-weight: 600; letter-spacing: -0.2px; }
h3 { font-size: 14px; line-height: 1.4; font-weight: 600; }
p { color: var(--muted); line-height: 1.6; }

/* ====== Buttons ====== */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 38px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  transition: all 0.15s;
}
.button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}
.button.primary:hover {
  background: var(--accent-dark);
  box-shadow: 0 4px 14px rgba(99,102,241,0.3);
  transform: translateY(-1px);
}
.button.secondary {
  color: var(--accent);
  border-color: var(--accent-light);
  background: var(--accent-light);
}
.button.secondary:hover { background: var(--accent-soft); }
.button.warning {
  color: var(--warning);
  border-color: var(--warning-bg);
  background: var(--warning-bg);
}
.button.danger {
  color: var(--danger);
  border-color: var(--danger-bg);
  background: var(--danger-bg);
}
.button.danger:hover {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}
.button:disabled {
  cursor: not-allowed;
  opacity: .45;
  transform: none !important;
  box-shadow: none !important;
}

/* ====== Form controls ====== */
input, select, textarea {
  width: 100%;
  min-width: 0;
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--text);
  padding: 9px 12px;
  transition: all 0.15s;
  font-size: 13px;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99,102,241,0.1);
  outline: none;
}
textarea { min-height: 80px; resize: vertical; }
label {
  display: grid;
  gap: 6px;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ====== Login page ====== */
.login-main {
  min-height: 100vh;
}
.login-layout {
  display: flex;
  min-height: 100vh;
}
.login-hero {
  flex: 1;
  background: linear-gradient(135deg, #1a1f4e 0%, #2d3a8c 50%, #4c4fc7 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 60px;
  position: relative;
  overflow: hidden;
}
.login-hero::before {
  content: '';
  position: absolute;
  width: 500px;
  height: 500px;
  border-radius: 50%;
  background: rgba(255,255,255,0.05);
  top: -120px;
  right: -120px;
}
.login-hero::after {
  content: '';
  position: absolute;
  width: 350px;
  height: 350px;
  border-radius: 50%;
  background: rgba(255,255,255,0.04);
  bottom: -80px;
  left: -80px;
}
.login-hero-content {
  position: relative;
  z-index: 1;
  text-align: center;
  color: white;
}
.login-hero-icon {
  width: 88px;
  height: 88px;
  border-radius: 24px;
  background: rgba(255,255,255,0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 32px;
  font-size: 40px;
  border: 1px solid rgba(255,255,255,0.15);
}
.login-hero h1 {
  font-size: 30px;
  font-weight: 700;
  margin-bottom: 16px;
  letter-spacing: -0.5px;
}
.login-hero p {
  font-size: 15px;
  opacity: 0.85;
  line-height: 1.7;
  max-width: 340px;
  margin: 0 auto;
}
.login-hero-stats {
  display: flex;
  gap: 40px;
  margin-top: 48px;
}
.hero-stat-value {
  font-size: 26px;
  font-weight: 700;
}
.hero-stat-label {
  font-size: 11px;
  opacity: 0.65;
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.login-side {
  width: 480px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
  background: var(--paper);
}
.login-card {
  width: 100%;
  max-width: 360px;
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 48px;
}
.login-brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
}
.login-brand-text {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}
.login-card h1 {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 8px;
  letter-spacing: -0.3px;
}
.login-card .subtitle {
  margin-bottom: 36px;
  line-height: 1.5;
}
.stack { display: grid; gap: 18px; }
.stack label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}
.stack input {
  height: 48px;
  padding: 0 14px;
  border-radius: 12px;
  background: var(--bg);
}
.stack input:focus {
  background: white;
}
.login-card .button.primary {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 15px;
  margin-top: 8px;
}
.form-footer {
  text-align: center;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid var(--line);
}
.form-footer p {
  font-size: 13px;
  line-height: 1.6;
}

/* ====== Page heading ====== */
.page-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
}
.page-heading p { margin-bottom: 0; font-size: 13px; }
.breadcrumb {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 10px;
  font-weight: 500;
}
.result-count {
  color: var(--muted);
  white-space: nowrap;
  font-size: 13px;
  font-weight: 500;
}
.heading-stats {
  display: flex;
  gap: 12px;
}
.stat-chip {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  padding: 10px 18px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
}
.stat-chip-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 500;
}
.stat-chip-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}
.stat-chip-value.accent { color: var(--accent); }

/* ====== Filters ====== */
.filters {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) 155px auto;
  gap: 10px;
  max-width: 650px;
  margin-bottom: 16px;
}
.filters input, .filters select {
  height: 40px;
  border-radius: var(--radius-sm);
}

/* ====== Add device section ====== */
.add-device-section {
  background: var(--paper);
  border: 1.5px dashed var(--accent-soft);
  border-radius: var(--radius);
  padding: 18px 22px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.add-device-icon-wrap {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--accent-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: var(--accent);
  flex-shrink: 0;
}
.add-device-text { flex: 1; min-width: 0; }
.add-device-text h3 { margin-bottom: 2px; }
.add-device-text p { margin: 0; font-size: 12px; }

/* ====== Meta form ====== */
.meta-form {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  flex-wrap: wrap;
}
.meta-form label { min-width: 160px; flex: 1; }
.meta-form .button { flex-shrink: 0; }

/* ====== Detail section (used in add form) ====== */
.detail-section {
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
}
.detail-section:last-child { padding-bottom: 0; border-bottom: 0; }
.detail-section h3 { margin-bottom: 11px; }

/* ====== Device workspace ====== */
.device-workspace {
  display: grid;
  grid-template-columns: minmax(280px, .85fr) minmax(400px, 1.15fr);
  gap: 16px;
  align-items: start;
}
.device-column { min-width: 0; }

/* ====== Batch toolbar ====== */
.batch-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 0 0 12px;
  padding: 12px 16px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
}
.batch-select-all {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.batch-count {
  color: var(--muted);
  font-size: 12px;
  flex: 1;
}

/* ====== Device cards ====== */
.device-list {
  display: grid;
  gap: 10px;
}
.device-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  align-items: start;
  gap: 8px;
}
.device-select {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 16px 0 0 4px;
}
.device-select input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}
.device-card {
  display: block;
  min-width: 0;
  padding: 16px 18px;
  text-decoration: none;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition: all 0.2s;
}
.device-card:hover {
  box-shadow: var(--shadow);
  transform: translateY(-1px);
  border-color: var(--accent-soft);
}
.device-card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow);
}
.device-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}
.short-id {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.2px;
}
.short-id-icon {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent-light);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--accent);
}
.device-card-name {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}
.state {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 11px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}
.state .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}
.state.active {
  background: var(--success-bg);
  color: #16a34a;
}
.state.active .dot { background: var(--success); }
.state.expired, .state.revoked {
  background: var(--danger-bg);
  color: var(--danger);
}
.state.expired .dot, .state.revoked .dot { background: var(--danger); }
.state.suspended {
  background: var(--warning-bg);
  color: var(--warning);
}
.state.suspended .dot { background: var(--warning); }
.state.pending {
  background: var(--info-bg);
  color: var(--info);
}
.state.pending .dot { background: var(--info); }

.device-card-facts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 14px;
}
.device-card-facts span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
}
.device-card-facts strong {
  display: block;
  font-size: 12px;
  font-weight: 600;
  margin-top: 2px;
  overflow-wrap: anywhere;
}
.device-card-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  border-top: 1px solid var(--line);
  margin-top: 14px;
  padding-top: 10px;
  color: var(--muted);
  font-size: 11px;
}
.device-card-foot code {
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 6px;
}
.device-card-foot span {
  color: var(--accent);
  white-space: nowrap;
  font-weight: 500;
}

/* ====== Detail panel ====== */
.detail-panel {
  min-width: 0;
  padding: 22px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  scroll-margin-top: 12px;
}
.detail-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}
.detail-heading h2 {
  margin-bottom: 3px;
  font-size: 18px;
}
.detail-heading p { margin-bottom: 0; font-size: 13px; }

.detail-facts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin: 0;
}
.detail-facts div { min-width: 0; }
.detail-facts dt {
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.detail-facts dd {
  margin: 4px 0 0;
  overflow-wrap: anywhere;
  font-size: 13px;
  font-weight: 500;
}
.long-id {
  overflow-wrap: anywhere;
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 6px;
}

/* ====== Grant form ====== */
.grant-fields {
  display: grid;
  grid-template-columns: 75px 75px minmax(150px, 1fr);
  gap: 10px;
  align-items: end;
}
.grant-preview {
  background: var(--accent-light);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin: 14px 0;
  font-size: 12px;
}
.grant-preview strong { color: var(--accent); font-weight: 600; }
.grant-preview span {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

/* ====== State / meta forms ====== */
.state-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ====== Audit list ====== */
.audit-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 8px;
}
.audit-list li {
  display: flex;
  gap: 10px;
  justify-content: space-between;
  font-size: 12px;
  padding: 8px 12px;
  background: var(--bg);
  border-radius: 8px;
}
.audit-list time {
  color: var(--muted);
  white-space: nowrap;
}

/* ====== Pagination ====== */
.pagination {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 12px;
}

/* ====== Flash messages ====== */
.flash {
  margin-bottom: 16px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--accent-light);
  color: var(--accent-dark);
  font-size: 13px;
  font-weight: 500;
}
.flash.error {
  background: var(--danger-bg);
  color: var(--danger);
}

/* ====== Empty state ====== */
.empty {
  padding: 60px 20px;
  text-align: center;
  color: var(--muted);
  background: var(--paper);
  border: 1.5px dashed var(--line);
  border-radius: var(--radius);
  font-size: 14px;
}

/* ====== Responsive ====== */
@media (max-width: 1050px) {
  .device-workspace {
    grid-template-columns: minmax(250px, .8fr) minmax(360px, 1.2fr);
  }
}
@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    transform: translateX(-100%);
    transition: transform 0.3s;
  }
  .sidebar.open { transform: translateX(0); }
  .app-main { margin-left: 0; }
  .login-hero { display: none; }
  .login-side { width: 100%; }
  .device-workspace { grid-template-columns: 1fr; }
  .device-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .add-device-section {
    flex-direction: column;
    align-items: stretch;
  }
}
@media (max-width: 620px) {
  .container {
    width: calc(100% - 24px);
    margin-top: 18px;
  }
  .topbar { padding: 0 16px; }
  .page-heading {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  .heading-stats { width: 100%; }
  .stat-chip { flex: 1; }
  .filters {
    grid-template-columns: 1fr;
  }
  .device-list { grid-template-columns: 1fr; }
  .grant-fields {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .grant-fields label:last-child {
    grid-column: 1 / -1;
  }
  .meta-form {
    flex-direction: column;
    align-items: stretch;
  }
  .meta-form label { min-width: 0; }
}
@media (max-width: 380px) {
  .detail-facts {
    grid-template-columns: 1fr;
  }
}
