@import url("https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;700&family=IBM+Plex+Mono:wght@400;500&display=swap");

/* =====================================================================
   DESIGN TOKENS
   All UI colors, typography, spacing, and radii are defined here.
   Never use raw hex/rgba values in HTML or JS — reference these tokens.
   ===================================================================== */

:root {
  /* ---- Backgrounds ---- */
  --bg-0: #fffaf4;
  --bg-1: #eef6ff;
  --bg-2: #f1fff5;
  --panel: rgba(255, 255, 255, 0.85);
  --panel-strong: rgba(255, 255, 255, 0.95);

  /* ---- Borders & Lines ---- */
  --line: rgba(78, 109, 165, 0.24);
  --radius-sm: 8px;
  --radius-md: 11px;
  --radius-lg: 12px;
  --radius-xl: 18px;
  --radius-pill: 999px;

  /* ---- Text ---- */
  --text: #243353;
  --text-dim: #5d6f92;

  /* ---- Brand & Semantic ---- */
  --accent: #2f8dff;
  --accent-alt: #ff8b5c;
  --danger: #d94848;
  --ok: #1f9c73;

  /* ---- Chart palette (RGB channels for alpha flexibility) ---- */
  --chart-blue: 40, 135, 255;
  --chart-teal: 17, 178, 143;
  --chart-teal-alt: 61, 212, 196;
  --chart-orange: 255, 129, 66;
  --chart-orange-alt: 255, 179, 107;
  --chart-purple: 135, 174, 248;
  --chart-label: #2f3f66;
  --chart-tick: #4a5f8f;
  --chart-grid: 56, 92, 160;

  /* ---- Typography ---- */
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --sans: "Space Grotesk", "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif;

  /* ---- Spacing scale (4px base) ---- */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 24px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--sans);
  color: var(--text);
  background:
    radial-gradient(circle at 8% 16%, #ffe6cc 0%, transparent 40%),
    radial-gradient(circle at 86% 10%, #d7efff 0%, transparent 38%),
    radial-gradient(circle at 50% 92%, #defbe7 0%, transparent 44%),
    linear-gradient(140deg, var(--bg-0), var(--bg-1) 45%, var(--bg-2));
  min-height: 100vh;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.08;
  background-image: linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-size: 28px 28px;
}

a {
  color: inherit;
}

.app-shell {
  max-width: 1440px;
  margin: 0 auto;
  padding: 28px 22px 56px;
  position: relative;
  z-index: 1;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-link {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
  color: inherit;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(145deg, var(--accent), #6ac9ff);
  color: #ffffff;
  display: grid;
  place-items: center;
  font-weight: 700;
  flex-shrink: 0;
}

.brand-title {
  margin: 0;
  font-size: 1.2rem;
  letter-spacing: 0.02em;
}

.brand-subtitle {
  margin: 2px 0 0;
  color: var(--text-dim);
  font-size: 0.82rem;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 10px;
}

.nav-link {
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 16px;
  color: var(--text-dim);
  transition: 0.2s ease;
  font-size: 0.9rem;
}

.nav-link:hover {
  color: var(--text);
  border-color: var(--accent);
  transform: translateY(-1px);
}

.nav-link.active {
  color: #ffffff;
  border-color: transparent;
  background: linear-gradient(135deg, var(--accent), #57b7ff);
}

.page-grid {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 16px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  backdrop-filter: blur(8px);
}

.panel h2,
.panel h3,
.panel h4 {
  margin: 0;
}

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

.badge {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  border: 1px solid rgba(61, 212, 196, 0.45);
  border-radius: 999px;
  padding: 5px 10px;
}

.panel-copy {
  color: var(--text-dim);
  margin: 0;
  line-height: 1.5;
  font-size: 0.95rem;
}

.input-grid {
  margin-top: 14px;
  display: grid;
  grid-template-columns: 1.1fr 1fr 1fr 120px auto;
  gap: 10px;
}

.input-grid.compact {
  grid-template-columns: 1.4fr 220px auto;
}

.input-grid-tight {
  grid-template-columns: 220px;
  margin-top: 0;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-label {
  font-size: 0.77rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.input,
.select,
.textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--text);
  font: inherit;
  padding: 11px 12px;
}

.textarea {
  resize: vertical;
  min-height: 120px;
}

.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(61, 212, 196, 0.16);
}

.password-field {
  position: relative;
}

.password-field .input {
  padding-right: 44px;
}

.password-toggle {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-dim);
  padding: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: color 0.15s ease, background 0.15s ease;
}

.password-toggle:hover {
  color: var(--text);
  background: rgba(78, 109, 165, 0.08);
}

.button {
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 11px 14px;
  font: inherit;
  cursor: pointer;
  color: var(--text);
  background: rgba(255, 255, 255, 0.95);
  transition: 0.2s ease;
}

.button:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.button.primary {
  border: 0;
  background: linear-gradient(135deg, var(--accent), #59beff);
  color: #ffffff;
  font-weight: 600;
}

.button.warn {
  border-color: rgba(217, 72, 72, 0.5);
  color: #b22f2f;
}

.button:disabled {
  opacity: 0.58;
  cursor: not-allowed;
  transform: none;
}

.status {
  margin-top: 12px;
  font-size: 0.9rem;
  color: var(--text-dim);
  min-height: 1.2em;
}

.status.error {
  color: #b53d3d;
}

.status.ok {
  color: #1f8d68;
}

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

.kpi-grid.kpi-grid-6 {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.kpi {
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
}

.kpi-label {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.74rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.kpi-value {
  margin: 8px 0 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.insight-grid {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 10px;
  margin-top: 12px;
}

.view-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.option-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.option-btn {
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--text);
  padding: 10px;
  cursor: pointer;
  transition: 0.18s ease;
}

.option-btn:hover {
  border-color: var(--accent);
}

.option-btn.active {
  border-color: rgba(255, 139, 92, 0.75);
  background: rgba(255, 233, 223, 0.85);
}

.option-title {
  display: block;
  font-size: 0.92rem;
  font-weight: 600;
}

.option-copy {
  margin-top: 4px;
  display: block;
  color: var(--text-dim);
  font-size: 0.8rem;
  line-height: 1.35;
}

.collapsible {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.94);
}

.collapsible > summary {
  cursor: pointer;
  padding: 10px 12px;
  color: var(--text-dim);
  font-size: 0.88rem;
  list-style: none;
}

.collapsible > summary::-webkit-details-marker {
  display: none;
}

.collapsible[open] > summary {
  border-bottom: 1px solid var(--line);
  color: var(--text);
}

.collapsible-body {
  padding: 10px 12px 12px;
}

.result-stack {
  display: grid;
  gap: 10px;
}

.card {
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}

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

.card-copy {
  color: var(--text-dim);
  margin: 4px 0 0;
  font-size: 0.9rem;
}

.code,
.json {
  margin: 0;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid rgba(47, 141, 255, 0.24);
  background: rgba(255, 255, 255, 0.96);
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.45;
  overflow: auto;
  max-height: 220px;
}

.chart-wrap {
  height: 300px;
}

.graph-wrap {
  min-height: 460px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  overflow: hidden;
}

.graph-meta {
  margin-top: 8px;
  color: var(--text-dim);
  font-size: 0.88rem;
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.list-item {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px;
  background: rgba(255, 255, 255, 0.96);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.list-item-name {
  font-weight: 600;
}

.list-actions {
  display: flex;
  gap: 8px;
}

.mono-block {
  margin: 10px 0 0;
  white-space: pre-wrap;
  font-family: var(--mono);
  font-size: 0.8rem;
  color: #4f6187;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: rgba(255, 255, 255, 0.96);
}

.row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.row .field {
  flex: 1;
  min-width: 220px;
}

.check {
  display: inline-flex;
  align-items: flex-start;
  gap: 8px;
  color: var(--text-dim);
  font-size: 0.88rem;
  line-height: 1.45;
}

.check input[type="checkbox"] {
  margin-top: 2px;
}

.check-text {
  display: inline-block;
}

.check-inline {
  align-items: center;
}

.check-inline input[type="checkbox"] {
  margin-top: 0;
}

.check-policy {
  max-width: 100%;
}

.recovery-card {
  margin-top: 12px;
  border-color: rgba(47, 141, 255, 0.3);
}

.recovery-title {
  margin: 0;
  font-size: 0.92rem;
  letter-spacing: 0.02em;
}

.recovery-code {
  margin: 10px 0 0;
  font-family: var(--mono);
  font-size: 0.9rem;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.96);
  padding: 10px 12px;
  word-break: break-word;
}

.footer-note {
  margin-top: 16px;
  color: var(--text-dim);
  font-size: 0.85rem;
}

body.site-locked {
  overflow: hidden;
}

.site-gate {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(49, 89, 146, 0.22);
  backdrop-filter: blur(8px);
}

.site-gate.visible {
  display: flex;
}

.site-gate-card {
  width: min(420px, 100%);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.98);
  padding: 18px;
  box-shadow: 0 24px 48px rgba(47, 89, 156, 0.18);
}

.site-gate-title {
  margin: 0;
  font-size: 1.08rem;
}

.site-gate-copy {
  margin: 6px 0 0;
  color: var(--text-dim);
  font-size: 0.9rem;
}

.site-gate-form {
  margin-top: 12px;
  display: grid;
  gap: 10px;
}

.auth-actions {
  display: flex;
  background: rgba(78, 109, 165, 0.08);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 20px;
}

.auth-action-btn {
  flex: 1;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-dim);
  transition: 0.15s ease;
  text-align: center;
}

.auth-action-btn:hover {
  color: var(--text);
}

.auth-action-btn.active {
  color: var(--text);
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.auth-reveal {
  animation: authReveal 0.2s ease;
}

@keyframes authReveal {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-form .button.primary {
  margin-top: 4px;
}

.auth-link {
  margin-top: 14px;
  font-size: 0.88rem;
  text-align: center;
}

.auth-link a {
  color: var(--accent);
  text-decoration: none;
}

.auth-link a:hover {
  text-decoration: underline;
}

.auth-divider {
  border: none;
  border-top: 1px solid var(--line);
  margin: 20px 0;
}

.account-section {
  margin-top: 18px;
}

.account-section-title {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin: 0 0 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.danger-zone {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid rgba(217, 72, 72, 0.25);
}

.danger-summary {
  color: var(--danger);
}

/* =====================================================================
   HOME PAGE LAYOUT
   Vertical stack of themed sections with labeled headers.
   ===================================================================== */

.home-layout {
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.home-section {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 20px;
  backdrop-filter: blur(8px);
}

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.section-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.kpi-grid.kpi-grid-5 {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

/* Monthly challenge card */
.challenge-card {
  border-color: rgba(47, 141, 255, 0.3);
  background: linear-gradient(135deg, rgba(238, 246, 255, 0.9), rgba(241, 255, 245, 0.9));
}

.challenge-name {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.challenge-participants {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.challenge-member {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.9);
  font-size: 0.88rem;
}

.challenge-member.completed {
  border-color: rgba(31, 156, 115, 0.5);
  background: rgba(222, 251, 231, 0.85);
}

.challenge-member.pending {
  color: var(--text-dim);
}

.challenge-check {
  font-size: 1rem;
  line-height: 1;
}

.challenge-films {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-left: 2px;
}

/* =====================================================================
   UTILITY CLASSES
   Reusable single-purpose classes to eliminate inline styles.
   Use these instead of style="..." attributes in HTML.
   ===================================================================== */

/* Layout */
.full-width       { grid-column: 1 / -1; }
.mx-auto          { margin-left: auto; margin-right: auto; }
.panel-narrow      { max-width: 480px; }
.panel-medium      { max-width: 620px; }
.panel-wide        { max-width: 780px; }
.panel-xl          { max-width: 900px; }
.panel-2xl         { max-width: 980px; }

/* Spacing — margin-top */
.mt-sm  { margin-top: var(--space-sm); }
.mt-md  { margin-top: var(--space-md); }
.mt-lg  { margin-top: var(--space-lg); }
.mt-xl  { margin-top: var(--space-xl); }

/* Spacing — margin-bottom */
.mb-sm  { margin-bottom: var(--space-sm); }
.mb-md  { margin-bottom: var(--space-md); }

/* Spacing — combined */
.m-0    { margin: 0; }

/* Flexbox cards */
.flex-card         { flex: 1; min-width: 280px; }
.flex-card-lg      { flex: 1; min-width: 320px; }
.flex-card-hero    { flex: 1 1 560px; }
.flex-card-side    { flex: 1 1 320px; }
.flex-fill         { flex: 1; }

/* Alignment */
.self-end    { align-self: end; }
.items-end   { align-items: end; }
.stretch     { align-items: stretch; }

/* Display helpers */
.btn-block   { width: 100%; }
.inline-block { display: inline-block; }
.no-underline { text-decoration: none; }

/* Ordered list indent inside cards */
.card ol.card-copy { margin: 0; padding-left: 18px; }
.pl-md { padding-left: 18px; }

/* ===================================================================== */

@media (max-width: 1180px) {
  .page-grid {
    grid-template-columns: 1fr;
  }

  .input-grid {
    grid-template-columns: 1fr 1fr;
  }

  .input-grid.compact {
    grid-template-columns: 1fr 1fr;
  }

  .kpi-grid {
    grid-template-columns: 1fr 1fr;
  }

  .kpi-grid.kpi-grid-5 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .kpi-grid.kpi-grid-6 {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

@media (max-width: 820px) {
  .app-shell {
    padding: 16px 12px 28px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .nav-links {
    width: 100%;
  }

  .nav-link {
    flex: 1;
    text-align: center;
  }

  .input-grid {
    grid-template-columns: 1fr;
  }

  .input-grid.compact {
    grid-template-columns: 1fr;
  }

  .insight-grid {
    grid-template-columns: 1fr;
  }

  .kpi-grid {
    grid-template-columns: 1fr;
  }

  .kpi-grid.kpi-grid-5 {
    grid-template-columns: 1fr 1fr;
  }

  .kpi-grid.kpi-grid-6 {
    grid-template-columns: 1fr;
  }

  .chart-wrap {
    height: 260px;
  }

  .graph-wrap {
    min-height: 360px;
  }
}

/* =====================================================================
   GRAPH VISUALIZATIONS (D3.js force-directed)
   ===================================================================== */

.graph-viz-container {
  position: relative;
  width: 100%;
  min-height: 420px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, #0d1b2e 0%, #152238 40%, #1a2d48 100%);
  overflow: hidden;
}

.graph-viz-container .graph-svg {
  width: 100%;
  height: 100%;
  cursor: grab;
}

.graph-viz-container .graph-svg:active {
  cursor: grabbing;
}

.graph-tooltip {
  position: absolute;
  padding: 8px 12px;
  background: rgba(13, 27, 46, 0.94);
  color: #e4ecf8;
  border: 1px solid rgba(40, 135, 255, 0.35);
  border-radius: 8px;
  font-size: 0.78rem;
  line-height: 1.5;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 10;
  max-width: 240px;
}

.graph-tooltip strong {
  color: #fff;
}

.graph-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  padding: 8px 12px;
  background: rgba(13, 27, 46, 0.7);
  border-top: 1px solid rgba(40, 135, 255, 0.15);
}

.graph-legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.72rem;
  color: #a0b4d0;
}

.graph-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.graph-legend-line {
  width: 18px;
  height: 4px;
  border-radius: 2px;
  display: inline-block;
}

.graph-legend-hint {
  font-size: 0.68rem;
  color: #5d7a9e;
  margin-left: auto;
}

/* Avatar upload */
.avatar-upload-area {
  display: flex;
  align-items: center;
  gap: var(--space-md, 1rem);
  margin-top: var(--space-sm, 0.5rem);
}
.avatar-preview {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2887ff 0%, #11b28f 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
  border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(40,135,255,0.15);
}
.avatar-initials {
  color: #fff;
  font-weight: 700;
  font-size: 1.4rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.avatar-controls {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs, 0.25rem);
}
.avatar-hint {
  font-size: 0.82rem;
  color: var(--text-dim);
}
