:root {
  color-scheme: light;
  --bg: #f7f8fb;
  --surface: #fff;
  --surface-2: #f9fafc;
  --line: #e9ebf2;
  --line-strong: #d8dce8;
  --ink: #071437;
  --text: #252f4a;
  --muted: #78829d;
  --faint: #a1a9bd;
  --primary: #1b84ff;
  --primary-hover: #056ee9;
  --primary-soft: #eaf3ff;
  --success: #17c653;
  --success-soft: #e7faee;
  --success-ink: #0f8f3c;
  --warning: #f6b100;
  --warning-soft: #fff6dd;
  --warning-ink: #a87800;
  --danger: #f8285a;
  --danger-soft: #ffeef3;
  --danger-ink: #d11c4b;
  --info: #7239ea;
  --info-soft: #f4effe;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(7, 20, 55, .04), 0 4px 16px rgba(7, 20, 55, .04);
  --sidebar: 288px;
  color: var(--text);
  font: 16px/1.7 Cairo, "Segoe UI", Tahoma, Arial, sans-serif;
  background: var(--bg);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0c0e14;
    --surface: #14171f;
    --surface-2: #181c26;
    --line: #232834;
    --line-strong: #31384a;
    --ink: #f4f6fb;
    --text: #cfd5e4;
    --muted: #8d97b0;
    --faint: #5f6983;
    --primary: #3d9bff;
    --primary-hover: #66b0ff;
    --primary-soft: #11253f;
    --success-soft: #0f2a1b;
    --success-ink: #4be083;
    --warning-soft: #2c2410;
    --warning-ink: #f6c84a;
    --danger-soft: #34131d;
    --danger-ink: #ff6b8e;
    --info-soft: #221a3a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
body { min-height: 100vh; margin: 0; background: var(--bg); -webkit-font-smoothing: antialiased; }
h1, h2, h3, p, dl, dd, ul { margin: 0; }
ul { padding: 0; list-style: none; }
h1 { color: var(--ink); font-size: 1.4rem; font-weight: 700; line-height: 1.3; }
h2 { color: var(--ink); font-size: 1rem; font-weight: 650; }
a { color: inherit; }
code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .9rem; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.icon { flex: none; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: .88rem; }
.kicker { color: var(--muted); font-size: .8rem; font-weight: 600; letter-spacing: .02em; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .45rem; min-height: 40px; padding: .45rem 1rem; border: 1px solid transparent; border-radius: 8px; font-size: .94rem; font-weight: 600; text-decoration: none; white-space: nowrap; transition: background .15s ease, border-color .15s ease, color .15s ease; }
.btn-sm { min-height: 32px; padding: .25rem .75rem; font-size: .86rem; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-light { background: var(--surface); border-color: var(--line-strong); color: var(--text); }
.btn-light:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }
.btn-danger-light { background: var(--danger-soft); color: var(--danger-ink); }
.btn-danger-light:hover { background: var(--danger); color: #fff; }
.btn-icon { display: inline-grid; place-items: center; width: 38px; height: 38px; padding: 0; border: 0; border-radius: 8px; background: transparent; color: var(--muted); }
.btn-icon:hover { background: var(--bg); color: var(--ink); }

/* App shell */
.app { display: flex; min-height: 100vh; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.sidebar { position: sticky; top: 0; align-self: flex-start; display: flex; flex-direction: column; flex: 0 0 var(--sidebar); width: var(--sidebar); height: 100vh; padding: 1.25rem 1rem; background: var(--surface); border-inline-end: 1px solid var(--line); overflow-y: auto; z-index: 30; }
.sidebar-brand { display: flex; align-items: center; gap: .75rem; padding: .25rem .5rem 1.4rem; text-decoration: none; }
.sidebar-brand span { display: grid; line-height: 1.25; }
.sidebar-brand strong { color: var(--ink); font-size: 1.05rem; }
.sidebar-brand small { color: var(--muted); font-size: .82rem; }
.brand-mark { display: block; flex: 0 0 40px; width: 40px !important; height: 40px !important; max-width: 40px; max-height: 40px; object-fit: contain; }
.menu { display: grid; gap: .15rem; align-content: start; flex: 1; }
.menu-label { margin: 1rem .6rem .35rem; color: var(--faint); font-size: .78rem; font-weight: 700; letter-spacing: .04em; }
.menu-label:first-child { margin-top: 0; }
.menu-item { display: flex; align-items: center; gap: .75rem; width: 100%; padding: .6rem .75rem; border: 0; border-radius: 8px; background: transparent; color: var(--muted); font-size: .96rem; font-weight: 550; text-align: start; text-decoration: none; transition: background .15s ease, color .15s ease; }
.menu-item:hover { background: var(--bg); color: var(--ink); }
.menu-item.is-active { background: var(--primary-soft); color: var(--primary); font-weight: 650; }
.menu-item.is-current { color: var(--primary); }
.menu-item .count { margin-inline-start: auto; min-width: 26px; padding: 0 .45rem; border-radius: 999px; background: var(--bg); color: var(--muted); font-size: .8rem; font-style: normal; font-weight: 650; text-align: center; font-variant-numeric: tabular-nums; }
.menu-item.is-current .count { background: var(--primary-soft); color: var(--primary); }
.sidebar-foot { display: grid; gap: .15rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.sidebar-scrim { display: none; }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 1rem; min-height: 68px; padding: .6rem 2rem; background: color-mix(in srgb, var(--surface) 92%, transparent); border-bottom: 1px solid var(--line); backdrop-filter: blur(8px); }
.menu-toggle { display: none; }
.crumbs { display: flex; align-items: center; gap: .5rem; color: var(--muted); font-size: .94rem; }
.crumbs a { text-decoration: none; }
.crumbs a:hover { color: var(--primary); }
.crumbs .sep { color: var(--faint); }
.crumbs [aria-current] { color: var(--ink); font-weight: 600; }
.topsearch { position: relative; display: flex; align-items: center; margin-inline-start: auto; width: min(360px, 100%); }
.topsearch .icon { position: absolute; inset-inline-start: .85rem; color: var(--faint); pointer-events: none; }
.topsearch input { padding-inline-start: 2.4rem; background: var(--bg); border-color: transparent; }
.topsearch input:focus { background: var(--surface); }
.user { display: flex; align-items: center; gap: .6rem; padding-inline-start: 1rem; border-inline-start: 1px solid var(--line); color: var(--ink); font-weight: 600; }
.avatar { display: inline-grid; place-items: center; flex: none; width: 34px; height: 34px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); font-size: .88rem; font-weight: 700; }
.avatar-lg { width: 38px; height: 38px; border-radius: 10px; }

/* Content */
.content { width: min(1440px, 100%); margin: 0 auto; padding: 1.75rem 2rem 4rem; display: grid; gap: 1.5rem; align-content: start; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.page-sub { margin-top: .2rem; color: var(--muted); }
.page-sub:empty { display: none; }
.page-actions { display: flex; gap: .6rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem 1.4rem; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }

/* KPI cards */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .75rem; }
.kpi { position: relative; display: flex; align-items: center; gap: .65rem; padding: .65rem .85rem .85rem; overflow: hidden; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); text-decoration: none; transition: border-color .15s ease, transform .15s ease; }
.kpi:hover { border-color: var(--line-strong); transform: translateY(-1px); }
.kpi.is-active { border-color: var(--tone); box-shadow: 0 0 0 1px var(--tone), var(--shadow); }
.kpi-icon { display: grid; place-items: center; flex: none; width: 34px; height: 34px; border-radius: 9px; background: var(--tone-soft); color: var(--tone); }
.kpi-body { display: grid; min-width: 0; }
.kpi-label { color: var(--muted); font-size: .78rem; font-weight: 600; }
.kpi-value { color: var(--ink); font-size: 1.3rem; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.kpi-bar { position: absolute; inset: auto 0 0; height: 3px; background: var(--line); }
.kpi-bar span { display: block; height: 100%; background: var(--tone); border-radius: 0 4px 4px 0; }
.tone-neutral { --tone: var(--muted); --tone-soft: var(--bg); }
.tone-primary { --tone: var(--primary); --tone-soft: var(--primary-soft); }
.tone-success { --tone: var(--success); --tone-soft: var(--success-soft); }
.tone-info { --tone: var(--info); --tone-soft: var(--info-soft); }
.tone-warning { --tone: var(--warning); --tone-soft: var(--warning-soft); }
.tone-danger { --tone: var(--danger); --tone-soft: var(--danger-soft); }

/* Layout of dashboard body */
.split { display: grid; grid-template-columns: minmax(0, 1fr); gap: 1.5rem; align-items: start; }
.keys-card { padding-bottom: .5rem; }

/* Table */
.table-wrap { overflow-x: auto; margin: 0 -1.4rem; }
table { width: 100%; min-width: 620px; border-collapse: collapse; }
th, td { text-align: start; padding: .6rem .75rem; border-bottom: 1px dashed var(--line); vertical-align: middle; }
th:first-child, td:first-child { padding-inline-start: 1.4rem; }
th:last-child, td:last-child { padding-inline-end: 1.4rem; }
th { background: var(--surface-2); color: var(--muted); font-size: .78rem; font-weight: 650; white-space: nowrap; border-bottom: 1px solid var(--line); }
td { color: var(--text); font-size: .85rem; }
td .badge, td .pill { font-size: .76rem; }
td .btn-sm { min-height: 28px; padding: .15rem .6rem; font-size: .8rem; }
tbody tr { transition: background .12s ease; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tr.is-new { background: var(--primary-soft); }
.cell-user { display: flex; align-items: center; gap: .6rem; }
.cell-user .avatar-lg { width: 30px; height: 30px; border-radius: 8px; font-size: .8rem; }
.cell-name { max-width: 16rem; overflow: hidden; color: var(--ink); font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
td:not(:first-child) { white-space: nowrap; }
td.actions { text-align: end; }
.pill { display: inline-block; padding: .05rem .6rem; border-radius: 6px; background: var(--info-soft); color: var(--info); font-size: .82rem; font-weight: 650; }

/* Badges */
.badge { display: inline-flex; align-items: center; gap: .4rem; border-radius: 6px; padding: .15rem .6rem; font-size: .82rem; font-weight: 650; white-space: nowrap; background: var(--bg); color: var(--muted); }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.status-unused, .status-created { color: var(--primary); background: var(--primary-soft); }
.status-activated, .status-renewed, .status-approved, .status-backup_approved { color: var(--success-ink); background: var(--success-soft); }
.status-released { color: var(--info); background: var(--info-soft); }
.status-revoked, .status-rejected { color: var(--danger-ink); background: var(--danger-soft); }
.status-requested, .status-backup_requested { color: var(--warning-ink); background: var(--warning-soft); }
.status-backup_rejected, .status-backup_stopped { color: var(--danger-ink); background: var(--danger-soft); }
.status-backup_takeover { color: var(--info); background: var(--info-soft); }
.status-expired { color: var(--warning-ink); background: var(--warning-soft); }

/* Empty */
.empty { display: grid; justify-items: center; gap: .6rem; padding: 2.5rem 1rem 2rem; color: var(--muted); }
.empty-icon { display: grid; place-items: center; width: 64px; height: 64px; border-radius: 50%; background: var(--bg); color: var(--faint); }
.empty-text { color: var(--muted); padding-bottom: .5rem; }

/* Timeline */
.timeline { position: relative; display: grid; gap: 1.1rem; max-height: 440px; overflow-y: auto; padding-inline-end: .35rem; }
.timeline li:not(:last-child)::before { content: ""; position: absolute; inset-block: 1rem -1.1rem; inset-inline-start: 5px; width: 2px; background: var(--line); }
.timeline li { position: relative; display: flex; gap: .85rem; }
.timeline-dot { position: relative; z-index: 1; flex: none; width: 12px; height: 12px; margin-top: .35rem; border-radius: 50%; border: 2px solid var(--surface); background: var(--muted); box-shadow: 0 0 0 1px var(--line-strong); }
.ev-created .timeline-dot { background: var(--primary); }
.ev-activated .timeline-dot, .ev-renewed .timeline-dot { background: var(--success); }
.ev-released .timeline-dot { background: var(--info); }
.ev-revoked .timeline-dot { background: var(--danger); }
.ev-rejected .timeline-dot, .ev-backup_requested .timeline-dot { background: var(--warning); }
.ev-backup_approved .timeline-dot { background: var(--success); }
.ev-backup_rejected .timeline-dot, .ev-backup_stopped .timeline-dot { background: var(--danger); }
.ev-backup_takeover .timeline-dot { background: var(--info); }
.timeline li > div { min-width: 0; }
.timeline-detail { margin-top: .2rem; color: var(--text); font-size: .9rem; overflow-wrap: anywhere; }
.timeline time { color: var(--faint); font-size: .82rem; }

/* Banners */
.banner { border-radius: 10px; padding: .75rem 1rem; font-size: .94rem; }
.banner.warn { background: var(--warning-soft); color: var(--warning-ink); border: 1px solid color-mix(in srgb, var(--warning) 30%, transparent); }
.banner.notice { background: var(--success-soft); color: var(--success-ink); border: 1px solid color-mix(in srgb, var(--success) 30%, transparent); }
.banner.error { background: var(--danger-soft); color: var(--danger-ink); border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent); }
.banner-card { border-color: color-mix(in srgb, var(--success) 40%, var(--line)); }
.new-keys { display: grid; gap: .5rem; }
.new-keys li { display: flex; align-items: center; gap: .8rem; flex-wrap: wrap; }
.key-chip { display: inline-flex; align-items: center; gap: .5rem; padding: .3rem .8rem; border-radius: 8px; background: var(--bg); color: var(--muted); }
.masked-key { direction: ltr; unicode-bidi: plaintext; letter-spacing: .06em; }

/* Forms */
label { display: grid; gap: .35rem; font-size: .88rem; font-weight: 650; color: var(--text); }
input, select { width: 100%; min-height: 40px; border: 1px solid var(--line-strong); border-radius: 8px; padding: .45rem .75rem; background: var(--surface); transition: border-color .15s ease, box-shadow .15s ease; }
input::placeholder { color: var(--faint); }
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent); outline: 0; }
.form-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .9rem; }
.form-grid .span-3 { grid-column: 1 / -1; }
.optional-fields { margin-top: 1rem; }
.optional-fields summary { width: max-content; cursor: pointer; color: var(--muted); font-size: .9rem; font-weight: 650; }
.optional-fields summary:hover, .optional-fields[open] summary { color: var(--primary); }
.optional-fields[open] summary { margin-bottom: .9rem; }

/* Modals */
.modal { width: min(440px, calc(100% - 2rem)); padding: 1.5rem; border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--text); box-shadow: 0 24px 64px rgba(7, 20, 55, .22); }
.modal-wide { width: min(680px, calc(100% - 2rem)); }
.modal::backdrop { background: rgba(7, 20, 55, .5); backdrop-filter: blur(2px); }
.modal form { display: grid; gap: 1rem; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; }
.modal-actions { display: flex; gap: .6rem; margin-top: .25rem; }
.download-error { color: var(--danger-ink); }

/* Key detail */
.key-hero { display: flex; align-items: center; gap: 1.1rem; padding: 1.4rem; }
.key-hero .kpi-icon { width: 56px; height: 56px; }
.key-hero-main { flex: 1; min-width: 0; display: grid; gap: .2rem; }
.key-code { display: block; direction: ltr; text-align: end; font-size: clamp(1rem, 3vw, 1.5rem); font-weight: 700; letter-spacing: .08em; color: var(--ink); overflow-wrap: anywhere; }
.key-hero-actions { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.5rem; }
.details-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.details-list div { min-width: 0; padding: .8rem 1rem; border-bottom: 1px dashed var(--line); }
.details-list div:nth-last-child(-n+2) { border-bottom: 0; }
.details-list div:nth-child(odd) { border-inline-start: 1px dashed var(--line); }
.details-list div:nth-child(odd):last-child, .details-list .wide { grid-column: span 2; border-inline-start: 0; }
.details-list dt { color: var(--muted); font-size: .8rem; font-weight: 650; }
.details-list dd { margin-top: .2rem; overflow-wrap: anywhere; color: var(--ink); font-size: .96rem; font-weight: 550; }
.details-list .machine { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .84rem; }
.detail-actions { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.detail-actions .actions { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.backup-actions { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; margin-top: 1rem; }
.download-form { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .75rem; }
.download-form input { width: auto; min-width: 240px; flex: 1; max-width: 340px; }
.renew-form { display: flex; gap: .5rem; }
.renew-form select { width: auto; min-width: 120px; }
.months-form { display: flex; align-items: center; gap: .5rem; padding: .25rem; border: 1px solid var(--line); border-radius: 10px; }
.months-form input[type=number] { width: 4.5rem; min-height: 36px; text-align: center; }
.months-form .unit { white-space: nowrap; color: var(--muted); font-size: .88rem; }

/* Auth pages */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; background: radial-gradient(900px 480px at 85% -10%, var(--primary-soft), transparent 70%), radial-gradient(700px 420px at 0% 110%, var(--success-soft), transparent 70%), var(--bg); }
.login-card { width: min(420px, 100%); display: grid; gap: .6rem; padding: 2.2rem; box-shadow: 0 16px 48px rgba(7, 20, 55, .1); }
.login-card h1 { font-size: 1.4rem; }
.login-card .lede { margin-bottom: .6rem; color: var(--muted); }
.login-card .eyebrow { color: var(--primary); font-size: .84rem; font-weight: 700; }
.login-card .primary { min-height: 42px; margin-top: .6rem; border: 0; border-radius: 8px; background: var(--primary); color: #fff; font-weight: 650; }
.login-card .primary:hover { background: var(--primary-hover); }
.login-card .crumb { width: max-content; color: var(--muted); font-size: .92rem; font-weight: 600; text-decoration: none; }
.login-card .crumb:hover { color: var(--primary); }
.login-card .banner { margin: 0; }
.brand-logo { display: block; width: 56px !important; height: 56px !important; max-width: 56px; max-height: 56px; object-fit: contain; justify-self: start; margin-bottom: .4rem; }

@media (max-width: 1280px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 1024px) {
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; transform: translateX(100%); transition: transform .22s ease; box-shadow: 0 0 40px rgba(7, 20, 55, .2); }
  .sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-open .sidebar-scrim { display: block; position: fixed; inset: 0; z-index: 25; background: rgba(7, 20, 55, .45); }
  .menu-toggle { display: inline-grid; }
  .topbar, .content { padding-inline: 1.25rem; }
}
@media (max-width: 760px) {
  .crumbs { display: none; }
  .topsearch { margin-inline-start: 0; flex: 1; }
  .user-name { display: none; }
  .user { padding-inline-start: 0; border: 0; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .detail-grid { grid-template-columns: minmax(0, 1fr); }
  .page-head { align-items: stretch; flex-direction: column; }
  .page-actions .btn { width: 100%; }
  .card-head { align-items: flex-start; flex-direction: column; gap: .75rem; }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .key-hero { flex-wrap: wrap; }
  .key-code { text-align: start; }
  .details-list { grid-template-columns: minmax(0, 1fr); }
  .details-list div, .details-list .wide, .details-list div:nth-child(odd):last-child { grid-column: auto; border-inline-start: 0 !important; border-bottom: 1px dashed var(--line) !important; }
  .details-list div:last-child { border-bottom: 0 !important; }
  .renew-form { width: 100%; }
  .renew-form select { flex: 1; }
  .months-form { width: 100%; }
  .months-form .btn { margin-inline-start: auto; }
}
