@font-face {
  font-family: "Onest";
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("/fonts/onest-latin.woff2") format("woff2");
}
@font-face {
  font-family: "JetBrains Mono";
  font-style: normal;
  font-weight: 100 800;
  font-display: swap;
  src: url("/fonts/jetbrains-mono-latin.woff2") format("woff2");
}

:root {
  --bg: #090d16;
  --surface: #111827;
  --surface-2: #0f1623;
  --line: #1f2937;
  --ink: #f8fafc;
  --muted: #94a3b8;
  --brand: #2563eb;
  --emerald: #10b981;
  --amber: #f59e0b;
  --danger: #ef4444;
  --ease-expo: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-spring: cubic-bezier(0.16, 1, 0.3, 1);
}

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

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: "Onest", -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

.mono {
  font-family: "JetBrains Mono", monospace !important;
}

.muted {
  color: var(--muted);
  font-size: 13px;
  margin: 0;
}

/* Login Gate */
.login-stage {
  position: relative;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(600px 340px at 50% 35%, rgba(37, 99, 235, 0.14), transparent 70%);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 26px;
  box-shadow: 0 28px 60px -18px rgba(0, 0, 0, 0.65);
  animation: cardEnter 0.45s var(--ease-expo) both;
}

.login-card.is-shaking {
  animation: shakeX 0.38s ease-in-out;
}

@keyframes cardEnter {
  from { opacity: 0; transform: translateY(16px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes shakeX {
  0%, 100% { transform: translateX(0); }
  22% { transform: translateX(-7px); }
  45% { transform: translateX(6px); }
  68% { transform: translateX(-4px); }
  88% { transform: translateX(3px); }
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.brand-row h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--brand);
  color: #ffffff;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  transition: transform 0.25s var(--ease-spring);
}

.brand-mark:hover {
  transform: scale(1.07);
}

/* App Shell */
.app-shell {
  animation: fadeSlideUp 0.36s var(--ease-expo) both;
}

/* Header */
.header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(9, 13, 22, 0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}

.header-inner {
  max-width: 1220px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.header-title {
  font-weight: 700;
  font-size: 15.5px;
  margin-right: 6px;
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface);
  padding: 4px;
  border-radius: 10px;
  border: 1px solid var(--line);
}

.tab-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 7px;
  cursor: pointer;
  transition: background-color 0.2s ease, color 0.2s ease, transform 0.16s var(--ease-spring);
}

.tab-btn:hover {
  color: var(--ink);
}

.tab-btn:active {
  transform: scale(0.96);
}

.tab-btn.is-active {
  background: #1e293b;
  color: var(--ink);
}

.tab-panel:not([hidden]) {
  animation: fadeSlideUp 0.32s var(--ease-expo) both;
}

@keyframes fadeSlideUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.ghost-btn {
  appearance: none;
  text-decoration: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease, transform 0.16s var(--ease-spring);
}

.ghost-btn:hover {
  border-color: #334155;
  background: #172033;
}

.ghost-btn:active {
  transform: scale(0.96);
}

.ghost-btn.is-copied {
  border-color: var(--emerald);
  color: #34d399;
}

.main-wrap {
  max-width: 1220px;
  margin: 0 auto;
  padding: 24px 20px 56px;
}

/* Metric Cards */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 18px;
}

.metric-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  display: grid;
  gap: 6px;
  animation: fadeSlideUp 0.4s var(--ease-expo) both;
  transition: border-color 0.22s ease, transform 0.22s var(--ease-expo);
}

.metric-card:nth-child(1) { animation-delay: 0.02s; }
.metric-card:nth-child(2) { animation-delay: 0.07s; }
.metric-card:nth-child(3) { animation-delay: 0.12s; }
.metric-card:nth-child(4) { animation-delay: 0.17s; }

.metric-card:hover {
  border-color: #334155;
  transform: translateY(-2px);
}

.metric-label {
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 500;
}

.metric-val {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.025em;
  font-variant-numeric: tabular-nums;
}

.metric-sub {
  font-size: 12px;
  color: var(--muted);
}

.device-status-line {
  display: flex;
  align-items: center;
  gap: 9px;
}

.radar-dot {
  position: relative;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #64748b;
  flex-shrink: 0;
}

.radar-dot.is-online {
  background: #10b981;
}

.radar-dot.is-online::after {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 1.5px solid #10b981;
  animation: radarPing 2s ease-out infinite;
}

.radar-dot.is-stale {
  background: #f59e0b;
}

@keyframes radarPing {
  0% { transform: scale(0.8); opacity: 0.75; }
  80%, 100% { transform: scale(2.1); opacity: 0; }
}

/* Surface Cards & Tables */
.surface-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
}

.surface-card h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.search-box {
  width: min(320px, 100%);
  padding: 9px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  font-family: inherit;
  font-size: 13.5px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.search-box:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.24);
}

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

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th {
  text-align: left;
  padding: 10px 10px;
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

td {
  padding: 12px 10px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

tbody tr {
  transition: background-color 0.16s ease;
  animation: fadeSlideUp 0.28s var(--ease-expo) both;
}

tbody tr:hover {
  background: rgba(255, 255, 255, 0.025);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-family: "JetBrains Mono", monospace;
  font-size: 11px;
  font-weight: 600;
}

.badge.is-paid,
.badge.is-delivered,
.badge.is-matched {
  background: rgba(16, 185, 129, 0.14);
  color: #34d399;
}

.badge.is-pending,
.badge.is-ping {
  background: rgba(245, 158, 11, 0.14);
  color: #fbbf24;
}

.badge.is-expired,
.badge.is-failed,
.badge.is-unmatched,
.badge.is-duplicate {
  background: rgba(239, 68, 68, 0.14);
  color: #f87171;
}

.badge.is-none {
  background: rgba(148, 163, 184, 0.12);
  color: var(--muted);
}

/* Forms */
.settings-card {
  max-width: 820px;
}

.form-stack {
  display: grid;
  gap: 15px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.field {
  display: grid;
  gap: 6px;
}

.field label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
}

.field input[type="text"],
.field input[type="url"],
.field input[type="password"],
.field input[type="number"],
.field select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink);
  font-family: inherit;
  font-size: 13.5px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.field select:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.24);
}

.api-code {
  margin: 0;
  padding: 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: #cbd5e1;
  font-size: 12px;
  line-height: 1.55;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.field input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.24);
}

.color-row,
.secret-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.color-row input[type="color"] {
  width: 42px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 2px;
  cursor: pointer;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13.5px;
  color: var(--muted);
  cursor: pointer;
}

.btn-primary {
  appearance: none;
  border: 0;
  border-radius: 10px;
  padding: 10px 18px;
  background: var(--brand);
  color: #ffffff;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.2s ease, transform 0.16s var(--ease-spring);
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-primary:active {
  transform: scale(0.97);
}

.btn-primary:disabled {
  opacity: 0.7;
  cursor: wait;
}

.save-msg {
  color: #34d399;
  font-size: 13px;
  font-weight: 600;
  animation: fadeSlideUp 0.24s var(--ease-expo) both;
}

.err-msg {
  margin: 0;
  color: #f87171;
  font-size: 13px;
}

/* Devices Tab */
.devices-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}

.pairing-layout {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.qr-frame {
  width: 156px;
  height: 156px;
  padding: 10px;
  border-radius: 12px;
  background: #ffffff;
  flex-shrink: 0;
  animation: qrPop 0.36s var(--ease-expo) both;
}

@keyframes qrPop {
  from { opacity: 0; transform: scale(0.92); }
  to { opacity: 1; transform: scale(1); }
}

.qr-frame svg {
  width: 100%;
  height: 100%;
  display: block;
}

.pair-creds {
  flex: 1;
  min-width: 200px;
  display: grid;
  gap: 10px;
}

.devices-stack {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.device-row {
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  animation: fadeSlideUp 0.28s var(--ease-expo) both;
  transition: border-color 0.2s ease;
}

.device-row:hover {
  border-color: #334155;
}

/* Floating Toast */
.admin-toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 50;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid #334155;
  background: #1e293b;
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 18px 40px -12px rgba(0, 0, 0, 0.7);
  animation: toastRise 0.28s var(--ease-expo) both;
}

@keyframes toastRise {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-width: 960px) {
  .metrics-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .devices-grid,
  .form-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  .metrics-grid {
    grid-template-columns: 1fr;
  }
}

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