:root {
  --bg: #eef1f6;
  --surface: #ffffff;
  --surface-alt: #f5f7fa;
  --ink: #151a23;
  --ink-muted: #5b6472;
  --border: #d7dce4;
  --accent: #245b8f;
  --accent-hover: #1b4569;
  --accent-ink: #ffffff;
  --danger: #a6332a;
  --danger-hover: #7e251e;
  --success: #2f7a4f;
  --success-bg: #e7f3ec;
  --warn: #9c6b15;
  --error-bg: #fbeceb;

  --role-administrator: #5b3a9e;
  --role-power_user: #0c6b62;
  --role-operator: #3b5b7a;
  --role-read_only: #6b7280;

  --radius: 6px;
  --font-sans: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #161b22;
    --surface-alt: #1c222b;
    --ink: #e6e9ef;
    --ink-muted: #8b94a3;
    --border: #2a313c;
    --accent: #5b9bd5;
    --accent-hover: #7cb0de;
    --accent-ink: #0f1419;
    --danger: #e5695f;
    --danger-hover: #ef8981;
    --success: #4caf77;
    --success-bg: #16281f;
    --warn: #d9a441;
    --error-bg: #2c1917;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

a { color: var(--accent); }

h1, h2, h3 {
  font-weight: 600;
  letter-spacing: -0.01em;
}

h1 { font-size: 1.375rem; margin: 0 0 1rem; }
h2 { font-size: 1rem; margin: 1.75rem 0 0.75rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
h2:first-child { margin-top: 0; padding-top: 0; border-top: none; }

/* -- Header / nav ------------------------------------------------------- */

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--ink);
  text-decoration: none;
  margin-right: 1.5rem;
}
.brand::after { content: "_"; color: var(--accent); }

nav { display: flex; align-items: center; }

nav a {
  margin-right: 1.25rem;
  color: var(--ink-muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
}
nav a:hover { color: var(--ink); }

.header-account { display: flex; align-items: center; gap: 0.75rem; }
.header-account a {
  color: var(--ink);
  text-decoration: none;
  font-size: 0.9rem;
}
.header-account a:hover { color: var(--accent); }
.header-account form { margin: 0; }
.header-account button {
  background: none;
  color: var(--ink-muted);
  border: 1px solid var(--border);
  padding: 0.35rem 0.75rem;
  font-size: 0.85rem;
}
.header-account button:hover { background: var(--surface-alt); color: var(--ink); }

.font-size-control { display: flex; gap: 0.25rem; }
.font-size-control button {
  background: none;
  color: var(--ink-muted);
  border: 1px solid var(--border);
  padding: 0.3rem 0.55rem;
  font-size: 0.8rem;
  line-height: 1;
}
.font-size-control button:hover { background: var(--surface-alt); color: var(--ink); }

.settings-menu { position: relative; }
.settings-menu summary {
  list-style: none;
  cursor: pointer;
  font-size: 1.15rem;
  color: var(--ink-muted);
  padding: 0.3rem 0.55rem;
  border-radius: var(--radius);
  user-select: none;
}
.settings-menu summary::-webkit-details-marker { display: none; }
.settings-menu summary:hover { color: var(--ink); background: var(--surface-alt); }
.settings-menu[open] summary { color: var(--ink); background: var(--surface-alt); }
.settings-menu nav {
  position: absolute;
  right: 0;
  top: calc(100% + 0.4rem);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.16);
  padding: 0.35rem;
  flex-direction: column;
  align-items: stretch;
  min-width: 180px;
  z-index: 20;
}
.settings-menu nav a {
  margin-right: 0;
  padding: 0.5rem 0.6rem;
  border-radius: var(--radius);
  white-space: nowrap;
}
.settings-menu nav a:hover { background: var(--surface-alt); color: var(--ink); }

/* -- Layout --------------------------------------------------------------*/

main {
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 1.5rem;
}

.table-scroll { overflow-x: auto; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.75rem;
}

.card + .card { margin-top: 1.25rem; }

.motd-banner {
  margin-top: 1rem;
  padding: 0.85rem 1rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  font-size: 0.9rem;
}

.dashboard-grid {
  margin-top: 1.25rem;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
}

.dashboard-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
}

.dashboard-tile-value {
  font-size: 2.25rem;
  font-weight: 600;
  color: var(--accent);
  line-height: 1.1;
}

.dashboard-tile-label {
  margin-top: 0.35rem;
  font-size: 0.85rem;
  color: var(--ink-muted);
}

.narrow { max-width: 420px; margin: 3.5rem auto; }

/* -- Forms --------------------------------------------------------------*/

label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--ink-muted);
  margin-bottom: 0.3rem;
}

input, select, textarea {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-family: inherit;
  margin-bottom: 1rem;
  background: var(--surface);
  color: var(--ink);
}

input:focus, select:focus, textarea:focus, button:focus-visible, a.button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

input[type="email"], input[name="email"], input[name="username"], input[type="text"][name$="secret"] {
  font-family: var(--font-mono);
}

.field-row { display: flex; gap: 1rem; }
.field-row > * { flex: 1; }

.search-field { position: relative; }
.search-field input { padding-right: 2.2rem; }
.search-field .clear-search {
  position: absolute;
  right: 0.5rem;
  top: 0.5rem;
  color: var(--ink-muted);
  text-decoration: none;
  font-size: 1.2rem;
  line-height: 1;
  padding: 0.15rem 0.35rem;
  border-radius: var(--radius);
}
.search-field .clear-search:hover { color: var(--ink); background: var(--surface-alt); }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.checkbox-row input[type="checkbox"] {
  width: auto;
  margin: 0;
}
.checkbox-row label {
  margin: 0;
  font-weight: 400;
  color: var(--ink);
  font-size: 0.95rem;
}

/* -- Buttons --------------------------------------------------------------*/

button, .button {
  display: inline-block;
  padding: 0.55rem 1.1rem;
  background: var(--accent);
  color: var(--accent-ink);
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
}
button:hover, .button:hover { background: var(--accent-hover); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.button-secondary {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--border);
}
.button-secondary:hover { background: var(--surface-alt); }

button.danger, .button.danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}
button.danger:hover, .button.danger:hover { background: var(--danger); color: white; }

/* -- Tables ---------------------------------------------------------------*/

table { width: 100%; border-collapse: collapse; }

th {
  text-align: left;
  padding: 0.6rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink-muted);
  border-bottom: 1px solid var(--border);
}

td {
  text-align: left;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

th a { color: var(--ink-muted); text-decoration: none; }
th a:hover { color: var(--ink); }
.sort-arrow { font-size: 0.7rem; }
button .icon { display: inline-block; margin-right: 0.3rem; font-size: 0.9em; vertical-align: -0.05em; }
button.icon-only { padding: 0.5rem 0.7rem; }
button.icon-only .icon { margin-right: 0; font-size: 1rem; }

.key-display { display: flex; align-items: center; gap: 0.5rem; }
.key-actions { display: flex; gap: 0.5rem; }

.icon-button {
  background: none;
  border: none;
  color: var(--ink-muted);
  cursor: pointer;
  padding: 0 0.2rem;
  font-size: 0.8rem;
  vertical-align: middle;
}
.icon-button:hover { color: var(--accent); }

td.mono, .mono { font-family: var(--font-mono); font-size: 0.85rem; }

tbody tr:hover { background: var(--surface-alt); }

/* -- Role badges & status ---------------------------------------------- */

.role-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
}
.role-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.role-administrator .role-dot { background: var(--role-administrator); }
.role-power_user .role-dot { background: var(--role-power_user); }
.role-operator .role-dot { background: var(--role-operator); }
.role-read_only .role-dot { background: var(--role-read_only); }

.status-pill {
  display: inline-block;
  font-size: 0.78rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--ink-muted);
}
.status-pill.on { color: var(--success); border-color: var(--success); }
.status-pill.off { color: var(--ink-muted); }

/* -- Messages -------------------------------------------------------------*/

.error {
  color: var(--danger);
  background: var(--error-bg);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  padding: 0.6rem 0.85rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.success {
  color: var(--success);
  background: var(--success-bg);
  border: 1px solid var(--success);
  border-radius: var(--radius);
  padding: 0.6rem 0.85rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

/* -- Toasts -----------------------------------------------------------*/

.toast {
  position: fixed;
  top: 1.25rem;
  right: 1.25rem;
  z-index: 1000;
  max-width: 22rem;
  padding: 0.65rem 1rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.toast-success {
  color: var(--success);
  background: var(--success-bg);
  border: 1px solid var(--success);
}
.toast-error {
  color: var(--danger);
  background: var(--error-bg);
  border: 1px solid var(--danger);
}
.toast-hide {
  opacity: 0;
  transform: translateY(-8px);
}

/* -- Self pill ----------------------------------------------------------*/

.self-pill {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  vertical-align: middle;
  margin-left: 0.4rem;
}

/* -- Danger zone ------------------------------------------------------- */

.danger-zone {
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-top: 1.75rem;
}
.danger-zone h2 { border-top: none; padding-top: 0; margin-top: 0; color: var(--danger); }
.danger-zone p { color: var(--ink-muted); font-size: 0.9rem; }

/* -- Password checklist -------------------------------------------------*/

.password-checklist {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0.75rem;
  background: var(--surface-alt);
  border-radius: var(--radius);
  font-size: 0.85rem;
}
.password-checklist li {
  color: var(--ink-muted);
  padding: 0.15rem 0;
}
.password-checklist li::before { content: "\2013\2002"; }
.password-checklist li.pass { color: var(--success); }
.password-checklist li.pass::before { content: "\2713\2002"; }
.password-checklist li.fail::before { content: "\2013\2002"; }

/* -- TOTP enrollment ------------------------------------------------------*/

.totp-qr {
  display: block;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  background: white;
  margin-bottom: 1rem;
}

.secret-key {
  font-family: var(--font-mono);
  background: var(--surface-alt);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

/* -- Modal dialogs (shared base - delete confirmation, new API key, etc.) */

dialog {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.75rem;
  max-width: 420px;
  width: calc(100% - 2rem);
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
}
dialog h2 {
  border-top: none;
  padding-top: 0;
  margin-top: 0;
}

/* -- Delete confirmation dialog ------------------------------------------ */

dialog#delete-confirm-dialog p.mono {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--surface-alt);
  border-radius: var(--radius);
  padding: 0.5rem 0.7rem;
}
dialog#delete-confirm-dialog #delete-confirm-copy-btn {
  padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--border);
}
dialog#delete-confirm-dialog #delete-confirm-copy-btn:hover {
  background: var(--surface-alt);
}

/* -- Filter links --------------------------------------------------------*/

.filter-links {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
}
.filter-links a {
  color: var(--ink-muted);
  text-decoration: none;
  font-size: 0.9rem;
  padding-bottom: 0.2rem;
  border-bottom: 2px solid transparent;
}
.filter-links a:hover { color: var(--ink); }
.filter-links a.filter-active {
  color: var(--ink);
  font-weight: 600;
  border-bottom-color: var(--accent);
}

/* -- Pagination -----------------------------------------------------------*/

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 1.25rem;
}
.pagination a {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
}
.pagination a:hover { color: var(--accent-hover); }
.pagination-nav { display: flex; align-items: center; gap: 1rem; }
.pagination-meta { display: flex; align-items: center; gap: 0.85rem; }
.page-size-form { display: flex; align-items: center; gap: 0.4rem; margin: 0; }
.page-size-form select {
  width: auto;
  margin-bottom: 0;
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
}
.page-size-form label { margin-bottom: 0; font-size: 0.85rem; }

/* -- Misc -----------------------------------------------------------------*/

.actions { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 0.75rem; }
.actions form { margin: 0; }

.muted { color: var(--ink-muted); font-size: 0.9rem; }
