:root {
  --primary-color: #007bff;
  --input-height: 48px;
  --bg-color: #f8f9fa;
  --card-bg: #ffffff;
  --text-color: #333;
  --border-color: #dee2e6;
  --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  --primary-rgb: 0, 123, 255;
  --heading-color: #343a40;
  --muted-color: #6c757d;
  --ai-bg: #e9f5ff;
  --tooltip-bg: #343a40;
  --tooltip-text: #ffffff;
  --danger-bg: #f8d7da;
  --danger-text: #721c24;
  --button-hover-bg: #4b9cd3;
  --success-color: #28a745;
  --success-hover: #218838;
}

/* Load Inter from Google Fonts for unified typography */
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;700&display=swap");
/* Dark theme overrides */
html.dark {
  --primary-color: #4b9cd3;
  --bg-color: #13294b;
  --card-bg: #0b1220;
  --text-color: #e6eef8;
  --border-color: #1f2a37;
  --shadow: 0 6px 14px rgba(0, 0, 0, 0.6);
  --primary-rgb: 102, 178, 255;
  --heading-color: #e6eef8;
  --muted-color: #9aa6b3;
  --ai-bg: #081226;
  --tooltip-bg: rgba(20, 24, 30, 0.95);
  --tooltip-text: #e6eef8;
  --danger-bg: #4b1e24;
  --danger-text: #f8d7da;
  --button-hover-bg: rgba(255, 255, 255, 0.04);
  --success-color: #2fbd63;
  --success-hover: rgba(47, 189, 99, 0.9);
}

* {
  box-sizing: border-box;
}

body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  margin: 0;
  background-color: var(--bg-color);
  color: var(--text-color);
  line-height: 1.6;
}

/* Ensure form controls use the same font and improved font rendering */
input,
textarea,
select,
button,
label {
  font-family: inherit;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* Header layout: use flexbox to center the title reliably. */
header {
  margin-bottom: 2rem;
}

h1 {
  color: var(--heading-color);
  font-size: clamp(1.6rem, 4vw, 3rem);
  font-weight: 700;
  margin: 0 0 1rem 0;
  text-align: center;
}

.header-row {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Ensure form controls use the same font and improved font rendering */
input,
textarea,
select,
button,
label {
  font-family: inherit;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* Buttons and inputs should have consistent font-size and weight */
button,
input[type="text"],
input[type="number"],
input[type="date"] {
  font-size: 1rem;
  font-weight: 400;
}
.container {
  max-width: 960px;
  margin: 2rem auto;
  padding: 1rem; /* unified horizontal padding to align cards and charts */
}
/* header spacing handled above */

/* Center and tidy secondary headings across the app */
h2,
h3 {
  text-align: center;
  color: var(--heading-color);
  margin-top: 0.5rem;
  margin-bottom: 1rem;
}
.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  align-items: end;
  margin-bottom: 2rem;
  background-color: var(--card-bg);
  padding: 1.25rem;
  border-radius: 12px;
  box-shadow: var(--shadow);
}

/* ensure the Run button sits on the right */
.controls > button {
  grid-column: 3 / 4;
  justify-self: end;
  align-self: center;
  padding: 0.9rem 1.6rem;
  border-radius: 8px;
}

.actions-column {
  display: flex;
  flex-direction: row;
  gap: 0.75rem;
  align-items: center;
  justify-content: flex-end;
}

.run-btn {
  height: 48px; /* match typical input height */
  display: inline-flex;
  align-items: center;
}

.mc-control .tooltip {
  height: 48px;
  display: inline-flex;
  align-items: center;
}

.tooltip .tooltip-icon {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

/* Header layout with actions is defined near the top to center title and place actions */

/* Floating theme toggle (fixed) */
#theme-toggle-btn {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 9999;
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow);
  cursor: pointer;
}

#theme-toggle-btn svg {
  width: 20px;
  height: 20px;
  fill: var(--text-color);
}

#theme-toggle-btn:hover {
  transform: translateY(-2px);
}
.input-group {
  display: flex;
  flex-direction: column;
}
label {
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
  font-weight: 500;
}
input[type="text"] {
  padding: 0.85rem 1rem;
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 10px;
  font-size: 1rem;
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: border-color 0.12s ease, box-shadow 0.12s ease, transform 0.06s;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.04);
  height: var(--input-height);
}
input[type="number"],
input[type="date"] {
  padding: 0.85rem 1rem;
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 10px;
  background-color: var(--bg-color);
  color: var(--text-color);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.04);
  height: var(--input-height);
}
input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 6px 18px rgba(75, 156, 211, 0.08);
  transform: translateY(-1px);
}
button {
  padding: 0.75rem 1.5rem;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 4px;
  width: 15.5rem;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.2s;
  font-family: bold;
}
button:hover {
  background-color: var(--button-hover-bg);
}
.hidden {
  display: none !important;
}
#results-container {
  margin-top: 2rem;
}
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}
.metric-card {
  background-color: var(--card-bg);
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: var(--shadow);
  text-align: center;
}
.metric-card h3 {
  margin-top: 0;
  font-size: 1rem;
  color: var(--muted-color);
}
.metric-card p {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 0;
  color: var(--primary-color);
}
#chart-container {
  width: 100%;
  height: 450px;
  background-color: var(--card-bg);
  padding: 0.75rem; /* slightly reduced to help alignment */
  border-radius: 8px;
  box-shadow: var(--shadow);
  margin-bottom: 2rem;
  overflow: hidden;
  max-width: 100%;
}
.ai-summary {
  background-color: var(--ai-bg);
  padding: 1.5rem;
  border-radius: 8px;
  border-left: 5px solid var(--primary-color);
  margin-top: 1.5rem;
}
.ai-summary h3 {
  margin-top: 0;
  text-align: center;
  font-size: 1.25rem;
  font-weight: 700;
}
#ai-summary-content p {
  white-space: pre-wrap;
}
/* Loading overlay and spinner */
#loading-spinner {
  display: none; /* toggled by JS */
  position: fixed;
  inset: 0;
  z-index: 9998;
  background: linear-gradient(rgba(8, 12, 20, 0.45), rgba(8, 12, 20, 0.45));
  display: none;
  align-items: center;
  justify-content: center;
}

.loading-card {
  background: var(--card-bg);
  color: var(--text-color);
  padding: 1.25rem 1.5rem;
  border-radius: 10px;
  box-shadow: 0 8px 30px rgba(2, 6, 23, 0.6);
  display: flex;
  gap: 1rem;
  align-items: center;
}

.spinner {
  border: 4px solid rgba(0, 0, 0, 0.08);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border-top-color: var(--primary-color);
  animation: spin 0.9s linear infinite;
}

.loading-text {
  font-size: 1rem;
  font-weight: 600;
}

.loading-dots span {
  display: inline-block;
  animation: loadingDots 1s infinite;
  opacity: 0.2;
  margin-left: 2px;
}
.loading-dots span:nth-child(1) {
  animation-delay: 0s;
}
.loading-dots span:nth-child(2) {
  animation-delay: 0.15s;
}
.loading-dots span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes loadingDots {
  0% {
    opacity: 0.2;
    transform: translateY(0);
  }
  50% {
    opacity: 1;
    transform: translateY(-3px);
  }
  100% {
    opacity: 0.2;
    transform: translateY(0);
  }
}
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
#error-message {
  background-color: var(--danger-bg);
  color: var(--danger-text);
  padding: 1rem;
  border: 1px solid rgba(245, 198, 203, 0.6);
  border-radius: 4px;
  margin-top: 1rem;
}

#error-message:empty {
  display: none;
  padding: 0;
  border: none;
  margin: 0;
}

.controls {
  flex-wrap: wrap;
}

#run-backtest-btn {
  background-color: var(--primary-color);
}
#run-backtest-btn:hover {
  background-color: var(--button-hover-bg);
}
#run-mc-btn {
  background-color: var(--success-color);
  color: white;
}
#run-mc-btn:hover {
  background-color: var(--success-hover);
}

/* Ensure Run button and tooltip align height with inputs */
.run-btn {
  height: var(--input-height);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 1.25rem;
  border-radius: 10px;
  font-weight: 700;
  text-align: center;
}

.mc-control .tooltip {
  height: var(--input-height);
  display: inline-flex;
  align-items: center;
}

.tooltip .tooltip-icon {
  width: calc(var(--input-height) - 12px);
  height: calc(var(--input-height) - 12px);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1px solid var(--border-color);
}

/* Make run button full width on small screens */
@media (max-width: 560px) {
  .controls {
    grid-template-columns: 1fr;
  }
  .actions-column {
    justify-content: stretch;
    width: 100%;
  }
  .run-btn {
    width: 100%;
  }
}

.mc-control {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.tooltip {
  position: relative;
  display: inline-block;
  cursor: pointer;
}
.tooltip .tooltip-icon {
  font-weight: bold;
  color: var(--muted-color);
  border: 1px solid var(--muted-color);
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  font-size: 0.8rem;
}
.tooltip .tooltip-text {
  visibility: hidden;
  width: 250px;
  background-color: var(--tooltip-bg);
  color: var(--tooltip-text);
  text-align: center;
  border-radius: 6px;
  padding: 8px;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -125px;
  opacity: 0;
  transition: opacity 0.3s;
  font-size: 0.85rem;
}
.input-group input::placeholder,
input::placeholder {
  color: var(--muted-color);
  opacity: 0.85;
}
.tooltip:hover .tooltip-text {
  visibility: visible;
  opacity: 1;
}

#mc-results-container {
  margin-top: 2rem;
}
#mc-chart-container {
  width: 100%;
  height: 450px;
  background-color: var(--card-bg);
  padding: 0.75rem; /* match primary chart */
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
  max-width: 100%;
}

/* Monte Carlo metrics grid: 3 columns (3 cards per row), responsive */
#mc-metrics-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-top: 1rem;
}

@media (max-width: 900px) {
  #mc-metrics-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 560px) {
  #mc-metrics-grid {
    grid-template-columns: 1fr;
  }
}

#ai-analysis-h3 {
  font-size: 100;
}

footer {
  text-align: center;
  margin-top: 3rem;
  padding: 1rem;
  font-size: 0.85rem;
  color: var(--muted-color);
  border-top: 1px solid var(--border-color);
}

/* Footer left/right layout */
.site-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  max-width: 960px;
  margin: 2rem auto 0 auto;
  padding: 1rem;
}
.site-footer .footer-left {
  flex: 1 1 60%;
  color: var(--muted-color);
  font-size: 0.9rem;
}
.site-footer .footer-right {
  flex: 0 0 auto;
}
.site-footer a {
  color: var(--text-color);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 6px;
  transition: background-color 0.12s ease, color 0.12s ease;
}
.site-footer a:hover {
  background-color: rgba(var(--primary-rgb), 0.06);
}

@media (max-width: 640px) {
  .site-footer {
    flex-direction: column;
    text-align: center;
    gap: 0.5rem;
  }
  .site-footer .footer-left {
    order: 1;
  }
  .site-footer .footer-right {
    order: 2;
  }
}
