:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --nav-text: #cbd5e1;
  --panel: #ffffff;
  --panel-bg: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warn: #d97706;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--panel-bg);
  font-size: 14px;
  line-height: 1.5;
}

/* Hide admin shell until session is verified client-side */
body.auth-pending .app { display: none !important; }
.auth-gate {
  display: none;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
body.auth-pending .auth-gate { display: flex; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Layout */
/* The hidden attribute must always win, even over component display rules
   (e.g. .nav-item { display: flex }). Used to gate admin-only nav for managers. */
[hidden] { display: none !important; }

.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 230px;
  background: var(--bg);
  color: var(--nav-text);
  display: flex;
  flex-direction: column;
  padding: 18px 0;
  flex-shrink: 0;
  /* Fixed, full-height sidebar: profile + links always visible without scrolling */
  position: sticky;
  top: 0;
  align-self: flex-start;
  height: 100vh;
}
.sidebar .brand {
  font-weight: 700;
  font-size: 18px;
  color: var(--nav-text);
  padding: 0 20px 18px;
  letter-spacing: 0.3px;
  flex-shrink: 0;
}
/* Scrollable navigation region so a long menu never pushes the user box off-screen */
.sidebar .nav-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}
.sidebar .brand span { color: #60a5fa; }
.sidebar .brand img { max-width: 170px; max-height: 48px; display: block; }
.nav-item {
  padding: 11px 20px;
  cursor: pointer;
  color: var(--nav-text);
  border-left: 3px solid transparent;
  display: flex; gap: 10px; align-items: center;
}
.nav-item:hover { background: var(--bg-soft); color: #fff; }
.nav-item.active { background: var(--bg-soft); color: #fff; border-left-color: var(--primary); }
.sidebar .spacer { flex: 1; }
.sidebar .user-box { padding: 14px 20px; border-top: 1px solid #334155; font-size: 13px; flex-shrink: 0; }

/* Grouped navigation */
.nav-group { margin-top: 8px; }
.nav-header {
  padding: 9px 20px 7px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.7px;
  text-transform: uppercase;
  color: #64748b;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
}
.nav-header:hover { color: #94a3b8; }
.nav-header::after {
  content: "";
  width: 6px; height: 6px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg);
  transition: transform 0.15s ease;
  margin-right: 2px;
}
.nav-group.collapsed .nav-header::after { transform: rotate(-45deg); }
.nav-group.collapsed .nav-sub { display: none; }
.nav-sub .nav-item { padding-left: 34px; }

.main { flex: 1; padding: 28px 36px; overflow-x: auto; }
.page-title { font-size: 22px; font-weight: 700; margin: 0 0 4px; }
.page-sub { color: var(--muted); margin: 0 0 22px; }

/* Cards / panels */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}
.card h3 { margin: 0 0 14px; font-size: 16px; }

/* Tabs + fixed scroll tables */
.ui-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: 0 0 0; }
.ui-tab {
  background: transparent;
  color: var(--muted);
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 10px 14px;
  font-weight: 600;
  font-size: 13px;
  margin-bottom: -1px;
}
.ui-tab:hover { background: transparent; color: var(--text); }
.ui-tab.active { color: var(--primary); border-bottom-color: var(--primary); background: transparent; }
.table-scroll {
  height: 360px;
  overflow: auto;
  margin-top: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.table-scroll table { margin: 0; }
.table-scroll thead th {
  position: sticky;
  top: 0;
  background: var(--panel);
  z-index: 1;
  box-shadow: 0 1px 0 var(--border);
}
.table-scroll tr:last-child td { border-bottom: 1px solid var(--border); }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; gap: 12px; flex-wrap: wrap; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); }
tr:last-child td { border-bottom: none; }
.empty { color: var(--muted); padding: 24px; text-align: center; }

/* Buttons */
button, .btn {
  font: inherit;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  font-weight: 500;
}
button:hover { background: var(--primary-dark); }
.btn-light { background: #fff; color: var(--text); border-color: var(--border); }
.btn-light:hover { background: var(--panel-bg); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-sm { padding: 5px 10px; font-size: 13px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

/* Forms */
label { display: block; font-weight: 600; margin: 12px 0 5px; font-size: 13px; }
input, select, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: #fff;
  color: var(--text);
}
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; min-height: 90px; }
.form-row { display: flex; gap: 16px; flex-wrap: wrap; }
.form-row > div { flex: 1; min-width: 180px; }
.hint { color: var(--muted); font-size: 12px; margin-top: 4px; }

/* Badges */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.on { background: #dcfce7; color: var(--success); }
.badge.off { background: #fee2e2; color: var(--danger); }
.badge.type { background: #e0e7ff; color: #4338ca; }
.badge.pending { background: #fef9c3; color: #a16207; }
.badge.running { background: #dbeafe; color: #1d4ed8; }
.badge.success { background: #dcfce7; color: var(--success); }
.badge.failed { background: #fee2e2; color: var(--danger); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55);
  display: none; align-items: flex-start; justify-content: center; z-index: 50;
  padding: 40px 16px; overflow-y: auto;
}
.modal-overlay.open { display: flex; }
.modal {
  background: #fff; border-radius: 12px; width: 100%; max-width: 640px;
  padding: 24px; box-shadow: 0 20px 50px rgba(0,0,0,0.3);
}
.modal h3 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* Toast */
#toast {
  position: fixed; bottom: 24px; right: 24px; z-index: 100;
  display: flex; flex-direction: column; gap: 10px;
}
.toast {
  background: #111827; color: #fff; padding: 12px 18px; border-radius: 8px;
  box-shadow: var(--shadow); max-width: 360px; animation: slidein 0.2s ease;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Auth pages */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); }
.auth-card { background: #fff; border-radius: 14px; padding: 36px; width: 100%; max-width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,0.4); }
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-card h1 span { color: var(--primary); }
.auth-card p.sub { color: var(--muted); margin: 0 0 22px; }
.auth-card button { width: 100%; margin-top: 18px; padding: 11px; }

th.actions-col,
td.actions-col {
  width: 250px;
  min-width: 250px;
  max-width: 250px;
  box-sizing: border-box;
  vertical-align: middle;
}
.actions-cell {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  white-space: nowrap;
  min-height: 30px;
}
.action-block { border: 1px solid var(--border); border-radius: 8px; padding: 14px; margin-bottom: 12px; background: var(--panel-bg); }
.action-block .ab-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

/* Table filter bar */
.filter-bar {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin-bottom: 16px;
}
.filter-bar-top {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.filter-bar-actions { margin-left: auto; display: flex; gap: 10px; align-items: center; }
.filter-dropdown { position: relative; }
.filter-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-weight: 500;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.filter-trigger:hover { background: var(--panel-bg); border-color: #cbd5e1; }
.filter-trigger.active { border-color: #93c5fd; background: #eff6ff; color: #1d4ed8; }
.filter-chevron { font-size: 11px; color: var(--muted); margin-left: 2px; }
.filter-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: #dbeafe;
  color: #1d4ed8;
  font-size: 12px;
  font-weight: 700;
}
.filter-panel {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 30;
  width: 280px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.12);
  overflow: hidden;
}
.filter-panel-search { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.filter-panel-search input {
  width: 100%;
  padding: 8px 10px 8px 32px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' fill='%2364748b' viewBox='0 0 16 16'%3E%3Cpath d='M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85zm-5.242 1.156a5 5 0 1 1 0-10 5 5 0 0 1 0 10z'/%3E%3C/svg%3E") no-repeat 10px center;
}
.filter-panel-list {
  max-height: 220px;
  overflow-y: auto;
  padding: 6px 0;
}
.filter-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
  cursor: pointer;
  font-weight: 400;
  margin: 0;
}
.filter-option:hover { background: #f8fafc; }
.filter-option.selected { background: #eff6ff; }
.filter-option input { width: auto; margin: 0; accent-color: var(--primary); }
.filter-option span { flex: 1; font-size: 14px; }
.filter-panel-foot {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: #fafafa;
}
.filter-panel-foot .btn { padding: 7px 16px; }
.filter-selected-count { font-size: 13px; color: var(--muted); }
.filter-clear-link {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--primary);
  font-weight: 500;
  padding: 0;
  cursor: pointer;
  font-size: 13px;
}
.filter-clear-link:hover { text-decoration: underline; background: none; }
.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.filter-chips:empty { display: none; margin: 0; padding: 0; border: none; }
.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px 4px 12px;
  border-radius: 999px;
  background: #eff6ff;
  color: #1e40af;
  font-size: 13px;
  font-weight: 500;
}
.filter-chip-x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: #64748b;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.filter-chip-x:hover { background: #dbeafe; color: #1e40af; }
.filter-table-search {
  position: relative;
  min-width: 200px;
  flex: 1;
  max-width: 280px;
}
.filter-table-search input {
  width: 100%;
  padding: 8px 12px 8px 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' fill='%2364748b' viewBox='0 0 16 16'%3E%3Cpath d='M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85zm-5.242 1.156a5 5 0 1 1 0-10 5 5 0 0 1 0 10z'/%3E%3C/svg%3E") no-repeat 12px center;
}

/* Row action menu */
.row-menu-cell { width: 52px; text-align: right; white-space: nowrap; }
.row-menu { position: relative; display: inline-block; }
.row-menu-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  cursor: pointer;
}
.row-menu-btn:hover { background: var(--panel-bg); color: var(--text); border-color: #cbd5e1; }
.row-menu-icon {
  display: block;
  width: 14px;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
  box-shadow: 0 -5px 0 currentColor, 0 5px 0 currentColor;
}
.row-menu-panel {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 40;
  min-width: 168px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.12);
  overflow: hidden;
  padding: 6px 0;
}
.row-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 14px;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
}
.row-menu-item:hover { background: var(--panel-bg); }
.row-menu-item.danger { color: var(--danger); }
.row-menu-item.danger:hover { background: #fef2f2; }
button.table-link-btn {
  background: none;
  border: none;
  border-radius: 0;
  padding: 0;
  color: var(--primary);
  font-weight: 600;
  font-size: inherit;
  font-family: inherit;
  line-height: inherit;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
button.table-link-btn:hover {
  background: none;
  color: var(--primary-dark);
  text-decoration: underline;
}
button.table-link-btn:focus { outline: none; }
button.table-link-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}

/* View loading state */
.view-loading { display: flex; align-items: center; justify-content: center; min-height: 240px; }
.spinner {
  display: inline-block;
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Auth page logo */
.auth-card .auth-logo { display: block; max-width: 220px; max-height: 70px; margin: 0 auto 18px; }
.profile-readonly input[readonly] { background: var(--bg); color: var(--muted); cursor: default; }
.profile-section { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }
.profile-section h4 { margin: 0 0 12px; font-size: 14px; font-weight: 600; }
.profile-readonly label { margin-top: 10px; }
.profile-readonly label:first-child { margin-top: 0; }
.totp-input { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 22px; letter-spacing: 0.35em; text-align: center; padding-left: 0.35em; }
.totp-qr { display: block; width: 220px; height: 220px; margin: 0 auto 16px; border-radius: 8px; border: 1px solid var(--border); }
.totp-secret { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; letter-spacing: 0.08em; word-break: break-all; background: var(--bg); padding: 10px 12px; border-radius: 8px; }

/* "Powered by" footer */
.app-footer {
  margin-top: 28px;
  padding: 14px 0 4px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}
.app-footer a { color: var(--muted); font-weight: 600; }
.app-footer a:hover { color: var(--primary); }
.auth-footer { margin-top: 18px; text-align: center; color: #cbd5e1; font-size: 12px; }
.auth-footer a { color: #93c5fd; font-weight: 600; }
.auth-footer a:hover { color: #bfdbfe; }
