:root {
  --bg: #0b1016;
  --surface: #121921;
  --surface-2: #18212b;
  --line: #243040;
  --text: #e8eef4;
  --muted: #8fa0b3;
  --profit: #38d39f;
  --loss: #f4707c;
  --warn: #f2b84b;
  --accent: #6ea8ff;
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

[hidden] {
  display: none !important;
}

.app {
  max-width: 980px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

.masthead {
  margin-bottom: 20px;
}

.masthead h1 {
  margin: 0;
  font-size: clamp(1.625rem, 6vw, 2.25rem);
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.15;
}

.masthead__sub {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 1rem;
}

.layout {
  display: grid;
  gap: 16px;
  grid-template-columns: minmax(0, 1fr);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
}

/* ---- Fields ---- */

.field {
  margin: 0 0 16px;
  padding: 0;
  border: 0;
  min-width: 0;
}

.field label,
.field legend {
  display: block;
  margin: 0 0 6px;
  padding: 0;
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 500;
}

.label--sl::before,
.label--tp::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 7px;
  border-radius: 50%;
  vertical-align: 0.05em;
}

.label--sl::before {
  background: var(--loss);
}

.label--tp::before {
  background: var(--profit);
}

input[type="text"],
select {
  display: block;
  width: 100%;
  height: 48px;
  padding: 0 12px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 1rem; /* 16px keeps iOS from zooming on focus */
  font-variant-numeric: tabular-nums;
  transition: border-color 0.12s;
}

input[type="text"]::placeholder {
  color: #5d6c7e;
}

input[type="text"]:hover,
select:hover {
  border-color: #33445a;
}

input[type="text"]:focus-visible,
select:focus-visible,
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 38px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%238fa0b3' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
}

.input-wrap {
  position: relative;
}

.input-wrap--prefix::before {
  content: attr(data-prefix);
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

.input-wrap--prefix input {
  padding-left: 26px;
}

.field__hint {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
}

.field__error {
  margin: 6px 0 0;
  color: var(--loss);
  font-size: 0.8125rem;
}

input[aria-invalid="true"] {
  border-color: var(--loss);
}

/* ---- Direction ---- */

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.segmented input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  pointer-events: none;
}

.field .seg {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 48px;
  margin: 0;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.field .seg--buy {
  border-radius: 8px 0 0 8px;
}

.field .seg--sell {
  border-radius: 0 8px 8px 0;
  margin-left: -1px;
}

.segmented input:checked + .seg--buy {
  background: rgba(56, 211, 159, 0.14);
  border-color: var(--profit);
  color: var(--profit);
  position: relative;
  z-index: 1;
}

.segmented input:checked + .seg--sell {
  background: rgba(244, 112, 124, 0.14);
  border-color: var(--loss);
  color: var(--loss);
  position: relative;
  z-index: 1;
}

.segmented input:focus-visible + .seg {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  z-index: 2;
}

/* ---- Buttons ---- */

.btn {
  display: block;
  width: 100%;
  height: 48px;
  margin-top: 4px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn:hover {
  background: var(--surface-2);
}

/* ---- Results ---- */

.status {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.875rem;
}

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

.tile {
  min-width: 0;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 3px solid var(--line);
  border-radius: 12px;
}

.tile--loss {
  border-top-color: var(--loss);
}

.tile--profit {
  border-top-color: var(--profit);
}

.tile--size {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 12px;
  border-top-width: 1px;
  padding: 12px 14px;
}

.tile__label {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 600;
}

.tile--size .tile__label {
  margin: 0;
}

.tile__dist {
  margin: 0 0 4px;
  color: var(--text);
  font-size: 0.9375rem;
  overflow-wrap: anywhere;
}

.tile__money,
.tile__value {
  margin: 0;
  font-size: clamp(1.25rem, 5.4vw, 1.875rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.tile__money--loss {
  color: var(--loss);
}

.tile__money--profit {
  color: var(--profit);
}

.tile__size {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.is-empty .tile__money,
.is-empty .tile__value,
.is-empty .tile__dist,
.is-empty .tile__size {
  color: var(--muted);
}

.tile--rr .tile__dist {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
}

/* Risk badge + meter */

.badge {
  display: inline-block;
  margin: 8px 0 0;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 0.8125rem;
  font-weight: 600;
  border: 1px solid currentColor;
}

[data-status="low"] .badge {
  color: var(--profit);
  background: rgba(56, 211, 159, 0.12);
}

[data-status="moderate"] .badge {
  color: var(--warn);
  background: rgba(242, 184, 75, 0.12);
}

[data-status="high"] .badge {
  color: var(--loss);
  background: rgba(244, 112, 124, 0.12);
}

.tile--risk {
  border-top-color: var(--line);
}

[data-status="low"] .tile--risk {
  border-top-color: var(--profit);
}

[data-status="moderate"] .tile--risk {
  border-top-color: var(--warn);
}

[data-status="high"] .tile--risk {
  border-top-color: var(--loss);
}

.meter {
  margin-top: 12px;
}

.meter__track {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  height: 8px;
}

.meter__zone {
  display: block;
  height: 100%;
}

.meter__zone--low {
  background: rgba(56, 211, 159, 0.55);
  border-radius: 4px 0 0 4px;
}

.meter__zone--mod {
  background: rgba(242, 184, 75, 0.55);
}

.meter__zone--high {
  background: rgba(244, 112, 124, 0.55);
  border-radius: 0 4px 4px 0;
}

.meter__marker {
  position: absolute;
  top: -5px;
  left: 0;
  width: 4px;
  height: 18px;
  margin-left: -2px;
  background: var(--text);
  border-radius: 2px;
  box-shadow: 0 0 0 2px var(--surface);
}

.meter__labels {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.6875rem;
}

.meter__labels span:nth-child(2) {
  text-align: center;
}

.meter__labels span:nth-child(3) {
  text-align: right;
}

/* Warnings + notes */

.warnings {
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 8px;
}

.warnings li {
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left-width: 3px;
  border-radius: 8px;
  font-size: 0.875rem;
}

.warnings .is-warn {
  border-left-color: var(--warn);
  background: rgba(242, 184, 75, 0.08);
}

.warnings .is-danger {
  border-left-color: var(--loss);
  background: rgba(244, 112, 124, 0.1);
}

.warnings .is-info {
  border-left-color: var(--accent);
  background: rgba(110, 168, 255, 0.08);
}

.basis {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
}

.footer {
  margin-top: 28px;
  color: var(--muted);
  font-size: 0.8125rem;
}

.footer p {
  margin: 0;
  max-width: 62ch;
}

/* ---- Wider screens: form left, results right ---- */

@media (min-width: 860px) {
  .app {
    padding-top: 40px;
  }

  .layout {
    grid-template-columns: minmax(340px, 400px) minmax(0, 1fr);
    align-items: start;
    gap: 24px;
  }

  .results {
    position: sticky;
    top: 24px;
  }

  .tile {
    padding: 18px;
  }

  .tile__money,
  .tile__value {
    font-size: 2rem;
  }
}

@media (max-width: 340px) {
  .tiles {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
