:root {
  --bg: #f4f6f9;
  --surface: #fff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --ok: #16a34a;
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

a {
  color: var(--primary);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.loading {
  padding: 2rem;
  color: var(--muted);
}

/* Auth pages */
.auth-card {
  max-width: 400px;
  margin: 4rem auto;
  padding: 2rem;
  background: var(--surface);
  border-radius: 8px;
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgb(0 0 0 / 6%);
}

.auth-card h1 {
  margin: 0 0 1.5rem;
  font-size: 1.5rem;
}

.auth-footer {
  margin-top: 1.5rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.auth-hint {
  margin: 0 0 1.25rem;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.auth-hint-small {
  margin-top: 0.75rem;
  font-size: 0.85rem;
}

.auth-divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.25rem 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.auth-divider::before,
.auth-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.auth-oauth {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.btn-oauth {
  width: 100%;
  padding: 0.65rem 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
}

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

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

.btn-oauth-secondary {
  background: #f8fafc;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

/* Shell */
.shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0 1.25rem;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
}

.nav {
  display: flex;
  gap: 1rem;
  flex: 1;
}

.nav a {
  color: var(--muted);
  font-size: 0.9rem;
}

.nav a:hover,
.nav a.active {
  color: var(--primary);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.uid {
  font-size: 0.75rem;
  color: var(--muted);
  font-family: ui-monospace, monospace;
}

.page {
  flex: 1;
  padding: 1.5rem 2rem;
  max-width: 960px;
}

/* Admin */
.admin-shell {
  flex-direction: row;
}

.admin-sidebar {
  width: 220px;
  background: #1e293b;
  color: #e2e8f0;
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-height: 100vh;
}

.admin-sidebar .brand {
  color: #fff;
  margin-bottom: 1rem;
}

.admin-nav {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex: 1;
}

.admin-nav a {
  color: #94a3b8;
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
}

.admin-nav a:hover {
  background: #334155;
  color: #fff;
  text-decoration: none;
}

.back-link {
  color: #94a3b8;
  font-size: 0.85rem;
  margin-top: auto;
}

.admin-page {
  max-width: none;
  flex: 1;
}

/* Forms */
.form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-narrow {
  max-width: 420px;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  font-weight: 500;
}

.form input,
.form select,
.table-input {
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
}

.form-inline {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.form-inline input {
  flex: 1;
  min-width: 160px;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.form-inline-block {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
}

.form-inline-block input,
.form-inline-block select {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.form-error {
  color: var(--danger);
  margin: 0;
  font-size: 0.9rem;
}

.form-ok {
  color: var(--ok);
  margin: 0;
  font-size: 0.9rem;
}

.hint {
  color: var(--muted);
  font-size: 0.9rem;
}

/* Buttons */
.btn-primary {
  padding: 0.55rem 1rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
}

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

.btn-primary:disabled {
  opacity: 0.6;
}

.btn-ghost {
  padding: 0.4rem 0.75rem;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

.btn-link {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  margin-top: 0.75rem;
}

.btn-danger,
.btn-danger-sm {
  background: var(--danger);
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  padding: 0.35rem 0.65rem;
  font-size: 0.85rem;
}

.admin-sidebar .btn-ghost {
  color: #e2e8f0;
  border-color: #475569;
}

/* Cards */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
}

.card h3 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
}

.card dl {
  margin: 0;
  font-size: 0.9rem;
}

.card dt {
  color: var(--muted);
  margin-top: 0.35rem;
}

.card dd {
  margin: 0;
}

.tag-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.tag {
  background: #e0e7ff;
  color: #3730a3;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

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

.section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.section h2 {
  margin: 0 0 0.75rem;
  font-size: 1.1rem;
}

.backup-codes {
  background: #f8fafc;
  padding: 0.75rem;
  border-radius: 4px;
  font-size: 0.85rem;
  margin-top: 0.5rem;
}

/* Table */
.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.table th,
.table td {
  padding: 0.65rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.table th {
  background: #f8fafc;
  font-weight: 600;
}

.table-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.role-assign-list {
  list-style: none;
  padding: 0;
}

.role-assign-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

/* TOTP QR */
.totp-qr-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  align-items: flex-start;
  margin: 1rem 0;
  padding: 1rem;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.totp-qr-panel img {
  display: block;
  border-radius: 4px;
  background: #fff;
}

.totp-qr-meta {
  flex: 1;
  min-width: 200px;
}

.totp-manual summary {
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--muted);
}

.otpauth-url {
  display: block;
  margin-top: 0.5rem;
  font-size: 0.7rem;
  word-break: break-all;
  color: var(--muted);
}

/* Admin permissions */
.admin-links {
  list-style: none;
  padding: 0;
  margin: 0;
}

.admin-links li {
  padding: 0.35rem 0;
}

.admin-toolbar {
  margin: 1rem 0;
}

.admin-toolbar select {
  min-width: 240px;
  margin-left: 0.5rem;
}

.perm-grid {
  display: grid;
  gap: 1rem;
  margin-top: 1rem;
}

.perm-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
}

.perm-group h3 {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
}

.perm-check-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.perm-check-list li {
  padding: 0.35rem 0;
}

.perm-check-list label {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  cursor: pointer;
}

.perm-label {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.perm-api {
  font-size: 0.8rem;
  color: var(--muted);
  font-family: ui-monospace, monospace;
}

@media (max-width: 768px) {
  .admin-shell {
    flex-direction: column;
  }
  .admin-sidebar {
    width: 100%;
    min-height: auto;
    flex-direction: row;
    flex-wrap: wrap;
  }
  .admin-nav {
    flex-direction: row;
    flex-wrap: wrap;
  }
}
