@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --primary-color: #079CFF;
  --primary-dark: #0578c7;
  --primary-darker: #045a99;
  --primary-light: #e6f2ff;
  --primary-lighter: #f0f8ff;
  --secondary-color: #00e5ff;
  --background: #f3f4f6;
  --sidebar-bg: #2d3748;
  --sidebar-hover: #3d4758;
  --sidebar-text: #e2e8f0;
  --text-primary: #1f2937;
  --text-secondary: #6b7280;
  --text-light: #9ca3af;
  --border-color: #e5e7eb;
  --success: #10b981;
  --success-light: #d1fae5;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --card-shadow: 0 1px 3px 0 rgba(0,0,0,.1), 0 1px 2px 0 rgba(0,0,0,.06);
  --card-shadow-md: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -1px rgba(0,0,0,.06);
  --card-shadow-hover: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--background);
  color: var(--text-primary);
  line-height: 1.6;
  font-size: 14px;
}

/* ── Layout ─────────────────────────────────────────────── */
.app-container {
  display: flex;
  min-height: 100vh;
  background: linear-gradient(to right, var(--sidebar-bg) 240px, transparent 240px);
}
.app-container:has(.sidebar.collapsed) {
  background: linear-gradient(to right, var(--sidebar-bg) 60px, transparent 60px);
}

/* ── Sidebar ─────────────────────────────────────────────── */
.sidebar {
  width: 240px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  flex-shrink: 0;
  align-self: flex-start;
  overflow-y: auto;
  z-index: 1000;
  box-shadow: 0 0 15px rgba(0,0,0,.1);
  transition: width .3s ease;
}
.sidebar.collapsed { width: 60px; }

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  min-height: 40px;
  overflow: hidden;
}
.logo-img {
  height: 40px;
  width: auto;
  object-fit: cover;
  object-position: left center;
  transition: width .3s ease;
}
.sidebar.collapsed .logo-img { width: 40px; }

.sidebar-toggle {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 8px;
  color: #7f8c8d;
  font-size: 18px;
  transition: color .2s;
}
.sidebar-toggle:hover { color: white; }
.sidebar.collapsed .sidebar-toggle { margin-left: auto; }

.sidebar-nav { flex: 1; padding: 20px 0; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  margin: 2px 10px;
  color: var(--sidebar-text);
  text-decoration: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  transition: all .15s ease-in-out;
  white-space: nowrap;
  overflow: hidden;
}
.nav-item:hover { background: var(--sidebar-hover); color: white; }
.nav-item.active {
  background: var(--sidebar-hover);
  color: white;
  box-shadow: 0 0 0 2px rgba(7,156,255,.2);
}
.nav-item i { width: 20px; text-align: center; font-size: 16px; flex-shrink: 0; }
.nav-item span { transition: opacity .3s ease, width .3s ease; }
.sidebar.collapsed .nav-item span { opacity: 0; width: 0; overflow: hidden; }
.sidebar.collapsed .nav-item { justify-content: center; padding: 12px; }

.nav-footer {
  border-top: 1px solid rgba(255,255,255,.08);
  padding-top: 16px;
  margin-top: auto;
}

/* ── Main content ────────────────────────────────────────── */
.main-content {
  flex: 1;
  min-width: 0;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Top gradient header ─────────────────────────────────── */
.top-header {
  background: linear-gradient(to right, #0859BD, #079CFF, #00C3FF, #00D7E8, #00EAB9, #84F491, #F9F871);
  padding: 14px 28px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  box-shadow: var(--card-shadow-md);
  position: sticky;
  top: 0;
  z-index: 100;
}

.breadcrumb-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: white;
}
.breadcrumb-nav a { color: white; text-decoration: none; opacity: .85; }
.breadcrumb-nav a:hover { opacity: 1; }
.breadcrumb-sep { opacity: .5; }
.breadcrumb-current { font-weight: 600; }

.header-actions { display: flex; gap: 10px; align-items: center; }

/* ── Page content ────────────────────────────────────────── */
.page-content {
  padding: 28px 32px;
  max-width: 1600px;
  width: 100%;
}

/* ── Flash messages ──────────────────────────────────────── */
.flash-danger, .flash-warning, .flash-success, .flash-info {
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 14px;
  font-weight: 500;
}
.flash-danger  { background: var(--danger-light);  color: var(--danger);  border: 1px solid #fca5a5; }
.flash-warning { background: var(--warning-light); color: var(--warning); border: 1px solid #fcd34d; }
.flash-success { background: var(--success-light); color: var(--success); border: 1px solid #6ee7b7; }
.flash-info    { background: var(--primary-light); color: var(--primary-dark); border: 1px solid #93c5fd; }

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all .15s ease-in-out;
  text-decoration: none;
  line-height: 1.4;
}
.btn-primary {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}
.btn-primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(7,156,255,.25);
  color: white;
}
.btn-secondary {
  background: white;
  color: var(--text-primary);
  border-color: var(--border-color);
}
.btn-secondary:hover {
  border-color: var(--primary-color);
  color: var(--primary-color);
  background: var(--primary-lighter);
}
.btn-outline {
  background: transparent;
  color: var(--primary-color);
  border-color: var(--primary-color);
}
.btn-outline:hover {
  background: var(--primary-light);
}
.btn-success {
  background: var(--success);
  color: white;
  border-color: var(--success);
}
.btn-success:hover { filter: brightness(1.08); transform: translateY(-1px); color: white; }
.btn-danger {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}
.btn-danger:hover { filter: brightness(1.08); color: white; }
.btn-warning-outline {
  background: transparent;
  color: var(--warning);
  border-color: var(--warning);
}
.btn-warning-outline:hover {
  background: var(--warning-light);
}
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn:disabled { opacity: .6; cursor: not-allowed; transform: none !important; }

/* ── Cards (section) ─────────────────────────────────────── */
.card {
  background: white;
  border-radius: 8px;
  padding: 20px 24px;
  box-shadow: var(--card-shadow);
  border: 1px solid var(--border-color);
}
.card-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 16px;
  letter-spacing: -.2px;
}

/* ── Form controls ───────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .5px;
  margin-bottom: 6px;
}
.form-control {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text-primary);
  background: white;
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
.form-control:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(7,156,255,.15);
}
select.form-control { cursor: pointer; appearance: auto; }
.input-group { display: flex; gap: 0; }
.input-group .form-control { border-radius: 6px 0 0 6px; }
.input-group .btn { border-radius: 0 6px 6px 0; flex-shrink: 0; }

/* ── Tabs ────────────────────────────────────────────────── */
.tabs { display: flex; gap: 0; border-bottom: 2px solid var(--border-color); margin-bottom: 24px; }
.tab-btn {
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  background: none;
  border: none;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all .15s;
}
.tab-btn:hover { color: var(--text-primary); }
.tab-btn.active { color: var(--primary-color); border-bottom-color: var(--primary-color); }
.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* ── Search result items ─────────────────────────────────── */
.result-item {
  background: white;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  transition: all .2s ease-in-out;
}
.result-item:hover {
  box-shadow: var(--card-shadow);
  border-color: var(--primary-light);
  transform: translateY(-1px);
}
.result-name { font-weight: 600; font-size: 14px; color: var(--text-primary); }
.result-id { font-size: 11px; color: var(--text-light); font-family: 'Courier New', monospace; margin-top: 2px; }
.result-addr { font-size: 12px; color: var(--text-secondary); margin-top: 3px; }
.result-actions { display: flex; gap: 8px; flex-shrink: 0; }

/* ── Tree / hierarchy cards ──────────────────────────────── */
.tree-wrap { overflow-x: auto; padding-bottom: 2rem; }

details > summary { user-select: none; }
details > summary::-webkit-details-marker { display: none; }
details[open] > summary .fa-chevron-right { transform: rotate(90deg); }

.tree-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, 270px);
  justify-content: center;
  align-items: stretch;
  gap: 14px;
  position: relative;
}
.tree-row + .tree-row { margin-top: 36px; }

.tree-connector-down {
  display: flex;
  justify-content: center;
  height: 36px;
  position: relative;
}
.tree-connector-down::before {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  left: 50%;
  border-left: 2px solid #9ca3af;
}

.tree-children-group { display: flex; flex-direction: column; align-items: center; }
.tree-h-bar { width: 100%; border-top: 2px solid #9ca3af; }
.tree-children-row { display: flex; justify-content: center; gap: 14px; position: relative; }
.tree-children-row .tree-card::before {
  content: '';
  position: absolute;
  top: -20px;
  left: 50%;
  height: 20px;
  border-left: 2px solid #9ca3af;
}

.tree-card {
  position: relative;
  background: white;
  border: 1.5px solid var(--border-color);
  border-radius: 10px;
  padding: 14px 16px;
  width: 270px;
  min-height: 160px;
  box-shadow: var(--card-shadow);
  font-size: .82rem;
  transition: box-shadow .2s, transform .2s;
  display: flex;
  flex-direction: column;
}
.tree-card:hover { box-shadow: var(--card-shadow-md); }

.tree-card.current  { border: 2px solid var(--primary-color); box-shadow: 0 0 0 3px rgba(7,156,255,.15); }
.tree-card.parent-card  { border-color: var(--success); }
.tree-card.subsidiary-card { border-color: #9ca3af; }
.tree-card.associated-card { border-color: var(--warning); }
.tree-card.ai-suggested { box-shadow: 0 0 0 3px rgba(245,158,11,.4), var(--card-shadow); }

.card-label {
  font-size: .65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 5px;
  color: var(--text-secondary);
}
.card-label.text-primary { color: var(--primary-color); }
.card-label.text-warning { color: var(--warning); }

.card-name { font-weight: 700; font-size: .92rem; margin-bottom: 3px; color: var(--text-primary); }
.card-id { font-size: .68rem; color: var(--text-light); word-break: break-all; font-family: 'Courier New', monospace; }
.card-id a { color: var(--primary-color); text-decoration: none; }
.card-id a:hover { text-decoration: underline; }

.card-detail { font-size: .7rem; color: var(--text-secondary); margin-top: 6px; line-height: 1.4; }
.detail-label { font-weight: 700; color: var(--text-light); font-size: .6rem; text-transform: uppercase; letter-spacing: .05em; }

.link-btn-wrap { margin-top: auto; padding-top: 10px; display: flex; flex-direction: column; gap: 5px; }
.link-btn { width: 100%; }

.ai-badge { display: none; }
.tree-card.ai-suggested .ai-badge { display: inline-block; }

.section-label {
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-secondary);
  text-align: center;
  margin-bottom: 8px;
}

/* Badge overrides (use Bootstrap badges but style them consistently) */
.badge { font-size: .6rem; font-weight: 700; letter-spacing: .04em; border-radius: 4px; }

/* ── Modal tweaks ────────────────────────────────────────── */
.modal-content { border-radius: 10px; border: none; box-shadow: var(--card-shadow-hover); }
.modal-header { border-bottom: 1px solid var(--border-color); }
.modal-footer { border-top: 1px solid var(--border-color); }

/* ── Spinner ─────────────────────────────────────────────── */
.spinner-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,.88);
  z-index: 9999;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
}
.spinner-overlay.active { display: flex; }
.spinner-overlay p { font-size: 1rem; font-weight: 600; color: var(--text-secondary); }
.spinner-overlay small { color: var(--text-light); font-size: .85rem; }

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar { width: 60px; }
  .sidebar .nav-item span, .sidebar .logo span { display: none; }
  .main-content { margin-left: 0; }
  .page-content { padding: 16px; }
}

/* ── Print / PDF export ──────────────────────────────────── */
@media print {

  .sidebar,
  .top-header,
  .link-btn-wrap,
  #analyze-overlay,
  #linkModal,
  #enrichModal,
  #connectModal,
  #reanalyze-form,
  #refresh-rels-btn,
  .flash-danger,
  .flash-success,
  .flash-warning,
  .flash-info { display: none !important; }

  /* Hide action buttons and AI analysis card */
  #header-actions    { display: none !important; }
  #ai-analysis-card  { display: none !important; }

  body           { background: white; font-size: 11px; }
  .app-container { display: block; }
  .main-content  { margin-left: 0 !important; overflow: visible !important; }
  .page-content  { padding: 0; max-width: none; margin: 0; }
  .tree-wrap     { overflow: visible; }

  /* Scale cards down so more fit per row */
  .tree-card {
    width: 210px;
    min-height: auto;
    padding: 10px 12px;
    box-shadow: none;
    border: 1px solid #ccc;
    font-size: .75rem;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .tree-row {
    grid-template-columns: repeat(auto-fit, 210px);
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .card-name  { font-size: .82rem; }
  .card-id    { font-size: .6rem; }
  .card-detail { font-size: .65rem; }
  .tree-connector-down { break-inside: avoid; }
  .section-label { font-size: .65rem; }
}
