:root {
  color-scheme: light;
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-soft: #eef3f1;
  --ink: #1f2a2a;
  --muted: #667370;
  --line: #d9e1de;
  --teal: #0f766e;
  --teal-dark: #0b4f4a;
  --gold: #b7791f;
  --red: #b42318;
  --green: #287a3e;
  --blue: #2867a0;
  --shadow: 0 18px 45px rgba(31, 42, 42, 0.09);
  --font-sans: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, "Roboto", "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  border-right: 1px solid var(--line);
  background: #fbfcfb;
  padding: 24px 18px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

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

.brand h1,
.topbar h2,
.panel h3 {
  margin: 0;
}

.brand h1 {
  font-size: 1.12rem;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--teal-dark);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.muted {
  color: var(--muted);
}

.search-field {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

.search-field input,
.search-field select,
.entry-form input,
.patient-form input,
.patient-form select,
.note-form textarea,
.note-form input[type="file"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 11px 12px;
  outline: none;
}

.search-field input:focus,
.search-field select:focus,
.entry-form input:focus,
.patient-form input:focus,
.patient-form select:focus,
.note-form textarea:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.db-status {
  margin: -8px 0 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--teal-dark);
  padding: 9px 10px;
  font-size: 0.82rem;
  font-weight: 800;
}

.db-status.offline {
  background: #fff8e8;
  color: #8a540d;
}

.patient-form {
  display: grid;
  gap: 9px;
  margin-bottom: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px;
}

.patient-form strong {
  font-size: 0.92rem;
}

.form-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 24px;
}

.form-title-inline {
  justify-content: flex-end;
}

.form-step-status {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--teal-dark);
  padding: 4px 8px;
  font-size: 0.74rem;
  font-weight: 900;
  white-space: nowrap;
}

.form-page {
  display: grid;
  gap: 9px;
}

.form-page[hidden] {
  display: none;
}

.patient-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 800;
}

.patient-list {
  display: grid;
  gap: 8px;
  margin-bottom: 18px;
}

.patient-item {
  display: grid;
  gap: 6px;
  width: 100%;
  min-height: 76px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px;
  color: var(--ink);
  text-align: left;
}

.patient-item.active {
  border-color: var(--teal);
  background: var(--surface-soft);
}

.patient-item strong {
  font-size: 0.98rem;
}

.patient-item span {
  color: var(--muted);
  font-size: 0.82rem;
}

.workspace {
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

.topbar h2 {
  font-size: clamp(1.45rem, 2vw, 2.2rem);
}

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

.risk-pill {
  min-width: 172px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  padding: 10px 14px;
  color: var(--teal-dark);
  font-weight: 800;
  text-align: center;
  white-space: nowrap;
}

.risk-pill.high {
  border-color: rgba(180, 35, 24, 0.28);
  background: #fff0ee;
  color: var(--red);
}

.risk-pill.medium {
  border-color: rgba(183, 121, 31, 0.32);
  background: #fff8e8;
  color: #8a540d;
}

.risk-pill.low {
  border-color: rgba(40, 122, 62, 0.28);
  background: #edf8f0;
  color: var(--green);
}

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

.metric-box,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.metric-box {
  display: grid;
  gap: 8px;
  min-height: 118px;
  padding: 16px;
}

.metric-box p {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 800;
}

.metric-box strong {
  font-size: clamp(1.35rem, 2vw, 2rem);
  line-height: 1.1;
}

.metric-box span {
  color: var(--muted);
  font-size: 0.85rem;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(320px, 0.8fr);
  gap: 18px;
  align-items: start;
}

.panel {
  min-width: 0;
  padding: 18px;
}

.chart-panel,
.lifecycle-panel,
.standards-panel {
  grid-column: 1;
}

.alert-panel,
.threshold-panel,
.input-panel,
.quality-panel,
.history-panel,
.notes-panel,
.risk-history-panel,
.report-panel {
  grid-column: 2;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.panel h3 {
  font-size: 1.1rem;
}

.segmented {
  display: inline-grid;
  grid-template-columns: repeat(3, minmax(74px, 1fr));
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.segment {
  border: 0;
  border-right: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  min-height: 38px;
  padding: 8px 10px;
  font-weight: 800;
}

.segment:last-child {
  border-right: 0;
}

.segment.active {
  background: var(--teal);
  color: #fff;
}

.chart-wrap {
  width: 100%;
  aspect-ratio: 16 / 7;
  min-height: 250px;
}

#trendCanvas {
  display: block;
  width: 100%;
  height: 100%;
}

.score {
  display: grid;
  min-width: 52px;
  min-height: 52px;
  place-items: center;
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--teal-dark);
  font-size: 1.3rem;
}

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

.alert-item {
  border-left: 4px solid var(--teal);
  border-radius: 8px;
  background: #f8faf9;
  padding: 12px;
}

.alert-item.high {
  border-left-color: var(--red);
}

.alert-item.medium {
  border-left-color: var(--gold);
}

.alert-item.low {
  border-left-color: var(--green);
}

.alert-item strong {
  display: block;
  margin-bottom: 5px;
}

.alert-item p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

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

.entry-form > .form-title-row,
.entry-form > .form-page,
.entry-form > .form-pager,
.entry-form > .form-actions {
  grid-column: 1 / -1;
}

.entry-form > .form-page {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.entry-form label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 800;
}

.form-wide {
  grid-column: 1 / -1;
}

.form-pager {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
}

.form-pager .secondary-action {
  width: auto;
  flex: 0 0 auto;
}

.form-step-dots {
  display: flex;
  flex: 1;
  justify-content: center;
  gap: 6px;
}

.form-step {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  padding: 0;
  font-size: 0.78rem;
  font-weight: 900;
}

.form-step.active {
  border-color: var(--teal);
  background: var(--teal);
  color: #fff;
}

.form-step.complete {
  border-color: rgba(15, 118, 110, 0.4);
  color: var(--teal-dark);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

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

.patient-form .form-actions .primary-action,
.patient-form .form-actions .secondary-action {
  width: 100%;
}

.entry-form .form-actions .primary-action {
  min-width: 180px;
}

.primary-action,
.secondary-action,
.danger-action {
  border: 1px solid transparent;
  border-radius: 8px;
  min-height: 42px;
  padding: 10px 14px;
  font-weight: 800;
}

.primary-action {
  background: var(--teal);
  color: #fff;
}

.primary-action:hover,
.segment.active:hover {
  background: var(--teal-dark);
}

.secondary-action {
  width: 100%;
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
}

.secondary-action:hover {
  border-color: var(--teal);
  color: var(--teal-dark);
}

.danger-action {
  border-color: rgba(180, 35, 24, 0.28);
  background: #fff0ee;
  color: var(--red);
}

.danger-action:hover {
  border-color: var(--red);
  background: #ffe4e0;
}

.danger-action:disabled,
.primary-action:disabled,
.secondary-action:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.compact {
  width: auto;
  min-height: 36px;
  padding: 8px 11px;
}

.button-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.hidden {
  display: none !important;
}

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

.summary-grid,
.threshold-grid,
.standards-grid {
  display: grid;
  gap: 10px;
}

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

.quality-item,
.summary-item,
.threshold-item,
.standard-item {
  min-height: 96px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fbfcfb;
}

.quality-item span,
.summary-item span,
.threshold-item span,
.standard-item span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  line-height: 1.35;
}

.quality-item strong,
.summary-item strong,
.threshold-item strong,
.standard-item strong {
  display: block;
  margin-top: 8px;
  font-size: 1.45rem;
}

.threshold-item strong {
  font-size: 1.02rem;
  line-height: 1.35;
}

.standard-item {
  min-height: 78px;
}

.standard-item strong {
  font-size: 1rem;
  line-height: 1.35;
}

.standard-item em,
.threshold-item em {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.82rem;
  font-style: normal;
  line-height: 1.4;
}

.standard-section {
  display: grid;
  gap: 8px;
}

.standard-section h4 {
  margin: 0;
  font-size: 0.95rem;
}

.history-list {
  display: grid;
  gap: 9px;
}

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

.note-form textarea {
  min-height: 96px;
  resize: vertical;
}

.notes-list {
  display: grid;
  gap: 9px;
}

.note-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
  padding: 11px;
}

.note-item p {
  margin: 0 0 7px;
  line-height: 1.45;
}

.note-item span {
  color: var(--muted);
  font-size: 0.82rem;
}

.history-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
  padding: 10px;
}

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

.history-item span {
  color: var(--muted);
  font-size: 0.82rem;
}

.empty-note {
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  padding: 12px;
  line-height: 1.45;
}

.report-summary {
  display: grid;
  gap: 10px;
  line-height: 1.5;
}

.report-summary h4 {
  margin: 0;
  font-size: 1rem;
}

.report-summary ul {
  margin: 0;
  padding-left: 18px;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  transform: translateY(12px);
  opacity: 0;
  max-width: min(360px, calc(100vw - 40px));
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  transition: opacity 160ms ease, transform 160ms ease;
  pointer-events: none;
}

.toast.visible {
  transform: translateY(0);
  opacity: 1;
}

@media (max-width: 1120px) {
  .app-shell {
    grid-template-columns: 260px minmax(0, 1fr);
  }

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

  .chart-panel,
  .lifecycle-panel,
  .standards-panel,
  .alert-panel,
  .threshold-panel,
  .input-panel,
  .quality-panel,
  .history-panel,
  .notes-panel,
  .risk-history-panel,
  .report-panel {
    grid-column: 1;
  }
}

@media (max-width: 780px) {
  .app-shell {
    display: block;
  }

  .sidebar {
    position: static;
    height: auto;
  }

  .workspace {
    padding: 18px 14px 28px;
  }

  .topbar,
  .panel-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .top-actions {
    width: 100%;
    justify-content: stretch;
  }

  .risk-pill {
    width: 100%;
  }

  .top-actions .danger-action {
    width: 100%;
  }

  .metric-grid,
  .summary-grid,
  .threshold-grid,
  .quality-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .segmented {
    width: 100%;
  }

  .chart-wrap {
    aspect-ratio: 4 / 3;
  }
}

@media (max-width: 480px) {
  .metric-grid,
  .entry-form,
  .entry-form > .form-page,
  .summary-grid,
  .threshold-grid,
  .quality-grid {
    grid-template-columns: 1fr;
  }

  .form-pager {
    align-items: stretch;
    flex-wrap: wrap;
  }

  .form-step-dots {
    order: -1;
    flex-basis: 100%;
  }
}
