:root {
  --navy: #02056e;
  --navy-dk: #010341;
  --orange: #ff950a;
  --orange-dk: #d4780a;
  --lime: #a6dc41;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #d0d7e2;
  --text: #1a1d23;
  --muted: #6b7280;
  --error: #dc3545;
  --ok: #4a8c12;
  --warn: #b45309;
  --shadow: 0 1px 4px rgba(0, 0, 0, .10);
  --shadow-lg: 0 12px 40px rgba(2, 5, 110, .12);
  --radius: 10px;
  --font: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
}

body {
  display: flex;
  flex-direction: column;
}

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

.topbar {
  background: var(--navy);
  color: #fff;
  padding: 14px 28px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 20px;
}

.brand { display: flex; flex-direction: column; margin-right: auto; }
.brand-mark { font-weight: 700; letter-spacing: .04em; font-size: 18px; }
.brand-sub { font-size: 12px; opacity: .8; }

.who { font-size: 13px; opacity: .9; }
.who strong { font-weight: 600; }

.nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
}

.nav a {
  color: #fff;
  text-decoration: none;
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 6px;
}

.nav a:hover { background: rgba(255, 255, 255, .12); }
.nav a.is-active { background: rgba(255, 255, 255, .2); font-weight: 600; }

.nav-logout { margin: 0; }
.nav-logout button {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, .35);
  color: #fff;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
}

.nav-logout button:hover { background: rgba(255, 255, 255, .12); }

.page {
  width: min(1120px, calc(100% - 32px));
  margin: 24px auto 48px;
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px 20px;
  margin-bottom: 18px;
}

.page-head h1 { margin: 0 0 6px; }
.page-head-lede { margin: 0; max-width: 46rem; }
.page-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.page-narrow { width: min(460px, calc(100% - 32px)); }

.lede {
  color: var(--muted);
  line-height: 1.5;
  margin: 0 0 20px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px 24px 24px;
  margin-bottom: 20px;
}

.login-card { box-shadow: var(--shadow-lg); }

h1, h2 { margin: 0 0 10px; color: var(--navy); }
h1 { font-size: 24px; }
h2 { font-size: 18px; }

.stack { display: flex; flex-direction: column; gap: 12px; }
.stack label, .row-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; }

input, select, textarea {
  font: inherit;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(2, 5, 110, .25);
  border-color: var(--navy);
}

textarea.pem {
  font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
  font-size: 12px;
  font-weight: 400;
  min-height: 11rem;
  resize: vertical;
}

.key-box {
  width: 100%;
  font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
  font-size: 13px;
  line-height: 1.45;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #f8fafc;
  word-break: break-all;
  resize: vertical;
}

.btn:disabled {
  opacity: .45;
  cursor: not-allowed;
}

.field-help, .hint, .muted { color: var(--muted); font-size: 13px; font-weight: 400; line-height: 1.45; }
.hint { margin-top: 16px; }
.break-all { word-break: break-all; }

.btn {
  font: inherit;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  padding: 9px 16px;
  cursor: pointer;
}

.btn-primary { background: var(--orange); color: #1a1d23; }
.btn-primary:hover { background: var(--orange-dk); color: #fff; }
.btn-secondary { background: var(--navy); color: #fff; }
.btn-secondary:hover { background: var(--navy-dk); }
.btn-ghost { background: transparent; color: var(--navy); border: 1px solid var(--border); }
.btn-ghost:hover { background: #eef0f8; }
.btn-danger { background: var(--error); color: #fff; }
.btn-danger:hover { background: #b02a37; }
.btn-sm { padding: 4px 10px; font-size: 12px; }

body.dlg-open { overflow: hidden; }

.dlg {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

#app-dialog { z-index: 46; }

.dlg[hidden] { display: none; }

.dlg-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(1, 3, 65, .46);
}

.dlg-box {
  position: relative;
  width: min(440px, 100%);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 22px 24px 20px;
}

.dlg-box-wide { width: min(520px, 100%); }
.panel-second-form {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.dlg-box h2 {
  margin: 0 0 8px;
  font-size: 20px;
}

.dlg-text {
  margin: 0 0 20px;
  color: var(--muted);
  line-height: 1.5;
  font-size: 14px;
}

.dlg-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.toast-host {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(360px, calc(100% - 24px));
  pointer-events: none;
}

.toast {
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  box-shadow: var(--shadow-lg);
  animation: toast-in .18s ease-out;
}

.toast-ok { background: #edf8c2; color: #3d6410; }
.toast-error { background: #fde8ea; color: #8a1f28; }
.toast-info { background: #e8e9f5; color: var(--navy); }
.toast.is-leaving { opacity: 0; transform: translateY(6px); transition: opacity .2s, transform .2s; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.row-form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  margin: 12px 0 8px;
}

.row-form .grow { flex: 1 1 220px; }
.inline-form { margin: 0; }
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.guide { margin: 8px 0 0; padding-left: 18px; line-height: 1.8; }
.back { margin: 0 0 16px; font-size: 14px; }
.back a { text-decoration: none; font-weight: 600; }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  text-decoration: none;
  color: inherit;
  display: block;
}
.stat:hover { border-color: var(--navy); }

.stat-n { display: block; font-size: 28px; font-weight: 700; color: var(--navy); }
.stat-l { font-size: 13px; color: var(--muted); }

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

table { border-collapse: collapse; width: 100%; }
th, td {
  border-bottom: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
  font-size: 13px;
  vertical-align: middle;
}
th { background: #f3f5f9; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.th-sort {
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.th-sort:hover,
.th-sort:focus {
  color: var(--navy);
  outline: none;
}
.th-sort:focus-visible {
  box-shadow: inset 0 0 0 2px rgba(2, 5, 110, .25);
}
.th-sort span::after {
  content: " ↕";
  opacity: .35;
  font-weight: 400;
}
.th-sort[aria-sort="ascending"] span::after {
  content: " ↑";
  opacity: 1;
  color: var(--navy);
}
.th-sort[aria-sort="descending"] span::after {
  content: " ↓";
  opacity: 1;
  color: var(--navy);
}
tr:hover td { background: #f8f9fd; }
.empty { color: var(--muted); font-style: italic; }
code { font-size: 12px; background: #eef1f6; padding: 1px 5px; border-radius: 4px; }

.flash {
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 14px;
}
.flash-error { background: #fde8ea; color: #8a1f28; }
.flash-ok { background: #edf8c2; color: #3d6410; }
.flash-info { background: #e8e9f5; color: var(--navy); }

.qr-wrap {
  width: 196px;
  height: 196px;
  margin: 12px 0;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
}
.qr-wrap svg { width: 100%; height: 100%; display: block; }

.secret code {
  font-size: 15px;
  letter-spacing: .08em;
  padding: 8px 10px;
  display: inline-block;
}

.pill {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: #eef1f6;
  color: var(--muted);
}
.pill-ok { background: #edf8c2; color: var(--ok); }
.pill-warn { background: #fff4e0; color: var(--warn); }
.pill-expired { background: #e8eaee; color: #4b5563; }

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; }
  .page { width: calc(100% - 20px); }
}
