/* DRT — Devin Retirement Tool — main stylesheet.
   Design notes:
   - Calm, professional. Inspired by RightCapital but more restrained.
   - High legibility. System font stack. No animations that imply confidence we don't have.
   - Two themes: light (default) — dark via prefers-color-scheme.
*/

:root {
  --bg: #f7f8fa;
  --bg-elev: #ffffff;
  --bg-panel: #ffffff;
  --fg: #14181f;
  --fg-muted: #5b6472;
  --fg-faint: #99a1b0;
  --accent: #2a6df4;       /* trust blue */
  --accent-soft: #e6efff;
  --success: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --border: #e3e6ec;
  --border-strong: #c9cfd9;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 24, 31, 0.04), 0 4px 12px rgba(20, 24, 31, 0.04);
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --bg-elev: #161a21;
    --bg-panel: #161a21;
    --fg: #e6e9ef;
    --fg-muted: #9aa3b2;
    --fg-faint: #5c6573;
    --accent: #6896ff;
    --accent-soft: #1c2740;
    --border: #242a35;
    --border-strong: #323a48;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 12px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ------------------------------ Auth page ------------------------------ */

.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 36px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}

.auth-brand { text-align: center; margin-bottom: 28px; }
.auth-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px; height: 56px;
  border-radius: 14px;
  background: var(--accent);
  color: white;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}
.auth-card h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.auth-tagline {
  margin: 4px 0 0;
  color: var(--fg-muted);
  font-size: 13px;
}

.auth-card form { display: flex; flex-direction: column; gap: 8px; }
.auth-card label { font-size: 12px; color: var(--fg-muted); }
.auth-card input[type="password"] {
  font-family: var(--font-sans);
  font-size: 15px;
  padding: 12px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--bg-panel);
  color: var(--fg);
}
.auth-card input[type="password"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.auth-card button[type="submit"] {
  margin-top: 8px;
  padding: 12px 14px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
.auth-card button[type="submit"]:hover { filter: brightness(1.06); }

.auth-error {
  margin: 8px 0 0;
  color: var(--danger);
  font-size: 13px;
}
.auth-footer {
  margin-top: 32px;
  text-align: center;
  color: var(--fg-faint);
  font-size: 11px;
}
.auth-footer p { margin: 2px 0; }
.auth-version { color: var(--fg-faint); }

/* ------------------------------ App ------------------------------ */

.app-page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  border-radius: 7px;
  background: var(--accent);
  color: white;
  font-weight: 700;
  font-size: 13px;
}
.brand-name { font-weight: 600; font-size: 14px; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.as-of { color: var(--fg-muted); font-size: 12px; margin-right: 8px; }
.user-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-right: 8px;
}
.user-badge::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--success);
}

.ghost-btn {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border-strong);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-family: var(--font-sans);
}
.ghost-btn:hover { background: var(--accent-soft); border-color: var(--accent); }
.ghost-btn.small { padding: 4px 8px; font-size: 11px; }

.app-main {
  flex: 1;
  padding: 20px;
  padding-right: 360px; /* room for chat sidebar */
  max-width: 1600px;
  margin: 0 auto;
  width: 100%;
}

/* ------------------------------ KPI strip ------------------------------ */

.kpi-strip {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
.kpi-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.kpi-label {
  font-size: 11px;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}
.kpi-value {
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}
.kpi-sub { font-size: 11px; color: var(--fg-faint); margin-top: 2px; }

.kpi-card[data-status="ok"] .kpi-value { color: var(--success); }
.kpi-card[data-status="warn"] .kpi-value { color: var(--warn); }
.kpi-card[data-status="danger"] .kpi-value { color: var(--danger); }

/* ------------------------------ Content grid ------------------------------ */

.content-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  grid-template-rows: auto auto auto;
  gap: 12px;
}
.chart-card { grid-column: 1 / 2; grid-row: 1 / 2; }
.assumptions-card { grid-column: 2 / 3; grid-row: 1 / 3; }
.stress-card { grid-column: 1 / 2; grid-row: 2 / 3; }
.allocation-card { grid-column: 1 / 2; grid-row: 3 / 4; }
.accounts-card { grid-column: 2 / 3; grid-row: 3 / 4; }
.actions-card { grid-column: 1 / 3; grid-row: 4 / 5; }

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.card-head h2 {
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
  letter-spacing: -0.005em;
  margin: 0;
  text-transform: uppercase;
}
.card-head h2 + h2 { margin-top: 0; }
.card-actions select {
  font-family: var(--font-sans);
  font-size: 12px;
  padding: 4px 8px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg-panel);
  color: var(--fg);
}
.card-foot {
  margin-top: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 11px;
  color: var(--fg-muted);
}
.assumption-link {
  background: var(--accent-soft);
  color: var(--accent);
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 500;
  cursor: pointer;
}
.assumption-link:hover { filter: brightness(0.97); }

/* ------------------------------ Charts ------------------------------ */

.chart-host { width: 100%; }
#projection-chart, #allocation-chart { display: block; width: 100%; }
.band-90 { fill: rgba(42, 109, 244, 0.10); }
.band-50 { fill: rgba(42, 109, 244, 0.20); }
.median-line { stroke: rgb(42, 109, 244); stroke-width: 2; fill: none; }
.axis { stroke: var(--border-strong); stroke-width: 1; }
.axis-label { fill: var(--fg-muted); font-size: 10px; font-family: var(--font-sans); }
.zero-line { stroke: var(--fg-faint); stroke-width: 1; stroke-dasharray: 2 4; }

/* ------------------------------ Assumptions panel ------------------------------ */

.assumptions-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.fortune-cookie {
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--bg-panel);
  border: 1px dashed var(--border-strong);
  border-radius: 6px;
  font-size: 12px;
  color: var(--fg-muted);
  font-style: italic;
  line-height: 1.4;
}
.assumption-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px dashed var(--border);
}
.assumption-row:last-child { border-bottom: none; }
.assumption-row-key { color: var(--fg-muted); font-size: 12px; }
.assumption-row-value { font-weight: 600; font-variant-numeric: tabular-nums; font-size: 13px; }
.assumption-out-of-range { color: var(--warn); }

/* ------------------------------ Stress / Allocation ------------------------------ */

.stress-results {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.stress-row {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.stress-row-name { font-size: 12px; font-weight: 600; }
.stress-row-result { font-size: 11px; color: var(--fg-muted); margin-top: 2px; }
.stress-row[data-status="fail"] { border-color: var(--danger); background: rgba(220, 38, 38, 0.04); }
.stress-row[data-status="pass"] { border-color: var(--success); background: rgba(22, 163, 74, 0.04); }

.allocation-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--fg-muted);
}
.legend-dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }

/* ------------------------------ Accounts table ------------------------------ */

.accounts-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
.accounts-table th {
  text-align: left;
  font-weight: 600;
  color: var(--fg-muted);
  padding: 6px 4px;
  border-bottom: 1px solid var(--border-strong);
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.04em;
}
.accounts-table td {
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
}
.accounts-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.accounts-table th.num { text-align: right; }
.tax-pill {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 600;
}
.tax-pill[data-tax="tax_free"] { background: rgba(22, 163, 74, 0.15); color: var(--success); }
.tax-pill[data-tax="tax_deferred"] { background: rgba(42, 109, 244, 0.15); color: var(--accent); }
.tax-pill[data-tax="taxable"] { background: rgba(217, 119, 6, 0.15); color: var(--warn); }

/* ------------------------------ Action items ------------------------------ */

.action-items {
  list-style: none;
  padding: 0;
  margin: 0;
}
.action-items li {
  padding: 8px 10px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 4px;
  margin-bottom: 6px;
  font-size: 12px;
}
.action-items li.empty {
  border-left-color: var(--fg-faint);
  background: transparent;
  color: var(--fg-faint);
}

/* ------------------------------ Chat ------------------------------ */

.chat-panel {
  position: fixed;
  top: 56px;
  right: 0;
  bottom: 0;
  width: 340px;
  background: var(--bg-elev);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 5;
}
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.chat-head h2 { margin: 0; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--fg-muted); }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chat-msg { padding: 10px 12px; border-radius: 10px; font-size: 13px; line-height: 1.5; }
.chat-msg-ai { background: var(--bg-panel); border: 1px solid var(--border); }
.chat-msg-user { background: var(--accent); color: white; align-self: flex-end; max-width: 80%; }
.chat-msg-thinking { padding: 14px 12px; }
.chat-dots { display: inline-flex; gap: 4px; }
.chat-dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--fg-muted);
  animation: chat-dot-bounce 1.2s infinite ease-in-out;
}
.chat-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes chat-dot-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}
.chat-msg pre {
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 8px 10px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 12px;
  overflow-x: auto;
  margin: 8px 0;
}
.chat-msg code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--bg);
  padding: 1px 4px;
  border-radius: 3px;
}
.chat-msg p { margin: 6px 0; }
.chat-msg p:first-child { margin-top: 0; }
.chat-msg p:last-child { margin-bottom: 0; }
.chat-msg-user code, .chat-msg-user pre { background: rgba(255,255,255,0.15); border-color: rgba(255,255,255,0.2); color: white; }
.chat-msg ul { margin: 6px 0 0; padding-left: 18px; }
.chat-msg a { color: var(--accent); text-decoration: none; }
.chat-msg a:hover { text-decoration: underline; }
.chat-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px 0;
  border-top: 1px solid var(--border);
}
.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 11px;
  color: var(--fg);
  max-width: 200px;
}
.attachment-chip svg { flex-shrink: 0; }
.attachment-chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-chip-size { color: var(--fg-muted); font-size: 10px; }
.attachment-chip-remove {
  background: none;
  border: none;
  color: var(--fg-muted);
  cursor: pointer;
  padding: 0;
  margin-left: 2px;
  font-size: 14px;
  line-height: 1;
}
.attachment-chip-remove:hover { color: var(--danger); }

.chat-panel.drop-active {
  outline: 2px dashed var(--accent);
  outline-offset: -8px;
  background: var(--accent-soft);
}

.chat-form {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  align-items: center;
}
.chat-icon-btn {
  background: transparent;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  width: 32px; height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--fg-muted);
  flex-shrink: 0;
}
.chat-icon-btn:hover { color: var(--accent); border-color: var(--accent); }
.chat-form input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg-panel);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 13px;
}
.chat-form button {
  padding: 8px 14px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  font-size: 13px;
}

/* ------------------------------ Modal ------------------------------ */

.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--fg);
  padding: 24px;
  max-width: 520px;
  box-shadow: var(--shadow);
}
.modal::backdrop { background: rgba(0, 0, 0, 0.4); }
.modal h2 { margin-top: 0; font-size: 16px; }
.modal menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 0;
  margin: 20px 0 0;
}
.modal button {
  padding: 8px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg-panel);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 13px;
  color: var(--fg);
}
.modal button[value="confirm"] { background: var(--accent); color: white; border-color: var(--accent); }
.ingest-preview {
  margin-top: 14px;
  padding: 12px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
  min-height: 40px;
  color: var(--fg-muted);
}

/* ------------------------------ Responsive ------------------------------ */

@media (max-width: 1100px) {
  .app-main { padding-right: 20px; }
  .chat-panel {
    position: relative;
    top: auto;
    right: auto;
    bottom: auto;
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    height: 360px;
  }
  .kpi-strip { grid-template-columns: repeat(2, 1fr); }
  .content-grid {
    grid-template-columns: 1fr;
  }
  .chart-card, .assumptions-card, .stress-card, .allocation-card, .accounts-card, .actions-card {
    grid-column: 1 / 2;
    grid-row: auto;
  }
}
