:root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-alt: #f8fafc;
    --border: #e2e8f0;
    --text: #0f172a;
    --muted: #5b6b82;
    --accent: #1d4ed8;
    --accent-dark: #1e40af;
    --accent-soft: #eef4ff;
    --header: #0f172a;
    --header-soft: #1e293b;
    --success: #166534;
    --success-soft: #dcfce7;
    --danger: #b91c1c;
    --danger-soft: #fee2e2;
    --warning: #b45309;
    --warning-soft: #fef3c7;
    --orange: #c2410c;
    --orange-soft: #ffedd5;
    --purple: #6d28d9;
    --purple-soft: #ede9fe;
    --slate-soft: #e2e8f0;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 8px 24px rgba(15, 23, 42, .05);
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.25; font-weight: 650; }
h1 { font-size: clamp(1.45rem, 1.1rem + 1.3vw, 1.9rem); letter-spacing: -.015em; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.02rem; }
p { margin: 0 0 .75rem; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: .86em; }
code { background: var(--surface-alt); border: 1px solid var(--border); border-radius: 5px; padding: .12rem .32rem; overflow-wrap: anywhere; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1.25rem 0; }

.container { width: min(1200px, 100% - 2rem); margin-inline: auto; }
.app-main { flex: 1; padding-block: 1.5rem 3rem; }

.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.skip-link {
    position: absolute; left: -999px; top: .5rem; z-index: 100;
    background: var(--surface); color: var(--text); padding: .6rem 1rem;
    border-radius: 6px; box-shadow: var(--shadow);
}
.skip-link:focus { left: .5rem; }

:focus-visible { outline: 3px solid #93c5fd; outline-offset: 2px; }

/* Header */
.app-header { background: var(--header); color: #e2e8f0; position: sticky; top: 0; z-index: 40; }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 1rem; min-height: 64px; }
.brand { display: inline-flex; align-items: center; gap: .65rem; color: #f8fafc; font-weight: 700; letter-spacing: -.01em; }
.brand:hover { text-decoration: none; }
.brand-mark {
    display: grid; place-items: center; width: 36px; height: 36px; border-radius: 9px;
    background: linear-gradient(135deg, #3b82f6, #1d4ed8); color: #fff; font-size: .8rem; font-weight: 800;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.1; font-size: .98rem; }
.brand-text span { font-size: .7rem; font-weight: 500; color: #94a3b8; letter-spacing: .06em; text-transform: uppercase; }

.nav-toggle {
    display: none; align-items: center; justify-content: center;
    min-width: 44px; min-height: 44px; border: 1px solid #334155; border-radius: 8px;
    background: var(--header-soft); color: #e2e8f0; font-size: 1.25rem; cursor: pointer;
}
.primary-nav { display: flex; align-items: center; gap: 1rem; flex: 1; justify-content: space-between; }
.nav-links { display: flex; align-items: center; gap: .15rem; list-style: none; margin: 0; padding: 0; flex-wrap: wrap; }
.nav-links a {
    display: inline-block; padding: .55rem .7rem; border-radius: 7px;
    color: #cbd5e1; font-size: .9rem; font-weight: 500;
}
.nav-links a:hover { background: #1e293b; color: #fff; text-decoration: none; }
.nav-links a.is-active { background: #1e293b; color: #fff; }
.nav-account { display: flex; align-items: center; gap: .6rem; }
.nav-user {
    display: inline-flex; flex-direction: column; line-height: 1.15;
    color: #cbd5e1; font-size: .88rem; max-width: 14rem;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.nav-user:hover { color: #fff; text-decoration: none; }
.nav-user-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.nav-user-hint { font-size: .66rem; color: #94a3b8; letter-spacing: .03em; }
.logout-form { margin: 0; }

.btn-ghost { background: transparent; border-color: #334155; color: #e2e8f0; }
.btn-ghost:hover { background: #1e293b; color: #fff; }

/* Global suspension banner */
.global-banner { background: #7f1d1d; color: #fee2e2; border-bottom: 1px solid #991b1b; }
.global-banner-inner { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem 1rem; padding-block: .6rem; font-size: .88rem; }
.global-banner-title { font-weight: 700; text-transform: uppercase; letter-spacing: .04em; font-size: .78rem; }
.global-banner-reason { color: #fecaca; }
.global-banner-link { color: #fff; text-decoration: underline; font-weight: 600; }

/* Layout helpers */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: .75rem 1rem; margin-bottom: 1.25rem; }
.page-head .subtitle { color: var(--muted); font-size: .9rem; margin: .15rem 0 0; }
.page-actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.stack > * + * { margin-top: 1rem; }
.section-gap { margin-top: 1.5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.tiny { font-size: .78rem; }
.nowrap { white-space: nowrap; }
.text-right { text-align: right; }
.inline-form { display: inline; margin: 0; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-header { padding: 1rem 1.15rem; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: .75rem; flex-wrap: wrap; }
.card-header h2, .card-header h3 { margin: 0; }
.card-body { padding: 1.15rem; }
.card-body > :last-child { margin-bottom: 0; }
.card-tight .card-body { padding: .85rem 1rem; }

/* Stats */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .85rem; margin-bottom: 1.5rem; }
.stat {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: .95rem 1rem; box-shadow: var(--shadow); min-width: 0;
}
.stat-label { display: block; font-size: .76rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.stat-value { display: block; font-size: 1.7rem; font-weight: 700; letter-spacing: -.02em; margin-top: .15rem; }
.stat small { color: var(--muted); font-size: .76rem; }
.stat-accent { border-top: 3px solid var(--accent); }
.stat-success { border-top: 3px solid #16a34a; }
.stat-warning { border-top: 3px solid #d97706; }
.stat-danger { border-top: 3px solid #dc2626; }
.stat-muted { border-top: 3px solid #94a3b8; }

/* Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
    min-height: 42px; padding: .5rem .95rem; border-radius: 8px; border: 1px solid transparent;
    font: inherit; font-size: .9rem; font-weight: 600; line-height: 1.2; cursor: pointer;
    text-decoration: none; color: #fff; background: var(--accent);
    transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.btn:hover { background: var(--accent-dark); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-sm { min-height: 36px; padding: .35rem .7rem; font-size: .82rem; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: #cbd5e1; }
.btn-secondary:hover { background: var(--surface-alt); }
.btn-danger { background: #dc2626; }
.btn-danger:hover { background: #b91c1c; }
.btn-warning { background: #d97706; }
.btn-warning:hover { background: #b45309; }
.btn-success { background: #15803d; }
.btn-success:hover { background: #166534; }
.btn-outline-danger { background: transparent; color: var(--danger); border-color: #fca5a5; }
.btn-outline-danger:hover { background: var(--danger-soft); }
.btn-link { background: none; border: 0; color: var(--accent); padding: 0; min-height: auto; font-weight: 600; cursor: pointer; }

/* Alerts */
.flash-area { padding-top: 1rem; }
.flash-area:empty { padding-top: 0; }
.alert { padding: .8rem 1rem; border-radius: 8px; border: 1px solid transparent; font-size: .9rem; margin-bottom: .75rem; }
.alert p { margin: 0; }
.alert-success { background: var(--success-soft); color: var(--success); border-color: #86efac; }
.alert-danger { background: var(--danger-soft); color: var(--danger); border-color: #fca5a5; }
.alert-warning { background: var(--warning-soft); color: var(--warning); border-color: #fcd34d; }
.alert-info { background: var(--accent-soft); color: var(--accent-dark); border-color: #bfdbfe; }
.alert-key { background: #ecfdf5; border: 2px dashed #34d399; color: #065f46; padding: 1rem; }
.alert-key code { display: block; margin: .5rem 0; padding: .6rem .7rem; background: #fff; border: 1px solid #6ee7b7; font-size: 1.05rem; letter-spacing: .06em; }

/* Badges */
.badge {
    display: inline-flex; align-items: center; gap: .25rem; padding: .2rem .55rem;
    border-radius: 999px; font-size: .72rem; font-weight: 700; letter-spacing: .03em;
    text-transform: uppercase; white-space: nowrap; background: var(--slate-soft); color: #334155;
}
.badge-active { background: var(--success-soft); color: var(--success); }
.badge-expired { background: var(--warning-soft); color: var(--warning); }
.badge-suspended { background: var(--orange-soft); color: var(--orange); }
.badge-revoked, .badge-blocked { background: var(--danger-soft); color: var(--danger); }
.badge-system-suspended { background: var(--purple-soft); color: var(--purple); }
.badge-not-activated, .badge-released { background: var(--slate-soft); color: #475569; }
.badge-super-admin { background: #ede9fe; color: #5b21b6; }
.badge-admin { background: var(--accent-soft); color: var(--accent-dark); }
.badge-inactive { background: var(--slate-soft); color: var(--muted); }

/* Tables */
.table-wrap { width: 100%; overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-size: .89rem; }
.table th, .table td { padding: .7rem .75rem; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
.table th { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: var(--surface-alt); font-weight: 700; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fbfdff; }
.table .cell-strong { font-weight: 600; }
.table .cell-actions { white-space: nowrap; }
.table .actions { display: flex; flex-wrap: wrap; gap: .35rem; }
.empty-state { padding: 2rem 1rem; text-align: center; color: var(--muted); }
.empty-state p { margin-bottom: .75rem; }

/* Forms */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 1rem; }
.form-field { display: flex; flex-direction: column; gap: .3rem; min-width: 0; }
.form-field-full { grid-column: 1 / -1; }
.form-field label { font-size: .84rem; font-weight: 600; color: #334155; }
.form-field .hint { font-size: .78rem; color: var(--muted); }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="search"], select, textarea {
    width: 100%; min-height: 44px; padding: .55rem .7rem; border: 1px solid #cbd5e1;
    border-radius: 8px; font: inherit; font-size: .93rem; color: var(--text); background: #fff;
}
textarea { min-height: 100px; resize: vertical; }
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, #64748b 50%), linear-gradient(135deg, #64748b 50%, transparent 50%); background-position: calc(100% - 18px) calc(50% - 2px), calc(100% - 13px) calc(50% - 2px); background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; padding-right: 2rem; }
input:focus, select:focus, textarea:focus { border-color: #93c5fd; box-shadow: 0 0 0 3px rgba(59, 130, 246, .15); outline: none; }
.field-error { color: var(--danger); font-size: .8rem; }
.is-invalid { border-color: #f87171 !important; }
.checkbox-field { flex-direction: row; align-items: center; gap: .5rem; }
.checkbox-field input { width: 1.15rem; height: 1.15rem; min-height: auto; }
.form-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 1.25rem; }
.form-note { font-size: .8rem; color: var(--muted); margin-top: -.25rem; }

.search-bar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: flex-end; }
.search-bar .form-field { flex: 1 1 180px; }
.search-bar .btn { flex: 0 0 auto; }
.filter-bar { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.filter-bar .form-field { flex: 1 1 170px; }

.inline-action { display: inline-block; }
.inline-action summary { list-style: none; cursor: pointer; }
.inline-action summary::-webkit-details-marker { display: none; }
.inline-action[open] summary { margin-bottom: .35rem; }
.inline-action form { display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; }
.inline-action input { min-width: 10rem; min-height: 38px; font-size: .82rem; }

/* Detail grids */
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; }
.detail-list { margin: 0; }
.detail-list dt { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; }
.detail-list dd { margin: .1rem 0 1rem; overflow-wrap: anywhere; }
.detail-list dd:last-child { margin-bottom: 0; }
.detail-split { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(0, 1fr); gap: 1.25rem; align-items: start; }
.action-stack { display: grid; gap: .75rem; }
.action-card { border: 1px solid var(--border); border-radius: 8px; padding: .85rem; background: var(--surface-alt); }
.action-card.danger { border-color: #fecaca; background: #fff7f7; }
.action-card h3 { margin-bottom: .25rem; }
.action-card form { display: flex; flex-direction: column; gap: .5rem; }
.action-row { display: flex; flex-wrap: wrap; gap: .5rem; }

/* Auth / login */
.auth-shell { min-height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 2rem 1rem; background: radial-gradient(circle at 20% 0%, #dbeafe 0, transparent 45%), var(--bg); }
.auth-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: 0 20px 50px rgba(15, 23, 42, .12); padding: 1.75rem; }
.auth-brand { display: flex; align-items: center; gap: .7rem; margin-bottom: 1.25rem; }
.auth-brand .brand-mark { width: 42px; height: 42px; }
.auth-brand h1 { font-size: 1.15rem; margin: 0; }
.auth-brand p { margin: .1rem 0 0; font-size: .78rem; color: var(--muted); }
.auth-card .form-field { margin-bottom: .9rem; }
.auth-card .btn { width: 100%; margin-top: .35rem; }
.auth-footnote { margin-top: 1.25rem; font-size: .78rem; color: var(--muted); text-align: center; }
.auth-footer { margin-top: 1.25rem; font-size: .75rem; color: var(--muted); text-align: center; }

/* Pagination */
.pagination { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; padding: .9rem 1.15rem; border-top: 1px solid var(--border); }
.pagination .pages { color: var(--muted); font-size: .82rem; }
.pagination .controls { display: flex; gap: .4rem; }

/* Footer */
.app-footer { border-top: 1px solid var(--border); background: var(--surface); color: var(--muted); font-size: .78rem; }
.footer-inner { display: flex; flex-wrap: wrap; gap: .35rem 1rem; justify-content: space-between; padding-block: 1rem; }

/* Utilities */
.mt-0 { margin-top: 0; }
.mt-1 { margin-top: .5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: .5rem; }
.mb-2 { margin-bottom: 1rem; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: .75rem; flex-wrap: wrap; }
.key-value { display: flex; gap: .5rem; align-items: baseline; flex-wrap: wrap; }
.reason-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.divider { height: 1px; background: var(--border); margin: 1rem 0; }
.pill-row { display: flex; flex-wrap: wrap; gap: .4rem; }

/* Responsive */
@media (max-width: 960px) {
    .detail-split { grid-template-columns: 1fr; }
}

@media (max-width: 860px) {
    .nav-toggle { display: inline-flex; }
    .primary-nav {
        display: none; position: absolute; left: 0; right: 0; top: 64px;
        flex-direction: column; align-items: stretch; gap: .5rem;
        background: var(--header); padding: .75rem 1rem 1rem;
        border-top: 1px solid #334155; box-shadow: 0 18px 30px rgba(15, 23, 42, .35);
    }
    .primary-nav.is-open { display: flex; }
    .nav-links { flex-direction: column; align-items: stretch; gap: .1rem; }
    .nav-links a { display: block; padding: .7rem .75rem; font-size: .95rem; }
    .nav-account { flex-direction: row; justify-content: space-between; border-top: 1px solid #334155; padding-top: .75rem; }
    .nav-user { max-width: 60%; }
}

@media (max-width: 767px) {
    .container { width: calc(100% - 1.5rem); }
    .app-main { padding-block: 1rem 2rem; }
    .page-head { align-items: stretch; }
    .page-actions { width: 100%; }
    .page-actions .btn { flex: 1 1 auto; }

    .table-wrap { overflow: visible; }
    table.table, .table thead, .table tbody, .table tr, .table td { display: block; width: 100%; }
    .table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
    .table tbody tr {
        border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
        margin-bottom: .75rem; padding: .35rem .6rem; box-shadow: var(--shadow);
    }
    .table tbody tr:hover { background: var(--surface); }
    .table tbody td {
        display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem;
        text-align: right; border: 0; padding: .45rem .1rem; font-size: .87rem;
    }
    .table tbody td::before {
        content: attr(data-label); font-size: .72rem; font-weight: 700; letter-spacing: .04em;
        text-transform: uppercase; color: var(--muted); text-align: left; flex: 0 0 42%;
    }
    .table tbody td[data-label=""]::before { content: none; }
    .table tbody td.cell-actions { justify-content: flex-end; }
    .table tbody td.cell-actions::before { flex: 0 0 auto; }
    .table .actions { justify-content: flex-end; }
    .empty-state { border: 1px dashed var(--border); border-radius: 10px; }
    .inline-action form { justify-content: flex-end; }
    .inline-action input { flex: 1 1 100%; }
    .auth-card { padding: 1.25rem; }
    .footer-inner { flex-direction: column; }
}

@media (max-width: 420px) {
    .stat-grid { grid-template-columns: 1fr 1fr; gap: .6rem; }
    .stat { padding: .8rem; }
    .stat-value { font-size: 1.4rem; }
    .btn { padding-inline: .75rem; }
    .btn-sm { min-height: 38px; }
}

@media (max-width: 767px) {
    .btn-sm { min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; scroll-behavior: auto !important; }
}
