:root {
  --bg: #edf3f6;
  --bg-strong: #f8fbfc;
  --surface: rgba(255, 255, 255, 0.9);
  --surface-strong: #ffffff;
  --line: rgba(15, 23, 42, 0.08);
  --line-strong: rgba(15, 23, 42, 0.14);
  --ink: #0f172a;
  --muted: #5b6878;
  --teal: #0f766e;
  --teal-deep: #0b5561;
  --teal-soft: rgba(15, 118, 110, 0.08);
  --amber: #b45309;
  --green: #15803d;
  --red: #dc2626;
  --shadow: 0 18px 44px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(14, 116, 144, 0.14), transparent 26%),
    radial-gradient(circle at top right, rgba(15, 118, 110, 0.12), transparent 24%),
    linear-gradient(180deg, #f8fbfd 0%, #edf2f6 100%);
  font-family: "Avenir Next", "IBM Plex Sans", "Segoe UI", sans-serif;
}

body.modal-open {
  overflow: hidden;
}

a {
  color: inherit;
  text-decoration: none;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1360px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 16px 0 24px;
}

.topbar {
  position: sticky;
  top: 14px;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
  padding: 12px 14px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: rgba(248, 251, 252, 0.88);
  backdrop-filter: blur(16px);
  box-shadow: 0 12px 24px rgba(15, 23, 42, 0.05);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  box-shadow: 0 12px 24px rgba(15, 118, 110, 0.18);
}

.brand-copy {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.brand-copy strong {
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-copy span {
  color: var(--muted);
  font-size: 0.84rem;
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.primary-button,
.ghost-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 160ms ease, border-color 160ms ease, box-shadow 160ms ease;
}

.primary-button {
  background: linear-gradient(135deg, var(--teal) 0%, var(--teal-deep) 100%);
  color: #ffffff;
  box-shadow: 0 12px 26px rgba(15, 118, 110, 0.22);
}

.ghost-button {
  background: rgba(255, 255, 255, 0.8);
  border-color: var(--line);
  color: var(--ink);
}

.ghost-button--small {
  min-height: 36px;
  padding: 0 12px;
}

.primary-button:hover,
.ghost-button:hover {
  transform: translateY(-1px);
}

.primary-button.is-disabled,
.ghost-button.is-disabled {
  opacity: 0.5;
  pointer-events: none;
}

.workspace {
  display: grid;
  gap: 14px;
}

.workspace-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  padding: 4px 4px 2px;
}

.workspace-header h1,
.section-heading h2,
.results-header h2,
.decision-head h3,
.rules-card h3,
.modal-header h2 {
  margin: 0;
  font-size: clamp(1.4rem, 2vw, 2rem);
  line-height: 1.05;
  letter-spacing: -0.03em;
}

.workspace-hint {
  max-width: 32ch;
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.5;
  text-align: right;
}

.eyebrow {
  display: inline-block;
  margin-bottom: 8px;
  color: var(--teal);
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.calculator {
  display: grid;
  grid-template-columns: minmax(340px, 0.92fr) minmax(420px, 1.18fr);
  gap: 14px;
  align-items: start;
}

.results-stack {
  display: grid;
  gap: 10px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--surface);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
}

.input-panel,
.results-panel {
  padding: 22px;
}

.input-panel {
  position: sticky;
  top: 86px;
}

.section-heading {
  margin-bottom: 16px;
}

.section-heading--split {
  margin-top: 22px;
}

.section-heading--compact {
  margin-bottom: 14px;
}

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

.field {
  display: grid;
  gap: 8px;
}

.field span {
  color: var(--muted);
  font-size: 0.88rem;
}

.field input,
.field select {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border-radius: 14px;
  border: 1px solid rgba(15, 23, 42, 0.1);
  background: rgba(255, 255, 255, 0.92);
  color: var(--ink);
}

.field input:focus,
.field select:focus {
  outline: 2px solid rgba(15, 118, 110, 0.14);
  border-color: rgba(15, 118, 110, 0.34);
}

.field--toggle {
  align-content: end;
}

.field--toggle input {
  appearance: none;
  width: 54px;
  min-height: 32px;
  padding: 0;
  border-radius: 999px;
  background: #dbe4ea;
  cursor: pointer;
  position: relative;
  transition: background 160ms ease;
}

.field--toggle input::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: #ffffff;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.16);
  transition: transform 160ms ease;
}

.field--toggle input:checked {
  background: linear-gradient(135deg, var(--teal) 0%, var(--teal-deep) 100%);
}

.field--toggle input:checked::before {
  transform: translateX(22px);
}

.field--conditional.is-hidden {
  display: none;
}

.settings-hint,
.inline-hint,
.inline-error {
  margin: 12px 0 0;
  font-size: 0.9rem;
}

.settings-hint,
.inline-hint {
  color: var(--muted);
}

.inline-error {
  color: var(--red);
}

.results-header,
.decision-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  background: #f1f5f9;
  color: #64748b;
  font-weight: 700;
  white-space: nowrap;
}

.status-badge[data-state="ok"] {
  background: #ecfdf3;
  color: var(--green);
}

.status-badge[data-state="warning"] {
  background: #fff7ed;
  color: var(--amber);
}

.status-badge[data-state="fail"] {
  background: #fef2f2;
  color: var(--red);
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 18px 0;
}

.metric-chip {
  padding: 16px;
  border-radius: 18px;
  border: 1px solid rgba(15, 23, 42, 0.08);
  background: rgba(248, 250, 252, 0.96);
}

.metric-chip span {
  display: block;
  color: var(--muted);
  font-size: 0.74rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.metric-chip strong {
  display: block;
  margin-top: 8px;
  font-size: 1.8rem;
  line-height: 1;
}

.metric-chip[data-state="pass"] {
  background: #ecfdf3;
  border-color: rgba(34, 197, 94, 0.28);
}

.metric-chip[data-state="warn"] {
  background: #fff7ed;
  border-color: rgba(245, 158, 11, 0.24);
}

.metric-chip[data-state="fail"] {
  background: #fef2f2;
  border-color: rgba(220, 38, 38, 0.22);
}

.decision-card,
.rules-card {
  padding: 20px;
  border-radius: 20px;
  background: var(--surface-strong);
  border: 1px solid rgba(15, 23, 42, 0.08);
}

.decision-card p {
  margin: 8px 0 0;
}

.segmented-bar {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0;
  height: 12px;
  margin-top: 16px;
  overflow: hidden;
  border-radius: 999px;
  background: #dbe4ea;
}

.segmented-bar span {
  min-width: 6px;
}

#segment-flap {
  background: #bfd0dc;
}

#segment-ablation {
  background: #f59e0b;
}

#segment-residual {
  background: #10b981;
}

.rules-card {
  margin-top: 16px;
}

.rules-list {
  display: grid;
  gap: 10px;
}

.rule-row {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid rgba(15, 23, 42, 0.06);
}

.rule-row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.rule-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.16);
  color: #64748b;
  font-weight: 700;
}

.rule-row[data-state="pass"] .rule-icon {
  background: rgba(34, 197, 94, 0.16);
  color: var(--green);
}

.rule-row[data-state="warn"] .rule-icon {
  background: rgba(245, 158, 11, 0.16);
  color: var(--amber);
}

.rule-row[data-state="fail"] .rule-icon {
  background: rgba(220, 38, 38, 0.16);
  color: var(--red);
}

.rule-row strong {
  display: block;
}

.rule-threshold {
  display: block;
  margin-top: 4px;
  color: #94a3b8;
  font-size: 0.82rem;
}

.rule-value {
  font-weight: 700;
}

.support-note {
  margin: 0;
  padding: 0 4px;
  color: var(--muted);
  font-size: 0.9rem;
}

.support-note a {
  color: var(--teal);
  font-weight: 700;
}

.modal-shell {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 18px;
}

.modal-shell.is-hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  border: 0;
  background: rgba(15, 23, 42, 0.44);
  cursor: pointer;
}

.modal-card {
  position: relative;
  z-index: 1;
  width: min(720px, 100%);
  padding: 22px;
  border-radius: 24px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 24px 80px rgba(15, 23, 42, 0.22);
}

.modal-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 14px;
}

.modal-body {
  margin-top: 18px;
  color: var(--muted);
  line-height: 1.6;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.info-card {
  padding: 16px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: var(--bg-strong);
}

.info-card strong {
  display: block;
  margin-bottom: 6px;
  color: var(--ink);
}

.info-card p,
.modal-body p {
  margin: 0;
}

.modal-disclaimer {
  margin-top: 16px;
  color: var(--ink);
  font-weight: 600;
}

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

  .input-panel {
    position: static;
  }
}

@media (max-width: 860px) {
  .topbar {
    display: grid;
    grid-template-columns: 1fr;
    align-items: stretch;
    gap: 10px;
  }

  .brand {
    width: 100%;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
    justify-content: stretch;
  }

  .topbar-actions > * {
    width: 100%;
  }
}

@media (max-width: 760px) {
  .app-shell {
    width: min(100vw - 14px, 100%);
    padding: 8px 0 16px;
  }

  .workspace-header,
  .results-header,
  .decision-head,
  .modal-header {
    flex-direction: column;
    align-items: stretch;
  }

  .field-grid,
  .info-grid {
    grid-template-columns: 1fr;
  }

  .topbar {
    position: static;
    top: auto;
    gap: 8px;
    margin-bottom: 8px;
    padding: 10px;
    border-radius: 16px;
  }

  .brand {
    gap: 10px;
    align-items: center;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 12px;
  }

  .brand-copy {
    max-width: 100%;
  }

  .brand-copy strong {
    font-size: 0.94rem;
  }

  .brand-copy span {
    display: none;
  }

  .topbar-actions {
    gap: 8px;
  }

  .topbar-actions > * {
    min-height: 34px;
    padding: 0 10px;
    font-size: 0.82rem;
    line-height: 1.1;
    box-shadow: none;
  }

  .workspace {
    gap: 10px;
  }

  .workspace-header {
    gap: 4px;
    padding: 0 2px;
  }

  .workspace-header h1 {
    font-size: 1.24rem;
  }

  .workspace-hint {
    display: none;
  }

  .panel {
    border-radius: 18px;
  }

  .input-panel,
  .results-panel {
    padding: 16px;
  }

  .section-heading {
    margin-bottom: 12px;
  }

  .section-heading--split {
    margin-top: 16px;
  }

  .field-grid {
    gap: 10px;
  }

  .field input,
  .field select {
    min-height: 44px;
    padding: 0 12px;
  }

  .settings-hint,
  .inline-hint,
  .inline-error {
    margin-top: 10px;
    font-size: 0.84rem;
  }

  .status-badge {
    min-height: 34px;
    padding: 0 12px;
    font-size: 0.88rem;
  }

  .metrics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin: 12px 0;
  }

  .metric-chip {
    padding: 12px;
    border-radius: 16px;
  }

  .metric-chip strong {
    margin-top: 6px;
    font-size: 1.34rem;
  }

  .decision-card,
  .rules-card {
    padding: 16px;
    border-radius: 16px;
  }

  .rules-card {
    margin-top: 12px;
  }

  .rule-row {
    gap: 8px;
    padding: 10px 0;
  }

  .support-note {
    padding: 0 2px;
    font-size: 0.84rem;
  }

  .modal-shell {
    padding: 10px;
  }

  .modal-card {
    padding: 16px;
    border-radius: 18px;
    max-height: calc(100vh - 20px);
    overflow: auto;
  }

  .modal-body {
    margin-top: 12px;
    font-size: 0.92rem;
  }
}

@media (max-width: 460px) {
  .topbar-actions {
    grid-template-columns: 1fr 1fr;
  }

  .topbar-actions > :last-child {
    grid-column: 1 / -1;
  }

  .topbar-actions > * {
    padding: 0 8px;
    font-size: 0.76rem;
  }

  .metric-chip strong {
    font-size: 1.2rem;
  }
}
