:root {
  --bg: #0b1117;
  --surface: #121a23;
  --surface-2: #18222e;
  --border: #253344;
  --text: #e6edf3;
  --muted: #8b9bb0;
  --primary: #10b981;
  --primary-hover: #059669;
  --primary-soft: rgba(16, 185, 129, 0.14);
  --danger: #ef4444;
  --warning: #f59e0b;
  --info: #38bdf8;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overflow-x: clip;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

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

.mono {
  font-family: var(--mono);
  font-size: 12px;
}

code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 6px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

/* Buttons & Inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn:hover:not(:disabled) {
  border-color: #3a4f66;
  background: #1e2a38;
}

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

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

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

.btn.ghost {
  background: transparent;
}

.btn.danger-outline {
  background: transparent;
  border-color: rgba(239, 68, 68, 0.4);
  color: #fca5a5;
}

.btn.danger-outline:hover {
  background: rgba(239, 68, 68, 0.12);
}

.btn.small {
  padding: 5px 10px;
  font-size: 12px;
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  font-size: 15px;
}

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

input[type="text"],
input[type="password"],
input[type="search"],
select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 13px;
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--primary);
}

/* Alerts */
.alert {
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 13px;
  margin-bottom: 16px;
}

.alert.warning {
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.35);
  color: #fcd34d;
}

.alert.error {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.35);
  color: #fca5a5;
}

/* Login Gate */
.login-gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}

.login-brand h1 {
  margin: 0;
  font-size: 20px;
}

.login-brand p {
  margin: 2px 0 0;
  font-size: 13px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 11px;
  background: linear-gradient(135deg, #10b981, #059669);
  color: #032318;
  font-weight: 800;
  font-size: 20px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.brand-mark.small {
  width: 34px;
  height: 34px;
  font-size: 16px;
  border-radius: 9px;
}

.config-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.auth-block {
  margin-bottom: 16px;
}

.auth-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 8px;
}

.google-btn-slot {
  display: flex;
  justify-content: center;
  min-height: 44px;
}

.auth-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 0;
  color: var(--muted);
  font-size: 12px;
}

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

.passcode-row {
  display: flex;
  gap: 8px;
}

.passcode-row input {
  flex: 1;
}

/* Dashboard Layout */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: rgba(11, 17, 23, 0.9);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

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

.topbar-title {
  margin: 0;
  font-size: 16px;
}

.topbar-sub {
  margin: 0;
  font-size: 12px;
}

.admin-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
}

.admin-badge img {
  width: 20px;
  height: 20px;
  border-radius: 50%;
}

.container {
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px 24px 48px;
}

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

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

.kpi-value {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.kpi-sub {
  font-size: 12px;
}

/* Tabs */
.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px;
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

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

.tab.active {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

.pill {
  background: var(--surface-2);
  color: var(--text);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
}

/* Panel & Toolbar */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.toolbar {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.search-input {
  flex: 1;
  min-width: 220px;
}

.uid-input {
  width: 210px;
}

.filter-group,
.breakdown-chips {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.breakdown-chips {
  padding: 12px 16px 0;
}

.chip {
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

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

.chip.active {
  background: var(--primary-soft);
  border-color: rgba(16, 185, 129, 0.45);
  color: #6ee7b7;
}

/* Table */
.table-wrap {
  overflow-x: auto;
}

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

.data-table th {
  text-align: left;
  padding: 11px 16px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  background: rgba(24, 34, 46, 0.5);
  white-space: nowrap;
}

.data-table td {
  padding: 12px 16px;
  border-bottom: 1px solid rgba(37, 51, 68, 0.6);
  vertical-align: middle;
}

.data-table tbody tr.clickable-row {
  cursor: pointer;
}

.data-table tbody tr:hover {
  background: rgba(24, 34, 46, 0.65);
}

.empty-cell {
  text-align: center;
  color: var(--muted);
  padding: 36px 16px !important;
}

.user-cell {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 210px;
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 13px;
  color: #6ee7b7;
  flex-shrink: 0;
}

.user-cell-text {
  min-width: 0;
}

.user-name {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.user-email {
  font-size: 12px;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  white-space: nowrap;
}

.badge.success {
  background: var(--primary-soft);
  border-color: rgba(16, 185, 129, 0.35);
  color: #6ee7b7;
}

.badge.pending {
  background: rgba(245, 158, 11, 0.12);
  border-color: rgba(245, 158, 11, 0.35);
  color: #fcd34d;
}

.badge.event {
  background: rgba(56, 189, 248, 0.12);
  border-color: rgba(56, 189, 248, 0.3);
  color: #7dd3fc;
  font-family: var(--mono);
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: #7dd3fc;
  cursor: pointer;
  font: inherit;
  text-align: left;
}

.link-btn:hover {
  text-decoration: underline;
}

.params-preview {
  max-width: 420px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  word-break: break-word;
}

.panel-footer {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
}

/* Detail Dialog */
.detail-dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  width: min(780px, calc(100vw - 24px));
  max-height: 90vh;
  padding: 0;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.65);
}

.detail-dialog::backdrop {
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
}

.detail-shell {
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.detail-header h2 {
  margin: 0;
  font-size: 17px;
}

.detail-body {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.detail-hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.detail-hero-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.avatar.large {
  width: 56px;
  height: 56px;
  font-size: 20px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 12px;
}

.info-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
}

.info-box label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 4px;
}

.info-box div {
  font-size: 13px;
  word-break: break-word;
}

.detail-section h3 {
  margin: 0 0 10px;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.event-timeline {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 280px;
  overflow-y: auto;
}

.event-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 12px;
}

.raw-json {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  font-family: var(--mono);
  font-size: 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 8px 0 0;
}

@media (max-width: 768px) {
  .container {
    padding: 12px 12px calc(32px + env(safe-area-inset-bottom));
  }

  .topbar {
    padding: 10px 12px;
    gap: 10px;
  }

  .topbar-right {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
  }

  .admin-badge {
    max-width: 185px;
    order: -1;
  }

  .admin-badge span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 14px;
  }

  .kpi-card {
    padding: 12px 13px;
  }

  .kpi-value {
    font-size: 22px;
  }

  .tabs {
    gap: 8px;
  }

  .tab {
    flex: 1;
    justify-content: center;
    min-height: 42px;
    padding: 8px 12px;
  }

  .toolbar {
    padding: 12px;
    gap: 8px;
  }

  input[type="text"],
  input[type="password"],
  input[type="search"],
  select {
    font-size: 16px;
    min-height: 42px;
  }

  .search-input,
  .select-input,
  .uid-input {
    width: 100%;
    min-width: 0;
  }

  #applyEventServerFilterBtn,
  #clearEventFilterBtn {
    flex: 1;
    min-height: 42px;
  }

  .filter-group {
    width: 100%;
  }

  .filter-group .chip {
    flex: 1;
    text-align: center;
    padding: 8px 10px;
  }

  .breakdown-chips {
    padding: 10px 12px 4px;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .breakdown-chips .chip {
    flex-shrink: 0;
  }

  /* Convert wide tables into mobile cards */
  .table-wrap {
    overflow-x: visible;
  }

  .data-table,
  .data-table tbody,
  .data-table tr {
    display: block;
    width: 100%;
  }

  .data-table thead {
    display: none;
  }

  .data-table tbody {
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .data-table tbody tr {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
  }

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

  .data-table td {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 7px 0;
    border-bottom: 1px solid rgba(37, 51, 68, 0.45);
    text-align: right;
    font-size: 13px;
  }

  .data-table td::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--muted);
    text-align: left;
    flex-shrink: 0;
    padding-top: 2px;
  }

  .data-table td.cell-user {
    display: block;
    text-align: left;
    padding-top: 0;
    padding-bottom: 10px;
    margin-bottom: 4px;
    border-bottom: 1px solid var(--border);
  }

  .data-table td.cell-user::before,
  .data-table td.cell-action::before,
  .data-table td.empty-cell::before {
    display: none;
  }

  .data-table td.cell-action {
    border-bottom: none;
    padding-top: 10px;
    padding-bottom: 0;
  }

  .data-table td.cell-action .btn {
    width: 100%;
    min-height: 40px;
  }

  .data-table td.empty-cell {
    display: block;
    border-bottom: none;
    text-align: center;
  }

  .user-cell {
    min-width: 0;
  }

  .params-preview {
    max-width: 100%;
    text-align: right;
  }

  .panel-footer {
    flex-direction: column;
    gap: 10px;
    align-items: stretch;
    text-align: center;
  }

  /* Bottom-sheet detail dialog on mobile */
  .detail-dialog {
    width: 100vw;
    max-width: 100vw;
    max-height: 92dvh;
    margin: auto 0 0 0;
    border-radius: 16px 16px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
  }

  .detail-shell {
    max-height: 92dvh;
  }

  .detail-header {
    padding: 14px 16px;
  }

  .detail-body {
    padding: 14px 16px calc(22px + env(safe-area-inset-bottom));
    gap: 14px;
  }

  .detail-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .event-item {
    flex-direction: column;
    gap: 6px;
  }

  .event-item > div:last-child {
    text-align: left !important;
  }

  .login-card {
    padding: 22px 18px;
  }

  .toast {
    left: 12px;
    right: 12px;
    bottom: calc(14px + env(safe-area-inset-bottom));
    text-align: center;
  }
}
