:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --border: #e2e4e9;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --accent: #3457d5;
  --accent-hover: #2a45ad;
  --accent-soft: #eaeefb;
  --danger: #c53030;
  --danger-bg: #fdecec;
  --success: #1f8a4c;
  --success-bg: #e8f7ee;
  --warn: #b7791f;
  --warn-bg: #fdf3e0;
  --gray-bg: #eef0f3;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(20, 22, 30, 0.08), 0 1px 2px rgba(20, 22, 30, 0.06);
  --shadow-lg: 0 20px 60px rgba(20, 22, 30, 0.22);
}

/* OS-level dark mode — applies whenever the toggle is on "system" (no
   data-theme attribute) and only then; an explicit "light" choice below
   must be able to override it. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161b;
    --surface: #1c1f26;
    --surface-2: #22252d;
    --border: #2c3038;
    --text: #e7e9ee;
    --text-muted: #9599a3;
    --accent: #6b87f0;
    --accent-hover: #8299f3;
    --accent-soft: #262b42;
    --danger: #f1897f;
    --danger-bg: #3a2323;
    --success: #6fcf97;
    --success-bg: #1f3327;
    --warn: #e0b263;
    --warn-bg: #392e18;
    --gray-bg: #262a32;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 20px 60px rgba(0, 0, 0, 0.55);
  }
}

/* Manual toggle forced to dark — wins regardless of OS setting. */
:root[data-theme="dark"] {
  --bg: #14161b;
  --surface: #1c1f26;
  --surface-2: #22252d;
  --border: #2c3038;
  --text: #e7e9ee;
  --text-muted: #9599a3;
  --accent: #6b87f0;
  --accent-hover: #8299f3;
  --accent-soft: #262b42;
  --danger: #f1897f;
  --danger-bg: #3a2323;
  --success: #6fcf97;
  --success-bg: #1f3327;
  --warn: #e0b263;
  --warn-bg: #392e18;
  --gray-bg: #262a32;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 20px 60px rgba(0, 0, 0, 0.55);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 24px;
  min-height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-header .brand {
  font-weight: 700;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}
.brand-icon {
  width: 22px;
  height: 22px;
  color: var(--accent);
  flex-shrink: 0;
}
.main-nav {
  display: flex;
  gap: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 3px;
  border-radius: 10px;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}
.app-header .user {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 13px;
  padding-left: 10px;
  border-left: 1px solid var(--border);
}

.tab-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 7px 14px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 500;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.tab-btn:hover {
  background: var(--surface);
  color: var(--text);
}
.tab-btn.active {
  background: var(--accent);
  color: #fff;
}
.tab-btn.active:hover {
  background: var(--accent);
}

.theme-toggle {
  display: flex;
  gap: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 3px;
  border-radius: 999px;
}
.theme-btn {
  border: none;
  background: transparent;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 13px;
  line-height: 1.3;
  cursor: pointer;
  color: var(--text-muted);
  transition: background-color 0.15s ease, color 0.15s ease;
  display: inline-flex;
  align-items: center;
}
.theme-btn svg {
  width: 16px;
  height: 16px;
}
.theme-btn:hover {
  color: var(--text);
}
.theme-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 24px 60px;
}
@media (max-width: 560px) {
  main {
    padding: 16px 14px 40px;
  }
  .app-header {
    padding: 10px 14px;
  }
  .header-actions {
    gap: 6px;
  }
}

.view {
  display: none;
}
.view.active {
  display: block;
  animation: view-in 0.18s ease;
}
@keyframes view-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

h1 {
  font-size: 21px;
  letter-spacing: -0.01em;
  margin: 0 0 4px;
}
h2 {
  font-size: 15.5px;
  margin: 0 0 4px;
}
.subtitle {
  color: var(--text-muted);
  margin: 0 0 20px;
  font-size: 13.5px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-pad {
  padding: 18px 20px;
}

.overview-grid {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 16px;
  margin-bottom: 20px;
  align-items: start;
}
@media (max-width: 800px) {
  .overview-grid {
    grid-template-columns: 1fr;
  }
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.stat-tile:hover {
  transform: translateY(-2px);
}
.stat-tile .num {
  font-size: 25px;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.01em;
}
.stat-tile .label {
  color: var(--text-muted);
  font-size: 12.5px;
  margin-top: 2px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.toolbar .spacer {
  flex: 1;
}

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--accent);
  cursor: pointer;
  padding: 0;
  vertical-align: middle;
}
label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.field {
  margin-bottom: 14px;
}
.field-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.field-row > .field {
  flex: 1;
  min-width: 140px;
}
.hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

button, .btn {
  font: inherit;
  cursor: pointer;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 7px 14px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.05s ease;
}
button svg, .btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}
button:hover, .btn:hover {
  background: var(--surface-2);
  text-decoration: none;
}
button:active, .btn:active {
  transform: translateY(1px);
}
button:disabled, .btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}
button:focus-visible, .btn:focus-visible, .tab-btn:focus-visible, .theme-btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
button.danger {
  color: var(--danger);
  border-color: var(--danger);
  background: transparent;
}
button.danger:hover {
  background: var(--danger-bg);
}
button.link {
  border: none;
  background: none;
  color: var(--accent);
  padding: 4px 6px;
}
button.small, .btn.small {
  padding: 4px 9px;
  font-size: 12.5px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tr:last-child td {
  border-bottom: none;
}
tbody tr {
  transition: background-color 0.1s ease;
}
tbody tr:hover {
  background: var(--surface-2);
}
tbody tr.row-selected {
  background: var(--accent-soft);
}
th.col-check, td.col-check {
  width: 34px;
  padding-right: 0;
}
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
}

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.badge-unused { background: var(--gray-bg); color: var(--text-muted); }
.badge-active { background: var(--success-bg); color: var(--success); }
.badge-expired { background: var(--warn-bg); color: var(--warn); }
.badge-revoked { background: var(--danger-bg); color: var(--danger); }

/* ---------- bulk action bar ---------- */

.bulk-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 13.5px;
  animation: bulk-bar-in 0.15s ease;
}
.bulk-bar[hidden] {
  display: none;
}
@keyframes bulk-bar-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 100;
  overflow-y: auto;
  opacity: 0;
  transition: opacity 0.16s ease;
}
.overlay[hidden] {
  display: none;
}
.overlay.open {
  opacity: 1;
}
.modal {
  background: var(--surface);
  border-radius: 10px;
  width: 100%;
  max-width: 520px;
  box-shadow: var(--shadow-lg);
  transform: translateY(-10px) scale(0.98);
  opacity: 0;
  transition: transform 0.18s ease, opacity 0.18s ease;
}
.overlay.open .modal {
  transform: translateY(0) scale(1);
  opacity: 1;
}
.modal.wide {
  max-width: 720px;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 {
  margin: 0;
}
.modal-body {
  padding: 20px;
  max-height: 70vh;
  overflow-y: auto;
}
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.close-x {
  border: none;
  background: none;
  font-size: 18px;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 4px;
  border-radius: 6px;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.close-x:hover {
  background: var(--surface-2);
  color: var(--text);
}

.toast-stack {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 13px;
  box-shadow: var(--shadow-lg);
  max-width: 340px;
  animation: toast-in 0.18s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.toast.error {
  background: var(--danger);
  color: #fff;
}
.toast.success {
  background: var(--success);
  color: #fff;
}

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}

.kv-list {
  display: grid;
  grid-template-columns: 140px 1fr;
  row-gap: 10px;
  column-gap: 12px;
  font-size: 13.5px;
}
.kv-list dt {
  color: var(--text-muted);
  font-weight: 600;
}
.kv-list dd {
  margin: 0;
}

.copy-key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 14px;
  color: var(--text-muted);
  font-size: 13px;
}

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.login-card {
  width: 100%;
  max-width: 360px;
  padding: 32px 28px;
}
.login-brand {
  margin-bottom: 10px;
  display: flex;
  justify-content: center;
}
.login-brand svg {
  width: 32px;
  height: 32px;
  color: var(--accent);
}
.login-card h1 {
  margin-bottom: 4px;
}
.login-error {
  color: var(--danger);
  font-size: 13px;
  margin: 10px 0 0;
  min-height: 1em;
}

.suspicious-flag {
  color: var(--warn);
  font-weight: 600;
  font-size: 12.5px;
}

.icon-line {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.icon {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}
.empty-state .icon {
  color: var(--success);
  width: 15px;
  height: 15px;
}

.events-list {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 13px;
}
.events-list li {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  gap: 12px;
}
.events-list li:last-child {
  border-bottom: none;
}
.events-list .ev-meta {
  color: var(--text-muted);
  font-size: 12px;
  flex: none;
}
.events-list li.empty-state {
  display: block;
  padding: 16px 0;
}

/* ---------- expiring soon widget ---------- */

.expiring-list {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 13px;
}
.expiring-list li {
  border-bottom: 1px solid var(--border);
}
.expiring-list li:last-child {
  border-bottom: none;
}
.expiring-list li.empty-state {
  border-bottom: none;
  padding: 24px 8px;
}
.expiring-row {
  all: unset;
  box-sizing: border-box;
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 6px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  color: inherit;
  text-align: left;
  transition: background-color 0.12s ease;
}
.expiring-row:hover, .expiring-row:focus-visible {
  background: var(--surface-2);
}
.expiring-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  overflow: hidden;
}
.expiring-key {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.expiring-product {
  color: var(--text-muted);
  font-size: 12px;
}
.expiring-when {
  flex: none;
  color: var(--warn);
  font-weight: 600;
  font-size: 12.5px;
  white-space: nowrap;
}

/* ---------- activation trend bar chart ---------- */

.bar-chart-plot {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 5px;
  height: 110px;
  padding-top: 24px;
  border-bottom: 1px solid var(--border);
}
.bar-col {
  flex: 1;
  min-width: 0;
  height: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  border-radius: 4px 4px 0 0;
  cursor: default;
}
.bar-col:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.bar {
  width: 100%;
  max-width: 20px;
  background: var(--accent);
  border-radius: 4px 4px 0 0;
  min-height: 3px;
  transition: height 0.3s ease, background-color 0.15s ease;
}
.bar-col:hover .bar, .bar-col:focus-visible .bar {
  background: var(--accent-hover);
}
.bar-labels {
  display: flex;
  gap: 5px;
  margin-top: 6px;
}
.bar-labels span {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 10.5px;
  color: var(--text-muted);
  visibility: hidden;
}
.bar-labels span.show {
  visibility: visible;
}
.chart-tooltip {
  position: absolute;
  bottom: 100%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  font-size: 11.5px;
  padding: 4px 9px;
  border-radius: 5px;
  white-space: nowrap;
  pointer-events: none;
  margin-bottom: 8px;
  box-shadow: var(--shadow-lg);
  z-index: 5;
}

/* ---------- loading states ---------- */

.spinner {
  display: inline-block;
  width: 15px;
  height: 15px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex: none;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}
.loading-inline {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  padding: 44px 0;
  font-size: 13.5px;
}

.skeleton-line {
  height: 12px;
  border-radius: 4px;
  width: 75%;
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: skeleton-pulse 1.4s ease infinite;
}
@keyframes skeleton-pulse {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}
.stat-tile .skeleton-num {
  height: 22px;
  width: 44px;
  margin-bottom: 8px;
}
.stat-tile .skeleton-label {
  height: 10px;
  width: 70%;
}
.events-list li .skeleton-line {
  align-self: center;
}

.error-state {
  color: var(--text-muted);
}
.error-state button.link {
  margin-top: 4px;
  font-size: 12.5px;
}
.stat-grid .error-state {
  grid-column: 1 / -1;
}

/* ---------- responsive tables ---------- */

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

/* ---------- key detail modal ---------- */

#key-detail-modal .modal {
  max-width: 900px;
}

.kd-header {
  padding-bottom: 14px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.kd-key-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.kd-key-string {
  font-size: 15px;
  font-weight: 600;
}
.kd-sub {
  color: var(--text-muted);
  font-size: 12.5px;
  margin-top: 4px;
}

.kd-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 16px;
  align-items: start;
}
@media (max-width: 680px) {
  .kd-columns {
    grid-template-columns: 1fr;
  }
}

.detail-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.detail-card + .detail-card {
  margin-top: 16px;
}
.detail-card h3 {
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  margin: 0 0 10px;
}
.detail-card .kv-list {
  grid-template-columns: 120px 1fr;
}
.action-toolbar {
  margin-bottom: 4px;
}
.kd-activity-log {
  margin-top: 16px;
}

.edit-details {
  margin-top: 12px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.edit-details summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}
.edit-details form {
  margin-top: 10px;
}

.hint-inline {
  color: var(--text-muted);
  font-style: italic;
  font-weight: 400;
}
.expiry-pending {
  color: var(--text-muted);
  font-style: italic;
}

/* ---------- expiry edit radio group ---------- */

.radio-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 10px 0;
}
.radio-option {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  margin: 0;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}
.radio-option input[type='radio'] {
  margin: 0;
  flex: none;
  accent-color: var(--accent);
}
.radio-option.selected {
  border-color: var(--accent);
  background: var(--surface);
}
.radio-suboption {
  width: auto;
  max-width: 200px;
}
.radio-suboption:disabled {
  opacity: 0.5;
}
.radio-suboption-num {
  width: 70px;
  max-width: 70px;
}

/* ---------- generated keys result ---------- */

.generated-summary {
  margin-bottom: 14px;
}
.generated-summary-row {
  font-size: 14.5px;
}
.generated-keys-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 320px;
  overflow-y: auto;
}
.generated-keys-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
}
.generated-keys-list li:last-child {
  border-bottom: none;
}
