:root {
  color-scheme: dark;
  --bg: #17181b;
  --panel: rgba(20, 24, 29, 0.48);
  --panel-soft: rgba(255, 255, 255, 0.035);
  --line: rgba(255, 255, 255, 0.11);
  --hairline: rgba(255, 255, 255, 0.075);
  --ink: #f8fbff;
  --muted: rgba(248, 251, 255, 0.76);
  --dim: rgba(248, 251, 255, 0.52);
  --aqua: #8cebe0;
  --green: #a8e06f;
  --gold: #f1ca76;
  --coral: #ff8d78;
  --violet: #b9a7ff;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
  font-family:
    -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text",
    "PingFang SC", "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  min-height: 100dvh;
  color: var(--ink);
  background: var(--bg);
  font-weight: 400;
  letter-spacing: 0;
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

button,
textarea {
  font: inherit;
}

.app {
  width: min(1680px, calc(100% - 20px));
  min-height: 100dvh;
  margin: 0 auto;
  padding: 18px 0 24px;
  display: flex;
  flex-direction: column;
}

.app.result-mode {
  padding-top: 58px;
}

.composer,
.chat-thread,
.panel-head,
.legend,
.chart-foot {
  display: flex;
  align-items: center;
}

.return-chip span,
.metric-grid span,
.chart-foot {
  color: var(--dim);
  font-size: 14px;
  letter-spacing: 0.04em;
  font-weight: 400;
}

.chat-dock,
.panel {
  background: transparent;
}

.history-toggle {
  position: fixed;
  top: max(18px, env(safe-area-inset-top));
  left: 18px;
  z-index: 26;
  min-width: 72px;
  height: 42px;
  padding: 0 16px;
  border: 0;
  border-radius: 999px;
  color: rgba(248, 251, 255, 0.9);
  background: rgba(255, 255, 255, 0.12);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  cursor: pointer;
  font-size: 16px;
  font-weight: 460;
}

.history-toggle:hover {
  background: rgba(255, 255, 255, 0.16);
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 28;
  background: rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(8px);
}

.drawer-head {
  display: none;
}

.close-history {
  width: 34px;
  height: 34px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: rgba(248, 251, 255, 0.78);
  background: rgba(255, 255, 255, 0.1);
  cursor: pointer;
  font-size: 24px;
  font-weight: 300;
  line-height: 1;
}

.chat-dock {
  position: relative;
  z-index: 10;
  margin-top: 0;
  width: 100%;
  min-height: calc(100dvh - 42px);
  padding: 20px 0 max(18px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.app.result-mode .chat-dock {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 30;
  width: min(430px, calc(100% - 44px));
  margin: 0;
  padding: max(18px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  background: rgba(23, 24, 27, 0.97);
  box-shadow: 22px 0 70px rgba(0, 0, 0, 0.38);
  transform: translateX(calc(-100% - 24px));
  transition: transform 220ms ease;
}

.app.result-mode .chat-dock.is-open {
  transform: translateX(0);
}

.app.result-mode .drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 42px;
  margin-bottom: 20px;
  color: rgba(248, 251, 255, 0.72);
  font-size: 17px;
  font-weight: 460;
}

.chat-dock > .chat-thread,
.chat-dock > .generate-row,
.chat-dock > .composer-row {
  width: min(960px, calc(100% - 36px));
  margin-left: auto;
  margin-right: auto;
}

.app.result-mode .chat-dock > .chat-thread,
.app.result-mode .chat-dock > .generate-row,
.app.result-mode .chat-dock > .composer-row {
  width: 100%;
  margin-left: 0;
  margin-right: 0;
}

p {
  margin: 0;
}

.chat-thread {
  display: grid;
  gap: 12px;
  flex: 1 1 auto;
  align-content: end;
  min-height: 0;
  max-height: none;
  margin-bottom: 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 4px;
}

.app.result-mode .chat-thread {
  flex: 1 1 auto;
  align-content: start;
  max-height: none;
  margin-bottom: 16px;
  padding-right: 2px;
}

.chat-thread:empty {
  display: block;
  visibility: hidden;
}

.chat-message {
  width: 100%;
  color: var(--muted);
  line-height: 1.66;
  font-size: 21px;
  font-weight: 400;
}

.app.result-mode .chat-message {
  font-size: 17px;
  line-height: 1.62;
}

.chat-message.user {
  justify-self: end;
  width: fit-content;
  max-width: min(920px, 86%);
  padding: 13px 17px;
  border-radius: 18px;
  background: rgba(140, 235, 224, 0.095);
  color: rgba(248, 251, 255, 0.88);
}

.app.result-mode .chat-message.user {
  max-width: 92%;
  padding: 11px 14px;
  border-radius: 16px;
}

.chat-message.assistant {
  justify-self: start;
  color: rgba(248, 251, 255, 0.74);
}

.composer-row {
  display: block;
}

.composer {
  align-items: stretch;
  gap: 12px;
  width: 100%;
  padding: 14px;
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.13);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

textarea {
  flex: 1 1 auto;
  min-height: 78px;
  max-height: min(34dvh, 280px);
  resize: none;
  overflow-y: auto;
  border: 0;
  outline: none;
  color: var(--ink);
  background: transparent;
  font-size: 22px;
  font-weight: 400;
  line-height: 1.6;
}

.app.result-mode textarea {
  min-height: 88px;
  max-height: 180px;
  font-size: 18px;
}

textarea::placeholder {
  color: rgba(248, 251, 255, 0.46);
}

.primary-action,
.send-action {
  min-width: 104px;
  min-height: 42px;
  padding: 0 16px;
  border-radius: 12px;
  cursor: pointer;
}

.primary-action {
  border: 0;
  color: #06100f;
  background: linear-gradient(135deg, var(--aqua), #f6ffe8);
  font-size: 18px;
  font-weight: 560;
}

.send-action {
  align-self: flex-end;
  display: grid;
  place-items: center;
  min-width: 46px;
  width: 46px;
  height: 46px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: #06100f;
  background: rgba(248, 251, 255, 0.88);
  font-size: 25px;
  font-weight: 520;
}

.generate-row {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-end;
  margin: 0 2px 12px;
  min-width: 0;
}

.app.result-mode .generate-row {
  margin-bottom: 12px;
}

.run-status {
  min-height: 18px;
  color: rgba(248, 251, 255, 0.56);
  font-size: 16px;
  font-weight: 400;
}

.generate-row .primary-action {
  min-width: 136px;
  min-height: 44px;
}

.primary-action:hover,
.send-action:hover {
  filter: brightness(1.06);
}

.primary-action:active,
.send-action:active {
  transform: translateY(1px);
}

.primary-action:disabled,
.send-action:disabled {
  cursor: wait;
  opacity: 0.62;
}

.primary-action.is-busy {
  color: rgba(6, 16, 15, 0.72);
}

.send-action.is-busy {
  color: transparent;
}

.send-action.is-busy::after {
  content: "";
  width: 14px;
  height: 14px;
  border: 2px solid rgba(6, 16, 15, 0.28);
  border-top-color: #06100f;
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

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

.ghost-action {
  background: rgba(255, 255, 255, 0.06);
}

.result-grid {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  column-gap: 36px;
  row-gap: 26px;
  margin-top: 0;
  margin-bottom: 28px;
}

.app.result-mode .result-grid {
  min-height: calc(100dvh - 88px);
}

.panel {
  min-width: 0;
  padding: 0;
}

.panel-head {
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  min-height: 0;
}

.backtest-panel,
.funds-panel {
  grid-column: 1 / -1;
}

.backtest-panel .panel-head {
  width: 100%;
  justify-content: flex-start !important;
  text-align: left;
}

.funds-panel,
.memo-panel {
  padding-top: 4px;
}

.inline-title {
  font-size: 22px;
  font-weight: 520;
  color: var(--ink);
}

.return-chip {
  display: grid;
  place-items: center;
  background: transparent;
}

.return-chip {
  min-width: 92px;
  padding: 0;
}

.return-chip strong {
  margin-top: 2px;
  color: var(--green);
  font-size: 26px;
  font-weight: 560;
}

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

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

.intent-tile,
.metric-grid div {
  min-height: 62px;
  padding: 0 18px 16px 0;
}

.intent-tile span {
  display: block;
  color: var(--dim);
  font-size: 14px;
}

.intent-tile strong,
.metric-grid strong {
  display: block;
  margin-top: 8px;
  font-size: 21px;
  font-weight: 520;
}

#backtestChart {
  display: block;
  width: 100%;
  height: 300px;
  border: 0;
  border-radius: 0;
  background:
    linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px),
    transparent;
  background-size: 100% 25%, 20% 100%, auto;
}

.legend {
  gap: 12px;
  flex-wrap: wrap;
  width: 100%;
  justify-content: flex-start !important;
  text-align: left;
}

.legend-item {
  color: var(--muted);
  font-size: 15px;
  font-weight: 400;
}

.legend-item::before {
  content: "";
  display: inline-block;
  width: 22px;
  height: 3px;
  margin-right: 7px;
  vertical-align: middle;
  border-radius: 999px;
}

.legend-item.portfolio::before {
  background: var(--aqua);
}

.chart-foot {
  justify-content: flex-start;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 10px;
  color: var(--dim);
  text-align: left;
}

.allocation-bars {
  display: grid;
  gap: 12px;
}

.bar-row {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr) 42px;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-weight: 400;
}

.track {
  overflow: hidden;
  height: 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
}

.fill {
  height: 100%;
  border-radius: 999px;
  transition: width 280ms ease;
}

.fund-list {
  display: grid;
  gap: 0;
}

.fund-row {
  display: grid;
  grid-template-columns: minmax(210px, 0.6fr) 72px minmax(280px, 1fr);
  gap: 18px;
  align-items: center;
  padding: 15px 0;
  border-top: 1px solid var(--hairline);
}

.fund-row span {
  display: block;
  color: var(--dim);
  font-size: 14px;
}

.fund-row strong {
  display: block;
  margin-top: 4px;
  color: var(--ink);
  font-size: 18px;
  font-weight: 520;
}

.fund-weight {
  color: var(--green);
  font-size: 22px;
  font-weight: 560;
}

.fund-row p {
  color: var(--muted);
  font-size: 16px;
  line-height: 1.55;
}

.memo-panel p {
  color: var(--muted);
  font-size: 16px;
  line-height: 1.75;
}

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

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

  .fund-row {
    grid-template-columns: 1fr 64px;
  }

  .fund-row p {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .app {
    width: min(100% - 24px, 1280px);
    padding-top: 14px;
  }

  .app.result-mode {
    padding-top: 66px;
    padding-bottom: 20px;
  }

  .history-toggle {
    left: 12px;
    top: max(12px, env(safe-area-inset-top));
    min-width: 66px;
    height: 40px;
    font-size: 15px;
  }

  .app.result-mode .chat-dock {
    width: min(390px, calc(100% - 34px));
    padding-left: 14px;
    padding-right: 14px;
  }

  .chart-foot {
    align-items: stretch;
    flex-direction: column;
  }

  .chat-dock {
    padding-top: 14px;
    min-height: calc(100dvh - 28px);
  }

  .chat-dock > .chat-thread,
  .chat-dock > .generate-row,
  .chat-dock > .composer-row {
    width: min(100%, calc(100% - 10px));
  }

  textarea {
    max-height: 38dvh;
  }

  .primary-action,
  .send-action {
    flex: 1 1 auto;
  }

  .generate-row {
    align-items: center;
    flex-direction: row;
    gap: 10px;
    min-width: 0;
    margin-bottom: 10px;
  }

  .run-status {
    order: 0;
    min-height: 20px;
    text-align: left;
  }

  .send-action {
    flex: 0 0 40px;
  }

  .generate-row .primary-action {
    flex: 0 0 auto;
    min-width: 116px;
  }

  .intent-grid,
  .metric-grid {
    grid-template-columns: 1fr;
  }

  #backtestChart {
    height: 260px;
  }
}
