/* ClinicFlow Stage 1. Never put overflow-x:hidden on html or body (iOS Safari lesson). */
:root {
  --cf-teal: #0f766e; --cf-teal-dark: #115e59; --cf-teal-soft: #ccfbf1;
  --cf-ink: #0f172a; --cf-muted: #64748b; --cf-line: #e2e8f0; --cf-bg: #f8fafc; --cf-card: #fff;
  --cf-danger: #b91c1c; --cf-warn: #b45309; --cf-ok: #15803d;
  --cf-shadow: 0 6px 20px rgba(15, 23, 42, 0.06); --cf-radius: 14px;
  --cf-font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, "Noto Sans Myanmar", sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body { font-family: var(--cf-font); color: var(--cf-ink); background: var(--cf-bg); min-height: 100vh; }
.boot-fallback, .fatal { max-width: 640px; margin: 3rem auto; padding: 1.5rem; background: #fff; border: 1px solid var(--cf-line); border-radius: var(--cf-radius); }
.fatal pre { white-space: pre-wrap; word-break: break-word; background: #f1f5f9; padding: 0.75rem; border-radius: 8px; }
.demo-banner { position: sticky; top: 0; z-index: 10001; background: #7f1d1d; color: #fff; text-align: center; font-size: 0.82rem; font-weight: 600; padding: 0.45rem 0.75rem; }
.app-shell { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: calc(100vh - 34px); }
.sidebar { background: linear-gradient(180deg, #0f766e 0%, #115e59 60%, #134e4a 100%); color: #ecfeff; padding: 1rem 0.8rem; display: flex; flex-direction: column; gap: 1rem; position: sticky; top: 34px; height: calc(100vh - 34px); overflow-y: auto; }
.brand { display: flex; gap: 0.7rem; align-items: center; }
.brand-mark { width: 42px; height: 42px; border-radius: 12px; background: #fff; color: var(--cf-teal); display: grid; place-items: center; font-weight: 800; flex: none; }
.brand.dark .brand-mark { background: var(--cf-teal); color: #fff; }
.brand strong { display: block; font-size: 1.05rem; }
.brand span { display: block; font-size: 0.78rem; opacity: 0.85; }
.side-nav { display: flex; flex-direction: column; gap: 0.2rem; flex: 1; }
.side-nav button { -webkit-appearance: none; appearance: none; border: 0; background: transparent; color: inherit; text-align: left; padding: 0.65rem 0.8rem; border-radius: 10px; cursor: pointer; font: inherit; font-weight: 600; font-size: 0.93rem; min-height: 44px; }
.side-nav button:hover, .side-nav button.active { background: rgba(255,255,255,0.16); }
.side-foot { display: grid; gap: 0.5rem; border-top: 1px solid rgba(255,255,255,0.2); padding-top: 0.6rem; }
.who strong { display: block; font-size: 0.85rem; }
.who span { display: block; font-size: 0.75rem; opacity: 0.85; }
.main-wrap { display: flex; flex-direction: column; min-width: 0; }
.app-top { display: flex; align-items: center; gap: 0.75rem; padding: 0.9rem 1.25rem 0.5rem; position: sticky; top: 34px; background: rgba(248,250,252,0.95); z-index: 5; }
.app-top h1 { margin: 0; font-size: 1.3rem; flex: 1; min-width: 0; }
.top-user { font-size: 0.8rem; color: var(--cf-muted); }
.menu-btn { display: none; }
.view { padding: 0.5rem 1.25rem 2.5rem; min-width: 0; }
.stack { display: grid; gap: 1rem; min-width: 0; }
.grid { display: grid; gap: 1rem; }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.split { display: grid; grid-template-columns: 1.2fr 1fr; gap: 1rem; }
.card { background: var(--cf-card); border: 1px solid var(--cf-line); border-radius: var(--cf-radius); box-shadow: var(--cf-shadow); padding: 1rem; min-width: 0; }
.card h2 { margin: 0 0 0.75rem; font-size: 1rem; }
.card.error { border-color: #fecaca; background: #fef2f2; color: var(--cf-danger); }
.stat { display: grid; gap: 0.2rem; }
.stat.link { cursor: pointer; }
.stat .label { color: var(--cf-muted); font-size: 0.8rem; font-weight: 600; }
.stat .value { font-size: 1.35rem; font-weight: 800; color: var(--cf-teal-dark); word-break: break-word; }
.toolbar { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.row-actions, .pill-row { display: flex; gap: 0.35rem; flex-wrap: wrap; }
input, select, textarea { border: 1px solid var(--cf-line); border-radius: 10px; padding: 0.55rem 0.7rem; font: inherit; font-size: 16px; background: #fff; color: var(--cf-ink); max-width: 100%; }
input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; padding: 0; }
.btn { -webkit-appearance: none; appearance: none; border: 1px solid var(--cf-line); background: #fff; color: var(--cf-ink); border-radius: 10px; padding: 0.55rem 0.85rem; font: inherit; font-weight: 650; cursor: pointer; min-height: 44px; }
.btn.small { min-height: 36px; padding: 0.35rem 0.6rem; font-size: 0.85rem; }
.btn:hover { background: #f1f5f9; }
.btn.primary { background: var(--cf-teal); border-color: var(--cf-teal); color: #fff; }
.btn.primary:hover { background: var(--cf-teal-dark); }
.btn.danger { background: #fee2e2; border-color: #fecaca; color: var(--cf-danger); }
.btn.ghost { background: transparent; border-color: rgba(255,255,255,0.35); color: inherit; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--cf-line); border-radius: 12px; max-width: 100%; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 0.6rem 0.7rem; border-bottom: 1px solid var(--cf-line); text-align: left; vertical-align: top; }
th { background: #f1f5f9; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--cf-muted); }
.badge { display: inline-flex; align-items: center; border-radius: 999px; padding: 0.12rem 0.55rem; font-size: 0.74rem; font-weight: 700; margin-left: 0.25rem; white-space: nowrap; }
.badge.ok { background: #dcfce7; color: var(--cf-ok); }
.badge.warn { background: #ffedd5; color: var(--cf-warn); }
.badge.danger { background: #fee2e2; color: var(--cf-danger); }
.badge.info { background: var(--cf-teal-soft); color: var(--cf-teal-dark); }
.badge.muted { background: #e2e8f0; color: #475569; }
.form { display: grid; gap: 0.7rem; }
.form.row-2 { grid-template-columns: 1fr 1fr; }
.form label { display: grid; gap: 0.25rem; font-size: 0.82rem; font-weight: 650; color: var(--cf-muted); }
.form .full { grid-column: 1 / -1; }
label.check { display: block; font-size: 0.9rem; color: var(--cf-ink); padding: 0.35rem 0; }
label.mini { display: grid; gap: 0.2rem; font-size: 0.75rem; color: var(--cf-muted); width: 110px; }
fieldset { border: 1px solid var(--cf-line); border-radius: 10px; padding: 0.6rem 0.8rem; }
.muted { color: var(--cf-muted); }
.small { font-size: 0.8rem; }
.warn-text { color: var(--cf-danger); font-weight: 600; }
.form-error { color: var(--cf-danger); font-weight: 600; min-height: 1em; }
.empty { padding: 1rem; text-align: center; color: var(--cf-muted); }
.list-card { border: 1px solid var(--cf-line); border-radius: 12px; padding: 0.75rem; background: #fff; display: grid; gap: 0.3rem; }
.kv { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 0.35rem 0.75rem; margin: 0; }
.kv dt { font-weight: 700; color: var(--cf-muted); }
.kv dd { margin: 0; }
.pre { white-space: pre-wrap; }
.addendum { background: #fffbeb; border-left: 3px solid #f59e0b; padding: 0.4rem 0.6rem; margin: 0.4rem 0; }
.attachments { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0.75rem; }
.attachments figure { margin: 0; }
img.scan { width: 100%; height: auto; border: 1px solid var(--cf-line); border-radius: 8px; display: block; }
figcaption { font-size: 0.75rem; color: var(--cf-muted); margin: 0.3rem 0; }
.pos-items { display: grid; gap: 0.5rem; max-height: 60vh; overflow-y: auto; }
.pos-item { display: flex; justify-content: space-between; gap: 0.5rem; align-items: center; border: 1px solid var(--cf-line); border-radius: 10px; padding: 0.55rem 0.65rem; background: #fff; flex-wrap: wrap; }
.totals { font-weight: 800; text-align: right; }
.modal-backdrop { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(15,23,42,0.45); z-index: 20000; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.modal { width: 760px; max-width: 100%; max-height: 90vh; overflow-y: auto; -webkit-overflow-scrolling: touch; background: #fff; border-radius: 16px; padding: 1.1rem; }
.modal h2 { margin: 0 0 0.8rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }
.toast-root { position: fixed; right: 1rem; bottom: 1rem; z-index: 30000; display: grid; gap: 0.5rem; }
.toast { background: #0f172a; color: #fff; padding: 0.7rem 0.9rem; border-radius: 12px; max-width: 90vw; font-size: 0.9rem; }
.toast.error { background: var(--cf-danger); }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.login-card { width: 460px; max-width: 100%; display: grid; gap: 0.6rem; }
.login-card h1 { margin: 0.4rem 0 0; font-size: 1.4rem; }
.login-list { display: grid; gap: 0.5rem; }
.login-account { -webkit-appearance: none; appearance: none; display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; text-align: left; border: 1px solid var(--cf-line); background: #fff; border-radius: 12px; padding: 0.75rem 0.85rem; font: inherit; cursor: pointer; min-height: 52px; flex-wrap: wrap; }
.login-account:hover { border-color: var(--cf-teal); background: #f0fdfa; }
code { font-size: 0.82rem; background: #f1f5f9; padding: 0.05rem 0.3rem; border-radius: 4px; }
.print-sheet { max-width: 720px; margin: 0 auto; }
.print-sheet .totals { margin-top: 0.8rem; }
.print-sheet .stamp { color: #666; font-size: 0.8rem; }

@media (max-width: 980px) {
  /* iOS Safari lesson: take the sidebar out of the grid; the shell is a plain block on mobile. */
  .app-shell { display: block; min-height: 0; }
  .main-wrap { min-height: 100vh; }
  .sidebar { position: fixed; top: 0; left: 0; bottom: 0; width: 280px; max-width: 86vw; height: auto; transform: translateX(-110%); -webkit-transform: translateX(-110%); transition: transform 0.2s ease; z-index: 15000; padding-top: calc(1rem + env(safe-area-inset-top, 0px)); }
  .sidebar.open { transform: translateX(0); -webkit-transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,0.3); }
  .menu-btn { display: inline-flex; align-items: center; }
  .app-top { top: 0; padding-left: calc(0.75rem + env(safe-area-inset-left, 0px)); padding-right: 0.75rem; }
  .view { padding: 0.5rem 0.75rem 2.5rem; }
  .grid.cols-3, .grid.cols-2, .split, .form.row-2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 600px) {
  /* Phone: tables become stacked cards so nothing needs sideways scrolling. */
  .table-wrap { border: 0; overflow: visible; }
  .table-wrap table, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; width: 100%; }
  .table-wrap thead { display: none; }
  .table-wrap tr { border: 1px solid var(--cf-line); border-radius: 12px; margin-bottom: 0.6rem; background: #fff; padding: 0.3rem 0; }
  .table-wrap td { border: 0; padding: 0.3rem 0.75rem; }
  .table-wrap td::before { content: attr(data-label); display: block; font-size: 0.7rem; text-transform: uppercase; color: var(--cf-muted); font-weight: 700; }
  .table-wrap td[data-label=""]::before { display: none; }
  .kv { grid-template-columns: minmax(0, 1fr); }
}
@media print {
  .demo-banner, .sidebar, .app-top, .toolbar, .modal-actions, .toast-root { display: none !important; }
  .app-shell, .main-wrap, .view { display: block; padding: 0; }
  .modal-backdrop { position: static; background: none; padding: 0; }
  .modal { max-height: none; box-shadow: none; }
}
