:root {
  --bg: #f4f6f0;
  --bg-contrast: #ffffff;
  --ink: #12222d;
  --ink-soft: #6d7880;
  --line: rgba(18, 34, 45, 0.12);
  --brand: #0e7c66;
  --brand-deep: #0a5b4c;
  --accent: #e76f51;
  --sidebar: #0f172a;
  --sidebar-ink: #d8e4ff;
  --glow: linear-gradient(135deg, rgba(14, 124, 102, 0.16), rgba(231, 111, 81, 0.12));
  --card-shadow: 0 24px 60px rgba(18, 34, 45, 0.08);
}

[data-theme="dark"] {
  --bg: #0b1220;
  --bg-contrast: #101b2f;
  --ink: #edf3ff;
  --ink-soft: #9fb0ca;
  --line: rgba(237, 243, 255, 0.12);
  --brand: #3bc8a8;
  --brand-deep: #29957c;
  --accent: #ff946f;
  --sidebar: #07101f;
  --sidebar-ink: #d3e2ff;
  --glow: linear-gradient(135deg, rgba(59, 200, 168, 0.2), rgba(255, 148, 111, 0.15));
  --card-shadow: 0 30px 80px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Manrope", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top right, rgba(14, 124, 102, 0.12), transparent 22rem),
    radial-gradient(circle at bottom left, rgba(231, 111, 81, 0.08), transparent 26rem),
    var(--bg);
}

.app-shell {
  min-height: 100vh;
}

.shell-bg {
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
  background-size: 24px 24px;
  opacity: 0.5;
  pointer-events: none;
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 290px;
  background: linear-gradient(180deg, rgba(255,255,255,0.04), transparent), var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 28px 20px;
  border-right: 1px solid rgba(255,255,255,0.06);
}

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

.brand-icon {
  width: 48px;
  height: 48px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--brand), var(--accent));
  color: white;
  font-size: 1.2rem;
}

.brand-title,
.page-heading,
.auth-title,
.section-header h2 {
  font-family: "Space Grotesk", sans-serif;
}

.brand-title {
  font-size: 1.05rem;
  font-weight: 700;
}

.brand-subtitle,
.eyebrow,
.user-role,
.stat-card small {
  color: var(--ink-soft);
}

.sidebar-nav {
  display: grid;
  gap: 0.35rem;
}

.sidebar-nav a {
  color: var(--sidebar-ink);
  text-decoration: none;
  padding: 0.95rem 1rem;
  border-radius: 16px;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  transition: 0.2s ease;
}

.sidebar-nav a:hover,
.sidebar-nav a.active {
  background: rgba(255,255,255,0.08);
  transform: translateX(3px);
}

.sidebar-footer {
  padding-top: 1rem;
  border-top: 1px solid rgba(255,255,255,0.08);
}

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

.user-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255,255,255,0.1);
  font-weight: 700;
}

.main-panel {
  margin-left: 290px;
  padding: 28px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.topbar-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.page-heading {
  margin: 0;
  font-size: clamp(1.6rem, 2vw, 2.35rem);
}

.content-card,
.auth-card,
.stat-card {
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.7);
  backdrop-filter: blur(18px);
  border-radius: 24px;
  box-shadow: var(--card-shadow);
}

[data-theme="dark"] .content-card,
[data-theme="dark"] .auth-card,
[data-theme="dark"] .stat-card {
  background: rgba(16, 27, 47, 0.72);
}

.content-card {
  padding: 1.5rem;
}

.narrow-card {
  max-width: 840px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.section-header h2 {
  margin: 0.15rem 0 0;
  font-size: 1.35rem;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 0.72rem;
  font-weight: 700;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1rem;
}

.stat-card {
  padding: 1.25rem;
  background-image: var(--glow);
}

.stat-card span {
  display: block;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--ink-soft);
}

.stat-card strong {
  display: block;
  font-size: 2rem;
  margin: 0.6rem 0 0.3rem;
}

.table-modern {
  --bs-table-bg: transparent;
  --bs-table-color: var(--ink);
  margin-bottom: 0;
}

.table-modern thead th {
  border-bottom-width: 1px;
  border-color: var(--line);
  color: var(--ink-soft);
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
}

.table-modern td,
.table-modern th {
  border-color: var(--line);
}

.guest-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
}

.guest-panel {
  width: min(1120px, 100%);
}

.auth-stage {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: 2rem;
  align-items: center;
}

.auth-copy {
  padding: 2rem;
}

.auth-title {
  font-size: clamp(2.4rem, 4vw, 4rem);
  line-height: 1.02;
  margin: 0.6rem 0 1rem;
}

.auth-text {
  max-width: 46rem;
  color: var(--ink-soft);
  font-size: 1.02rem;
}

.auth-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 2rem;
}

.auth-metrics div {
  min-width: 150px;
  padding: 1rem 1.1rem;
  border-radius: 20px;
  background: rgba(255,255,255,0.55);
  border: 1px solid var(--line);
}

[data-theme="dark"] .auth-metrics div {
  background: rgba(16,27,47,0.6);
}

.auth-metrics strong {
  display: block;
  font-size: 1.5rem;
}

.auth-card {
  padding: 2rem;
}

.auth-card-compact {
  max-width: 520px;
  margin: 0 auto;
}

.stack-list {
  display: grid;
  gap: 1rem;
}

.mini-block {
  padding: 1rem;
  border-radius: 18px;
  background: rgba(255,255,255,0.4);
  border: 1px solid var(--line);
}

[data-theme="dark"] .mini-block {
  background: rgba(255,255,255,0.03);
}

.mini-block h3 {
  font-size: 1rem;
  margin-bottom: 0.9rem;
}

.stack-link {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  text-decoration: none;
  color: var(--ink);
  padding: 0.55rem 0;
  border-top: 1px dashed var(--line);
}

.stack-link:first-of-type {
  border-top: 0;
  padding-top: 0;
}

.form-control,
.form-select {
  border-radius: 14px;
  border-color: var(--line);
  background-color: rgba(255,255,255,0.78);
  color: var(--ink);
}

[data-theme="dark"] .form-control,
[data-theme="dark"] .form-select {
  background-color: rgba(8, 17, 31, 0.82);
  color: var(--ink);
}

.btn {
  border-radius: 14px;
  font-weight: 700;
}

.btn-primary {
  background: linear-gradient(135deg, var(--brand), var(--brand-deep));
  border-color: transparent;
}

.btn-outline-primary {
  border-color: var(--brand);
  color: var(--brand);
}

.btn-outline-primary:hover {
  background: var(--brand);
  border-color: var(--brand);
}

.dataTables_wrapper .dataTables_paginate .paginate_button {
  border-radius: 12px !important;
}

canvas {
  max-width: 100%;
}

.upload-surface {
  border: 1px dashed var(--line);
  border-radius: 22px;
  padding: 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  background: rgba(255,255,255,0.42);
}

[data-theme="dark"] .upload-surface {
  background: rgba(255,255,255,0.03);
}

.upload-surface h3 {
  margin: 0.35rem 0;
  font-family: "Space Grotesk", sans-serif;
}

.pdf-toolbar {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.pdf-viewer-shell {
  background: linear-gradient(180deg, rgba(18,34,45,0.06), rgba(18,34,45,0.12));
  border-radius: 20px;
  min-height: 700px;
  display: grid;
  place-items: center;
  overflow: auto;
  padding: 1rem;
}

#pdfViewerCanvas {
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0,0,0,0.18);
}

.meta-grid {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 0.65rem 1rem;
  margin: 0;
}

.meta-grid dt {
  color: var(--ink-soft);
  font-size: 0.86rem;
}

.meta-grid dd {
  margin: 0;
  font-weight: 600;
}

.hash-field {
  word-break: break-all;
}

@media (max-width: 1199px) {
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 991px) {
  .sidebar {
    position: static;
    width: auto;
    min-height: auto;
  }

  .main-panel {
    margin-left: 0;
  }

  .auth-stage {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 767px) {
  .main-panel,
  .guest-shell {
    padding: 1rem;
  }

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

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

/* ---- Document relationships (Module 3) ---- */
.relationship-tree,
.relationship-tree ul {
  list-style: none;
  padding-left: 1.1rem;
  border-left: 1px dashed var(--border, #d0d5dd);
  margin-left: 0.25rem;
}
.relationship-tree li {
  margin: 0.35rem 0;
  position: relative;
}
.relationship-tree li::before {
  content: "";
  position: absolute;
  left: -1.1rem;
  top: 0.8rem;
  width: 0.9rem;
  border-top: 1px dashed var(--border, #d0d5dd);
}
.relationship-timeline {
  list-style: none;
  padding-left: 1.25rem;
  border-left: 2px solid var(--border, #d0d5dd);
}
.relationship-timeline li {
  position: relative;
  padding: 0.5rem 0 0.5rem 0.5rem;
}
.relationship-timeline li::before {
  content: "";
  position: absolute;
  left: -1.6rem;
  top: 0.85rem;
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  background: var(--accent, #0e7c66);
}
.relationship-timeline .rt-date {
  display: inline-block;
  min-width: 6.5rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted, #6c757d);
  font-size: 0.85rem;
}

/* ---- OCR extracted text (Module 5) ---- */
.ocr-text {
  max-height: 320px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--surface-2, rgba(0,0,0,0.03));
  border: 1px solid var(--border, #d0d5dd);
  border-radius: 8px;
  padding: 0.75rem;
  font-size: 0.85rem;
}

/* ---- App footer (credit line) ---- */
.app-footer {
  margin-top: 2rem;
  padding: 1rem 0 0.5rem;
  border-top: 1px solid var(--border, #d0d5dd);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--text-muted, #6c757d);
}
.app-footer strong { color: var(--accent, #0e7c66); font-weight: 700; }
.app-footer.guest-footer {
  justify-content: center;
  text-align: center;
  gap: 1.25rem;
  margin-top: 1.5rem;
}
@media (max-width: 576px) {
  .app-footer { justify-content: center; text-align: center; }
}

/* ---- Dashboard chart sizing (constrain Chart.js canvases) ---- */
.chart-box {
  position: relative;
  width: 100%;
  height: 300px;
}
.chart-box canvas {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
}
