:root {
  color-scheme: light;
  --bg: #f4f6f7;
  --surface: #ffffff;
  --surface-alt: #eef3f4;
  --text: #1b1f24;
  --muted: #606a73;
  --line: #d9e0e4;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --focus: #d97706;
  --soft-accent: #e4f4f1;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

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

.app-shell {
  width: 80vw;
  max-width: 1440px;
  margin: 0 auto;
  padding: 24px 0 36px;
}

.topbar {
  display: grid;
  gap: 16px;
  margin-bottom: 18px;
}

h1 {
  margin: 0;
  font-size: 2.1rem;
  line-height: 1.1;
}

.intro-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 0.95fr);
  gap: 18px;
  padding: 16px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(27, 31, 36, 0.04);
}

.intro-summary,
.intro-steps {
  display: grid;
  gap: 10px;
  align-content: start;
}

.intro-summary p {
  margin: 0;
  color: var(--text);
  line-height: 1.65;
}

.intro-warning {
  padding: 10px 12px;
  border: 1px solid #f3d6a2;
  border-radius: 6px;
  background: #fff7ed;
  color: #7c2d12;
  font-weight: 700;
}

.intro-steps strong {
  font-size: 1rem;
}

.intro-steps ol {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 1.35rem;
  color: var(--muted);
  line-height: 1.6;
}

.intro-steps li::marker {
  color: var(--accent-strong);
  font-weight: 700;
}

.status {
  min-height: 28px;
  color: var(--muted);
  text-align: left;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.search-panel,
.patient-panel,
.current-medication,
.ai-prompt-panel,
.results-section {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(27, 31, 36, 0.04);
}

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

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

.patient-panel {
  overflow: hidden;
}

.section-toolbar {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.patient-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  padding: 16px;
}

.allergy-field {
  grid-column: span 2;
}

.field-note {
  color: var(--muted);
  font-size: 0.9rem;
}

.pregnancy-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  padding: 0 16px 16px;
}

.pregnancy-fields[hidden] {
  display: none;
}

.allergy-options {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.allergy-field textarea[hidden] {
  display: none;
}

.checkbox-field {
  display: inline-grid;
  grid-template-columns: 18px auto;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  font-weight: 700;
}

.checkbox-field input {
  width: 18px;
  height: 18px;
  margin: 0;
}

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

.query-row label {
  grid-column: 1 / -1;
}

label,
.field-label,
.section-toolbar strong,
.ai-prompt-toolbar strong,
.prescription-toolbar strong,
.results-toolbar strong {
  font-weight: 700;
}

.field-label {
  display: block;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  outline: none;
}

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

textarea {
  min-height: 140px;
  resize: vertical;
  padding: 12px;
  line-height: 1.5;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px rgba(217, 119, 6, 0.18);
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 6px;
  padding: 0 16px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: var(--accent-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.current-medication {
  display: grid;
  overflow: hidden;
}

.prescription-toolbar {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.ai-prompt-panel {
  display: grid;
  overflow: hidden;
}

.ai-prompt-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

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

.secondary-button {
  background: #6b7280;
}

.secondary-button:hover:not(:disabled) {
  background: #4b5563;
}

#aiPrompt {
  min-height: 360px;
  border: 0;
  border-radius: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.92rem;
}

#aiPrompt:focus {
  border-color: transparent;
  box-shadow: inset 0 0 0 3px rgba(217, 119, 6, 0.18);
}

.prescription-table-wrap {
  overflow-x: auto;
}

.prescription-table {
  width: 100%;
  min-width: 1180px;
  table-layout: fixed;
  border-collapse: collapse;
}

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

.prescription-table th {
  background: var(--surface-alt);
  color: var(--muted);
  font-size: 0.9rem;
  white-space: normal;
}

.prescription-table td {
  line-height: 1.45;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.prescription-table a {
  color: var(--accent-strong);
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.prescription-table a:hover {
  color: var(--focus);
}

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

.prescription-table select,
.prescription-table input {
  min-width: 0;
}

.prescription-table input[type="number"] {
  min-width: 0;
}

.prescription-table .single-dose-input {
  max-width: 82px;
}

.empty-prescription {
  height: 112px;
  color: var(--muted);
  text-align: center;
  vertical-align: middle;
}

.remove-prescription {
  min-width: 64px;
  background: #6b7280;
}

.remove-prescription:hover:not(:disabled) {
  background: #4b5563;
}

.results-toolbar {
  display: grid;
  align-items: stretch;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.results-list {
  display: grid;
  gap: 12px;
  padding: 14px;
}

.pagination {
  display: grid;
  grid-template-columns: minmax(96px, auto) 1fr minmax(96px, auto);
  align-items: center;
  gap: 10px;
  padding: 0 14px 14px;
}

.pagination[hidden] {
  display: none;
}

.pagination span {
  color: var(--muted);
  text-align: center;
  font-weight: 700;
}

.page-actions {
  display: flex;
  justify-content: flex-end;
}

.site-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: center;
  gap: 8px 14px;
  padding: 22px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.4;
}

.site-footer a {
  color: var(--accent-strong);
  font-weight: 700;
  line-height: inherit;
  text-decoration: none;
}

.site-footer a:hover {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.clear-data {
  min-width: 112px;
  background: #6b7280;
}

.clear-data:hover:not(:disabled) {
  background: #4b5563;
}

.result-row {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}

.result-row:hover {
  border-color: #b7c7cb;
  background: #fbfdfd;
}

.result-row input {
  width: 18px;
  height: 18px;
  margin: 0;
}

.result-line {
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.empty-state {
  display: grid;
  min-height: 120px;
  place-items: center;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 760px) {
  .app-shell {
    width: min(100vw - 20px, 760px);
    padding-top: 16px;
  }

  .search-panel {
    grid-template-columns: 1fr;
  }

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

  .allergy-field {
    grid-column: 1 / -1;
  }

  h1 {
    font-size: 1.7rem;
  }

  .intro-panel {
    grid-template-columns: 1fr;
  }

  .prescription-table td {
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .ai-prompt-toolbar {
    grid-template-columns: 1fr;
  }

  .ai-prompt-actions {
    justify-content: stretch;
  }

  .ai-prompt-actions button {
    flex: 1 1 150px;
  }

}

@media (max-width: 420px) {
  .app-shell {
    width: min(100vw - 16px, 760px);
    padding-top: 12px;
  }

  .search-panel,
  .patient-grid,
  .results-list,
  .pagination {
    padding: 12px;
  }

  .intro-panel {
    padding: 12px;
  }

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

  .section-toolbar,
  .ai-prompt-toolbar,
  .prescription-toolbar {
    padding: 12px;
  }

  .pregnancy-fields {
    padding: 0 12px 12px;
  }

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

  .result-row {
    padding: 13px;
  }
}
