:root {
  --teal: #1a7f8c;
  --teal-dark: #12626d;
  --ink: #1d2b34;
  --muted: #5d6f7a;
  --line: #d7e0e5;
  --bg: #eef3f5;
  --card: #ffffff;
  --danger: #b3352b;
  --warn: #9a6700;
  --ok: #1f7a45;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(16, 42, 54, 0.12);
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.45;
}

a { color: var(--teal-dark); }

h1, h2, h3 { margin: 0 0 12px; line-height: 1.25; }
h1 { font-size: 24px; }
h2 { font-size: 19px; }
h3 { font-size: 16px; }

#topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  padding: 10px 22px;
  position: sticky;
  top: 0;
  z-index: 20;
}
#topbar[hidden] { display: none; }
#topbar .brand { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--teal-dark); }
#topbar nav { display: flex; gap: 4px; flex: 1; }
#topbar nav a {
  text-decoration: none;
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 6px;
  font-weight: 500;
}
#topbar nav a:hover { background: var(--bg); color: var(--ink); }
#topbar nav a.active { background: #e3f1f3; color: var(--teal-dark); }
#topbar .userbox { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 14px; }

main { max-width: 1120px; margin: 0 auto; padding: 24px 20px 60px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: var(--teal);
  color: #fff;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--teal-dark); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-ghost { background: #fff; color: var(--teal-dark); border-color: var(--line); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #922a22; }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-block { width: 100%; }
.mt-16 { margin-top: 16px; }
.mt-18 { margin-top: 18px; }
.mt-20 { margin-top: 20px; }
.mt-22 { margin-top: 22px; }
.amend-note { background: #fff4d6; padding: 10px; border-radius: 6px; margin-bottom: 16px; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }

label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  color: var(--ink);
  background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #9fd3da; border-color: var(--teal); }
textarea { resize: vertical; min-height: 64px; }
.field { margin-bottom: 14px; }
.grid { display: grid; gap: 14px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.hint { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.req { color: var(--danger); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); background: #f6f9fa; }
tbody tr:hover { background: #f7fafb; }
.row-link { cursor: pointer; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; text-transform: capitalize; }
.badge-draft { background: #fff4d6; color: var(--warn); }
.badge-finalized { background: #e0f3e7; color: var(--ok); }
.badge-cancelled { background: #fbe3e0; color: var(--danger); }

.empty { padding: 28px 12px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); margin-bottom: 4px; }

.searchbar { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.searchbar .field { flex: 1; min-width: 240px; margin: 0; }

.patient-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; align-items: flex-start; }
.patient-id { color: var(--muted); font-size: 14px; }
.detail-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.detail-list dt { font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: 0.03em; }
.detail-list dd { margin: 2px 0 0; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 380px; }
.login-card .brand { justify-content: center; font-size: 20px; margin-bottom: 6px; }
.login-card p.sub { text-align: center; color: var(--muted); margin: 0 0 20px; font-size: 13.5px; }

.medicine-row { border: 1px solid var(--line); border-radius: 8px; padding: 14px; margin-bottom: 12px; background: #fbfdfd; }
.medicine-row .row-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.medicine-row .row-head strong { font-size: 13.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.medicine-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; }
.medicine-grid .wide { grid-column: span 2; }
.medicine-grid .full { grid-column: 1 / -1; }

.med-field { position: relative; }
.med-suggest {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 30;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 8px 20px rgba(16, 42, 54, 0.16);
  max-height: 260px;
  overflow: auto;
}
.med-suggest[hidden] { display: none; }
.med-opt {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  border-bottom: 1px solid #eef3f5;
  background: #fff;
  padding: 8px 10px;
  font: inherit;
  cursor: pointer;
}
.med-opt:last-child { border-bottom: none; }
.med-opt:hover, .med-opt.active { background: #eef7f8; }
.med-opt .hint { display: block; }

.has-suggest { position: relative; }
.field-suggest {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 29;
  min-width: 200px;
  max-width: calc(100vw - 48px);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 8px 20px rgba(16, 42, 54, 0.16);
  max-height: 240px;
  overflow: auto;
}
.field-suggest[hidden] { display: none; }
.field-opt {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  border-bottom: 1px solid #eef3f5;
  background: #fff;
  padding: 8px 10px;
  font: inherit;
  cursor: pointer;
}
.field-opt:last-child { border-bottom: none; }
.field-opt:hover, .field-opt.active { background: #eef7f8; }
.field-opt-main { display: block; font-weight: 600; }
.field-opt-urdu {
  display: block;
  direction: rtl;
  text-align: right;
  font-family: "Urdu Typesetting", "Jameel Noori Nastaleeq", "Noto Nastaliq Urdu", "Arabic Typesetting", serif;
  line-height: 1.8;
  color: var(--teal-dark);
  margin-top: 2px;
}
.field-opt .hint { display: block; }

.urdu-input {
  direction: rtl;
  text-align: right;
  font-family: "Urdu Typesetting", "Jameel Noori Nastaleeq", "Noto Nastaliq Urdu", "Arabic Typesetting", serif;
  line-height: 1.8;
  min-height: 64px;
  resize: vertical;
}
.urdu-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 7px; }
.urdu-status { flex: 1; min-width: 200px; color: var(--muted); font-size: 12.5px; line-height: 1.45; }
.urdu-status.loading { color: var(--teal-dark); }
.urdu-status.ok { color: #1d7a4d; }
.urdu-status.warn { color: #a35300; }
.qty-hint { color: var(--teal-dark); }

.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--teal-dark); margin: 22px 0 10px; border-bottom: 1px solid var(--line); padding-bottom: 6px; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px dashed var(--line); font-size: 14px; }
.timeline li:last-child { border-bottom: none; }
.timeline .meta { color: var(--muted); font-size: 12.5px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(12, 32, 40, 0.45);
  display: grid; place-items: center; padding: 20px; z-index: 50;
}
.modal { background: #fff; border-radius: 10px; max-width: 560px; width: 100%; padding: 22px; box-shadow: 0 12px 32px rgba(0,0,0,0.2); max-height: 90vh; overflow: auto; }
.modal h3 { margin-top: 0; }
.modal .actions { justify-content: flex-end; margin-top: 18px; }

.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 60;
  background: #20323c; color: #fff; padding: 10px 16px; border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.25); max-width: 420px; font-size: 14px;
}
.toast.error { background: var(--danger); }

.dupe-list { margin: 10px 0; padding-left: 18px; }
.dupe-list li { margin-bottom: 6px; }

.statusbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.statusbar .spacer { flex: 1; }

.rx-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }

.split { display: grid; grid-template-columns: 3fr 2fr; gap: 20px; align-items: start; }

.mt-12 { margin-top: 12px; }

.note-field.ai-accepted textarea { border-color: #8bc4a1; box-shadow: inset 3px 0 0 #8bc4a1; }
.note-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.note-head label { margin-bottom: 0; }
.note-tools { display: flex; gap: 4px; flex-wrap: wrap; }
.note-tools .btn { padding: 2px 8px; font-size: 12px; }
.ai-tag {
  display: inline-block; padding: 1px 7px; border-radius: 9px;
  background: #e6f2fb; color: #15618f; font-size: 11px; font-weight: 600;
}
.note-panel {
  margin-top: 8px; padding: 10px; border: 1px solid var(--line);
  border-radius: 6px; background: #f8fbfc;
}
.note-panel[hidden] { display: none; }
.note-panel-title { font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 8px; }
.note-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.note-chip {
  border: 1px solid #bcdde2; background: #fff; color: var(--teal-dark);
  border-radius: 14px; padding: 4px 10px; font: inherit; font-size: 12.5px; cursor: pointer;
}
.note-chip:hover { background: #e3f1f3; }
.note-prompts { margin-top: 8px; }
.note-finding { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; padding: 5px 0; border-bottom: 1px dashed var(--line); }
.note-finding:last-of-type { border-bottom: none; }
.note-finding-name { font-size: 13px; }
.note-states { display: flex; gap: 4px; flex-wrap: wrap; }
.note-state {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 12px; padding: 2px 9px; font: inherit; font-size: 12px; cursor: pointer;
}
.note-state:hover { border-color: var(--teal); }
.note-state.selected { background: var(--teal); border-color: var(--teal); color: #fff; }

.ai-draft { border-left: 3px solid #15618f; padding-left: 10px; }
.ai-badge {
  display: inline-block; background: #e6f2fb; color: #15618f;
  border-radius: 4px; padding: 2px 8px; font-size: 11.5px; font-weight: 600; margin-bottom: 6px;
}
.ai-text { white-space: pre-line; font-size: 14px; background: #fff; border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; }
.ai-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.ai-error { color: var(--danger); font-size: 13px; }
.ai-empty, .ai-accepted-note { color: var(--muted); font-size: 13px; }

@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; }
  .medicine-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .medicine-grid .wide { grid-column: span 2; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 620px) {
  main { padding: 16px 12px 48px; }
  .grid-2, .grid-3, .grid-4, .medicine-grid { grid-template-columns: 1fr; }
  .medicine-grid .wide { grid-column: span 1; }
  #topbar { flex-wrap: wrap; gap: 8px 16px; padding: 8px 12px; }
  #topbar nav { order: 3; flex-basis: 100%; }
}
