:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e2e6ec;
  --text: #16202c;
  --muted: #64748b;
  --accent: #0f766e;
  --accent-soft: #e6f4f1;
  --patient: #eef2f7;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  --danger-border: #fda29b;
  --warn: #b54708;
  --warn-bg: #fffaeb;
  --warn-border: #fec84b;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}

/* Three theme states, not two. An explicit choice stamps data-theme on the
   root; the default "system" setting stamps nothing, so only the media query
   separates light from dark there. The media block is guarded so an explicit
   day choice still wins on a dark OS, and the [data-theme="night"] block makes
   the toggle win in the other direction. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="day"]) {
    --bg: #0f1419;
    --surface: #171d25;
    --border: #2a323d;
    --text: #e6edf3;
    --muted: #94a3b8;
    --accent: #2dd4bf;
    --accent-soft: #10302d;
    --patient: #222b36;
    --danger: #fda29b;
    --danger-bg: #2d1512;
    --danger-border: #7a271a;
    --warn: #fec84b;
    --warn-bg: #2b1d05;
    --warn-border: #7a4b06;
  }
}

:root[data-theme="night"] {
    --bg: #0f1419;
    --surface: #171d25;
    --border: #2a323d;
    --text: #e6edf3;
    --muted: #94a3b8;
    --accent: #2dd4bf;
    --accent-soft: #10302d;
    --patient: #222b36;
    --danger: #fda29b;
    --danger-bg: #2d1512;
    --danger-border: #7a271a;
    --warn: #fec84b;
    --warn-bg: #2b1d05;
    --warn-border: #7a4b06;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .9em;
  background: var(--patient);
  padding: 1px 5px;
  border-radius: 5px;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}

.brand {
  font-weight: 650; text-decoration: none; color: var(--text);
  display: inline-flex; align-items: center; gap: 8px;
}

.nav-right { display: flex; align-items: center; gap: 16px; }
.inline { display: inline; }

button.link {
  background: none; border: 0; padding: 0; cursor: pointer;
  color: var(--accent); font: inherit;
}
button.link:hover { text-decoration: underline; }

/* ----------------------------------------------------------- layout bits */

.shell { max-width: 760px; margin: 32px auto; padding: 0 20px; }
.stack { display: flex; flex-direction: column; gap: 24px; }
.head h1 { margin: 0 0 6px; font-size: 26px; }
.head p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 16px; font-size: 17px; }

.auth { max-width: 400px; margin: 8vh auto; }
.auth h1 { margin: 0 0 4px; font-size: 22px; }
.auth p { margin: 0 0 20px; }

label { display: block; font-weight: 550; font-size: 13px; margin: 14px 0 6px; }

input, select, textarea {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 9px;
  background: var(--bg); color: var(--text); font: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent;
}

button.primary {
  margin-top: 18px; padding: 10px 18px; border: 0; border-radius: 9px;
  background: var(--accent); color: #fff; font: inherit; font-weight: 600;
  cursor: pointer;
}
button.primary:hover { filter: brightness(1.08); }
button.primary:disabled { opacity: .5; cursor: not-allowed; }

.alert { padding: 10px 14px; border-radius: 9px; font-size: 14px; }
.alert.error {
  background: var(--danger-bg); color: var(--danger);
  border: 1px solid var(--danger-border);
}

.row-form { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.row-form .grow { flex: 1 1 200px; }
.row-form label { margin-top: 0; }
.row-form button { margin-top: 0; }

/* ---------------------------------------------------------- patient list */

.patient-list { list-style: none; margin: 0; padding: 0;
  display: flex; flex-direction: column; gap: 10px; }

.patient-list a {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 16px; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.patient-list a:hover { border-color: var(--accent); }
.patient-list .who { display: flex; flex-direction: column; flex: 1; }
.chevron { color: var(--muted); font-size: 22px; }

.avatar {
  width: 38px; height: 38px; flex: none;
  display: grid; place-items: center; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-weight: 650;
}

/* ------------------------------------------------------------------ chat */

.chat-shell {
  display: grid; grid-template-columns: minmax(0, 1fr) 320px;
  gap: 20px; max-width: 1100px; margin: 20px auto; padding: 0 20px;
  align-items: start;
}
@media (max-width: 900px) {
  .chat-shell { grid-template-columns: minmax(0, 1fr); }
  .memory { order: -1; }
}

.conversation {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}

.convo-head {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.convo-head small { display: block; }

.messages {
  display: flex; flex-direction: column; gap: 12px;
  padding: 18px; min-height: 45vh; max-height: 62vh; overflow-y: auto;
}

.msg { display: flex; }
.msg.from-patient { justify-content: flex-end; }

.bubble {
  max-width: 82%; padding: 10px 14px; border-radius: 14px;
  white-space: pre-wrap; word-wrap: break-word;
}
.from-doctor .bubble {
  background: var(--accent-soft); border-bottom-left-radius: 4px;
}
.from-patient .bubble {
  background: var(--patient); border-bottom-right-radius: 4px;
}
.bubble.thinking { color: var(--muted); font-style: italic; }

.recalled {
  margin: 0 18px; padding: 8px 12px; font-size: 12.5px;
  color: var(--muted); background: var(--bg);
  border-left: 3px solid var(--accent); border-radius: 0 8px 8px 0;
}
.recalled strong { color: var(--text); font-weight: 550; }

/* ---------------------------------------------------------------- banner */

.banner {
  margin: 14px 18px 0; padding: 14px 16px; border-radius: 10px;
  border: 1px solid var(--danger-border); background: var(--danger-bg);
  color: var(--danger);
}
.banner.urgent {
  border-color: var(--warn-border); background: var(--warn-bg); color: var(--warn);
}
.banner h3 { margin: 0 0 6px; font-size: 15px; }
.banner p { margin: 0; font-size: 14px; }
.banner ul { margin: 6px 0 0; padding-left: 18px; font-size: 14px; }

/* ------------------------------------------------------------- composer */

.suggestions {
  display: flex; flex-wrap: wrap; gap: 8px; padding: 0 18px 4px;
}
.suggestion {
  padding: 7px 12px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--bg);
  color: var(--text); font: inherit; font-size: 13.5px;
}
.suggestion:hover { border-color: var(--accent); color: var(--accent); }

.composer {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 14px 18px; border-top: 1px solid var(--border);
}
.composer textarea { resize: none; max-height: 160px; }
.composer .send { margin-top: 0; }

.composer .stop {
  padding: 10px 18px; border-radius: 9px; cursor: pointer;
  border: 1px solid var(--danger-border); background: var(--danger-bg);
  color: var(--danger); font: inherit; font-weight: 600;
}
.composer .stop:hover { filter: brightness(1.05); }

.attach {
  flex: none; cursor: pointer; font-size: 19px; line-height: 1;
  padding: 9px 10px; border: 1px solid var(--border); border-radius: 9px;
  background: var(--bg);
}
.attach:hover { border-color: var(--accent); }
.attach input { display: none; }

.file-chip {
  margin: 0 18px; font-size: 13px; color: var(--accent);
}
.disclaimer { margin: 8px 18px 14px; }

/* ----------------------------------------------------------- care record */

.record { max-width: 800px; }

.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; margin-bottom: 14px;
}
.card-head h2 { margin: 0; }
button.primary.compact { margin-top: 0; padding: 8px 14px; font-size: 14px; }

.alert.warn {
  background: var(--warn-bg); color: var(--warn);
  border: 1px solid var(--warn-border); margin-bottom: 16px;
}

.plan-summary {
  margin: 0 0 20px; padding: 14px 16px;
  background: var(--accent-soft); border-radius: 10px;
}

.plan-group { margin-bottom: 20px; }
.plan-group h3 {
  margin: 0 0 10px; font-size: 14px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted);
}
.plan-group ul { list-style: none; margin: 0; padding: 0;
  display: flex; flex-direction: column; gap: 10px; }
.plan-group li {
  padding: 10px 14px; border-radius: 9px; background: var(--bg);
  border-left: 3px solid var(--border);
}
.plan-group li strong { display: block; font-weight: 550; }
.plan-group li small { display: block; margin-top: 3px; }

.kind-do li      { border-left-color: #12b76a; }
.kind-avoid li   { border-left-color: var(--danger); }
.kind-eat li     { border-left-color: #66c61c; }
.kind-limit li   { border-left-color: var(--warn); }
.kind-exercise li{ border-left-color: #2e90fa; }
.kind-monitor li { border-left-color: #9e77ed; }

.transcript { list-style: none; margin: 0; padding: 0;
  display: flex; flex-direction: column; gap: 14px;
  max-height: 60vh; overflow-y: auto; }
.transcript li { padding-left: 12px; border-left: 3px solid var(--border); }
.transcript li.from-doctor { border-left-color: var(--accent); }
.transcript .who { font-weight: 600; font-size: 13px; }
.transcript time { color: var(--muted); font-size: 12px; margin-left: 8px; }
.transcript p { margin: 4px 0 0; white-space: pre-wrap; }

/* ---------------------------------------------------------------- memory */

.memory {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px;
  position: sticky; top: 76px;
}
.memory h2 { margin: 0 0 12px; font-size: 15px; }
.memory h3 { margin: 0 0 4px; font-size: 13.5px; }

.profile {
  margin: 0; padding: 12px; border-radius: 9px;
  background: var(--bg); border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; line-height: 1.6;
  white-space: pre-wrap; word-wrap: break-word;
  max-height: 48vh; overflow-y: auto;
}

.pending {
  margin-bottom: 16px; padding: 12px; border-radius: 9px;
  background: var(--warn-bg); border: 1px solid var(--warn-border);
}
.pending ul { list-style: none; margin: 10px 0 0; padding: 0;
  display: flex; flex-direction: column; gap: 10px; }
.pending li { font-size: 13.5px; display: flex; flex-direction: column; gap: 6px; }
.pending .actions { display: flex; gap: 6px; }

.chip {
  padding: 4px 12px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; font-size: 12.5px;
}
.chip.yes:hover { border-color: var(--accent); color: var(--accent); }
.chip.no:hover { border-color: var(--danger); color: var(--danger); }

/* --------------------------------------------------------------- theme btn */

.theme-btn {
  font: inherit;
  font-size: .82rem;
  padding: .35rem .65rem;
  min-height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.theme-btn:hover { background: var(--accent-soft); }

/* --------------------------------------------------------------- admin ui */

.admin-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .7rem; }

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .8rem .9rem;
  display: flex;
  flex-direction: column;
  gap: .15rem;
}
.stat-card .n { font-size: 1.5rem; font-weight: 650; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat-card .k { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }

.panel { display: flex; flex-direction: column; gap: .75rem; margin-bottom: 2rem; }
.panel-head { display: flex; align-items: baseline; gap: .6rem; flex-wrap: wrap; }
.panel-head h2 { margin: 0; font-size: 1.1rem; }
.panel-head .muted { margin-left: auto; }

table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th, td { text-align: left; padding: .5rem .65rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 600; white-space: nowrap; }
tbody tr:hover { background: var(--accent-soft); }

.tag {
  display: inline-block;
  font-size: .7rem;
  padding: .1rem .45rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.tag-admin { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.tag-emergency { background: var(--danger-bg); border-color: var(--danger-border); color: var(--danger); font-weight: 650; }
.tag-urgent { background: var(--warn-bg); border-color: var(--warn-border); color: var(--warn); font-weight: 650; }

.notice {
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  color: var(--muted);
}
.notice-error { border-left-color: var(--danger); color: var(--text); }

/* ------------------------------------------------------------ breakpoints */

@media (max-width: 620px) {
  .admin-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel-head .muted { margin-left: 0; flex-basis: 100%; }
  th, td { padding: .45rem .5rem; }
  .topbar { flex-wrap: wrap; gap: .5rem; }
}

@media (max-width: 380px) {
  .admin-grid { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------- scrollbars (shared)
   Default scrollbars are a light-grey chrome widget that sits badly on a dark
   panel and changes width between platforms, which shifts layout. These are
   theme-aware, overlay-thin, and sized identically everywhere.

   Two mechanisms, both needed:
     * scrollbar-width / scrollbar-color — the standard, Firefox and modern
       Chromium. Only accepts a thumb and track colour, nothing else.
     * ::-webkit-scrollbar-* — Chromium and Safari, where the finer control is.
   Colours come from existing theme variables, with literal fallbacks so this
   block still works when pasted into a page that has no token layer.

   Scroll containers also get overscroll-behavior so a table that reaches its
   end stops rather than scrolling the page behind it, and a momentum flag for
   iOS. */

:root {
  --sb-size: 10px;
  --sb-thumb: color-mix(in srgb, currentColor 26%, transparent);
  --sb-thumb-hover: color-mix(in srgb, currentColor 45%, transparent);
  --sb-track: transparent;
}

/* color-mix is widely supported but not universal; these fall back to fixed
   translucent values where it is missing. */
@supports not (color: color-mix(in srgb, red 50%, transparent)) {
  :root {
    --sb-thumb: rgba(128, 149, 173, 0.42);
    --sb-thumb-hover: rgba(128, 149, 173, 0.68);
  }
}

* {
  scrollbar-width: thin;
  scrollbar-color: var(--sb-thumb) var(--sb-track);
}

*::-webkit-scrollbar {
  width: var(--sb-size);
  height: var(--sb-size);
}

*::-webkit-scrollbar-track {
  background: var(--sb-track);
  border-radius: 999px;
}

*::-webkit-scrollbar-thumb {
  background: var(--sb-thumb);
  border-radius: 999px;
  /* A transparent border with background-clip gives the thumb breathing room
     inside the gutter, so it reads as a floating pill rather than a bar
     welded to the edge. */
  border: 2px solid transparent;
  background-clip: padding-box;
}

*::-webkit-scrollbar-thumb:hover {
  background: var(--sb-thumb-hover);
  background-clip: padding-box;
}

*::-webkit-scrollbar-corner { background: transparent; }

/* Any element that scrolls: keep the gesture contained and smooth. */
.table-wrap,
.scroll-x,
.scroll-y,
pre,
textarea {
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* Horizontal scroll container that hints there is more to the right. The
   gradient is painted on the container background and pinned with
   background-attachment: local, so it appears only when content overflows and
   fades out as the user reaches each end. */
.scroll-x,
.table-wrap {
  overflow-x: auto;
  background:
    linear-gradient(to right, var(--bg-elevated, #fff) 30%, rgba(0, 0, 0, 0)) left center,
    linear-gradient(to left, var(--bg-elevated, #fff) 30%, rgba(0, 0, 0, 0)) right center,
    radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, 0.22), rgba(0, 0, 0, 0)) left center,
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, 0.22), rgba(0, 0, 0, 0)) right center;
  background-repeat: no-repeat;
  background-size: 40px 100%, 40px 100%, 14px 100%, 14px 100%;
  background-attachment: local, local, scroll, scroll;
}

/* Vertical scroll container with a capped height — used for any table or list
   that would otherwise run past the fold. */
.scroll-y {
  overflow-y: auto;
  max-height: min(70vh, 640px);
}

@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

/* The topbar spacer pushes the Hub/theme controls right without giving the
   nav block a margin that would fight the flex gap at narrow widths. */
.topbar-spacer { margin-left: auto; }
.theme-btn[href] { text-decoration: none; display: inline-flex; align-items: center; }
