:root {
  --bg: #0b1020;
  --bg-soft: #111832;
  --card: #141c33;
  --card-2: #1a2440;
  --border: #24304f;
  --text: #e8edf9;
  --muted: #8fa0c2;
  --primary: #3b82f6;
  --primary-2: #2563eb;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
  --violet: #8b5cf6;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(3, 7, 18, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: radial-gradient(1200px 600px at 80% -10%, #16224a 0%, var(--bg) 55%) fixed;
  color: var(--text);
  font-family: "Segoe UI", "Tahoma", "Helvetica Neue", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  min-height: 100vh;
}

h1, h2, h3 { margin: 0 0 .5rem; font-weight: 650; letter-spacing: -.2px; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.15rem; }

.muted { color: var(--muted); }
.xs { font-size: .8rem; }
.grow { flex: 1 1 auto; min-width: 0; }
[hidden] { display: none !important; }

/* ---------------------------- عناصر عامة ---------------------------- */
.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: .55rem 1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary-2); color: #fff; }
.btn-primary:hover { background: var(--primary); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--card-2); }
.btn-danger { background: #7f1d1d; color: #fff; }
.btn-danger:hover { background: var(--red); }
.btn-sm { padding: .35rem .7rem; font-size: .85rem; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.card {
  background: linear-gradient(180deg, var(--card) 0%, var(--bg-soft) 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

input, select, textarea {
  width: 100%;
  background: #0d1428;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: .55rem .75rem;
  font: inherit;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59,130,246,.18); }
textarea { resize: vertical; }

.field { display: block; margin-bottom: .85rem; }
.field > span { display: block; margin-bottom: .3rem; font-size: .85rem; color: var(--muted); }
.check { display: flex; align-items: center; gap: .5rem; font-size: .9rem; padding-top: 1.1rem; }
.check input { width: auto; }

.error { color: #fca5a5; background: rgba(239,68,68,.1); border: 1px solid rgba(239,68,68,.35); padding: .5rem .75rem; border-radius: 10px; font-size: .9rem; }
.warn { color: #fcd34d; background: rgba(245,158,11,.1); border: 1px solid rgba(245,158,11,.3); padding: .5rem .75rem; border-radius: 10px; font-size: .88rem; }

/* ---------------------------- الدخول ---------------------------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: min(420px, 100%); padding: 1.75rem; }
.brand { display: flex; align-items: center; gap: .8rem; margin-bottom: 1.4rem; }
.brand h1 { margin: 0; }
.brand p { margin: 0; font-size: .85rem; }
.brand-mark {
  display: grid; place-items: center;
  width: 46px; height: 46px; border-radius: 12px;
  background: linear-gradient(140deg, var(--primary), var(--violet));
  font-weight: 800; font-size: .95rem; color: #fff;
}
.brand-sm .brand-mark { width: 34px; height: 34px; border-radius: 9px; font-size: .75rem; }

/* ---------------------------- الشريط العلوي ---------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: .7rem 1.25rem;
  background: rgba(11,16,32,.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-actions { display: flex; align-items: center; gap: .5rem; }

.container { max-width: 1180px; margin: 0 auto; padding: 1.25rem; }

/* ---------------------------- الإحصائيات ---------------------------- */
.stats {
  display: grid; gap: .75rem; margin-bottom: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}
.stat {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .85rem 1rem;
}
.stat b { display: block; font-size: 1.6rem; line-height: 1.2; font-weight: 700; }
.stat span { font-size: .8rem; color: var(--muted); }
.stat.accent b { color: var(--green); }
.stat.warnish b { color: var(--amber); }
.stat.bad b { color: var(--red); }
.stat.info b { color: var(--primary); }

.toolbar { display: flex; gap: .6rem; padding: .8rem; margin-bottom: 1rem; flex-wrap: wrap; }
.toolbar select { width: auto; min-width: 140px; }

/* ---------------------------- الجدول ---------------------------- */
.table-card { padding: .25rem .25rem 0; overflow: hidden; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .7rem .8rem; text-align: start; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tbody tr:hover { background: rgba(59,130,246,.06); }
tbody tr:last-child td { border-bottom: none; }
.mono { font-family: "Consolas", "SF Mono", ui-monospace, monospace; direction: ltr; unicode-bidi: embed; }

.badge {
  display: inline-block; padding: .15rem .55rem; border-radius: 999px;
  font-size: .75rem; font-weight: 700; border: 1px solid transparent;
}
.badge.active { color: #86efac; background: rgba(34,197,94,.12); border-color: rgba(34,197,94,.35); }
.badge.issued { color: #93c5fd; background: rgba(59,130,246,.12); border-color: rgba(59,130,246,.35); }
.badge.expired { color: #fcd34d; background: rgba(245,158,11,.12); border-color: rgba(245,158,11,.35); }
.badge.revoked { color: #fca5a5; background: rgba(239,68,68,.12); border-color: rgba(239,68,68,.35); }
.badge.archived { color: #cbd5e1; background: rgba(148,163,184,.12); border-color: rgba(148,163,184,.3); }

.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.pager { display: flex; align-items: center; justify-content: space-between; padding: .6rem .8rem; }

/* ---------------------------- النوافذ ---------------------------- */
.dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--card);
  color: var(--text);
  padding: 1.4rem;
  width: min(560px, calc(100vw - 2rem));
  box-shadow: var(--shadow);
}
.dialog-wide { width: min(760px, calc(100vw - 2rem)); }
.dialog::backdrop { background: rgba(3,7,18,.7); backdrop-filter: blur(3px); }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.2rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }

.keys-output { display: grid; gap: .5rem; margin: 1rem 0; }
.key-row {
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  background: #0d1428; border: 1px dashed var(--border); border-radius: 10px;
  padding: .55rem .75rem; font-size: .95rem;
}

/* ---------------------------- التفاصيل ---------------------------- */
.detail-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: .9rem; }
.detail-key {
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  background: #0d1428; border: 1px solid var(--border); border-radius: 10px;
  padding: .6rem .8rem; margin-bottom: 1rem;
}
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.detail-grid div { background: #0f1730; border: 1px solid var(--border); border-radius: 10px; padding: .5rem .7rem; }
.detail-grid small { display: block; color: var(--muted); font-size: .72rem; }
.detail-grid b { font-weight: 650; font-size: .9rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.events { max-height: 240px; overflow: auto; border: 1px solid var(--border); border-radius: 10px; }
.events table { font-size: .82rem; }
.events th { position: sticky; top: 0; background: var(--card-2); }

.toast {
  position: fixed; inset-inline: 0; bottom: 1.2rem; margin: 0 auto;
  width: fit-content; max-width: 90vw;
  background: var(--card-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 999px;
  padding: .55rem 1.1rem; box-shadow: var(--shadow); z-index: 60; font-size: .9rem;
}
.toast.ok { border-color: rgba(34,197,94,.5); }
.toast.err { border-color: rgba(239,68,68,.5); }

/* ---------------------------- الجوال ---------------------------- */
@media (max-width: 720px) {
  .container { padding: .8rem; }
  .topbar { padding: .6rem .8rem; }
  #whoami { display: none; }
  table { font-size: .84rem; }
  th:nth-child(6), td:nth-child(6), th:nth-child(5), td:nth-child(5) { display: none; }
  .grid-2 { grid-template-columns: 1fr; }
  .stat b { font-size: 1.35rem; }
  .toolbar select { flex: 1 1 140px; }
}
