:root {
  --bg: #0a0612;
  --bg2: #120a1f;
  --card: rgba(22, 12, 38, 0.72);
  --border: rgba(255, 107, 214, 0.22);
  --text: #f3ecff;
  --muted: #a894c9;
  --pink: #ff6bd6;
  --purple: #9d6bff;
  --gold: #ffd166;
  --cyan: #6bf0ff;
  --danger: #ff6b8a;
  --glow: 0 0 40px rgba(255, 107, 214, 0.15);
  --radius: 18px;
  --font: "Outfit", system-ui, sans-serif;
  --display: "Zen Tokyo Zoo", "Outfit", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

.bg-mesh {
  position: fixed; inset: 0; z-index: -2;
  background:
    radial-gradient(ellipse 80% 60% at 10% 10%, rgba(157, 107, 255, 0.18), transparent 55%),
    radial-gradient(ellipse 70% 50% at 90% 20%, rgba(255, 107, 214, 0.14), transparent 50%),
    radial-gradient(ellipse 60% 40% at 50% 100%, rgba(107, 240, 255, 0.08), transparent 50%),
    linear-gradient(180deg, #0a0612, #140820 40%, #0a0612);
}

.petals {
  position: fixed; inset: 0; z-index: -1; pointer-events: none; opacity: 0.35;
  background-image:
    radial-gradient(circle at 20% 30%, rgba(255, 107, 214, 0.4) 0 1px, transparent 2px),
    radial-gradient(circle at 70% 60%, rgba(255, 107, 214, 0.3) 0 1px, transparent 2px),
    radial-gradient(circle at 40% 80%, rgba(255, 209, 102, 0.25) 0 1px, transparent 2px);
  background-size: 120px 120px, 180px 180px, 240px 240px;
  animation: drift 40s linear infinite;
}
@keyframes drift { to { background-position: 120px 240px, -80px 360px, 60px -180px; } }

.glass {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  backdrop-filter: blur(14px);
  box-shadow: var(--glow);
}

.container { max-width: 1280px; margin: 0 auto; padding: 1.25rem 1.5rem 3rem; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1.25rem;
  max-width: 1280px; margin: 1rem auto 0; padding: 0.9rem 1.25rem;
}
.brand { display: flex; align-items: center; gap: 0.85rem; min-width: 0; flex-shrink: 1; }
.brand-text { min-width: 0; }
.brand-icon { font-size: 2rem; flex-shrink: 0; filter: drop-shadow(0 0 12px rgba(255,107,214,0.6)); }
.brand h1 {
  margin: 0; font-family: var(--display); font-size: 1.45rem; letter-spacing: 0.04em;
  background: linear-gradient(90deg, var(--pink), var(--purple), var(--cyan));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  line-height: 1.2;
}
.brand p {
  margin: 0.15rem 0 0; color: var(--muted); font-size: 0.85rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.topbar-actions {
  display: flex; align-items: center; gap: 0.65rem; flex-shrink: 0; flex-wrap: nowrap;
}
.topbar-actions a.btn { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }

.range-select {
  display: flex; align-items: center; gap: 0.5rem; font-size: 0.75rem; color: var(--muted);
}
.range-label {
  font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; white-space: nowrap;
}
.range-select select { width: auto; min-width: 9.5rem; }

select, input[type=text], input[type=password] {
  background: rgba(0,0,0,0.35); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 0.5rem 0.75rem; font: inherit;
}
select:focus, input:focus { outline: 2px solid rgba(255,107,214,0.35); }

.btn {
  border: none; border-radius: 10px; padding: 0.55rem 0.95rem; font: inherit; cursor: pointer;
  background: linear-gradient(135deg, var(--pink), var(--purple)); color: #fff; font-weight: 600;
  white-space: nowrap;
}
.btn.ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }

.stats-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1rem; margin-bottom: 1.25rem;
}
.stat-card { padding: 1rem 1.1rem; position: relative; overflow: hidden; }
.stat-card::after {
  content: ""; position: absolute; top: -30px; right: -30px; width: 80px; height: 80px;
  border-radius: 50%; background: rgba(255,255,255,0.04);
}
.stat-card .label { display: block; color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; }
.stat-card strong { font-size: 1.75rem; margin-top: 0.35rem; display: block; }
.stat-card.accent strong { color: var(--cyan); }
.stat-card.pink strong { color: var(--pink); }
.stat-card.purple strong { color: var(--purple); }
.stat-card.gold strong { color: var(--gold); }

.charts-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1.25rem;
}
@media (max-width: 1024px) { .charts-grid { grid-template-columns: 1fr; } }

.split-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1.25rem;
}
@media (max-width: 900px) { .split-grid { grid-template-columns: 1fr; } }

.panel { padding: 1.1rem 1.2rem; margin-bottom: 0; }
.panel h2 {
  margin: 0 0 1rem; font-size: 1rem; font-weight: 600;
  display: flex; align-items: center; gap: 0.5rem;
}
.panel h2::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: linear-gradient(135deg, var(--pink), var(--purple));
  box-shadow: 0 0 10px var(--pink);
}
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.75rem; }
.panel-head h2 { margin: 0; }
.panel-hint { margin: 0 0 1rem; font-size: 0.88rem; }
.suggestion-text { max-width: 42rem; white-space: pre-wrap; word-break: break-word; }
.load-error {
  margin-bottom: 1rem;
  padding: 0.85rem 1rem;
  border: 1px solid rgba(255, 107, 138, 0.45);
  color: #ffb4c8;
  font-size: 0.92rem;
}
.filters { display: flex; gap: 0.5rem; flex-wrap: wrap; width: 100%; }
.filters input[type=text] { flex: 1 1 140px; min-width: 0; }
.filters select { flex: 1 1 140px; min-width: 0; }

.table-wrap {
  overflow: auto; border-radius: 12px; border: 1px solid rgba(255,255,255,0.06);
  -webkit-overflow-scrolling: touch;
}
.table-wrap.table-scroll {
  background: linear-gradient(to right, var(--card) 30%, rgba(22, 12, 38, 0)),
              linear-gradient(to right, rgba(22, 12, 38, 0), var(--card) 70%) 100% 0,
              radial-gradient(farthest-side at 0% 50%, rgba(0,0,0,0.35), transparent),
              radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,0.35), transparent) 100% 0;
  background-repeat: no-repeat;
  background-size: 40px 100%, 40px 100%, 14px 100%, 14px 100%;
  background-attachment: local, local, scroll, scroll;
}
.table-wrap.tall { max-height: 420px; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; min-width: 520px; }
th, td { padding: 0.65rem 0.75rem; text-align: left; border-bottom: 1px solid rgba(255,255,255,0.05); vertical-align: top; }
th { color: var(--muted); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; position: sticky; top: 0; background: rgba(10,6,18,0.95); z-index: 1; }
tr:hover td { background: rgba(255,107,214,0.04); }
tr:last-child td { border-bottom: none; }

.badge {
  display: inline-block; padding: 0.15rem 0.5rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600;
}
.badge.online { background: rgba(107,240,255,0.15); color: var(--cyan); border: 1px solid rgba(107,240,255,0.35); }
.badge.offline { background: rgba(255,255,255,0.05); color: var(--muted); }

.event-name { color: var(--pink); font-family: ui-monospace, monospace; font-size: 0.82rem; }
.props-preview {
  color: var(--muted); font-size: 0.8rem; max-width: 420px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.suggestion-text { max-width: 42rem; white-space: pre-wrap; word-break: break-word; }

.viewer-chip {
  font-size: 0.82rem; padding: 0.4rem 0.7rem; border-radius: 999px;
  background: rgba(255, 107, 214, 0.12); border: 1px solid var(--border); color: var(--pink);
  max-width: 11rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.muted { color: var(--muted); }
.error { color: var(--danger); }

#eventsChart { max-height: 280px; }
#actionsChart { max-height: 320px; }

/* ---- Mobile ---- */
@media (max-width: 720px) {
  .container { padding: 0.85rem 0.85rem 2rem; }
  .topbar {
    margin: 0.5rem 0.5rem 0;
    padding: 0.85rem 0.9rem;
    flex-direction: column;
    align-items: stretch;
    gap: 0.85rem;
    max-width: none;
  }
  .brand { flex: none; }
  .brand-icon { font-size: 1.65rem; }
  .brand h1 { font-size: 1.2rem; }
  .brand p { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .topbar-actions {
    flex-wrap: wrap;
    justify-content: stretch;
    gap: 0.5rem;
  }
  .viewer-chip { max-width: none; flex: 1 1 100%; }
  .range-select {
    flex: 1 1 calc(100% - 5.5rem);
    flex-direction: column;
    align-items: stretch;
    gap: 0.25rem;
  }
  .range-select select { width: 100%; min-width: 0; min-height: 44px; }
  .topbar-actions .btn { min-height: 44px; flex: 0 0 auto; }
  select, input[type=text], input[type=password] { min-height: 44px; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.65rem; }
  .stat-card { padding: 0.85rem 0.9rem; }
  .stat-card strong { font-size: 1.45rem; }
  .panel { padding: 0.9rem 0.85rem; margin-bottom: 0.85rem; }
  .panel-head { flex-direction: column; align-items: stretch; }
  .filters { flex-direction: column; }
  .filters input[type=text], .filters select { width: 100%; flex: none; }
  .footer { padding: 1rem 0.85rem 1.5rem; font-size: 0.78rem; }
}

@media (max-width: 560px) {
  .table-wrap.table-scroll table.data-table { min-width: 0; }
  .table-wrap.table-scroll thead { display: none; }
  .table-wrap.table-scroll tbody tr {
    display: block;
    margin-bottom: 0.65rem;
    padding: 0.65rem 0.75rem;
    border: 1px solid rgba(255,255,255,0.07);
    border-radius: 12px;
    background: rgba(0,0,0,0.15);
  }
  .table-wrap.table-scroll tbody tr:hover td { background: transparent; }
  .table-wrap.table-scroll tbody td {
    display: grid;
    grid-template-columns: minmax(5.5rem, 34%) 1fr;
    gap: 0.65rem;
    align-items: start;
    padding: 0.45rem 0;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    white-space: normal;
    overflow: visible;
    text-overflow: unset;
    max-width: none;
  }
  .table-wrap.table-scroll tbody td:last-child { border-bottom: none; }
  .table-wrap.table-scroll tbody td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
    padding-top: 0.1rem;
  }
  .table-wrap.table-scroll tbody td[data-label=""]::before,
  .table-wrap.table-scroll tbody td:not([data-label])::before { content: none; }
  .table-wrap.tall { max-height: none; }
  .props-preview { white-space: normal; word-break: break-word; }
  .event-name { font-size: 0.85rem; }
}

.footer { text-align: center; padding: 1.5rem; font-size: 0.82rem; }
.footer code { color: var(--cyan); }

.login-page { display: grid; place-items: center; min-height: 100vh; padding: 1rem; }
.login-card { width: min(380px, 100%); padding: 2rem; text-align: center; }
.login-badge { font-size: 2rem; margin-bottom: 0.5rem; }
.login-card h1 { font-family: var(--display); margin: 0 0 0.25rem; }
.login-card form { display: grid; gap: 0.75rem; margin-top: 1.25rem; }
.login-form input { width: 100%; }
.login-card button { width: 100%; }
.muted.small { font-size: 0.82rem; margin-top: 0.35rem; }
.admin-hint { margin-top: 1.25rem; opacity: 0.75; }
.admin-hint code { color: var(--cyan); }
