/* =========================================================
   style.css — how everything LOOKS (colors, sizes, layout).
   ========================================================= */

/* Colors are kept in one place as "variables" so they're easy to change. */
:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #1d2733;
  --muted: #6b7785;
  --border: #e1e6eb;
  --primary: #0f766e;        /* calm teal */
  --primary-soft: #e6f4f2;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --ok: #1a7f37;
  --ok-soft: #e7f6ec;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* Anything marked "hidden" in the HTML stays hidden, even if another rule says otherwise. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  display: grid;
  grid-template-columns: 220px 1fr;  /* menu | main area */
  min-height: 100vh;
}

/* ---------- Left menu ---------- */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand {
  font-size: 18px;
  color: var(--primary);
  margin: 0 8px 16px;
}
.nav-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  background: none;
  border: none;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 15px;
  color: var(--text);
  cursor: pointer;
}
.nav-btn:hover { background: var(--bg); }
.nav-btn.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav-btn.sign-out { margin-top: auto; color: var(--muted); }

/* ---------- Sign-in screen ---------- */
/* While signed out, hide the menu and centre the sign-in box. */
body.logged-out { grid-template-columns: 1fr; }
body.logged-out .sidebar { display: none; }
.login-card { max-width: 380px; margin: 10vh auto 0; padding: 28px; }
.login-card h2 { margin: 0 0 4px; color: var(--primary); }
.login-card .btn { width: 100%; padding: 11px; }
.error { color: var(--danger); font-size: 14px; min-height: 1em; }

/* ---------- Main area ---------- */
main {
  padding: 28px 32px;
  max-width: 1100px;
  width: 100%;
}
.page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.page-head h2 { margin: 0; font-size: 24px; }
.page-head p { margin: 4px 0 0; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.muted { color: var(--muted); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 20px;
}
.card h3 { margin: 0 0 12px; font-size: 17px; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.card-head h3 { margin: 0; }
.welcome { background: var(--primary-soft); border-color: transparent; }
.welcome p { margin: 0 0 12px; }

/* Four number boxes on the dashboard */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 4px; }
.stat .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* Patient info boxes */
.info-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px 24px; }
.info-grid .full { grid-column: 1 / -1; white-space: pre-wrap; }
.label { display: block; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 2px; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.link-btn { background: none; border: none; color: var(--primary); cursor: pointer; padding: 0; margin-bottom: 12px; font-size: 14px; }
.icon-btn { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 4px 8px; }
.icon-btn:hover { color: var(--danger); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--bg); }
td.num, th.num { text-align: right; }
.row-actions { text-align: right; white-space: nowrap; }
.row-actions .btn { margin-left: 4px; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
.empty { color: var(--muted); text-align: center; padding: 24px; margin: 0; }

.search {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  margin-bottom: 14px;
}

/* Paid / Partial / Unpaid labels */
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.badge.paid { background: var(--ok-soft); color: var(--ok); }
.badge.partial { background: var(--warn-soft); color: var(--warn); }
.badge.unpaid { background: var(--danger-soft); color: var(--danger); }

/* ---------- Pop-up forms ---------- */
dialog {
  border: none;
  border-radius: 14px;
  padding: 0;
  width: min(620px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(20, 30, 40, .45); }
dialog form { padding: 22px 24px; }
dialog h2 { margin: 0 0 16px; font-size: 20px; }

label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 12px; color: var(--muted); }
input, select, textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 12px; margin: 0 0 12px; }
legend { font-size: 13px; font-weight: 600; color: var(--muted); padding: 0 4px; }
.item-row { display: flex; gap: 6px; margin-bottom: 6px; align-items: center; }
.item-row input { margin-top: 0; }
.item-row .grow { flex: 3; }
.item-row .small-in { flex: 1; min-width: 70px; }
.total-line { text-align: right; font-weight: 700; margin: 4px 0 12px; }

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

/* ---------- "Saved" message ---------- */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: all .25s;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* =========================================================
   RESPONSIVE: adjusting the layout for smaller screens.
   "@media (max-width: 900px)" means "only when the screen
   is 900 pixels wide or less".
   ========================================================= */

/* ---------- Tablets (and small laptops) ---------- */
@media (max-width: 1024px) {
  body { grid-template-columns: 190px 1fr; }
  main { padding: 24px 20px; }
}

/* ---------- Tablets and phones: tables become cards ---------- */
@media (max-width: 900px) {
  /* Hide the column titles; each value shows its own label instead. */
  main thead { display: none; }
  main table, main tbody, main tr, main td { display: block; width: 100%; }

  main tr {
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
  }
  main tbody tr:last-child { border-bottom: none; }
  main td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 4px 0;
    border: none;
    text-align: right;
  }
  /* The little grey label on the left, taken from data-label="…" in app.js */
  main td[data-label]::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 13px;
    text-align: left;
  }
  main td.primary { font-size: 17px; padding-bottom: 6px; }
  main td.empty { display: block; text-align: center; }
  main td.row-actions {
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 8px;
    padding-top: 10px;
  }
  .row-actions .btn { margin-left: 0; }
  tr.clickable:hover td { background: none; }
  .table-wrap { overflow-x: visible; }
}

/* ---------- Phones ---------- */
@media (max-width: 760px) {
  body {
    grid-template-columns: 1fr;
    /* leave room at the bottom for the tab bar */
    padding-bottom: calc(68px + env(safe-area-inset-bottom));
  }

  /* The menu becomes a tab bar fixed to the bottom of the screen */
  .sidebar {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    height: auto;
    z-index: 10;
    flex-direction: row;
    justify-content: space-around;
    gap: 0;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    border-right: none;
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, .05);
  }
  .brand { display: none; }
  .nav-btn {
    flex: 1;
    flex-direction: column;
    gap: 2px;
    padding: 6px 2px;
    font-size: 11px;
    text-align: center;
  }
  .nav-icon { font-size: 20px; }
  .nav-btn.sign-out { display: none; }   /* Sign out is on the Settings screen instead */

  main { padding: 16px 16px 24px; }
  .page-head { margin-bottom: 14px; }
  .page-head h2 { font-size: 21px; }
  .page-head > .btn,
  .page-head .head-actions { width: 100%; }
  .head-actions .btn,
  .page-head > .btn { flex: 1; padding: 11px 10px; }
  .card { padding: 14px 16px; }

  /* Dashboard numbers: two per row */
  .stats { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 12px 14px; }
  .stat .value { font-size: 20px; }

  /* Bigger buttons for fingers */
  .btn.small { padding: 8px 12px; font-size: 14px; }

  /* 16px text stops iPhones from zooming in when you tap a field */
  input, select, textarea, .search { font-size: 16px; }
  .row { grid-template-columns: 1fr; gap: 0; }

  /* Forms open full-screen, with Save / Cancel always visible at the bottom */
  dialog {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border-radius: 0;
  }
  dialog form { padding: 18px 16px 0; min-height: 100%; display: flex; flex-direction: column; }
  .form-actions {
    position: sticky;
    bottom: 0;
    margin: auto -16px 0;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
  }
  .form-actions .btn { flex: 1; padding: 12px; }

  /* Medicine / bill lines: name on the first line, the rest underneath */
  .item-row {
    display: grid;
    grid-template-columns: 1fr 1fr 36px;
    gap: 6px;
    padding-bottom: 8px;
    border-bottom: 1px dashed var(--border);
  }
  .item-row .grow { grid-column: 1 / 3; }
  .item-row .icon-btn { grid-column: 3; grid-row: 1; }
  .item-row .small-in { min-width: 0; }

  /* Sign-in box */
  .login-card { margin-top: 6vh; }

  /* Keep the "Saved" message above the tab bar */
  #toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
}

/* ---------- Printing ---------- */
/* On screen, the print area is hidden. When printing, ONLY the print area shows. */
#print-area { display: none; }

@media print {
  @page { margin: 16mm; }
  body { display: block; background: #fff; }
  body > * { display: none !important; }
  #print-area { display: block !important; color: #000; font-size: 14px; }
}
.letterhead { border-bottom: 2px solid #000; padding-bottom: 10px; margin-bottom: 16px; }
.letterhead h1 { margin: 0; font-size: 22px; }
.letterhead p { margin: 2px 0; }
.print-meta { display: flex; justify-content: space-between; margin-bottom: 14px; }
.rx { font-size: 30px; font-weight: 700; margin: 12px 0 4px; }
.print-table { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.print-table th, .print-table td { border: 1px solid #999; padding: 6px 8px; color: #000; }
.print-totals { margin-left: auto; width: 260px; }
.print-totals div { display: flex; justify-content: space-between; padding: 3px 0; }
.signature { margin-top: 60px; text-align: right; }
.signature span { display: inline-block; border-top: 1px solid #000; padding-top: 4px; min-width: 200px; text-align: center; }
