:root {
  color-scheme: light;
  --bg: #fafaf9;
  --surface: #ffffff;
  --ink: #0c0a09;
  --muted: #78716c;
  --soft: #e7e5e4;
  --line: #d6d3d1;
  --blue: #3ba6f1;
  --blue-ink: #075985;
  --green: #16a34a;
  --amber: #b45309;
  --red: #dc2626;
  --shadow: 0 14px 40px rgba(12, 10, 9, 0.08);
  font-family: Inter, "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
  scroll-behavior: smooth;
}

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(59, 166, 241, 0.08), rgba(250, 250, 249, 0) 280px),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(16px, 4vw, 44px);
  background: rgba(250, 250, 249, 0.88);
  border-bottom: 1px solid rgba(214, 211, 209, 0.72);
  backdrop-filter: blur(16px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: max-content;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  color: #fff;
  font-weight: 800;
  background: var(--blue);
  border-radius: 50%;
}

.brand strong,
.rail-heading strong,
.result-toolbar strong {
  display: block;
  line-height: 1.1;
}

.brand small,
.rail-heading span,
.result-toolbar span,
.kicker {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.patient-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.patient-strip label {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 10px;
  background: var(--surface);
  border: 1px solid var(--soft);
  border-radius: 999px;
}

.patient-strip label span,
.patient-strip label b {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.patient-strip input {
  width: 72px;
  color: var(--ink);
  font-weight: 800;
  text-align: right;
  background: transparent;
  border: 0;
  outline: 0;
}

.segmented {
  display: grid;
  grid-auto-flow: column;
  gap: 2px;
  padding: 3px;
  background: var(--surface);
  border: 1px solid var(--soft);
  border-radius: 999px;
}

.segmented button,
.category-tabs button,
.tool-list button,
.result-toolbar button,
.mobile-tabs select {
  border: 1px solid transparent;
}

.segmented button {
  min-width: 42px;
  height: 32px;
  color: var(--muted);
  font-weight: 800;
  background: transparent;
  border-radius: 999px;
}

.segmented button.is-active {
  color: #fff;
  background: var(--ink);
}

.context-flags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

.context-flags button {
  min-height: 34px;
  padding: 0 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  background: var(--surface);
  border: 1px solid var(--soft);
  border-radius: 999px;
}

.context-flags button.is-active {
  color: #fff;
  background: var(--amber);
  border-color: var(--amber);
}

.context-flags button[data-flag="cpr"].is-active,
.context-flags button[data-flag="shock"].is-active {
  background: var(--red);
  border-color: var(--red);
}

.context-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  min-height: 34px;
  margin: -4px 0 18px;
}

.context-summary span {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--soft);
  border-radius: 999px;
}

.context-summary .is-alert {
  color: #991b1b;
  background: #fef2f2;
  border-color: #fecaca;
}

.context-summary .is-caution {
  color: #92400e;
  background: #fffbeb;
  border-color: #fde68a;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(230px, 280px) minmax(0, 1fr);
  gap: clamp(18px, 3vw, 34px);
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: clamp(18px, 4vw, 42px);
}

.tool-rail {
  align-self: start;
  position: sticky;
  top: 86px;
}

.rail-heading {
  margin-bottom: 14px;
}

.quick-tools {
  display: grid;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--soft);
}

.quick-tools section {
  display: grid;
  gap: 7px;
}

.quick-tools section > span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.quick-tools div {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.quick-tools button {
  min-height: 32px;
  padding: 0 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  background: #fff;
  border: 1px solid var(--soft);
  border-radius: 999px;
}

.quick-tools button.is-active {
  color: #fff;
  background: var(--ink);
  border-color: var(--ink);
}

.category-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.category-tabs button {
  min-height: 34px;
  padding: 0 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  background: var(--surface);
  border-color: var(--soft);
  border-radius: 999px;
}

.category-tabs button.is-active {
  color: #fff;
  background: var(--blue);
  border-color: var(--blue);
}

.tool-list {
  display: grid;
  gap: 8px;
}

.tool-list button {
  display: grid;
  gap: 5px;
  width: 100%;
  min-height: 72px;
  padding: 12px;
  color: var(--ink);
  text-align: left;
  background: rgba(255, 255, 255, 0.72);
  border-color: var(--soft);
  border-radius: 8px;
}

.tool-list button.is-active {
  border-color: var(--blue);
  box-shadow: inset 3px 0 0 var(--blue);
}

.tool-list strong {
  font-size: 15px;
  line-height: 1.25;
}

.tool-list span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.calculator {
  min-width: 0;
}

.calculator-head {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--soft);
}

.calculator-head h1 {
  margin: 5px 0 8px;
  font-size: clamp(28px, 4vw, 48px);
  line-height: 1.04;
}

.calculator-head p {
  max-width: 720px;
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.7;
}

.tool-badges {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  justify-content: flex-end;
  gap: 8px;
}

.tool-badges span,
.pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  color: var(--blue-ink);
  font-size: 12px;
  font-weight: 800;
  background: rgba(59, 166, 241, 0.12);
  border: 1px solid rgba(59, 166, 241, 0.24);
  border-radius: 999px;
}

.mobile-tabs {
  display: none;
  margin-bottom: 14px;
}

.mobile-tabs select {
  width: 100%;
  min-height: 44px;
  padding: 0 14px;
  color: var(--ink);
  font-weight: 800;
  background: var(--surface);
  border-color: var(--line);
  border-radius: 8px;
}

.calculator-grid {
  display: grid;
  grid-template-columns: minmax(280px, 410px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.calculator-grid.no-inputs {
  grid-template-columns: minmax(0, 1fr);
}

.input-panel,
.result-panel {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--soft);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.input-panel {
  display: grid;
  gap: 16px;
  padding: 16px;
}

.input-panel.is-empty {
  display: none;
}

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

.field label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
}

.field label small {
  color: var(--muted);
  font-weight: 700;
}

.field input,
.field select {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: 0;
}

.field input:focus,
.field select:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(59, 166, 241, 0.16);
}

.field input[type="range"] {
  min-height: 28px;
  padding: 0;
  accent-color: var(--blue);
  border: 0;
}

.slider-field {
  padding: 12px;
  background: #fff;
  border: 1px solid var(--soft);
  border-radius: 8px;
}

.slider-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 86px;
  gap: 10px;
  align-items: center;
}

.range-wrap {
  position: relative;
  min-width: 0;
  padding-top: 24px;
}

.slider-control input[type="range"] {
  width: 100%;
  height: 8px;
  min-height: 8px;
  background:
    linear-gradient(90deg, var(--blue) 0 var(--range-progress), #3a3a3a var(--range-progress) 100%);
  border-radius: 999px;
  cursor: pointer;
}

.slider-bubble {
  position: absolute;
  top: 0;
  left: clamp(22px, var(--range-progress), calc(100% - 22px));
  z-index: 1;
  min-width: 44px;
  padding: 3px 8px;
  border-radius: 999px;
  color: #fff;
  font-size: 0.72rem;
  font-weight: 900;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  background: #111;
  box-shadow: 0 8px 18px rgba(17, 17, 17, 0.16);
  transform: translateX(-50%);
  transition: left 0.08s linear;
}

.slider-number {
  min-height: 38px;
  padding: 0 8px;
  font-weight: 900;
  text-align: center;
  background: #f8fafc;
}

.range-value {
  color: var(--blue-ink);
  font-variant-numeric: tabular-nums;
}

.toggle-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 52px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.toggle-field strong {
  display: block;
  font-size: 13px;
}

.toggle-field span {
  color: var(--muted);
  font-size: 12px;
}

.switch {
  position: relative;
  flex: 0 0 auto;
  width: 48px;
  height: 28px;
  background: var(--line);
  border: 0;
  border-radius: 999px;
}

.switch::after {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 20px;
  height: 20px;
  content: "";
  background: #fff;
  border-radius: 50%;
  transition: transform 0.18s ease;
}

.switch[aria-checked="true"] {
  background: var(--blue);
}

.switch[aria-checked="true"]::after {
  transform: translateX(20px);
}

.result-panel {
  min-height: 420px;
  padding: 16px;
}

.result-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.result-toolbar button {
  min-height: 34px;
  padding: 0 14px;
  color: var(--muted);
  font-weight: 800;
  background: #fff;
  border-color: var(--line);
  border-radius: 999px;
}

.warnings {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.order-snippets {
  margin-bottom: 14px;
}

.order-copy {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 46px;
  padding: 9px 12px;
  text-align: left;
  background: #f8fafc;
  border: 1px solid #dbeafe;
  border-radius: 8px;
}

.order-copy span {
  color: #fff;
  min-width: max-content;
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 900;
  background: var(--ink);
  border-radius: 999px;
}

.order-copy strong {
  overflow: hidden;
  color: var(--blue-ink);
  font-size: 13px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.order-copy small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.warning {
  padding: 10px 12px;
  color: #7c2d12;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.5;
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-radius: 8px;
}

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

.metric {
  min-height: 98px;
  padding: 13px;
  background: #fff;
  border: 1px solid var(--soft);
  border-radius: 8px;
}

.metric.is-primary {
  color: #fff;
  background: var(--ink);
  border-color: var(--ink);
}

.metric.is-blue {
  color: #fff;
  background: var(--blue);
  border-color: var(--blue);
}

.metric span {
  display: block;
  color: inherit;
  opacity: 0.72;
  font-size: 12px;
  font-weight: 800;
  line-height: 1.35;
}

.metric strong {
  display: block;
  margin-top: 7px;
  font-size: clamp(24px, 4vw, 38px);
  line-height: 1;
}

.metric small {
  display: block;
  margin-top: 5px;
  color: inherit;
  opacity: 0.72;
  font-size: 12px;
  font-weight: 800;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 18px 0 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--soft);
}

.section-title h2 {
  margin: 0;
  font-size: 16px;
}

.result-table {
  width: 100%;
  overflow: hidden;
  border: 1px solid var(--soft);
  border-radius: 8px;
}

.stat-title h2 {
  color: #991b1b;
}

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

.stat-card {
  min-height: 126px;
  padding: 14px;
  color: #fff;
  background: #991b1b;
  border: 1px solid #7f1d1d;
  border-radius: 8px;
  box-shadow: 0 10px 24px rgba(153, 27, 27, 0.16);
}

.stat-card span {
  display: block;
  font-size: 13px;
  font-weight: 900;
  line-height: 1.35;
}

.stat-card strong {
  display: block;
  margin-top: 8px;
  font-size: clamp(28px, 5vw, 46px);
  line-height: 1;
}

.stat-card small {
  display: block;
  margin-top: 9px;
  color: rgba(255, 255, 255, 0.78);
  font-size: 12px;
  font-weight: 800;
  line-height: 1.45;
}

.result-row {
  display: grid;
  grid-template-columns: minmax(140px, 1.1fr) minmax(120px, 0.9fr) minmax(160px, 1.2fr);
  gap: 10px;
  align-items: center;
  min-height: 52px;
  padding: 10px 12px;
  background: #fff;
  border-bottom: 1px solid var(--soft);
}

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

.result-row b {
  line-height: 1.35;
}

.result-row strong {
  font-size: 20px;
}

.result-row span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.clinical-note {
  margin-top: 18px;
  padding: 13px;
  color: var(--muted);
  background: rgba(250, 250, 249, 0.9);
  border: 1px solid var(--soft);
  border-radius: 8px;
}

.clinical-note strong {
  color: var(--ink);
}

.clinical-note p {
  margin: 6px 0 0;
  font-size: 13px;
  line-height: 1.65;
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
    gap: 14px;
    padding-top: 0;
  }

  .tool-rail {
    position: sticky;
    top: var(--sticky-tools-top, 68px);
    z-index: 9;
    display: grid;
    gap: 5px;
    margin: 0 calc(clamp(18px, 4vw, 42px) * -1);
    padding: 6px clamp(18px, 4vw, 42px);
    background: rgba(250, 250, 249, 0.94);
    border-bottom: 1px solid rgba(214, 211, 209, 0.72);
    box-shadow: 0 10px 24px rgba(12, 10, 9, 0.06);
    backdrop-filter: blur(16px);
  }

  .rail-heading {
    display: none;
  }

  .quick-tools {
    display: flex;
    gap: 8px;
    margin: 0;
    padding: 0 0 2px;
    overflow-x: auto;
    border-bottom: 0;
    scrollbar-width: none;
  }

  .quick-tools::-webkit-scrollbar {
    display: none;
  }

  .quick-tools section {
    display: contents;
  }

  .quick-tools section > span {
    display: none;
  }

  .quick-tools div {
    display: flex;
    flex: 0 0 auto;
    flex-wrap: nowrap;
    gap: 6px;
  }

  .quick-tools button {
    min-height: 30px;
    max-width: 104px;
    padding: 0 8px;
    overflow: hidden;
    font-size: 11px;
    line-height: 1;
    text-overflow: ellipsis;
    white-space: nowrap;
    box-shadow: 0 4px 12px rgba(12, 10, 9, 0.04);
  }

  .tool-list {
    display: none;
  }

  .category-tabs {
    display: none;
  }

  .mobile-tabs {
    display: block;
    margin-bottom: 0;
  }

  .mobile-tabs select {
    min-height: 32px;
    padding: 0 10px;
    font-size: 12px;
    border-radius: 999px;
  }

  .calculator-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 680px) {
  .topbar {
    position: static;
    align-items: stretch;
    flex-direction: column;
  }

  .tool-rail {
    top: 0;
  }

  .patient-strip {
    width: 100%;
    justify-content: flex-start;
  }

  .patient-strip label {
    flex: 1;
  }

  .patient-strip input {
    width: 100%;
  }

  .segmented {
    flex: 0 0 auto;
  }

  .context-flags {
    width: 100%;
    justify-content: flex-start;
  }

  .calculator-head {
    flex-direction: column;
  }

  .tool-badges {
    justify-content: flex-start;
  }

  .metric-grid {
    grid-template-columns: 1fr;
  }

  .stat-grid {
    grid-template-columns: 1fr;
  }

  .order-copy {
    grid-template-columns: 1fr;
  }

  .order-copy strong {
    white-space: normal;
  }

  .slider-control {
    grid-template-columns: 1fr;
  }

  .slider-number {
    width: 100%;
  }

  .result-row {
    grid-template-columns: 1fr;
  }
}
