:root {
  color-scheme: light;
  --bg: #f5f6f3;
  --surface: #ffffff;
  --surface-2: #eef1ed;
  --surface-3: #f9faf7;
  --text: #17201e;
  --muted: #66706d;
  --line: #d7ddd7;
  --line-strong: #bcc8c1;
  --teal: #0f766e;
  --teal-strong: #0a5f59;
  --amber: #b7791f;
  --coral: #c9533e;
  --blue: #315b7c;
  --danger: #a33a2d;
  --shadow: 0 16px 38px rgba(22, 31, 28, 0.1);
  --radius: 8px;
  --focus: 0 0 0 4px rgba(15, 118, 110, 0.16);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111615;
  --surface: #1a211f;
  --surface-2: #202a27;
  --surface-3: #161d1b;
  --text: #edf5ef;
  --muted: #a8b5af;
  --line: #31413c;
  --line-strong: #50645e;
  --teal: #42aa9e;
  --teal-strong: #72cbc2;
  --amber: #d69e42;
  --coral: #df755f;
  --blue: #7fa8c7;
  --danger: #f09182;
  --shadow: 0 18px 46px rgba(0, 0, 0, 0.34);
  --focus: 0 0 0 4px rgba(66, 170, 158, 0.18);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(15, 118, 110, 0.08), transparent 240px),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
  letter-spacing: 0;
}

button {
  cursor: pointer;
}

svg {
  display: block;
  width: 1.08rem;
  height: 1.08rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-sprite {
  display: none;
}

.app-shell {
  width: min(1500px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0 18px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 850;
  text-decoration: none;
  white-space: nowrap;
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--teal);
  box-shadow: var(--shadow);
}

.top-actions,
.output-actions,
.form-actions,
.upload-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(460px, 0.98fr) minmax(480px, 1.02fr);
  gap: 18px;
  align-items: start;
}

.panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
  overflow: hidden;
}

:root[data-theme="dark"] .panel {
  background: rgba(26, 33, 31, 0.92);
}

.planner,
.results {
  padding: 20px;
}

.section-head,
.output-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.section-head {
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--teal);
  font-size: 0.76rem;
  font-weight: 850;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  overflow-wrap: anywhere;
}

h1 {
  margin: 0;
  max-width: 760px;
  font-size: 1.9rem;
  line-height: 1.1;
}

h2 {
  margin: 0;
  font-size: 1.18rem;
  line-height: 1.2;
}

h3 {
  margin: 0;
  font-size: 0.94rem;
}

.form-grid,
.form-section {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.full {
  grid-column: 1 / -1;
}

.form-section {
  padding: 16px 0;
  border-top: 1px solid var(--line);
}

.form-section:first-of-type {
  padding-top: 0;
  border-top: 0;
}

.section-label {
  display: flex;
  grid-column: 1 / -1;
  align-items: center;
  gap: 8px;
  color: var(--text);
}

.section-label svg {
  color: var(--teal);
}

.field {
  display: grid;
  grid-column: span 2;
  gap: 7px;
  min-width: 0;
}

.field.compact {
  grid-column: span 1;
}

label,
legend {
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 780;
}

legend {
  padding: 0;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
}

input,
select {
  min-height: 42px;
  padding: 0 11px;
}

textarea {
  resize: vertical;
  min-height: 82px;
  padding: 11px;
  line-height: 1.48;
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible {
  border-color: var(--teal);
  box-shadow: var(--focus);
}

.control-group {
  margin: 0;
  padding: 0;
  border: 0;
}

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

.check-chip,
.toggle {
  min-width: 0;
}

.check-chip input,
.toggle input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.check-chip span,
.toggle span {
  display: grid;
  min-height: 40px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 780;
  line-height: 1.25;
  text-align: center;
  overflow-wrap: anywhere;
}

.check-chip input:checked + span,
.toggle input:checked + span {
  border-color: rgba(15, 118, 110, 0.45);
  background: rgba(15, 118, 110, 0.11);
  color: var(--teal-strong);
}

.toggle span {
  justify-items: start;
  padding: 10px 12px;
  text-align: left;
}

input[type="file"] {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.upload-target {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  min-width: 150px;
  padding: 0 14px;
  border: 1px dashed rgba(15, 118, 110, 0.48);
  border-radius: var(--radius);
  background: rgba(15, 118, 110, 0.08);
  color: var(--teal-strong);
  font-weight: 850;
  text-align: center;
}

.field-help {
  margin: 0;
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.45;
}

.risk-note {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  padding: 12px;
  border: 1px solid rgba(183, 121, 31, 0.36);
  border-radius: var(--radius);
  background: rgba(183, 121, 31, 0.1);
  color: var(--muted);
}

.risk-note svg {
  color: var(--amber);
}

.risk-note p {
  margin: 0;
  line-height: 1.45;
}

.button,
.icon-button,
.tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-weight: 830;
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease;
}

.button {
  gap: 8px;
  min-height: 42px;
  padding: 0 14px;
}

.button:hover,
.icon-button:hover,
.tab:hover {
  transform: translateY(-1px);
}

.button.primary {
  background: var(--teal);
  color: #ffffff;
}

.button.primary:hover {
  background: var(--teal-strong);
}

.button.secondary {
  border-color: var(--line);
  background: var(--surface);
  color: var(--text);
}

.button.ghost {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.icon-button {
  position: relative;
  width: 42px;
  height: 42px;
  border-color: var(--line);
  background: var(--surface);
  color: var(--text);
}

.icon-button::after {
  position: absolute;
  z-index: 10;
  bottom: calc(100% + 8px);
  left: 50%;
  padding: 5px 8px;
  border-radius: 6px;
  background: var(--text);
  color: var(--surface);
  content: attr(data-tooltip);
  font-size: 0.73rem;
  font-weight: 830;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 4px);
  transition: opacity 140ms ease, transform 140ms ease;
  white-space: nowrap;
}

.icon-button:hover::after,
.icon-button:focus-visible::after {
  opacity: 1;
  transform: translate(-50%, 0);
}

.tabs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin: 18px 0 14px;
}

.tab {
  min-height: 38px;
  background: var(--surface);
  border-color: var(--line);
  color: var(--muted);
}

.tab.active {
  border-color: rgba(15, 118, 110, 0.45);
  background: rgba(15, 118, 110, 0.11);
  color: var(--teal-strong);
}

.tab-panel {
  min-height: 520px;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.metric-card {
  display: grid;
  gap: 4px;
  min-height: 82px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-3);
}

.metric-card span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 820;
  text-transform: uppercase;
}

.metric-card strong {
  color: var(--text);
  font-size: 1.25rem;
  line-height: 1.1;
}

.metric-card small {
  color: var(--muted);
  line-height: 1.3;
}

.metric-card.warning {
  border-color: rgba(183, 121, 31, 0.48);
  background: rgba(183, 121, 31, 0.1);
}

.metric-card.danger {
  border-color: rgba(163, 58, 45, 0.48);
  background: rgba(163, 58, 45, 0.1);
}

.chart-wrap {
  height: 260px;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

#equityChart {
  display: block;
  width: 100%;
  height: 100%;
}

.assumption-list {
  display: grid;
  gap: 8px;
  margin: 12px 0;
}

.assumption-item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-3);
  color: var(--muted);
  line-height: 1.4;
}

.assumption-item strong {
  color: var(--text);
}

.trade-table-wrap {
  max-height: 360px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: auto;
}

.trade-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

.trade-table th,
.trade-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}

.trade-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.76rem;
  font-weight: 850;
  text-transform: uppercase;
}

.trade-table td {
  color: var(--muted);
  font-size: 0.86rem;
}

.trade-table td:last-child {
  white-space: normal;
  min-width: 180px;
}

.trade-win {
  color: var(--teal-strong) !important;
  font-weight: 850;
}

.trade-loss {
  color: var(--danger) !important;
  font-weight: 850;
}

.output-text {
  min-height: 520px;
  max-height: 760px;
  white-space: pre-wrap;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 20;
  max-width: min(340px, calc(100vw - 36px));
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--text);
  color: var(--surface);
  font-weight: 830;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 160ms ease, transform 160ms ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

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

  .results {
    position: static;
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 12px;
  }

  .topbar,
  .section-head,
  .output-head {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions,
  .output-actions {
    width: 100%;
  }

  .planner,
  .results {
    padding: 14px;
  }

  h1 {
    font-size: 1.45rem;
  }

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

  .field,
  .field.compact {
    grid-column: 1 / -1;
  }

  .check-grid,
  .tabs {
    grid-template-columns: 1fr;
  }

  .button {
    width: 100%;
  }

  .icon-button {
    flex: 0 0 auto;
  }

  .upload-row {
    align-items: stretch;
    flex-direction: column;
  }

  .upload-target {
    width: 100%;
  }

  .chart-wrap {
    height: 220px;
  }

  .output-text,
  .tab-panel {
    min-height: 430px;
  }
}

@media (max-width: 460px) {
  .form-section,
  .metrics-grid {
    grid-template-columns: 1fr;
  }
}
