/* ===== Base ===== */
:root {
  --bg: #0a0a0c;
  --bg-2: #15151a;
  --bg-3: #1d1d24;
  --border: rgba(255,255,255,0.08);
  --border-2: rgba(255,255,255,0.14);
  --text: #f5f5f7;
  --text-muted: #98989d;
  --text-dim: #6e6e76;
  --primary: #0a84ff;
  --primary-h: #409cff;
  --success: #30d158;
  --warning: #ffd60a;
  --danger: #ff453a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 24px rgba(0,0,0,0.4);
  --ease: cubic-bezier(.4,0,.2,1);
}

* { box-sizing: border-box; }

/* HTML `hidden` attribute must override display:flex/grid set on .screen / .modal-backdrop */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', sans-serif;
  background:
    radial-gradient(ellipse at top left, rgba(10,132,255,0.12), transparent 50%),
    radial-gradient(ellipse at bottom right, rgba(48,209,88,0.08), transparent 50%),
    var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.5;
}

/* ===== Login / Setup screens ===== */
.screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
}

.brand {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 4px;
  letter-spacing: -0.5px;
}
.brand-sub {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  margin-top: 2px;
}

.hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 18px;
  padding: 8px 10px;
  background: rgba(255,214,10,0.08);
  border: 1px solid rgba(255,214,10,0.2);
  border-radius: var(--radius-sm);
}

form { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
label { font-size: 12px; font-weight: 500; color: var(--text-muted); }
input, select, textarea {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  transition: border-color 0.15s, background 0.15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  background: rgba(10,132,255,0.05);
}

/* ===== Buttons ===== */
.btn {
  padding: 9px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s var(--ease);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn.primary {
  background: var(--primary);
  color: white;
  margin-top: 6px;
}
.btn.primary:hover { background: var(--primary-h); }
.btn.primary:active { transform: scale(0.98); }
.btn.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border-2);
}
.btn.ghost:hover { background: var(--bg-3); }
.btn.danger {
  background: rgba(255,69,58,0.12);
  color: var(--danger);
  border: 1px solid rgba(255,69,58,0.3);
}
.btn.danger:hover { background: rgba(255,69,58,0.2); }
.btn.small { padding: 4px 10px; font-size: 11px; }

.err {
  color: var(--danger);
  font-size: 12px;
  margin-top: 4px;
  min-height: 16px;
}

/* ===== App ===== */
.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}
.logo { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; }
.muted { color: var(--text-muted); font-weight: 500; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-tag {
  background: rgba(48,209,88,0.12);
  color: var(--success);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid rgba(48,209,88,0.3);
}

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  background: var(--bg-2);
  padding: 4px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  width: fit-content;
}
.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 16px;
  border-radius: 6px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { background: var(--bg-3); color: var(--text); }

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ===== Stats grid ===== */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.stat-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  padding: 16px;
  border-radius: var(--radius);
}
.stat-card .label { font-size: 12px; color: var(--text-muted); }
.stat-card .value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.stat-card.success .value { color: var(--success); }
.stat-card.danger .value { color: var(--danger); }
.stat-card.warning .value { color: var(--warning); }
.stat-card.primary .value { color: var(--primary-h); }

/* ===== Generate form ===== */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin-top: 14px;
}
.form-grid .full { grid-column: 1 / -1; }
.form-grid button { grid-column: 1 / -1; max-width: 220px; }

.gen-result {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.gen-result h3 { margin: 0 0 12px; font-size: 14px; }
.code-list {
  background: var(--bg-3);
  padding: 12px;
  border-radius: var(--radius-sm);
  font-family: 'SF Mono', Menlo, monospace;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 300px;
  overflow-y: auto;
  margin-bottom: 12px;
}
.code-list .code-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 8px;
  border-radius: 4px;
}
.code-list .code-line:hover { background: rgba(255,255,255,0.04); }
.code-list .copy-btn {
  background: transparent;
  border: none;
  color: var(--primary-h);
  cursor: pointer;
  font-size: 11px;
  font-family: inherit;
  padding: 2px 8px;
  border-radius: 4px;
}
.code-list .copy-btn:hover { background: rgba(10,132,255,0.15); }

/* ===== Codes list / Table ===== */
.filters {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.filters input, .filters select { padding: 8px 12px; }
.filters input { flex: 1; min-width: 220px; }

.table-wrap {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}
.codes-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.codes-table th {
  text-align: left;
  padding: 12px;
  background: var(--bg-3);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.codes-table td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.codes-table tr:last-child td { border-bottom: none; }
.codes-table tr:hover { background: rgba(255,255,255,0.02); }
.codes-table code {
  font-family: 'SF Mono', Menlo, monospace;
  font-size: 12px;
  background: var(--bg-3);
  padding: 3px 8px;
  border-radius: 4px;
  cursor: pointer;
}
.codes-table code:hover { background: rgba(10,132,255,0.15); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge.active { background: rgba(48,209,88,0.15); color: var(--success); }
.badge.revoked { background: rgba(255,69,58,0.15); color: var(--danger); }
.badge.expired { background: rgba(110,110,118,0.2); color: var(--text-muted); }
.badge.type {
  background: rgba(10,132,255,0.12);
  color: var(--primary-h);
}
.badge.type.lifetime { background: rgba(255,214,10,0.12); color: var(--warning); }

.row-actions { display: flex; gap: 4px; justify-content: flex-end; }

.empty {
  padding: 60px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

/* ===== Modal ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(8px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 600px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-header code {
  font-family: 'SF Mono', Menlo, monospace;
  background: var(--bg-3);
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: normal;
}
.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
}
.device-row {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}
.device-row:last-child { border-bottom: none; }
.device-info { flex: 1; min-width: 0; }
.device-name { font-weight: 600; }
.device-id { font-size: 11px; color: var(--text-dim); font-family: 'SF Mono', Menlo, monospace; margin-top: 2px; word-break: break-all; }
.device-meta { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--bg-3);
  border: 1px solid var(--border-2);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 2000;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast.success { border-color: rgba(48,209,88,0.4); }
.toast.error { border-color: rgba(255,69,58,0.4); }

/* ===== Mobile ===== */
@media (max-width: 640px) {
  .app { padding: 12px; }
  .topbar { flex-direction: column; gap: 12px; align-items: flex-start; }
  .tabs { width: 100%; overflow-x: auto; }
  .form-grid { grid-template-columns: 1fr; }
  .codes-table th:nth-child(6),
  .codes-table td:nth-child(6),
  .codes-table th:nth-child(7),
  .codes-table td:nth-child(7) { display: none; }
}
