/* ── Tokens ─────────────────────────────────────────── */
:root {
  --bg: #0f1219;
  --bg-elevated: #161b26;
  --bg-card: #1a2030;
  --bg-hover: #222a3d;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text: #eef1f7;
  --text-muted: #8b95a8;
  --text-dim: #5c667a;
  --primary: #5b8cff;
  --primary-hover: #7aa3ff;
  --primary-soft: rgba(91, 140, 255, 0.15);
  --danger: #f07178;
  --danger-soft: rgba(240, 113, 120, 0.12);
  --success: #7fd99a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  --font: "DM Sans", system-ui, -apple-system, sans-serif;
  --topbar-h: 72px;
}

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

html,
body {
  height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  /* Avoid iOS tap highlight flash */
  -webkit-tap-highlight-color: transparent;
}

/* Soft radial glow */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(91, 140, 255, 0.18), transparent),
    radial-gradient(ellipse 40% 30% at 100% 100%, rgba(120, 80, 200, 0.08), transparent);
  pointer-events: none;
  z-index: 0;
}

.screen {
  position: relative;
  z-index: 1;
  min-height: 100%;
}

[hidden] {
  display: none !important;
}

code {
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 0.9em;
  background: var(--bg-hover);
  padding: 0.1em 0.4em;
  border-radius: 4px;
}

/* ── Login ──────────────────────────────────────────── */
#login-screen {
  display: grid;
  place-items: center;
  padding: 24px;
  min-height: 100vh;
}

.login-card {
  width: 100%;
  max-width: 28rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 32px;
  box-shadow: var(--shadow);
  overflow: visible;
}

.clerk-signin-host {
  width: 100%;
  min-height: 48px;
  overflow: visible;
}

.clerk-signin-host .cl-rootBox,
.clerk-signin-host .cl-cardBox,
.clerk-signin-host .cl-card,
.clerk-signin-host .cl-main,
.clerk-signin-host .cl-socialButtons {
  width: 100% !important;
  overflow: visible !important;
}

.clerk-signin-host .cl-header,
.clerk-signin-host .cl-headerTitle,
.clerk-signin-host .cl-headerSubtitle,
.clerk-signin-host .cl-logoBox,
.clerk-signin-host .cl-footerAction {
  display: none !important;
}

.login-brand {
  text-align: center;
  margin-bottom: 28px;
}

.login-brand .brand-mark {
  margin-bottom: 4px;
}

.login-brand h1 {
  font-size: 1.65rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-top: 14px;
}

.login-brand p {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-top: 6px;
}

/* Logo canvas color from icon.jpeg edge (~#EBECEE) so tile matches the image */
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 96px;
  height: 96px;
  border-radius: 20px;
  background: #ebecee;
  overflow: hidden;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.brand-mark img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  display: block;
  padding: 0;
}

/* Top bar logo */
.brand-mark.sm {
  width: 44px;
  height: 44px;
  border-radius: 11px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.brand-mark.sm img {
  padding: 0;
}

.login-hint {
  margin-top: 20px;
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.btn-google {
  background: #ffffff;
  color: #3c4043;
  border: 1px solid #dadce0;
  font-weight: 600;
  gap: 12px;
  padding: 12px 16px;
  margin-top: 8px;
}

.btn-google:hover:not(:disabled) {
  background: #f8f9fa;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.google-icon {
  flex-shrink: 0;
}

#login-error {
  margin-top: 16px;
  text-align: center;
}

.cell-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
  word-break: break-all;
}

.status-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 3px 8px;
  border-radius: 999px;
}

.status-badge.active {
  background: rgba(127, 217, 154, 0.12);
  color: var(--success);
}

.status-badge.pending {
  background: rgba(245, 158, 11, 0.12);
  color: #fbbf24;
}

.field input[readonly] {
  opacity: 0.75;
  cursor: default;
}

/* ── Loading overlay ────────────────────────────────── */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  place-items: center;
  background: rgba(15, 18, 25, 0.55);
  backdrop-filter: blur(2px);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* ── Fields & buttons ───────────────────────────────── */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}

.field > span {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
}

.field input,
.field select {
  appearance: none;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus,
.field select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.field select {
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='none'%3E%3Cpath d='M1 1.5 6 6.5 11 1.5' stroke='%238b95a8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
}

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

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
}

.btn-ghost:hover:not(:disabled) {
  color: var(--text);
  background: var(--bg-hover);
}

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(240, 113, 120, 0.22);
}

.btn-sm {
  padding: 6px 10px;
  font-size: 0.8rem;
}

.btn-block {
  width: 100%;
  margin-top: 4px;
}

.error-msg {
  color: var(--danger);
  font-size: 0.85rem;
  margin-bottom: 12px;
}

/* ── Top bar ────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: var(--topbar-h);
  justify-content: center;
  padding: 12px 24px;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  background: rgba(15, 18, 25, 0.9);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.brand-name {
  font-weight: 700;
  letter-spacing: -0.02em;
}

.topbar-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  max-width: min(420px, 100%);
  min-width: 0;
  overflow: hidden;
}

.user-chip strong {
  color: var(--text);
  font-weight: 600;
}

.user-chip-full {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.user-chip-short {
  display: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.user-chip-role {
  flex-shrink: 0;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--primary-hover);
  background: var(--primary-soft);
  border-radius: 999px;
  padding: 2px 8px;
}

.btn-logout {
  flex-shrink: 0;
  white-space: nowrap;
}

/* ── Tabs ───────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  width: fit-content;
  max-width: 100%;
  align-self: center;
}

.tab {
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-radius: 7px;
  padding: 8px 18px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  /* Comfortable touch target */
  min-height: 40px;
  flex: 1 1 auto;
}

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

.tab.active {
  background: var(--primary-soft);
  color: var(--primary-hover);
}

/* ── Main ───────────────────────────────────────────── */
.main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 24px 64px;
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.panel-header--sub {
  margin-top: 40px;
  margin-bottom: 20px;
}

.panel-header h2 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.admin-block + .admin-block {
  margin-top: 40px;
}

.dashboard-section {
  margin-top: 8px;
}

.muted {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-top: 4px;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 48px 24px;
  background: var(--bg-card);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
}

/* ── App grid ───────────────────────────────────────── */
.app-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.app-card {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
}

.app-card:hover {
  border-color: var(--border-strong);
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}

.app-card-icon {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  font-size: 1.25rem;
  font-weight: 700;
  color: #fff;
  margin-bottom: 16px;
  flex-shrink: 0;
  overflow: hidden;
}

/* Image logos: full logo visible (no crop), padded on a light tile */
.app-card-icon--img {
  width: 72px;
  height: 72px;
  background: #ffffff;
  border: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  padding: 6px;
}

.app-card-icon--img img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  display: block;
  /* Avoid blurry downscale on high-DPI */
  image-rendering: auto;
}

.app-card h3 {
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: -0.01em;
  margin-bottom: 6px;
}

.app-card .app-desc {
  color: var(--text-muted);
  font-size: 0.88rem;
  flex: 1;
  margin-bottom: 16px;
}

.app-card .app-link {
  font-size: 0.75rem;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 14px;
}

.app-card--shortcut .app-card-icon {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
}

.access-tag.link {
  background: rgba(139, 92, 246, 0.18);
  color: #c4b5fd;
}

.shortcuts-url-cell {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* ── Admin table ────────────────────────────────────── */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
}

.users-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.users-table th,
.users-table td {
  text-align: left;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.users-table th {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: rgba(0, 0, 0, 0.2);
}

.users-table tr:last-child td {
  border-bottom: none;
}

.users-table tbody tr:hover td {
  background: var(--bg-hover);
}

.role-badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 3px 8px;
  border-radius: 999px;
}

.role-badge.admin {
  background: var(--primary-soft);
  color: var(--primary-hover);
}

.role-badge.staff,
.role-badge.user {
  background: rgba(127, 217, 154, 0.12);
  color: var(--success);
}

.role-badge.guest {
  background: rgba(245, 158, 11, 0.14);
  color: #fbbf24;
}

.field-hint {
  font-size: 0.85rem;
  margin: -8px 0 14px;
  line-height: 1.4;
}

.access-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.access-tag {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
}

.access-tag.none {
  color: var(--text-dim);
  border-style: dashed;
}

.row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* ── Modal ──────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.modal-header h3 {
  font-size: 1.15rem;
  font-weight: 700;
}

.icon-btn {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.access-fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px 14px;
  margin-bottom: 16px;
}

.access-fieldset legend {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
  padding: 0 6px;
}

.checkbox-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 6px;
}

.checkbox-row:hover {
  background: var(--bg-hover);
}

.checkbox-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
  cursor: pointer;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

/* ── Desktop: single-row top bar ────────────────────── */
@media (min-width: 961px) {
  .topbar {
    min-height: 72px;
    padding: 10px 24px;
    padding-top: max(10px, env(safe-area-inset-top));
  }
}

/* ── Responsive: tablet ─────────────────────────────── */
@media (max-width: 960px) {
  .topbar {
    padding: 12px 18px;
    padding-top: max(12px, env(safe-area-inset-top));
  }

  .tabs {
    width: 100%;
    align-self: stretch;
  }

  .tab {
    text-align: center;
  }

  .app-grid {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  }

  .main {
    padding: 24px 18px 56px;
  }

  /* Prefer short name chip earlier on tablet portrait */
  .user-chip-full {
    display: none;
  }

  .user-chip-short {
    display: inline;
  }
}

/* ── Responsive: phone ──────────────────────────────── */
@media (max-width: 640px) {
  :root {
    --topbar-h: auto;
  }

  #login-screen {
    padding: 16px;
    padding-top: max(16px, env(safe-area-inset-top));
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    align-items: flex-start;
    padding-top: max(48px, calc(env(safe-area-inset-top) + 32px));
  }

  .login-card {
    padding: 28px 20px;
    border-radius: 14px;
  }

  .brand-mark {
    width: 88px;
    height: 88px;
  }

  .topbar {
    gap: 10px;
    padding: 10px 12px 12px;
    padding-top: max(10px, env(safe-area-inset-top));
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  .topbar-row {
    gap: 8px;
  }

  .brand-mark.sm {
    width: 36px;
    height: 36px;
  }

  .brand-name {
    font-size: 0.95rem;
    letter-spacing: -0.03em;
  }

  .topbar-right {
    gap: 6px;
  }

  .user-chip {
    padding: 5px 8px 5px 10px;
    font-size: 0.8rem;
    max-width: none;
    flex: 1;
    min-width: 0;
  }

  .user-chip-full {
    display: none;
  }

  .user-chip-short {
    display: inline;
  }

  .btn-logout {
    padding: 8px 10px;
    font-size: 0.8rem;
  }

  .tabs {
    width: 100%;
    align-self: stretch;
  }

  .tab {
    padding: 10px 12px;
    min-height: 44px;
    font-size: 0.9rem;
  }

  .main {
    padding: 16px 14px max(40px, env(safe-area-inset-bottom));
  }

  .panel-header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-bottom: 18px;
  }

  .panel-header h2 {
    font-size: 1.3rem;
  }

  .panel-header .btn {
    width: 100%;
    min-height: 44px;
  }

  .app-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .app-card {
    padding: 16px;
  }

  .app-card-icon--img {
    width: 64px;
    height: 64px;
  }

  .app-card .btn {
    width: 100%;
    min-height: 46px;
    font-size: 0.95rem;
  }

  .app-card .app-link {
    font-size: 0.7rem;
  }

  /* Admin table → stacked cards on phone */
  .table-wrap {
    border: none;
    background: transparent;
    overflow: visible;
  }

  .users-table {
    display: block;
  }

  .users-table thead {
    display: none;
  }

  .users-table tbody {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .users-table tr {
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
  }

  .users-table tbody tr:hover td {
    background: transparent;
  }

  .users-table th,
  .users-table td {
    display: block;
    padding: 0;
    border: none;
  }

  .users-table td::before {
    content: attr(data-label);
    display: block;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-dim);
    margin-bottom: 4px;
  }

  .users-table td:first-child::before {
    display: none;
  }

  .row-actions {
    width: 100%;
  }

  .row-actions .btn {
    flex: 1;
    min-height: 40px;
  }

  .modal {
    padding: 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    align-items: flex-end;
  }

  .modal-card {
    max-width: 100%;
    max-height: min(92vh, 100%);
    border-radius: 16px 16px 12px 12px;
    padding: 18px 16px max(18px, env(safe-area-inset-bottom));
  }

  .modal-actions {
    flex-direction: column-reverse;
  }

  .modal-actions .btn {
    width: 100%;
    min-height: 44px;
  }

  .checkbox-row {
    min-height: 44px;
    padding: 10px 8px;
  }

  .checkbox-row input {
    width: 20px;
    height: 20px;
  }

  .empty-state {
    padding: 32px 16px;
    font-size: 0.95rem;
  }

  .btn-google {
    min-height: 48px;
  }
}

/* ── Landscape phones / small height ────────────────── */
@media (max-height: 500px) and (orientation: landscape) {
  #login-screen {
    align-items: center;
    padding-top: 12px;
  }

  .login-brand .brand-mark {
    width: 56px;
    height: 56px;
  }

  .login-brand h1 {
    margin-top: 8px;
    font-size: 1.25rem;
  }

  .topbar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }

  .topbar-row {
    flex: 1 1 auto;
  }

  .tabs {
    width: auto;
    flex: 0 0 auto;
  }
}
