:root {
  --bg: #f6f7f4;
  --paper: #ffffff;
  --ink: #233033;
  --muted: #627074;
  --line: #d9ded8;
  --teal: #0f766e;
  --teal-soft: #dff3ef;
  --rose: #a33a55;
  --rose-soft: #f8e4ea;
  --amber: #9a6700;
  --amber-soft: #fff2cc;
  --blue: #2563eb;
  --blue-soft: #e7eeff;
  --green: #256f3e;
  --green-soft: #e3f4e8;
  --shadow: 0 16px 40px rgba(37, 48, 51, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button {
  color: inherit;
}

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

.topbar {
  display: grid;
  gap: 14px;
  align-items: start;
  margin-bottom: 14px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--teal);
  font-size: 0.78rem;
  font-weight: 760;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.65rem, 4vw, 2.5rem);
  line-height: 1.05;
}

h2 {
  font-size: 1.25rem;
  margin-bottom: 14px;
}

h3 {
  font-size: 1rem;
  margin-bottom: 8px;
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
}

.role-badge {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--blue-soft);
  color: #1f4fbc;
  font-size: 0.82rem;
  font-weight: 720;
}

.segmented {
  display: inline-grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  flex: 1 1 260px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}

.segmented[hidden] {
  display: none;
}

.segmented button,
.tabs button,
.filter-pills button,
.metric-chip {
  border: 0;
  background: transparent;
  cursor: pointer;
}

.segmented button {
  min-width: 0;
  min-height: 36px;
  padding: 7px 10px;
  border-radius: 6px;
  color: var(--muted);
}

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

.logout {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--muted);
  text-decoration: none;
}

.tabs {
  position: sticky;
  top: 8px;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(86px, 1fr));
  gap: 4px;
  padding: 4px;
  margin-bottom: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
}

.tabs button {
  min-height: 42px;
  padding: 6px 4px;
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.88rem;
}

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

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

.panel,
.stat,
.note,
.timeline-item,
.medication,
.handoff-hero,
.handoff-card,
.doc-row,
.doc-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: 0 8px 24px rgba(35, 48, 51, 0.05);
}

.panel {
  padding: 4px 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.panel-head {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 12px;
}

.panel-head p,
.muted {
  color: var(--muted);
}

.handoff-hero {
  display: grid;
  gap: 18px;
  margin-bottom: 18px;
  padding: 16px;
}

.handoff-hero h2 {
  margin-bottom: 10px;
  font-size: 1.5rem;
}

.identity-list,
.handoff-metrics,
.drawer-facts,
.source-status {
  margin: 0;
}

.identity-list,
.handoff-metrics,
.source-status {
  display: grid;
  gap: 10px;
}

.identity-list div,
.handoff-metrics div,
.drawer-facts div,
.source-status div {
  display: grid;
  gap: 2px;
}

.identity-list dt,
.handoff-metrics dt,
.drawer-facts dt,
.source-status dt {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 760;
  text-transform: uppercase;
}

.identity-list dd,
.handoff-metrics dd,
.drawer-facts dd,
.source-status dd {
  margin: 0;
}

.handoff-summary {
  margin: 14px 0 0;
  color: var(--muted);
  line-height: 1.45;
}

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

.handoff-metrics div {
  min-height: 92px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.handoff-metrics dd {
  font-size: 1.18rem;
  font-weight: 780;
}

.handoff-metrics span {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.3;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.compact-actions {
  margin-top: 10px;
}

.text-button {
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  font-weight: 720;
}

.text-button:hover,
.text-button:focus-visible {
  border-color: var(--teal);
  color: var(--teal);
}

.handoff-cards {
  display: grid;
  gap: 10px;
}

.handoff-card {
  min-height: 96px;
  padding: 12px;
}

.handoff-card strong {
  display: block;
  margin-bottom: 6px;
}

.handoff-card p {
  margin: 0;
  color: var(--muted);
}

.summary {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
}

.summary strong {
  display: block;
  margin-bottom: 2px;
}

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

.stat {
  min-height: 112px;
  padding: 12px;
}

.stat .label {
  display: flex;
  gap: 7px;
  align-items: center;
  color: var(--muted);
  font-size: 0.84rem;
}

.stat .value {
  margin: 10px 0 2px;
  font-size: 1.45rem;
  font-weight: 780;
}

.stat .sub {
  margin: 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.flag-high,
.flag-low,
.flag-caution {
  border-color: #efc6a7;
  background: var(--amber-soft);
}

.flag-ok {
  border-color: #bedfc7;
  background: var(--green-soft);
}

.timeline {
  display: grid;
  gap: 10px;
}

.timeline-item {
  display: grid;
  grid-template-columns: 84px 1fr;
  gap: 12px;
  padding: 12px;
}

.timeline-item time {
  color: var(--teal);
  font-size: 0.82rem;
  font-weight: 750;
}

.timeline-item p {
  margin: 0;
  color: var(--muted);
}

.meds-grid {
  display: grid;
  gap: 10px;
}

.medication {
  padding: 12px;
}

.medication strong {
  display: block;
  margin-bottom: 4px;
}

.medication p {
  margin-bottom: 6px;
  color: var(--muted);
}

.medication-legacy {
  font-size: 0.84rem;
  line-height: 1.35;
}

.medication-carryover {
  margin-top: 14px;
}

.lab-explanation-panel {
  min-width: 0;
  max-width: 100%;
  margin-top: 14px;
}

.lab-explanation-stack {
  display: grid;
  gap: 14px;
  min-width: 0;
  max-width: 100%;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}

.pill.high {
  border-color: #e7bd72;
  background: var(--amber-soft);
  color: #6e4a00;
}

.pill.low {
  border-color: #d994aa;
  background: var(--rose-soft);
  color: #7a243b;
}

.pill.ok {
  border-color: #a9d8b5;
  background: var(--green-soft);
  color: #1d5c32;
}

.pill.caveat {
  border-color: #bcc7e8;
  background: var(--blue-soft);
  color: #1f4fbc;
}

.help {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--teal);
  cursor: help;
  font-size: 0.82rem;
  font-weight: 760;
}

.hint {
  display: none;
  position: absolute;
  z-index: 25;
  width: min(260px, calc(100vw - 48px));
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  box-shadow: var(--shadow);
  font-size: 0.86rem;
  line-height: 1.35;
}

.with-help {
  position: relative;
}

.with-help:hover .hint,
.with-help:focus-within .hint {
  display: block;
}

.controls {
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
}

.select-row,
.search-row {
  display: grid;
  gap: 6px;
}

.select-row label,
.search-row label {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

select,
input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 8px 10px;
}

.chart-wrap {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.chart-wrap svg {
  display: block;
  width: 100%;
  min-height: 260px;
}

.chart-empty {
  padding: 28px;
  color: var(--muted);
  text-align: center;
}

.metric-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(142px, 1fr));
  gap: 8px;
  padding: 2px 0 8px;
}

.metric-chip {
  width: 100%;
  min-height: 42px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  line-height: 1.2;
  text-align: left;
  white-space: normal;
}

.metric-chip.is-active {
  border-color: var(--teal);
  background: var(--teal-soft);
  color: var(--teal);
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

th,
td {
  padding: 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
}

td {
  font-size: 0.92rem;
}

tr:last-child td {
  border-bottom: 0;
}

.table-note {
  margin: 10px 2px 0;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.35;
}

.filter-pills {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
}

.filter-pills button {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
}

.filter-pills button.is-active {
  border-color: var(--rose);
  background: var(--rose-soft);
  color: var(--rose);
}

.shares-layout {
  margin-bottom: 18px;
}

.share-actions {
  display: grid;
  gap: 8px;
}

.danger-button,
.secondary-button,
.share-form button,
.generated-share button,
.share-inline-form button,
.danger-link {
  min-height: 42px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 760;
}

.danger-button {
  border: 1px solid #d994aa;
  background: var(--rose-soft);
  color: #7a243b;
}

.secondary-button,
.share-inline-form button,
.generated-share button {
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
}

.secondary-button:disabled,
.danger-link:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.share-form,
.share-inline-form {
  display: grid;
  gap: 10px;
}

.share-form label,
.share-inline-form label,
.generated-share label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

.share-form button {
  border: 0;
  background: var(--teal);
  color: #fff;
}

.generated-share {
  display: grid;
  gap: 6px;
  margin-top: 14px;
}

.generated-share > div {
  display: grid;
  gap: 8px;
}

.share-message {
  margin: 10px 0 0;
  color: var(--teal);
  font-weight: 700;
}

.share-list {
  display: grid;
  gap: 12px;
}

.share-card {
  display: grid;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}

.share-card-head {
  display: flex;
  gap: 10px;
  align-items: start;
  justify-content: space-between;
}

.share-card-head strong,
.share-card-head .muted {
  display: block;
}

.share-meta {
  display: grid;
  gap: 8px;
  margin: 0;
}

.share-meta div {
  display: grid;
  gap: 2px;
}

.share-meta dt {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 760;
  text-transform: uppercase;
}

.share-meta dd {
  margin: 0;
}

.danger-link {
  width: max-content;
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid #d994aa;
  background: #fff;
  color: #7a243b;
}

.doc-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
}

.doc-card {
  padding: 8px;
  text-align: left;
  cursor: zoom-in;
}

.doc-card img {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: 6px;
  background: var(--bg);
}

.doc-card strong {
  display: block;
  margin: 8px 0 4px;
  font-size: 0.92rem;
}

.doc-card p {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.doc-dialog {
  width: min(1100px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}

.doc-dialog::backdrop {
  background: rgba(20, 28, 31, 0.72);
}

.dialog-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.dialog-head button {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font-size: 1.3rem;
}

.doc-dialog img {
  display: block;
  width: 100%;
  height: auto;
}

.note {
  padding: 12px;
  color: var(--muted);
}

.source-list {
  display: grid;
  gap: 6px;
  padding-left: 18px;
  color: var(--muted);
}

.source-list a {
  color: var(--blue);
}

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

.source-status dd {
  overflow-wrap: anywhere;
}

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

.doc-row {
  display: grid;
  gap: 2px;
  width: 100%;
  padding: 10px;
  text-align: left;
  cursor: zoom-in;
}

.doc-row span,
.doc-row small {
  color: var(--muted);
  font-size: 0.78rem;
}

.detail-drawer {
  width: min(620px, calc(100vw - 20px));
  max-width: 620px;
  height: calc(100vh - 20px);
  max-height: calc(100vh - 20px);
  margin: 10px 10px 10px auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.detail-drawer::backdrop {
  background: rgba(20, 28, 31, 0.38);
}

.drawer-shell {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100%;
}

.drawer-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.drawer-head button {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font-size: 1.3rem;
}

.drawer-body {
  display: grid;
  align-content: start;
  gap: 16px;
  overflow: auto;
  padding: 14px;
}

.drawer-section {
  display: grid;
  gap: 10px;
}

.drawer-section p {
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.45;
}

.drawer-facts {
  display: grid;
  gap: 10px;
}

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

.drawer-list article {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.drawer-list p {
  margin-top: 6px;
}

.login-body {
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-shell {
  width: min(460px, 100%);
}

.login-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow);
  padding: 24px;
}

.login-panel p {
  color: var(--muted);
}

.login-form {
  display: grid;
  gap: 10px;
  margin-top: 18px;
}

.login-form label {
  font-weight: 720;
}

.login-form button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--teal);
  color: #fff;
  cursor: pointer;
  font-weight: 760;
}

.login-error {
  padding: 10px;
  border: 1px solid #e09cab;
  border-radius: 8px;
  background: var(--rose-soft);
  color: #7a243b !important;
}

.loading-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  padding: 24px;
}

[data-mode="clinical"] .plain-only {
  display: none;
}

@media (min-width: 760px) {
  .app-shell {
    padding: 28px 20px 40px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
    align-items: center;
  }

  .tabs {
    position: static;
    width: max-content;
    min-width: 620px;
    margin-bottom: 18px;
    box-shadow: none;
  }

  .tabs button {
    min-width: 124px;
  }

  .overview-layout {
    grid-template-columns: minmax(0, 1.2fr) minmax(320px, 0.8fr);
  }

  .handoff-hero {
    grid-template-columns: minmax(0, 1.25fr) minmax(360px, 0.75fr);
    align-items: start;
    padding: 20px;
  }

  .identity-list,
  .handoff-cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .source-status {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .stats-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

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

  .controls {
    grid-template-columns: 1fr minmax(220px, 0.35fr);
    align-items: end;
  }

  .shares-layout {
    grid-template-columns: minmax(0, 0.8fr) minmax(360px, 1.2fr);
  }

  .share-actions,
  .generated-share > div,
  .share-inline-form {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: end;
  }

  .share-form {
    grid-template-columns: 140px minmax(180px, 1fr) 210px auto;
    align-items: end;
  }

  .share-meta {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
