/* ─── Reset & Variables ─────────────────────────────────────────────────── */
:root {
  --nord0:  #2E3440;
  --nord1:  #3B4252;
  --nord2:  #434C5E;
  --nord3:  #4C566A;
  --nord4:  #D8DEE9;
  --nord5:  #E5E9F0;
  --nord6:  #ECEFF4;
  --nord8:  #88C0D0;
  --nord9:  #81A1C1;
  --nord10: #5E81AC;
  --nord14: #A3BE8C;

  --accent:       #A07EC0;
  --accent-light: #C9A8E0;
  --accent-dim:   #7A5FA0;
  --accent-glow:  rgba(160, 126, 192, 0.14);

  --bg:         var(--nord0);
  --bg-raised:  var(--nord1);
  --bg-card:    var(--nord2);
  --border:     var(--nord3);
  --text:       var(--nord6);
  --text-dim:   var(--nord4);
  --text-muted: var(--nord3);

  --sidebar-w: 280px;
  --radius: 4px;
  --radius-sm: 3px;
  --transition: 180ms ease;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }

body {
  font-family: "Ubuntu", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  font-size: 15px;
  height: 100%;
  overflow: hidden;
}

/* ─── Utilities ─────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.brand { font-style: italic; font-weight: 800; letter-spacing: -0.03em; color: var(--accent-light); }

/* ─── Buttons ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background var(--transition), opacity var(--transition);
  text-decoration: none;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-light); }
.btn-outline {
  background: var(--bg-card);
  border: none;
  color: var(--text-dim);
}
.btn-outline:hover:not(:disabled) { background: var(--bg-card); color: var(--accent-light); }
.btn-sm { padding: 6px 14px; font-size: 13px; }
.btn-google {
  background: #fff;
  color: #3c4043;
  border: 1.5px solid #dadce0;
  width: 100%;
  justify-content: center;
  font-size: 15px;
  padding: 12px 20px;
}
.btn-google:hover { background: #f8f8f8; }

.btn-x {
  background: #000;
  color: #fff;
  border: 1.5px solid #000;
  width: 100%;
  justify-content: center;
  font-size: 15px;
  padding: 12px 20px;
}
.btn-x:hover { background: #1a1a1a; }

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 18px;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: color var(--transition), background var(--transition);
  line-height: 1;
}
.icon-btn:hover { color: var(--text); background: var(--bg-card); }

/* ─── Auth Overlay ──────────────────────────────────────────────────────── */
.auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.auth-card {
  background: var(--bg-raised);
  border-radius: var(--radius);
  padding: 48px 40px;
  max-width: 420px;
  width: calc(100% - 32px);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.auth-logo {
  font-size: 2.4rem;
}

.auth-tagline {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  font-weight: 600;
}

.auth-sub {
  font-size: 14px;
  color: var(--text-dim);
  line-height: 1.7;
}

.auth-note {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.7;
}
.auth-note a { color: var(--accent-light); text-decoration: none; }
.auth-note a:hover { text-decoration: underline; }

/* ─── App Shell ─────────────────────────────────────────────────────────── */
.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ─── Sidebar ───────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-raised);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 220ms ease, transform var(--transition);
}

.sidebar-header {
  display: none;
}

.sidebar-logo { font-size: 1.3rem; }

.sidebar-close { display: none; }

.new-analysis-btn {
  margin: 12px 12px 8px;
  justify-content: center;
  font-size: 13px;
  padding: 9px 12px;
}

.sidebar-section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 8px 16px 4px;
  flex-shrink: 0;
}

.conversation-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 0 8px;
}

.conv-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background var(--transition), color var(--transition);
}
.conv-item:hover { background: var(--bg-card); color: var(--text); }
.conv-item.active { background: var(--accent-glow); color: var(--accent-light); }

.sidebar-footer {
  background: var(--bg-card);
  border-radius: 0;
  margin: 0;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

/* User menu dropdown */
.user-menu-wrap {
  position: relative;
}

.user-pill-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}
.user-pill-btn:hover { background: var(--bg-raised); }

.user-menu {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  right: 0;
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  padding: 4px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 50;
}

.user-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 12px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
}
.user-menu-item:hover { background: var(--bg-raised); color: var(--text); }

.user-name-label {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}

.user-credits-label {
  font-size: 12px;
  color: var(--text-dim);
}

.purchase-btn {
  width: 100%;
  justify-content: center;
  background: color-mix(in srgb, var(--accent) 15%, var(--bg-card));
  color: var(--accent-light);
}
.purchase-btn:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 28%, var(--bg-card));
  color: var(--accent-light);
}

/* Collapsible sidebar — tab pinned to right edge of sidebar, vertically centred */
.sidebar-collapse-btn {
  position: fixed;
  left: var(--sidebar-w);
  top: 50%;
  transform: translateY(-50%);
  z-index: 45;
  background: var(--bg-card);
  border: none;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 18px;
  line-height: 1;
  width: 16px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 0 var(--radius) var(--radius) 0;
  transition: left 220ms ease, color var(--transition), background var(--transition);
}
.sidebar-collapse-btn:hover { color: var(--text); background: var(--bg-raised); }

.app.sidebar-collapsed .sidebar-collapse-btn {
  left: 0;
}

.sidebar-reopen-btn {
  display: none;
}

.app.sidebar-collapsed .sidebar {
  width: 0;
  overflow: hidden;
}

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

/* ─── Topbar (mobile) ───────────────────────────────────────────────────── */
.topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-raised);
  flex-shrink: 0;
}

.topbar-logo { font-size: 1.2rem; }

/* ─── Welcome ───────────────────────────────────────────────────────────── */
.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 24px;
  gap: 16px;
}

.welcome h2 {
  font-size: 1.7rem;
  font-weight: 700;
  color: var(--text);
}

.welcome-sub {
  font-size: 15px;
  color: var(--text-dim);
  line-height: 1.7;
}

.example-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
  max-width: 600px;
}

.chip {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 7px 16px;
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
}
.chip:hover { border-color: var(--accent); color: var(--accent-light); }

/* ─── Thread ────────────────────────────────────────────────────────────── */
.thread {
  flex: 1;
  overflow-y: auto;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  scroll-behavior: smooth;
}

.msg {
  display: flex;
  gap: 12px;
  max-width: 800px;
  width: 100%;
}

.msg.user { margin-left: auto; flex-direction: row-reverse; }

.msg-body {
  flex: 1;
  min-width: 0;
}

.msg-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 5px;
}

.msg.user .msg-name { text-align: right; color: var(--accent-dim); }

.msg-content {
  background: var(--bg-raised);
  border-radius: var(--radius);
  padding: 14px 18px;
  font-size: 14px;
  line-height: 1.75;
  color: var(--text);
  word-break: break-word;
}

.msg.user .msg-content {
  background: var(--accent-dim);
  border-color: var(--accent-dim);
  color: #fff;
  border-radius: var(--radius) var(--radius-sm) var(--radius) var(--radius);
}

.msg.assistant .msg-content {
  border-radius: var(--radius-sm) var(--radius) var(--radius) var(--radius);
}

/* HTML content from API (Telegram-style bold/italic) */
.msg-content b  { font-weight: 700; color: var(--nord6); }
.msg-content i  { font-style: italic; color: var(--text-dim); }

/* Copy button */
.msg-copy-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 5px;
  padding: 3px 6px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--transition), color var(--transition), background var(--transition);
}
.msg:hover .msg-copy-btn { opacity: 1; }
.msg-copy-btn:hover { color: var(--text); background: var(--bg-card); }
.msg.user .msg-copy-btn { margin-left: auto; }

/* Status / loading message */
.msg-status {
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
  padding: 4px 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Step log (replaces single .msg-status during analysis) */
.status-log {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 4px 0;
}

.status-step {
  display: grid;
  grid-template-columns: 14px 1fr;
  align-items: start;
  gap: 0 8px;
  font-size: 13px;
  font-style: italic;
  transition: opacity var(--transition), color var(--transition);
}

.status-step.active {
  color: var(--text-dim);
}

.status-step.done {
  color: var(--text-muted);
  opacity: 0.6;
}

.step-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  font-size: 11px;
  color: var(--nord14);
  grid-column: 1;
  grid-row: 1;
  margin-top: 1px;
}

.status-step.active .step-icon {
  color: inherit;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

.step-text {
  grid-column: 2;
  grid-row: 1;
}

.step-detail {
  grid-column: 2;
  grid-row: 2;
  font-size: 11px;
  font-style: normal;
  color: var(--text-muted);
  white-space: pre-wrap;
  line-height: 1.5;
  margin-top: 3px;
  opacity: 0.85;
}

/* ─── Purchase modal ────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal-card {
  background: var(--bg-raised);
  border-radius: var(--radius);
  padding: 28px 28px 24px;
  max-width: 380px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 18px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
}

.modal-desc {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.7;
}

.modal-qty-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
}

.modal-qty-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.qty-btn {
  font-size: 20px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0;
}
.qty-btn:hover { border-color: var(--accent); color: var(--accent-light); }

.qty-input {
  width: 56px;
  text-align: center;
  background: var(--bg-card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  padding: 5px 8px;
  outline: none;
  -moz-appearance: textfield;
}
.qty-input::-webkit-outer-spin-button,
.qty-input::-webkit-inner-spin-button { -webkit-appearance: none; }
.qty-input:focus { border-color: var(--accent); }

.modal-total {
  font-size: 15px;
  color: var(--text-dim);
  text-align: right;
}
.modal-total strong { color: var(--accent-light); font-size: 1.1em; }

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

/* ─── Credits banner ────────────────────────────────────────────────────── */
.credits-banner {
  margin: 0 24px 0;
  padding: 12px 18px;
  background: rgba(191, 97, 106, 0.15);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--nord5);
  flex-shrink: 0;
}

/* ─── Input bar ─────────────────────────────────────────────────────────── */
.input-bar {
  display: flex;
  flex-direction: column;
  gap: 0;
  align-items: stretch;
  flex-shrink: 0;
  background: var(--bg-card);
}

.input-wrapper {
  display: flex;
  gap: 0;
  align-items: stretch;
}

.attach-btn {
  width: 44px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: transparent;
  color: var(--nord9);
  border: none;
  cursor: pointer;
  transition: all var(--transition);
  opacity: 0.85;
}
.attach-btn:hover {
  color: var(--accent-light);
  opacity: 1;
  transform: scale(1.1);
}
.attach-btn:active {
  transform: scale(0.95);
}

.topic-textarea {
  flex: 1;
  background: var(--bg-card);
  border: none;
  border-radius: 0;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.6;
  padding: 14px 18px;
  resize: none;
  outline: none;
  max-height: 180px;
  overflow-y: auto;
}
.topic-textarea::placeholder { color: var(--text-muted); }
.topic-textarea:focus { outline: none; }

.send-btn {
  width: 56px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
  margin: 6px;
  background: var(--bg-card);
  color: var(--accent-light);
}
.send-btn:hover:not(:disabled) {
  background: var(--bg-card);
  color: #fff;
}

/* Document preview */
.document-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
}

.document-preview-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.document-preview-name {
  flex: 1;
  font-size: 13px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.document-remove-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: transparent;
  color: var(--text-dim);
  border: none;
  cursor: pointer;
  font-size: 20px;
  transition: all var(--transition);
  opacity: 0.7;
}
.document-remove-btn:hover {
  color: #ff6b6b;
  opacity: 1;
  transform: scale(1.15);
}
.document-remove-btn:active {
  transform: scale(0.9);
}

/* ─── Mobile ────────────────────────────────────────────────────────────── */
@media (max-width: 700px) {
  .sidebar {
    position: fixed;
    inset: 0;
    width: 100%;
    max-width: 320px;
    z-index: 50;
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,0.4);
  }
  .sidebar-header { display: flex; align-items: center; justify-content: flex-end; padding: 12px 10px 8px; flex-shrink: 0; min-height: 44px; }
  .sidebar-close { display: flex; }
  .sidebar-collapse-btn { display: none !important; }
  .topbar { display: flex; }

  .thread { padding: 20px 16px; gap: 20px; }
  .input-bar { padding: 0; }
  .welcome { padding: 32px 16px; }
  .welcome h2 { font-size: 1.4rem; }
  .credits-banner { margin: 0 16px 0; }
  
  /* Graph sidebar mobile behavior - matches left sidebar */
  .graph-sidebar {
    position: fixed;
    inset: 0;
    width: 100%;
    max-width: 100%;
    z-index: 60;
    transform: translateX(100%);
    border-left: none;
  }
  
  .graph-sidebar.open {
    transform: translateX(0);
    box-shadow: -4px 0 24px rgba(0,0,0,0.4);
  }
  
  /* Show header with close button on mobile */
  .graph-sidebar-header {
    display: flex !important;
    align-items: center;
    justify-content: flex-end;
    padding: 12px 10px 8px;
    flex-shrink: 0;
    min-height: 44px;
  }
  
  .graph-sidebar-close {
    display: flex !important;
  }
  
  /* Fix text overflow in node details on mobile */
  .node-details {
    padding: 16px;
  }
  
  .node-details-content {
    word-wrap: break-word;
    overflow-wrap: break-word;
  }
  
  /* Hide desktop collapse button on mobile */
  .graph-sidebar-collapse-btn {
    display: none !important;
  }
}

@media (max-width: 400px) {
  .auth-card { padding: 36px 24px; }
}

/* ─── Stakeholder Graph Sidebar ────────────────────────────────────────── */
.graph-sidebar {
  width: 450px;
  flex-shrink: 0;
  background: var(--bg);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 220ms ease;
}

.graph-sidebar.hidden {
  width: 0;
  border: none;
}

.graph-sidebar-collapse-btn {
  position: fixed;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  z-index: 50;
  background: var(--bg-card);
  border: none;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 18px;
  line-height: 1;
  width: 16px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius) 0 0 var(--radius);
  transition: right 220ms ease, opacity 220ms ease, color var(--transition), background var(--transition);
  opacity: 0;
  pointer-events: none;
}

.graph-sidebar-collapse-btn.visible {
  opacity: 1;
  pointer-events: auto;
}

.graph-sidebar-collapse-btn:hover {
  background: var(--bg-card);
  color: var(--text);
}

/* When graph sidebar is open, button moves with it */
.app:has(.graph-sidebar:not(.hidden)) .graph-sidebar-collapse-btn {
  right: 450px;
}

.graph-sidebar-header {
  display: none;
  position: relative;
  z-index: 10;
}

.graph-sidebar-close {
  display: none;
}

.graph-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--bg-raised);
}

.node-details {
  flex-shrink: 0;
  max-height: 40%;
  overflow-y: auto;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 20px;
}

.node-details.hidden {
  display: none;
}

.node-details-content {
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: break-word;
  max-width: 100%;
}

.node-details-content h3 {
  margin: 0 0 16px 0;
  color: var(--text);
  font-size: 18px;
  font-weight: 500;
}

.node-details-content strong {
  color: var(--accent-light);
}

.graph-toolbar button:hover {
  background: var(--nord1) !important;
  border-color: var(--nord4) !important;
}

.node .node-circle {
  transition: all 0.2s ease;
}

.node .node-glow {
  transition: all 0.3s ease;
}

.node:hover .node-circle {
  stroke-width: 2;
  filter: brightness(1.2);
}

.node.active .node-circle {
  stroke: var(--accent-light);
  stroke-width: 2.5;
  filter: brightness(1.3);
}

.node.active .node-glow {
  stroke-width: 4;
  opacity: 0.6;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.4; transform: scale(1); }
  50% { opacity: 0.7; transform: scale(1.1); }
}

/* Actor/interest links in analysis text */
.actor-link {
  color: var(--accent-light);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-style: dotted;
}

.actor-link:hover {
  color: var(--accent);
  text-decoration-style: solid;
}
