:root {
  --bg: #f4f2f7;
  --bg-accent: radial-gradient(ellipse 120% 80% at 50% -20%, rgba(139, 24, 116, 0.12), transparent 55%);
  --surface: #ffffff;
  --surface-2: #faf8fc;
  --border: #e8e4ef;
  --border-strong: #d9d2e3;
  --text: #1a1625;
  --text-muted: #6b6578;
  --text-soft: #948fa3;
  --brand: #8b1874;
  --brand-dark: #5c0f4d;
  --brand-glow: rgba(139, 24, 116, 0.35);
  --success: #0d7a4e;
  --success-bg: #e8f7ef;
  --warn: #b45309;
  --warn-bg: #fff7ed;
  --danger: #c53030;
  --danger-bg: #fff5f5;
  --info-bg: #f3e8ff;
  --shadow-sm: 0 1px 2px rgba(26, 22, 37, 0.04);
  --shadow-md: 0 4px 24px rgba(26, 22, 37, 0.06), 0 1px 3px rgba(26, 22, 37, 0.04);
  --shadow-lg: 0 12px 40px rgba(26, 22, 37, 0.08);
  --radius: 14px;
  --radius-sm: 10px;
  --font: "Inter", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  background-image: var(--bg-accent);
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 14px 18px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* —— Top bar —— */
.topbar {
  padding: 12px 0 10px;
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

h1 {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text);
  line-height: 1.2;
}

.title-short { display: none; }

.brand-tag {
  display: none;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar-user-sync {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  max-width: 100%;
}

.status-line--top {
  margin: 6px 0 0;
  padding: 0 2px;
}

.btn--xs {
  padding: 7px 10px;
  font-size: 0.75rem;
  gap: 4px;
}

.btn--xs .btn-icon {
  font-size: 1em;
}

.btn-txt-short { display: none; }

.user-chip {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  padding: 6px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.btn--sm {
  padding: 8px 14px;
  font-size: 0.8125rem;
}

.panel--admin {
  border-color: #e9d8fd;
  background: linear-gradient(180deg, #fffbff 0%, var(--surface) 100%);
}

.token-access-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.token-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font);
}

.token-toggle.on {
  border-color: #9ae6b4;
  background: var(--success-bg);
  color: var(--success);
}

.token-toggle.off {
  border-color: #feb2b2;
  background: var(--danger-bg);
  color: var(--danger);
}

.main { flex: 1; }

/* —— KPI cards —— */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

@media (max-width: 768px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: 6px; }
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  box-shadow: var(--shadow-sm);
}

.kpi-label {
  display: block;
  font-size: 0.625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-soft);
  margin-bottom: 4px;
}

.kpi-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 4px;
}

.kpi-label-row .kpi-label {
  margin-bottom: 0;
}

.kpi-value {
  display: block;
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.15;
  color: var(--text);
}

.kpi-new {
  font-size: 0.6875rem;
  font-weight: 700;
  color: var(--success);
  background: var(--success-bg);
  padding: 2px 7px;
  border-radius: 999px;
  line-height: 1.3;
  flex: 0 1 auto;
}

.kpi-value--sm {
  font-size: 0.875rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

/* —— Panels —— */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
}

.panel--compact:last-of-type {
  margin-bottom: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.panel-head--tight {
  margin-bottom: 8px;
}

.panel-head--split {
  align-items: flex-start;
}

.panel-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.panel-hint {
  margin: 4px 0 0;
  font-size: 0.75rem;
  color: var(--text-soft);
}

/* —— Badge —— */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--success-bg);
  color: var(--success);
}

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

.badge--fetch {
  background: var(--info-bg);
  color: var(--brand);
}

.badge--fetch::before {
  animation: pulse 1.2s ease-in-out infinite;
}

.badge--err {
  background: var(--danger-bg);
  color: var(--danger);
}

.badge--warn {
  background: var(--warn-bg);
  color: var(--warn);
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.85); }
}

/* —— Buttons —— */
.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.action-row--grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.15s, opacity 0.15s;
}

.btn:active:not(:disabled) { transform: scale(0.98); }

.btn--primary {
  color: #fff;
  background: linear-gradient(180deg, #9d2284 0%, var(--brand) 45%, var(--brand-dark) 100%);
  box-shadow: 0 2px 8px var(--brand-glow);
}

.btn--primary:hover:not(:disabled) {
  box-shadow: 0 4px 16px var(--brand-glow);
}

.btn--primary:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

.btn--secondary {
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
}

.btn--secondary:hover:not(:disabled) {
  border-color: var(--brand);
  color: var(--brand);
}

.btn--outline {
  font-size: 0.8125rem;
  padding: 8px 16px;
  color: var(--brand);
  background: var(--surface);
  border: 1px solid #e9d8fd;
}

.btn--outline:hover:not(:disabled) {
  background: var(--info-bg);
}

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

.btn-icon {
  font-size: 1.1em;
  line-height: 1;
}

.status-line {
  margin: 8px 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
  min-height: 0;
  line-height: 1.35;
}

.status-line:empty {
  display: none;
}

.status-line.err { color: var(--danger); font-weight: 500; }

/* —— Search —— */
.search-row {
  display: flex;
  gap: 10px;
}

.search-row .btn {
  flex-shrink: 0;
  min-width: 88px;
  padding-left: 14px;
  padding-right: 14px;
}

.search-input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-input::placeholder {
  color: var(--text-soft);
  font-weight: 500;
  opacity: 0.8;
}

.search-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(139, 24, 116, 0.12);
  background: var(--surface);
}

.search-meta {
  margin: 8px 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 500;
}

.empty {
  margin: 16px 0 0;
  padding: 24px;
  text-align: center;
  font-size: 0.875rem;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px dashed var(--border-strong);
}

/* —— Table —— */
.table-shell {
  margin-top: 8px;
  overflow: auto;
  max-height: min(360px, 45vh);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.table-shell table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8125rem;
}

.table-shell th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 10px 12px;
  text-align: left;
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--brand-dark);
  background: linear-gradient(180deg, #faf5ff 0%, #f3ebfa 100%);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.table-shell td {
  padding: 10px 12px;
  border-bottom: 1px solid #f0ecf4;
  color: var(--text);
  vertical-align: top;
}

.table-shell tbody tr:hover td {
  background: #fdfcfe;
}

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

.table-shell td:nth-child(2) {
  font-family: var(--mono);
  font-weight: 600;
  font-size: 0.75rem;
}

/* —— Mobile —— */
.btn-label-short { display: none; }

@supports (padding: env(safe-area-inset-top)) {
  .app {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
  .topbar {
    padding-top: max(10px, env(safe-area-inset-top));
  }
}

@media (max-width: 640px) {
  body { font-size: 13px; }
  .topbar { padding-bottom: 8px; }
  .title-long { display: none; }
  .title-short { display: inline; }
  h1 { font-size: 1rem; }
  .topbar-inner {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .brand { flex: none; }
  .topbar-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: nowrap;
    gap: 5px;
  }
  .topbar-user-sync {
    flex: 1;
    min-width: 0;
    flex-wrap: nowrap;
  }
  .user-chip {
    max-width: 38vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 6px 10px;
    font-size: 0.6875rem;
  }
  #logoutBtn {
    padding: 6px 10px;
    font-size: 0.6875rem;
    white-space: nowrap;
  }
  .badge {
    font-size: 0.5625rem;
    padding: 4px 7px;
    flex-shrink: 0;
  }
  .btn--sm { padding: 6px 10px; font-size: 0.6875rem; }
  .panel-head--split { flex-wrap: wrap; }
  .panel-hint--desktop { display: none; }
  #fetchBtn .btn-txt { display: none; }
  .btn--xs { padding: 7px 10px; min-width: 38px; flex-shrink: 0; }
  .kpi-new {
    font-size: 0.5625rem;
    padding: 2px 5px;
    max-width: 52%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .kpi-value { font-size: 1.1rem; }
  .kpi-value--sm { font-size: 0.8125rem; }
  .table-shell {
    max-height: none;
    -webkit-overflow-scrolling: touch;
    margin-left: -2px;
    margin-right: -2px;
  }
  .table-shell th,
  .table-shell td {
    padding: 8px 8px;
    font-size: 0.75rem;
  }
  .search-input,
  .field input {
    font-size: 16px;
  }
  .empty { padding: 16px 12px; margin-top: 8px; }
}

.mobile-url-hint {
  margin-top: 16px;
  padding: 12px 14px;
  font-size: 0.75rem;
  line-height: 1.45;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.mobile-url-hint strong {
  color: var(--brand);
  word-break: break-all;
}
