/* Wireframe 1: Structured Sidebar — Design System
 * Loaded AFTER Bootstrap 5.1.3. Our .card, .badge, .btn overrides
 * win by source order. The sidebar/main shell is wire-native.
 */

/* =====================================================================
   CSS Custom Properties (Light theme = default)
   ===================================================================== */
:root {
  --brand:       #0d6efd;
  --brand-dark:  #0a58ca;
  --brand-muted: #cfe2ff;
  --surface:     #ffffff;
  --surface-2:   #f8f9fa;
  --surface-3:   #e9ecef;
  --border:      #dee2e6;
  --text:        #212529;
  --text-muted:  #6c757d;
  --sidebar-bg:  #1a2236;
  --sidebar-text:#c9d1e0;
  --sidebar-active: #0d6efd;
  --sidebar-hover: rgba(255,255,255,0.07);
  --success:     #198754;
  --warning:     #ffc107;
  --danger:      #dc3545;
  --info:        #0dcaf0;
  --shadow-sm:   0 1px 3px rgba(0,0,0,0.08);
  --shadow:      0 2px 8px rgba(0,0,0,0.10);
  --radius:      6px;
}

/* =====================================================================
   Dark theme overrides
   ===================================================================== */
html[data-theme="dark"] {
  --surface:     #1a1d23;
  --surface-2:   #212530;
  --surface-3:   #2a2f3d;
  --border:      #343a46;
  --text:        #e2e8f0;
  --text-muted:  #8896a7;
  --sidebar-bg:  #0f1117;
  --sidebar-text:#9ba8bc;
  --brand-muted: #1a3a6b;
  --shadow-sm:   0 1px 3px rgba(0,0,0,0.25);
  --shadow:      0 2px 8px rgba(0,0,0,0.35);
}

/* =====================================================================
   Base reset + body
   NOTE: body uses flex so the sidebar+main shell works. Login and
   mfa-prompt pages render their own full-width layout inside the
   flex body (their container-fluid vh-100 still centres correctly).
   ===================================================================== */
*, *::before, *::after { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  background: var(--surface-2);
  color: var(--text);
  display: flex;
  min-height: 100vh;
  margin: 0;
  padding: 0;
}

/* =====================================================================
   Sidebar
   ===================================================================== */
.sidebar {
  width: 240px;
  min-height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 100;
}

.sidebar-logo {
  padding: 20px 20px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.07);
}

.sidebar-logo .logo-text {
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
}

.sidebar-logo .logo-text i {
  color: var(--brand);
  font-size: 18px;
}

.sidebar-logo .logo-sub {
  color: var(--sidebar-text);
  font-size: 11px;
  margin-top: 2px;
  padding-left: 26px;
}

.nav-section {
  padding: 12px 0 4px;
}

.nav-section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #4a5568;
  padding: 0 16px 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: 13px;
  border-left: 3px solid transparent;
  transition: all 0.15s;
  cursor: pointer;
}

.nav-item:hover {
  background: var(--sidebar-hover);
  color: #fff;
  text-decoration: none;
}

.nav-item.active {
  background: rgba(13, 110, 253, 0.15);
  color: #fff;
  border-left-color: var(--brand);
}

.nav-item i {
  width: 16px;
  text-align: center;
  flex-shrink: 0;
}

.nav-item .badge {
  margin-left: auto;
  background: var(--brand);
  color: #fff;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
}

.nav-item .badge.warn {
  background: var(--warning);
  color: #000;
}

.nav-item.sub {
  padding-left: 38px;
  font-size: 12px;
}

.sidebar-footer {
  margin-top: auto;
  padding: 12px 16px;
  border-top: 1px solid rgba(255,255,255,0.07);
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--sidebar-text);
  font-size: 12px;
  text-decoration: none;
}

.user-chip:hover {
  color: #fff;
  text-decoration: none;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--brand);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}

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

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-title i {
  color: var(--brand);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.content {
  padding: 24px;
  flex: 1;
}

/* =====================================================================
   Cards — override Bootstrap's .card
   ===================================================================== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.card-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: transparent;
}

.card-header i {
  color: var(--brand);
  margin-right: 6px;
}

/* Icons inside buttons in card-headers must inherit the button's own color,
   not the brand accent the header uses for decorative icons. */
.card-header .btn i {
  color: inherit;
  margin-right: 4px;
}

.card-body {
  padding: 18px;
}

/* =====================================================================
   Stat tiles
   ===================================================================== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: flex-start;
  gap: 14px;
}

.stat-icon {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.stat-icon.blue  { background: #dbeafe; color: var(--brand); }
.stat-icon.green { background: #d1fae5; color: var(--success); }
.stat-icon.amber { background: #fef3c7; color: #d97706; }
.stat-icon.red   { background: #fee2e2; color: var(--danger); }

html[data-theme="dark"] .stat-icon.blue  { background: #1e3a5f; }
html[data-theme="dark"] .stat-icon.green { background: #14532d; }
html[data-theme="dark"] .stat-icon.amber { background: #451a03; }
html[data-theme="dark"] .stat-icon.red   { background: #450a0a; }

.stat-body {}

.stat-label {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 26px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text);
}

.stat-sub {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* =====================================================================
   Grid layouts
   ===================================================================== */
.dashboard-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.full-width {
  grid-column: 1 / -1;
}

.section-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 16px;
  margin-bottom: 20px;
}

/* =====================================================================
   Tables — override Bootstrap's table defaults
   ===================================================================== */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th {
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  vertical-align: middle;
}

tr:last-child td {
  border-bottom: none;
}

tr:hover td {
  background: var(--surface-2);
}

/* Certificate rows are clickable (open the detail offcanvas); signal it. */
tr[data-cert-id] {
  cursor: pointer;
}

/* =====================================================================
   Badges — override Bootstrap's .badge
   ===================================================================== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
}

.badge-success { background: #d1fae5; color: #065f46; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger  { background: #fee2e2; color: #991b1b; }
.badge-info    { background: #dbeafe; color: #1e40af; }
.badge-muted   { background: var(--surface-3); color: var(--text-muted); }
.badge-dark    { background: #1f2937; color: #e5e7eb; }
.badge-system  { background: #ede9fe; color: #5b21b6; }

html[data-theme="dark"] .badge-success { background: #064e3b; color: #6ee7b7; }
html[data-theme="dark"] .badge-warning { background: #78350f; color: #fcd34d; }
html[data-theme="dark"] .badge-danger  { background: #7f1d1d; color: #fca5a5; }
html[data-theme="dark"] .badge-info    { background: #1e3a5f; color: #93c5fd; }
html[data-theme="dark"] .badge-muted   { background: var(--surface-3); color: var(--text-muted); }
html[data-theme="dark"] .badge-system  { background: #2e1065; color: #c4b5fd; }

/* =====================================================================
   Buttons — override Bootstrap's .btn
   ===================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}

.btn-primary:hover {
  background: var(--brand-dark);
  color: #fff;
}

.btn-outline {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.btn-outline:hover {
  background: var(--surface-3);
  color: var(--text);
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

.btn-icon {
  padding: 6px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-muted);
}

.btn-icon:hover {
  background: var(--surface-3);
  color: var(--text);
}

/* =====================================================================
   Health / progress rows
   ===================================================================== */
.health-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.health-row:last-child {
  border-bottom: none;
}

.health-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.health-dot.ok   { background: var(--success); }
.health-dot.warn { background: var(--warning); }
.health-dot.err  { background: var(--danger); }

.health-label {
  flex: 1;
  margin-left: 10px;
  font-size: 13px;
}

.health-value {
  font-size: 12px;
  color: var(--text-muted);
}

/* =====================================================================
   Theme toggle
   ===================================================================== */
.theme-btn {
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
}

.theme-btn:hover {
  color: var(--text);
}

/* =====================================================================
   ACME / info lists
   ===================================================================== */
.info-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.info-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.info-list li:last-child {
  border-bottom: none;
}

.info-list .key {
  color: var(--text-muted);
}

.info-list .val {
  font-weight: 500;
  text-align: right;
}

/* =====================================================================
   Sidebar-less pages (login, mfa-prompt) — ensure they span full width
   ===================================================================== */
.ncz-no-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* =====================================================================
   Bootstrap modal + form dark-mode theming
   All rules here bind Bootstrap 5 components to the design-system
   variables so they respond to html[data-theme="dark"].
   Light-mode values equal Bootstrap's own defaults, so light mode is
   visually unchanged.
   ===================================================================== */

/* --- Modal shell ---------------------------------------------------- */
.modal-content {
  background-color: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.modal-header {
  border-bottom-color: var(--border);
}

.modal-footer {
  border-top-color: var(--border);
}

.modal-title {
  color: var(--text);
}

.modal-body {
  color: var(--text);
}

/* --- Offcanvas shell ------------------------------------------------- */
.offcanvas {
  background-color: var(--surface);
  color: var(--text);
}

.offcanvas-header {
  border-bottom-color: var(--border);
}

.offcanvas-title {
  color: var(--text);
}

/* --- Form controls -------------------------------------------------- */
.form-control,
.form-select,
textarea.form-control {
  background-color: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.form-control::placeholder,
textarea.form-control::placeholder {
  color: var(--text-muted);
}

.form-control:focus,
.form-select:focus,
textarea.form-control:focus {
  background-color: var(--surface);
  color: var(--text);
  border-color: var(--brand);
  box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.18);
}

.form-control:disabled,
.form-control[readonly] {
  background-color: var(--surface-2);
  color: var(--text-muted);
  border-color: var(--border);
  opacity: 1;
}

.form-select:disabled {
  background-color: var(--surface-2);
  color: var(--text-muted);
  border-color: var(--border);
  opacity: 1;
}

/* Native <option> items — must match the surface so they are legible
   in browsers that paint the dropdown using the element's own colors.  */
.form-select option,
select option {
  background-color: var(--surface);
  color: var(--text);
}

/* In dark mode Bootstrap's default arrow SVG is a dark path on a light
   background — replace it with a white-path version so it stays visible
   on dark surfaces.  The filter approach is the most robust cross-browser
   way without duplicating the full SVG inline.                           */
html[data-theme="dark"] .form-select {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23e2e8f0' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e");
}

/* --- Form labels ----------------------------------------------------- */
.form-label,
.col-form-label {
  color: var(--text);
}

/* --- Form helper text ----------------------------------------------- */
.form-text {
  color: var(--text-muted);
}

/* Bootstrap's .text-muted utility — used extensively in modal/form
   contexts and in JS-injected certificate-details markup.               */
.text-muted {
  color: var(--text-muted) !important;
}

/* --- Input group ---------------------------------------------------- */
.input-group .form-control,
.input-group .form-select {
  background-color: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.input-group-text {
  background-color: var(--surface-2);
  color: var(--text-muted);
  border-color: var(--border);
}

/* btn-outline-secondary inside an input-group (e.g. password-toggle) */
.input-group .btn-outline-secondary {
  color: var(--text-muted);
  border-color: var(--border);
  background-color: var(--surface-2);
}

.input-group .btn-outline-secondary:hover {
  color: var(--text);
  background-color: var(--surface-3);
  border-color: var(--border);
}

/* --- Form check (checkbox / radio / switch) ------------------------- */
.form-check-input {
  background-color: var(--surface);
  border-color: var(--border);
}

.form-check-input:checked {
  background-color: var(--brand);
  border-color: var(--brand);
}

.form-check-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.18);
}

.form-check-label {
  color: var(--text);
}

/* Switch track — unchecked state in dark needs a visible track.        */
html[data-theme="dark"] .form-check-input[type="checkbox"].form-check-input:not(:checked) {
  background-color: var(--surface-3);
  border-color: var(--border);
}

/* --- Close button --------------------------------------------------- */
/* Bootstrap's .btn-close uses a dark SVG mask; in dark mode it becomes
   invisible.  Invert + grayscale makes it a light icon.                 */
html[data-theme="dark"] .btn-close {
  filter: invert(1) grayscale(100%) brightness(200%);
}

/* --- List group (member pickers, ACME template lists, etc.) --------- */
.list-group-item {
  background-color: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.list-group-item:hover,
.list-group-item:focus {
  background-color: var(--surface-2);
  color: var(--text);
}

.list-group-item.active {
  background-color: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* --- Tables inside modals / offcanvas (details panel) --------------- */
/* The certificate-details JS injects .table .table-sm with .text-muted
   td cells and <code class="small"> for ID / serial values.             */
.modal .table,
.offcanvas .table,
.modal table,
.offcanvas table {
  color: var(--text);
}

.modal .table td,
.modal .table th,
.offcanvas .table td,
.offcanvas .table th,
.modal table td,
.modal table th,
.offcanvas table td,
.offcanvas table th {
  color: var(--text);
  border-color: var(--border);
}

/* <code> snippets inside modals (ID, serial number in cert details).   */
.modal code,
.offcanvas code,
.modal pre,
.offcanvas pre {
  background-color: var(--surface-2);
  color: var(--text);
  border-radius: 3px;
  padding: 1px 4px;
}

/* --- nav-tabs (import cert / key paste-vs-file switcher) ------------ */
.nav-tabs {
  border-bottom-color: var(--border);
}

.nav-tabs .nav-link {
  color: var(--text-muted);
  border-color: transparent;
}

.nav-tabs .nav-link:hover,
.nav-tabs .nav-link:focus {
  color: var(--text);
  border-color: var(--border) var(--border) transparent;
  background-color: var(--surface-2);
}

.nav-tabs .nav-link.active,
.nav-tabs .nav-item.show .nav-link {
  color: var(--text);
  background-color: var(--surface);
  border-color: var(--border) var(--border) var(--surface);
}

/* --- Tab pane content ----------------------------------------------- */
.tab-content {
  background-color: var(--surface);
  color: var(--text);
}

/* --- Bootstrap alert variants (error/success/info/warning banners in
   modals and settings pages) ----------------------------------------- */
.alert {
  color: var(--text);
  border-color: var(--border);
  background-color: var(--surface-2);
}

.alert-danger {
  color: #f87171;
  background-color: #2d0a0a;
  border-color: #7f1d1d;
}

.alert-success {
  color: #6ee7b7;
  background-color: #052e16;
  border-color: #064e3b;
}

.alert-warning {
  color: #fcd34d;
  background-color: #1c0f00;
  border-color: #78350f;
}

.alert-info {
  color: #93c5fd;
  background-color: #0c1a36;
  border-color: #1e3a5f;
}

/* --- btn-outline-secondary (standalone and in input-groups) --------- */
/* Bootstrap's default is grey text on white bg — in dark mode the grey
   text becomes near-invisible against the dark surface.                 */
html[data-theme="dark"] .btn-outline-secondary {
  color: var(--text-muted);
  border-color: var(--border);
  background-color: transparent;
}

html[data-theme="dark"] .btn-outline-secondary:hover,
html[data-theme="dark"] .btn-outline-secondary:focus {
  color: var(--text);
  background-color: var(--surface-3);
  border-color: var(--border);
}

/* In light mode keep Bootstrap's own alert colours by resetting the
   dark overrides above only when NOT in dark mode.                      */
html:not([data-theme="dark"]) .alert-danger {
  color: #842029;
  background-color: #f8d7da;
  border-color: #f5c2c7;
}

html:not([data-theme="dark"]) .alert-success {
  color: #0f5132;
  background-color: #d1e7dd;
  border-color: #badbcc;
}

html:not([data-theme="dark"]) .alert-warning {
  color: #664d03;
  background-color: #fff3cd;
  border-color: #ffecb5;
}

html:not([data-theme="dark"]) .alert-info {
  color: #055160;
  background-color: #cff4fc;
  border-color: #b6effb;
}

/* --- Bootstrap cards inside modals (generic colour inheritance) ------ */
.modal .card,
.offcanvas .card {
  background-color: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

/* --- Banner (dismissible alert in the topbar area) ------------------ */
/* banner.html uses .btn-close inside a Bootstrap alert.  The dark-mode
   .btn-close rule above already covers this.                            */

/* =====================================================================
   Responsive breakpoints
   ===================================================================== */
@media (max-width: 1100px) {
  .stats-grid       { grid-template-columns: repeat(2, 1fr); }
  .dashboard-grid   { grid-template-columns: 1fr; }
  .section-row      { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-240px);
    transition: transform 0.2s;
  }
  .main {
    margin-left: 0;
  }
}
