/* ── Reset & base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-w: 220px;

  --bg:        #F1F5F9;
  --surface:   #FFFFFF;
  --border:    #E2E8F0;
  --border-md: #CBD5E1;

  --text-1:    #0F172A;
  --text-2:    #475569;
  --text-3:    #94A3B8;

  --sidebar-bg:     #0F172A;
  --sidebar-text:   #94A3B8;
  --sidebar-active: #FFFFFF;
  --sidebar-hover:  #1E293B;
  --sidebar-accent: #6366F1;

  --accent:    #6366F1;
  --accent-dk: #4F46E5;
  --success:   #10B981;
  --warning:   #F59E0B;
  --danger:    #EF4444;

  --radius-sm: 6px;
  --radius:    10px;
  --radius-lg: 14px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow:    0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,.07), 0 2px 4px -1px rgba(0,0,0,.05);

  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: var(--text-1);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

/* ── Sidebar ──────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  inset: 0 auto 0 0;
  overflow-y: auto;
  z-index: 30;
  transition: transform .2s ease;
}

/* ── Mobile nav: hamburger + off-canvas sidebar ──────────────────── */
.sidebar-toggle {
  display: none;
  align-items: center; justify-content: center;
  width: 34px; height: 34px; flex-shrink: 0;
  background: transparent; border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer; color: var(--text-2);
}
.sidebar-toggle svg { width: 18px; height: 18px; }
.sidebar-toggle:hover { background: var(--bg); color: var(--text-1); }

.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(15,23,42,.5);
  z-index: 25;
}
.sidebar-backdrop.open { display: block; }

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 18px 18px;
  border-bottom: 1px solid #1E293B;
  text-decoration: none;
}
.sidebar-brand .brand-icon {
  width: 30px; height: 30px;
  background: var(--accent);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; color: #fff; font-weight: 700; flex-shrink: 0;
}
.sidebar-brand .brand-name {
  font-size: 13px; font-weight: 600;
  color: var(--sidebar-active);
  line-height: 1.3;
}
.sidebar-brand .brand-sub {
  font-size: 11px; color: var(--sidebar-text);
  display: block;
}

.sidebar-nav { padding: 12px 10px; flex: 1; }
.nav-section { margin-bottom: 20px; }
.nav-label {
  font-size: 10px; font-weight: 600; letter-spacing: .08em;
  text-transform: uppercase; color: #334155;
  padding: 0 8px; margin-bottom: 4px;
}
.nav-item {
  display: flex; align-items: center; gap: 9px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--sidebar-text); text-decoration: none;
  font-size: 13px; font-weight: 500;
  transition: background .12s, color .12s;
  margin-bottom: 1px;
}
.nav-item:hover { background: var(--sidebar-hover); color: var(--sidebar-active); }
.nav-item.active { background: #1E293B; color: var(--sidebar-active); }
.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; opacity: .7; }
.nav-item.active svg, .nav-item:hover svg { opacity: 1; }

.sidebar-footer {
  padding: 12px 10px;
  border-top: 1px solid #1E293B;
}

/* ── Main ─────────────────────────────────────────────────────── */
.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-width: 0; /* let children shrink instead of overflowing on narrow screens */
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
  position: sticky; top: 0; z-index: 5;
}
.topbar-title { font-size: 15px; font-weight: 600; color: var(--text-1); }
.topbar-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: flex-end; }

.content { padding: 28px; flex: 1; max-width: 1100px; min-width: 0; }

/* ── Mobile layout ────────────────────────────────────────────── */
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-toggle { display: inline-flex; }

  .main { margin-left: 0; }

  .topbar {
    padding: 10px 14px;
    height: auto; min-height: 56px;
    flex-wrap: wrap;
  }
  .topbar-title { flex: 1 1 auto; }

  .content { padding: 16px; }

  .login-shell { padding: 16px; }

  .filter-input { width: 100% !important; }

  .form-grid input[type=text], .form-grid input[type=password] { min-width: 0; flex: 1 1 160px; }

  .grid-2col { grid-template-columns: 1fr !important; }
}

/* ── Flash ────────────────────────────────────────────────────── */
.flash {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 14px; border-radius: var(--radius-sm);
  margin-bottom: 20px; font-size: 13px; font-weight: 500;
}
.flash::before { font-size: 15px; }
.flash.success { background: #ECFDF5; color: #065F46; border: 1px solid #A7F3D0; }
.flash.success::before { content: "✓"; }
.flash.error   { background: #FEF2F2; color: #991B1B; border: 1px solid #FECACA; }
.flash.error::before { content: "✕"; }

/* ── Cards ────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 8px;
}
.card-header h2 { font-size: 13px; font-weight: 600; color: var(--text-1); }
.card-body { padding: 18px; }
.card + .card { margin-top: 16px; }

/* ── Stat cards ───────────────────────────────────────────────── */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px 20px;
}
.stat-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); margin-bottom: 6px; }
.stat-value { font-size: 26px; font-weight: 700; color: var(--text-1); line-height: 1; }
.stat-sub { font-size: 12px; color: var(--text-3); margin-top: 4px; }

/* ── Tables ───────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead tr { border-bottom: 1px solid var(--border); }
th {
  text-align: left; padding: 10px 16px;
  font-size: 11px; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: var(--text-3);
  white-space: nowrap;
}
td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #F8FAFC; }

/* ── Badges ───────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 99px;
  font-size: 11px; font-weight: 600; letter-spacing: .03em;
  white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
.badge-active  { background: #ECFDF5; color: #065F46; } .badge-active::before  { background: #10B981; }
.badge-built   { background: #ECFDF5; color: #065F46; } .badge-built::before   { background: #10B981; }
.badge-signed  { background: #EFF6FF; color: #1D4ED8; } .badge-signed::before  { background: #3B82F6; }
.badge-pending { background: #FFFBEB; color: #92400E; } .badge-pending::before { background: #F59E0B; animation: pulse 1.5s infinite; }
.badge-failed  { background: #FEF2F2; color: #991B1B; } .badge-failed::before  { background: #EF4444; }
.badge-offline { background: #F1F5F9; color: #64748B; } .badge-offline::before { background: #94A3B8; }
.badge-unknown { background: #F1F5F9; color: #475569; } .badge-unknown::before { background: #94A3B8; }

@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:.4; } }

/* ── Buttons ──────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 500; cursor: pointer;
  border: 1px solid transparent; text-decoration: none;
  transition: background .12s, box-shadow .12s, opacity .12s;
  line-height: 1.4; white-space: nowrap;
}
.btn:active { opacity: .85; }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 1px 2px rgba(99,102,241,.25); }
.btn-primary:hover { background: var(--accent-dk); }
.btn-ghost { background: transparent; color: var(--text-2); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); color: var(--text-1); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #DC2626; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn svg { width: 14px; height: 14px; }

/* ── Forms ────────────────────────────────────────────────────── */
.form-grid { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 5px; }
label { font-size: 12px; font-weight: 500; color: var(--text-2); }
input[type=text], input[type=password] {
  padding: 7px 11px; border: 1px solid var(--border-md);
  border-radius: var(--radius-sm); font-size: 13px;
  color: var(--text-1); background: var(--surface);
  transition: border-color .12s, box-shadow .12s;
  min-width: 200px; outline: none;
}
input[type=text]:focus, input[type=password]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}
.hint { font-size: 11px; color: var(--text-3); margin-top: 2px; }

/* ── Empty state ──────────────────────────────────────────────── */
.empty {
  text-align: center; padding: 40px 20px;
  color: var(--text-3); font-size: 13px;
}
.empty-icon { font-size: 32px; margin-bottom: 8px; line-height: 1; }
.empty p { margin-top: 4px; }

/* ── Misc helpers ─────────────────────────────────────────────── */
code {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: .85em; background: #F1F5F9;
  padding: 2px 6px; border-radius: 4px; color: #334155;
}
.text-muted { color: var(--text-3); }
.text-sm    { font-size: 12px; }
.mono       { font-family: monospace; font-size: .9em; }
.mt         { margin-top: 16px; }
.section    { margin-bottom: 28px; }
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px; flex-wrap: wrap; gap: 8px;
}
.section-head h2 { font-size: 14px; font-weight: 600; }
.page-head { margin-bottom: 22px; }
.page-head h1 { font-size: 20px; font-weight: 700; }
.page-head p  { color: var(--text-2); margin-top: 4px; font-size: 13px; }

/* ── Login ────────────────────────────────────────────────────── */
.login-shell {
  min-height: 100vh; display: flex; align-items: center;
  justify-content: center; background: var(--bg);
}
.login-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-md);
  padding: 40px 36px; width: 100%; max-width: 360px;
}
.login-logo {
  display: flex; align-items: center; gap: 10px; margin-bottom: 28px;
}
.login-logo .brand-icon {
  width: 36px; height: 36px; background: var(--accent);
  border-radius: 10px; display: flex; align-items: center;
  justify-content: center; font-size: 16px; color: #fff; font-weight: 700;
}
.login-logo .brand-name { font-size: 15px; font-weight: 700; color: var(--text-1); }
.login-logo .brand-sub  { font-size: 12px; color: var(--text-3); display: block; }
.login-card h2 { font-size: 17px; font-weight: 700; margin-bottom: 6px; }
.login-card p  { font-size: 13px; color: var(--text-2); margin-bottom: 24px; }
.login-field   { margin-bottom: 14px; }
.login-field label { display: block; margin-bottom: 6px; }
.login-field input { width: 100%; }
.login-card .btn { width: 100%; justify-content: center; padding: 9px; margin-top: 6px; }

/* ── Label column expands; ID and Actions columns stay tight ─────── */
#devices-table th:nth-child(2),
#devices-table td:nth-child(2) { width: 100%; }
#devices-table td:nth-child(1),
#devices-table td:nth-child(8) { white-space: nowrap; width: 1px; }

/* ── Sortable table headers ───────────────────────────────────── */
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-1); }
.sort-icon { font-style: normal; color: var(--text-3); margin-left: 3px; font-size: 11px; }

/* ── Copy button ──────────────────────────────────────────────── */
.btn-copy {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 8px; border-radius: var(--radius-sm);
  font-size: 12px; font-weight: 500; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-3); transition: color .12s, border-color .12s, background .12s;
  line-height: 1; white-space: nowrap;
}
.btn-copy:hover { color: var(--text-1); border-color: var(--border-md); background: var(--bg); }
.btn-copy svg { width: 13px; height: 13px; flex-shrink: 0; }

/* ── Key-value detail ─────────────────────────────────────────── */
.kv-grid { display: grid; grid-template-columns: auto 1fr; gap: 0; }
.kv-row { display: contents; }
.kv-row .kv-key,
.kv-row .kv-val {
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  font-size: 13px; align-items: center; display: flex;
}
.kv-row:last-child .kv-key,
.kv-row:last-child .kv-val { border-bottom: none; }
.kv-key { font-weight: 500; color: var(--text-2); white-space: nowrap; background: #F8FAFC; min-width: 140px; }
.kv-val { color: var(--text-1); word-break: break-all; }

/* ── Modal backdrops (shared by users.html's .modal-backdrop and the
   ad-hoc inline-styled backdrops in devices/groups/group_detail) ──── */
.modal-backdrop { padding: 16px; }

.filter-input {
  padding: 7px 11px; border: 1px solid var(--border-md);
  border-radius: var(--radius-sm); font-size: 13px;
  outline: none; background: var(--surface);
}

/* ── Pubkey highlight ─────────────────────────────────────────── */
.pubkey-box {
  background: #0F172A; color: #A5F3FC;
  font-family: monospace; font-size: 13px;
  padding: 14px 16px; border-radius: var(--radius-sm);
  word-break: break-all; line-height: 1.6;
  border: 1px solid #1E293B;
}
