:root {
  --teal: #0f766e;
  --teal-dark: #0b5f58;
  --teal-soft: #e3f2f0;
  --ink: #17212b;
  --ink-soft: #5b6773;
  --line: #e4e8ec;
  --bg: #f4f6f8;
  --card: #ffffff;
  --amber: #b45309;
  --amber-soft: #fdf1de;
  --green: #117a4a;
  --green-soft: #e2f5ea;
  --red: #b02a2a;
  --red-soft: #fdecec;
  --blue: #1d4ed8;
  --blue-soft: #e8eefc;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(23, 33, 43, .06), 0 6px 18px rgba(23, 33, 43, .06);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  font-family: "Segoe UI", Tahoma, "Noto Kufi Arabic", "Noto Naskh Arabic", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.55;
  padding-bottom: env(safe-area-inset-bottom);
}

[hidden] { display: none !important; }

button, input { font-family: inherit; font-size: inherit; }

h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }

.muted { color: var(--ink-soft); margin: 2px 0 0; }
.small { font-size: .87rem; }
.center { text-align: center; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: linear-gradient(135deg, var(--teal), var(--teal-dark));
  color: #fff;
  box-shadow: 0 2px 10px rgba(15, 118, 110, .25);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand-mark {
  width: 38px; height: 38px; flex: 0 0 38px;
  display: grid; place-items: center;
  border-radius: 11px; background: rgba(255, 255, 255, .18);
  font-size: 1.5rem; font-weight: 700; line-height: 1;
}

.brand-text { min-width: 0; }
.clinic-name { font-weight: 700; font-size: 1.06rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.clinic-sub { font-size: .78rem; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sync-button {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 13px; border-radius: 999px; border: 1px solid rgba(255, 255, 255, .35);
  background: rgba(255, 255, 255, .12); color: #fff; font-size: .86rem; cursor: pointer;
}
.sync-button:active { background: rgba(255, 255, 255, .24); }
.sync-icon { display: none; }

@media (max-width: 420px) {
  .topbar { padding-left: 10px; padding-right: 10px; gap: 8px; }
  .brand { gap: 8px; }
  .brand-mark { width: 32px; height: 32px; flex-basis: 32px; font-size: 1.3rem; border-radius: 9px; }
  .clinic-name { font-size: .98rem; }
  .clinic-sub { font-size: .72rem; }
  .sync-button { padding: 6px 10px; }
  .sync-button #sync-label { display: none; }
  .sync-icon { display: inline; font-size: 1.05rem; }
  .field-row { grid-template-columns: minmax(0, 1fr); }
  .info-row { flex-direction: column; gap: 1px; }
  .info-row b { text-align: start; }
}

.dot { width: 9px; height: 9px; border-radius: 50%; background: #fbbf24; }
.dot.ok { background: #4ade80; }
.dot.busy { background: #fff; animation: pulse 1s infinite; }
.dot.off { background: #f87171; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .25; } }

/* ------------------------------------------------------------------ views */
main { max-width: 680px; margin: 0 auto; }

.view { padding: 16px 14px 96px; animation: fade .18s ease; }
@keyframes fade { from { opacity: .4; } to { opacity: 1; } }

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

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

/* ------------------------------------------------------------------ setup */
.setup-hero { text-align: center; padding: 26px 8px 20px; }

.hero-mark {
  display: inline-grid; place-items: center;
  width: 66px; height: 66px; border-radius: 20px;
  background: linear-gradient(135deg, var(--teal), var(--teal-dark));
  color: #fff; font-size: 2.4rem; font-weight: 700; line-height: 1;
  box-shadow: 0 10px 24px rgba(15, 118, 110, .3);
}

.setup-hero h1 { margin-top: 14px; }
.setup-hero p { color: var(--ink-soft); max-width: 34ch; margin: 8px auto 0; font-size: .95rem; }

.form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.field { display: grid; gap: 6px; font-size: .92rem; color: var(--ink-soft); }
.field input, .field select, .field textarea {
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 13px;
  background: #fbfcfd; color: var(--ink); font-size: 1rem;
  width: 100%; min-width: 0;
}
.field textarea { font-family: inherit; font-size: .88rem; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--teal); outline-offset: 1px; background: #fff; }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }

.paste-link { display: grid; gap: 10px; border-top: 1px solid var(--line); padding-top: 12px; }
.paste-link summary { cursor: pointer; font-size: .88rem; color: var(--teal-dark); }

.hint { color: var(--ink-soft); font-size: .82rem; margin: 0; }
.hint.error { color: var(--red); background: var(--red-soft); padding: 9px 11px; border-radius: 10px; }

.btn {
  border: 0; border-radius: 12px; padding: 12px 16px;
  font-weight: 600; cursor: pointer; background: #eef1f4; color: var(--ink);
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--teal); color: #fff; }
.btn-primary:active { background: var(--teal-dark); }
.btn-ghost { background: var(--teal-soft); color: var(--teal-dark); }
.btn-danger { background: var(--red-soft); color: var(--red); }
.btn-wide { width: 100%; }
.btn-small { padding: 8px 12px; font-size: .85rem; white-space: nowrap; }
.btn:disabled { opacity: .55; }
.book-cta { margin-bottom: 12px; }

/* ------------------------------------------------------------------- lock */
.view-lock { display: grid; place-items: center; min-height: 72vh; }
.lock-box { width: 100%; max-width: 360px; text-align: center; display: grid; gap: 10px; }
.lock-mark { font-size: 2.2rem; }
.pin-input {
  width: 100%; text-align: center; letter-spacing: .5em; font-size: 1.5rem;
  padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: #fbfcfd;
  margin: 8px 0;
}
.pin-input:focus { outline: 2px solid var(--teal); outline-offset: 1px; }

/* ------------------------------------------------------------------ today */
.pills { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.pill {
  font-size: .78rem; padding: 4px 10px; border-radius: 999px;
  background: var(--teal-soft); color: var(--teal-dark); font-weight: 600;
}
.pill.amber { background: var(--amber-soft); color: var(--amber); }
.pill.green { background: var(--green-soft); color: var(--green); }

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

.appt {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 12px 14px; display: grid; grid-template-columns: 72px minmax(0, 1fr) auto;
  gap: 10px; align-items: center; box-shadow: var(--shadow); cursor: pointer;
}
.appt:active { background: #f8fafb; }
.appt-time { font-weight: 700; color: var(--teal-dark); font-size: .95rem; white-space: nowrap; }
.appt-time small { display: block; font-weight: 500; color: var(--ink-soft); font-size: .7rem; white-space: normal; }
.appt-main { min-width: 0; }
.appt-name {
  font-weight: 600; overflow: hidden;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.appt-meta { color: var(--ink-soft); font-size: .8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chip { font-size: .75rem; font-weight: 600; padding: 4px 9px; border-radius: 999px; white-space: nowrap; }
.chip.wait { background: var(--amber-soft); color: var(--amber); }
.chip.arrived { background: var(--blue-soft); color: var(--blue); }
.chip.done { background: var(--green-soft); color: var(--green); }
.chip.cancel { background: #eef1f4; color: var(--ink-soft); }

.appt-summary {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--teal-soft); border-radius: 12px; padding: 10px 12px;
}
.appt-summary > div { min-width: 0; }
.appt-summary b { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.appt-summary small { display: block; color: var(--ink-soft); font-size: .82rem; }
.appt-actions { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }

.section-title { margin: 20px 4px 10px; font-size: .95rem; color: var(--ink-soft); font-weight: 600; }

/* ---------------------------------------------------------------- patients */
.search-box { margin-bottom: 12px; }
.search-box input {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: #fff; font-size: 1rem;
}
.search-box input:focus { outline: 2px solid var(--teal); outline-offset: 1px; }

.person {
  display: flex; align-items: center; gap: 12px; cursor: pointer;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 11px 13px; box-shadow: var(--shadow);
}
.person:active { background: #f8fafb; }
.avatar {
  width: 42px; height: 42px; flex: 0 0 42px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700;
  background: var(--teal-soft); color: var(--teal-dark);
}
.person-main { min-width: 0; flex: 1; }
.person-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.person-meta { color: var(--ink-soft); font-size: .8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.empty { text-align: center; color: var(--ink-soft); padding: 34px 10px; font-size: .95rem; }

/* ------------------------------------------------------------ patient file */
.link-back {
  border: 0; background: none; color: var(--teal-dark); font-weight: 600;
  padding: 4px 2px 10px; cursor: pointer; font-size: .95rem;
}

.patient-card { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; margin-bottom: 12px; }
.patient-head { display: flex; align-items: center; gap: 12px; }
.patient-head .avatar { width: 52px; height: 52px; flex-basis: 52px; font-size: 1.15rem; }
.patient-title { min-width: 0; }
.patient-title h2 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.patient-title p { margin: 2px 0 0; color: var(--ink-soft); font-size: .84rem; }

.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge { font-size: .76rem; padding: 3px 9px; border-radius: 999px; background: #eef1f4; color: var(--ink-soft); }
.badge.warn { background: var(--red-soft); color: var(--red); }
.badge.info { background: var(--blue-soft); color: var(--blue); }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; background: #e9edf1; border-radius: 12px; padding: 4px; margin-bottom: 12px; }
.segmented button {
  border: 0; background: none; padding: 9px 4px; border-radius: 9px; font-weight: 600;
  color: var(--ink-soft); cursor: pointer;
}
.segmented button.active { background: #fff; color: var(--teal-dark); box-shadow: 0 1px 3px rgba(23, 33, 43, .12); }

.record { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; box-shadow: var(--shadow); }
.record-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.record-date { font-weight: 700; font-size: .9rem; color: var(--teal-dark); }
.record-kind { font-size: .78rem; color: var(--ink-soft); }
.record-line { margin-top: 6px; font-size: .92rem; }
.record-line b { font-weight: 600; color: var(--ink); }
.vitals { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.vital { background: #f2f5f7; border-radius: 8px; padding: 3px 8px; font-size: .76rem; color: var(--ink-soft); }
.vital b { color: var(--ink); font-weight: 600; }

.rx { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); display: grid; gap: 4px; }
.rx-item { font-size: .87rem; }
.rx-item span { color: var(--ink-soft); font-size: .8rem; }

.money-row { display: flex; justify-content: space-between; font-size: .9rem; margin-top: 6px; }
.money-row b { font-weight: 700; }
.record-foot { margin-top: 8px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tiny { font-size: .78rem; color: var(--ink-soft); }

/* ---------------------------------------------------------------- settings */
.info-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; margin-bottom: 12px; }
.info-row { display: flex; justify-content: space-between; gap: 12px; font-size: .9rem; }
.info-row span { color: var(--ink-soft); }
.info-row b { font-weight: 600; text-align: left; word-break: break-all; }
.stack { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; margin-bottom: 12px; }
.stack h3 { color: var(--ink); }

/* ----------------------------------------------------------------- tab bar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; justify-content: space-around;
  background: rgba(255, 255, 255, .96);
  border-top: 1px solid var(--line);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  backdrop-filter: blur(8px);
}
.tabbar button {
  flex: 1; display: grid; justify-items: center; gap: 1px;
  border: 0; background: none; color: var(--ink-soft);
  font-size: .74rem; font-weight: 600; padding: 5px 0; cursor: pointer;
}
.tabbar button.active { color: var(--teal-dark); }
.tab-icon { font-size: 1.15rem; line-height: 1.2; filter: grayscale(1) opacity(.65); }
.tabbar button.active .tab-icon { filter: none; }

/* ------------------------------------------------------------------- toast */
.toast {
  position: fixed; left: 50%; bottom: calc(76px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #17212b; color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: .86rem; z-index: 40; max-width: 86vw; text-align: center;
  box-shadow: 0 8px 24px rgba(23, 33, 43, .3);
}
.toast.error { background: var(--red); }

/* ------------------------------------------------------------------ dialog */
dialog {
  border: 0; border-radius: var(--radius); padding: 18px;
  width: min(92vw, 360px); max-width: none; margin: auto;
  max-height: 86vh; overflow-x: hidden; overflow-y: auto;
  background: var(--card); color: var(--ink);
  box-shadow: 0 18px 50px rgba(23, 33, 43, .35);
}
dialog::backdrop { background: rgba(23, 33, 43, .45); }
dialog h3 { margin-bottom: 10px; }
.dialog-form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.pin-form .field input { letter-spacing: .3em; text-align: center; }
.picked {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--teal-soft); border-radius: 12px; padding: 9px 12px;
}
.picked-main { min-width: 0; display: grid; }
.picked-main b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.picked-main small { color: var(--ink-soft); direction: ltr; text-align: start; }
.picked .link-back { padding: 0; color: var(--teal-dark); }
.pick-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; max-height: 190px; overflow-y: auto; }
.pick {
  display: grid; text-align: start; gap: 1px; width: 100%;
  border: 1px solid var(--line); border-radius: 11px; background: #fbfcfd;
  padding: 8px 11px; cursor: pointer; color: var(--ink); font: inherit;
}
.pick:active { background: var(--teal-soft); }
.pick small { color: var(--ink-soft); font-size: .78rem; }
.pick-new { border-style: dashed; border-color: var(--teal); background: #fff; }
.pick-new b { color: var(--teal-dark); }
.row-buttons { display: flex; gap: 8px; }
.row-buttons .btn { flex: 1; }
.meds { display: grid; gap: 8px; border-top: 1px solid var(--line); padding-top: 10px; }
.meds-head { display: flex; align-items: center; justify-content: space-between; }
.meds-head .link-back { padding: 0; color: var(--teal-dark); }
.med-row {
  display: grid; gap: 8px; padding: 10px; border: 1px solid var(--line);
  border-radius: 12px; background: #fbfcfd;
}
.med-row .link-back { justify-self: start; color: var(--danger, #b42318); }
.visit-form textarea { min-height: 44px; }

