:root {
  --bg: #0b0f16;
  --surface: #131a25;
  --surface-2: #1a2230;
  --border: #242e3f;
  --border-2: #2f3a4e;
  --text: #e6edf6;
  --muted: #8a97ac;
  --muted-2: #5d6b81;
  --accent: #6366f1;
  --accent-2: #818cf8;
  --green: #10b981;
  --yellow: #f59e0b;
  --red: #ef4444;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 0 rgba(255,255,255,0.04) inset, 0 1px 2px rgba(0,0,0,0.3);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* HEADER */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  background: rgba(19, 26, 37, 0.6);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 32px; height: 32px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center;
  font-weight: 700;
  font-size: 13px;
  color: white;
  letter-spacing: -0.02em;
}
h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 15px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
h3 { font-size: 13px; font-weight: 600; margin: 0 0 12px 0; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
nav { display: flex; gap: 2px; }
nav button {
  background: transparent;
  color: var(--muted);
  border: 0;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
}
nav button:hover { color: var(--text); background: var(--surface); }
nav button.active { color: var(--text); background: var(--surface-2); }

main { max-width: 1200px; margin: 0 auto; padding: 24px 28px 60px; }
.tab { display: none; }
.tab.active { display: block; }

/* CARDS */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

/* CALCULATOR GRID */
.calc-grid {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 20px;
}
@media (max-width: 860px) {
  .calc-grid { grid-template-columns: 1fr; }
}

.results { display: flex; flex-direction: column; gap: 20px; }

/* FIELDS */
.field { margin-bottom: 14px; }
.field label, .toggle-row label.switch {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 500;
}
.field .micro { display: block; margin-top: 6px; }
input[type=text], input[type=number] {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 16px; /* prevents iOS auto-zoom on focus */
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
  font-variant-numeric: tabular-nums;
}
@media (min-width: 768px) {
  input[type=text], input[type=number] { font-size: 14px; padding: 9px 12px; }
}
input[type=text]::placeholder, input[type=number]::placeholder { color: var(--muted-2); }
input[type=text]:focus, input[type=number]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }

.prefix-wrap, .suffix-wrap { position: relative; }
.prefix-wrap .prefix, .suffix-wrap .suffix {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}
.prefix-wrap .prefix { left: 12px; }
.prefix-wrap input { padding-left: 24px; }
.suffix-wrap .suffix { right: 12px; }
.suffix-wrap input { padding-right: 28px; }

hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

/* TOGGLE ROWS */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.toggle-row .switch {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  margin: 0;
}
.switch input { display: none; }
.switch span {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  background: var(--border-2);
  border-radius: 999px;
  transition: background 0.2s;
}
.switch span::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.toggle-input {
  width: 110px;
  position: relative;
  transition: opacity 0.2s;
}
.toggle-input.disabled { opacity: 0.35; pointer-events: none; }
.toggle-input input {
  padding-right: 26px;
  text-align: right;
}
.toggle-input .suffix {
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
}

/* DETAILS */
details {
  margin-top: 6px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
details summary {
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  user-select: none;
  margin-bottom: 14px;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
}
details summary::-webkit-details-marker { display: none; }
details summary::before {
  content: "▶";
  font-size: 9px;
  transition: transform 0.15s;
}
details[open] summary::before { transform: rotate(90deg); }

.checkrow {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.checkrow input[type=checkbox] {
  width: 16px; height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}
.help-tip {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 16px; height: 16px;
  border: 1px solid var(--border-2);
  border-radius: 50%;
  font-size: 10px;
  color: var(--muted);
  cursor: help;
  margin-left: auto;
  transition: color 0.15s, border-color 0.15s;
}
.help-tip:hover { color: var(--text); border-color: var(--accent); }
.help-tip::before,
.help-tip::after {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
  z-index: 100;
}
.help-tip::before {
  content: attr(data-tip);
  bottom: calc(100% + 10px);
  right: -6px;
  width: max-content;
  max-width: 260px;
  background: #0a0e15;
  color: var(--text);
  border: 1px solid var(--border-2);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.4;
  font-weight: 400;
  text-align: left;
  white-space: normal;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  transform: translateY(4px);
}
.help-tip::after {
  content: "";
  bottom: calc(100% + 4px);
  right: 2px;
  border: 6px solid transparent;
  border-top-color: var(--border-2);
  transform: translateY(4px);
}
.help-tip:hover::before,
.help-tip:hover::after,
.help-tip:focus::before,
.help-tip:focus::after {
  opacity: 1;
  transform: translateY(0);
}

/* BUTTONS */
button {
  background: var(--accent);
  color: white;
  border: 0;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  transition: all 0.15s;
}
button:hover { background: var(--accent-2); }
button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border-2);
}
button.ghost:hover { background: var(--surface-2); color: var(--text); }
button.sm { padding: 5px 10px; font-size: 12px; }
button.danger { background: var(--red); }
button.danger:hover { background: #f87171; }
button.icon {
  padding: 6px 8px;
  background: transparent;
  color: var(--muted);
}
button.icon:hover { background: var(--surface-2); color: var(--text); }

.save-row { margin-top: 18px; display: flex; gap: 8px; }

/* RESULTS - HEADLINE */
.headline {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
  background: linear-gradient(135deg, var(--surface) 0%, var(--surface-2) 100%);
}
.headline-left { display: flex; flex-direction: column; gap: 2px; }
.headline-left .label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.headline-value {
  font-size: 44px;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.headline-sub {
  font-size: 18px;
  color: var(--muted);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

/* STATUS */
.status-big {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border: 1px solid var(--border-2);
  background: var(--surface);
}
.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--muted-2);
  box-shadow: 0 0 0 3px rgba(0,0,0,0.2);
}
.status-big.GREEN { border-color: rgba(16,185,129,0.3); background: rgba(16,185,129,0.08); color: var(--green); }
.status-big.GREEN .status-dot { background: var(--green); box-shadow: 0 0 12px rgba(16,185,129,0.5); }
.status-big.YELLOW { border-color: rgba(245,158,11,0.3); background: rgba(245,158,11,0.08); color: var(--yellow); }
.status-big.YELLOW .status-dot { background: var(--yellow); box-shadow: 0 0 12px rgba(245,158,11,0.5); }
.status-big.RED { border-color: rgba(239,68,68,0.3); background: rgba(239,68,68,0.08); color: var(--red); }
.status-big.RED .status-dot { background: var(--red); box-shadow: 0 0 12px rgba(239,68,68,0.5); }

/* BREAKDOWN */
.breakdown-table { width: 100%; border-collapse: collapse; }
.breakdown-table td {
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.breakdown-table tr:last-child td { border-bottom: 0; }
.breakdown-table td:first-child { color: var(--muted); }
.breakdown-table td:nth-child(2) { text-align: right; color: var(--muted); width: 80px; }
.breakdown-table td:last-child { text-align: right; font-weight: 600; width: 120px; }
.breakdown-table tr.total td {
  padding-top: 14px;
  border-top: 1px solid var(--border-2);
  border-bottom: 0;
  color: var(--text);
  font-weight: 600;
}
.breakdown-table tr.total td:last-child { font-size: 15px; }
.breakdown-table tr.subtract td:first-child::before {
  content: "−";
  margin-right: 6px;
  color: var(--red);
  opacity: 0.7;
}
.breakdown-table tr.base td:first-child::before {
  content: "+";
  margin-right: 6px;
  color: var(--green);
  opacity: 0.7;
}
.breakdown-table tr.muted td { color: var(--muted-2); }
.breakdown-table tr.muted td:last-child { color: var(--muted-2); font-weight: 400; }
.breakdown-table tr.opcost td:first-child { color: var(--muted-2); }
.breakdown-table tr.opcost td:first-child::before { color: var(--yellow); opacity: 0.8; }
.breakdown-table tr.total.net td { color: var(--accent-2); border-top-color: rgba(99,102,241,0.3); }

/* SEGMENTED CONTROL */
.segmented {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 2px;
  width: 100%;
}
.segmented button {
  flex: 1;
  background: transparent;
  color: var(--muted);
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 4px;
  transition: all 0.15s;
}
.segmented button:hover { color: var(--text); }
.segmented button.active {
  background: var(--accent);
  color: #fff;
}

/* SUB FIELD (indented) */
.sub-field {
  padding-left: 0;
  transition: opacity 0.2s;
}
.sub-field.disabled { opacity: 0.35; pointer-events: none; }

/* PARTNERS INPUT */
.partners-input {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.partner-chip {
  display: grid;
  grid-template-columns: 28px 1fr 90px;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s;
}
.partner-chip.active { border-color: var(--accent); background: rgba(99, 102, 241, 0.06); }
.partner-chip input[type=checkbox] {
  width: 16px; height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}
.partner-chip .pname {
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.02em;
}
.partner-chip .suffix-wrap input { padding: 6px 24px 6px 10px; font-size: 13px; }
.partner-chip.inactive .suffix-wrap { opacity: 0.35; pointer-events: none; }
.partner-sum { margin-top: 8px; }
.partner-sum.bad b { color: var(--red); }
.partner-sum.good b { color: var(--green); }

/* PARTNER SPLITS */
.partner-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}
.partner-cards:empty::before {
  content: "No partners selected";
  color: var(--muted-2);
  font-size: 13px;
  grid-column: 1 / -1;
  text-align: center;
  padding: 14px;
}
.partner {
  padding: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.partner .label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}
.partner-value {
  font-size: 24px;
  font-weight: 700;
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.partner .micro { margin-top: 2px; display: block; }

/* SCENARIOS */
.scenarios-list { display: flex; flex-direction: column; gap: 8px; }
.scenario-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 16px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s;
}
.scenario-row:hover { border-color: var(--border-2); }
.scenario-row .info { min-width: 0; }
.scenario-row .name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; }
.scenario-row .meta { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.scenario-row .stats-line { display: flex; align-items: center; gap: 14px; }
.scenario-row .margin { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.scenario-row .status-big { padding: 4px 10px; font-size: 11px; }
.scenario-row .actions { display: flex; gap: 6px; }

/* SETTINGS */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
}

/* MISC */
.micro { font-size: 12px; color: var(--muted); }
.micro b { color: var(--text); font-weight: 600; }

/* OPERATING COSTS */
.opcosts-list { display: flex; flex-direction: column; gap: 6px; }
.opcost-row {
  display: grid;
  grid-template-columns: 1fr 130px 32px;
  gap: 8px;
  align-items: center;
}
.opcost-row input[type=text] { padding: 8px 10px; font-size: 13px; }
@media (min-width: 768px) { .opcost-row input[type=text] { font-size: 13px; } }
.opcost-row .prefix-wrap input { padding: 8px 10px 8px 22px; font-size: 13px; }
@media (min-width: 768px) { .opcost-row .prefix-wrap input { font-size: 13px; } }
.opcost-row .prefix-wrap .prefix { left: 10px; }
.opcost-row button {
  padding: 8px 0;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
}
.opcost-row button:hover { background: rgba(239,68,68,0.1); color: var(--red); }
.opcosts-list:empty::before {
  content: "No operating costs added.";
  color: var(--muted-2);
  font-size: 12px;
  padding: 6px 0;
}

/* NET MARGIN CARD */
.net-card {
  background: linear-gradient(135deg, var(--surface) 0%, rgba(99,102,241,0.05) 100%);
  border-color: rgba(99,102,241,0.25);
}
.net-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}
.net-head .label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.headline-value.small { font-size: 32px; margin-top: 2px; }
@media (max-width: 680px) { .headline-value.small { font-size: 28px; } }
.opcost-total { text-align: right; }
.opcost-value {
  font-size: 18px;
  font-weight: 600;
  margin-top: 2px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* SPLITS HEADER + BASIS TOGGLE */
.splits-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.splits-head h3 { margin-bottom: 0; }
.split-basis-toggle {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
  gap: 2px;
}
.split-basis-toggle button {
  background: transparent;
  color: var(--muted);
  padding: 4px 12px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-radius: 4px;
}
.split-basis-toggle button:hover { color: var(--text); }
.split-basis-toggle button.active { background: var(--accent); color: #fff; }

/* TOAST */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--surface-2);
  border: 1px solid var(--border-2);
  color: var(--text);
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  opacity: 0;
  transition: all 0.25s;
  pointer-events: none;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===================== MOBILE ===================== */
@media (max-width: 680px) {
  header {
    padding: 14px 16px 12px;
    gap: 14px;
    flex-wrap: wrap;
    row-gap: 12px;
  }
  .brand { gap: 12px; }
  .logo { width: 30px; height: 30px; font-size: 12px; border-radius: 7px; }
  h1 { font-size: 16px; }
  nav {
    order: 3;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    white-space: nowrap;
    gap: 4px;
    padding-top: 4px;
    border-top: 1px solid var(--border);
    margin: 0 -16px;
    padding-left: 16px;
    padding-right: 16px;
  }
  nav::-webkit-scrollbar { display: none; }
  nav button {
    padding: 9px 14px;
    font-size: 13px;
    flex-shrink: 0;
  }

  main { padding: 14px 14px 80px; }

  .card { padding: 16px; border-radius: 10px; }
  .card-head { margin-bottom: 14px; }
  h2 { font-size: 14px; }
  h3 { font-size: 12px; }

  .calc-grid { gap: 14px; }

  /* Headline: stack vertically on mobile */
  .headline {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  .headline-value { font-size: 36px; }
  .headline-sub { font-size: 16px; }
  .status-big {
    align-self: stretch;
    justify-content: center;
    padding: 8px 14px;
    font-size: 12px;
  }

  /* Breakdown table: tighter */
  .breakdown-table td { padding: 8px 0; font-size: 13px; }
  .breakdown-table td:nth-child(2) { width: 64px; font-size: 12px; }
  .breakdown-table td:last-child { width: 100px; }
  .breakdown-table tr.total td:last-child { font-size: 14px; }

  /* Partner splits: single column on very narrow, 2 on small */
  .partner-cards { grid-template-columns: 1fr 1fr; gap: 10px; }
  .partner { padding: 12px; }
  .partner-value { font-size: 20px; }

  /* Toggle rows: bigger touch targets */
  .toggle-row { margin-bottom: 12px; }
  .switch span { width: 40px; height: 22px; }
  .switch span::after { width: 18px; height: 18px; top: 2px; left: 2px; }
  .switch input:checked + span::after { transform: translateX(18px); }
  .toggle-input { width: 100px; }

  /* Segmented control: smaller text on mobile */
  .segmented button { padding: 8px 6px; font-size: 11px; }

  /* Partner chips */
  .partner-chip {
    grid-template-columns: 24px 1fr 80px;
    padding: 10px 12px;
    gap: 10px;
  }

  /* Scenarios: flat stack on mobile */
  .scenario-row {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 14px;
  }
  .scenario-row > div:nth-child(1) { order: 1; }
  .scenario-row .stats-line {
    order: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }
  .scenario-row .margin { font-size: 14px; }
  .scenario-row .status-big { padding: 4px 10px; font-size: 11px; }
  .scenario-row .actions { order: 3; justify-content: flex-end; }
  .scenario-row .actions button { flex: 1; max-width: 160px; }

  /* Settings grid single column */
  .settings-grid { grid-template-columns: 1fr; gap: 12px; }

  /* Tooltip: narrower, flip if near edge */
  .help-tip::before { max-width: 200px; right: auto; left: -6px; }
  .help-tip::after { right: auto; left: 2px; }

  .save-row button { flex: 1; }
  .save-row { gap: 10px; }

  details summary { margin-bottom: 12px; }

  .toast { bottom: 16px; padding: 10px 16px; font-size: 13px; }
}

/* Very narrow phones */
@media (max-width: 380px) {
  .partner-cards { grid-template-columns: 1fr; }
  .headline-value { font-size: 32px; }
  .toggle-input { width: 88px; }
}
