:root {
  --bg: #0b0f14;
  --bg-elev: #121821;
  --bg-elev-2: #172030;
  --border: #223046;
  --text: #e7eef7;
  --muted: #8ea3bd;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --pending: #f59e0b;
  --active: #22c55e;
  --expired: #ef4444;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

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

h1,
h2 {
  margin: 0;
  font-weight: 650;
  letter-spacing: -0.01em;
}

h1 {
  font-size: 1.35rem;
}

h2 {
  font-size: 1rem;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.8rem;
}

.break {
  word-break: break-all;
}

[hidden] {
  display: none !important;
}

/* ------------------------------- layout -------------------------------- */
.view {
  min-height: 100vh;
}

.login-view {
  display: grid;
  place-items: center;
  padding: 2rem 1rem;
}

.app-view {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1.25rem 1rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.85rem;
  flex-wrap: wrap;
}

/* -------------------------------- login -------------------------------- */
.login-card {
  width: min(420px, 100%);
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  text-align: center;
  box-shadow: var(--shadow);
}

.login-card .logo {
  font-size: 2rem;
}

/* ------------------------------- topbar -------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.brand .logo {
  font-size: 1.4rem;
}

.brand div {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

.brand .muted {
  font-size: 0.75rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.topbar-actions {
  display: flex;
  gap: 0.5rem;
}

/* -------------------------------- stats -------------------------------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
}

.stat {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  text-align: left;
  cursor: pointer;
  color: inherit;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  transition: border-color 0.15s, transform 0.08s, background 0.15s;
}

.stat:hover {
  border-color: #35507a;
  background: var(--bg-elev-2);
}

.stat:active {
  transform: translateY(1px);
}

.stat-value {
  font-size: 1.7rem;
  font-weight: 680;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.stat-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

/* -------------------------------- forms -------------------------------- */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  text-align: left;
}

.field > span {
  font-size: 0.8rem;
  color: var(--muted);
}

input[type="text"],
input[type="url"],
input[type="password"],
input[type="search"],
select {
  width: 100%;
  padding: 0.6rem 0.7rem;
  background: #0d131c;
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
}

.config-form,
.input-row {
  display: flex;
  gap: 0.6rem;
}

.config-form input {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
}

.input-row input {
  flex: 1;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.toolbar input[type="search"] {
  width: 240px;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

/* -------------------------------- tabs --------------------------------- */
.tabs {
  display: flex;
  gap: 0.3rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.4rem;
  flex-wrap: wrap;
}

.tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  padding: 0.5rem 0.75rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 6px 6px 0 0;
}

.tab:hover {
  color: var(--text);
  background: var(--bg-elev-2);
}

.tab.is-active {
  color: var(--text);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

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

table.devices {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
}

table.devices th {
  text-align: left;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

table.devices td {
  padding: 0.6rem;
  border-bottom: 1px solid #1a2432;
  vertical-align: middle;
}

table.devices tr:hover td {
  background: #141b26;
}

th.right,
td.right {
  text-align: right;
}

.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.95rem;
  font-weight: 620;
  letter-spacing: 0.08em;
  background: none;
  border: none;
  color: var(--text);
  cursor: copy;
  padding: 0;
}

.code:hover {
  color: var(--accent);
}

.code-lg {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 1.6rem;
  letter-spacing: 0.16em;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.8rem;
  color: var(--muted);
}

.hash {
  max-width: 220px;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border: 1px solid transparent;
}

.badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.badge-pending {
  color: var(--pending);
  background: rgba(245, 158, 11, 0.12);
  border-color: rgba(245, 158, 11, 0.3);
}

.badge-active {
  color: var(--active);
  background: rgba(34, 197, 94, 0.12);
  border-color: rgba(34, 197, 94, 0.3);
}

.badge-expired {
  color: var(--expired);
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.3);
}

.actions {
  display: flex;
  gap: 0.35rem;
  justify-content: flex-end;
}

.empty {
  padding: 2.5rem 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ------------------------------- buttons ------------------------------- */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  padding: 0.5rem 0.85rem;
  border-radius: 9px;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}

.btn:hover:not(:disabled) {
  border-color: #3b567e;
  background: #1c2738;
}

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

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
}

.btn-danger {
  color: #fda4a4;
  border-color: rgba(239, 68, 68, 0.35);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.14);
}

.btn-block {
  width: 100%;
  padding: 0.65rem;
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.78rem;
}

.error {
  color: #fca5a5;
  font-size: 0.85rem;
  margin: 0;
}

/* --------------------------- activate by code -------------------------- */
.code-entry {
  display: flex;
  gap: 0.6rem;
  align-items: stretch;
}

.code-input {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 1.6rem;
  font-weight: 650;
  letter-spacing: 0.4em;
  text-align: center;
  padding: 0.55rem 0.5rem 0.55rem 0.9rem;
}

.code-input::placeholder {
  color: #2f3d52;
  letter-spacing: 0.4em;
}

.code-result {
  margin-top: 0.9rem;
}

.found {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0d131c;
  padding: 0.9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  animation: slide-in 0.16s ease-out;
}

.found.is-missing {
  border-color: rgba(245, 158, 11, 0.35);
  background: rgba(245, 158, 11, 0.06);
}

.found.is-done {
  border-color: rgba(34, 197, 94, 0.35);
  background: rgba(34, 197, 94, 0.06);
}

.found-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.found-head > div {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.found-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0.6rem 1rem;
  margin: 0;
}

.found-meta dt {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.found-meta dd {
  margin: 0.1rem 0 0;
  font-size: 0.86rem;
}

.activate-form {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) auto;
  gap: 0.6rem;
  align-items: end;
}

@media (max-width: 720px) {
  .activate-form {
    grid-template-columns: 1fr;
  }

  .code-input {
    font-size: 1.3rem;
  }
}

/* ------------------------------- dialogs ------------------------------- */
.dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-elev);
  color: var(--text);
  padding: 0;
  width: min(460px, 92vw);
  box-shadow: var(--shadow);
}

.dialog::backdrop {
  background: rgba(3, 6, 11, 0.7);
  backdrop-filter: blur(2px);
}

.dialog form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 1.25rem;
}

.device-summary {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  background: #0d131c;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.8rem 0.9rem;
}

.device-summary > div {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.2rem;
}

/* -------------------------------- toasts ------------------------------- */
.toasts {
  position: fixed;
  bottom: 1.1rem;
  right: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 50;
  max-width: min(380px, 90vw);
}

.toast {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  font-size: 0.85rem;
  box-shadow: var(--shadow);
  animation: slide-in 0.18s ease-out;
}

.toast-success {
  border-left-color: var(--active);
}

.toast-error {
  border-left-color: var(--expired);
}

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

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ------------------------------ responsive ----------------------------- */
@media (max-width: 720px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .toolbar input[type="search"] {
    width: 100%;
  }

  .config-form {
    flex-direction: column;
  }

  .card-head {
    align-items: stretch;
  }
}
