/* ---------------------------------------------------------------- tokens */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --hairline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #d03b3b;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;

  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --series-5: #e87ba4; --series-6: #008300; --series-7: #4a3aa7; --series-8: #e34948;

  --sev-1: #86b6ef; --sev-2: #5598e7; --sev-3: #2a78d6; --sev-4: #1c5cab; --sev-5: #104281;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.05), 0 4px 16px rgba(11, 11, 11, 0.05);
  --nav-h: 58px;
  font-family: system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --hairline: #2c2c2a;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --good-text: #0ca30c;
    --shadow: none;

    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;

    --sev-1: #86b6ef; --sev-2: #5598e7; --sev-3: #3987e5; --sev-4: #2a78d6; --sev-5: #1c5cab;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #262624;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --hairline: #2c2c2a;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --good-text: #0ca30c;
  --shadow: none;

  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;

  --sev-1: #86b6ef; --sev-2: #5598e7; --sev-3: #3987e5; --sev-4: #2a78d6; --sev-5: #1c5cab;
}

/* ---------------------------------------------------------------- base */

* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  background: var(--page);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; display: block; }
a { color: var(--accent); }

.boot-message { padding: 40px; text-align: center; color: var(--muted); }

/* ---------------------------------------------------------------- shell */

.app { min-height: 100dvh; }

.shell { display: flex; min-height: 100dvh; }

.sidenav {
  display: none;
  flex-direction: column;
  gap: 4px;
  width: 216px;
  padding: 20px 12px;
  border-right: 1px solid var(--hairline);
  background: var(--surface);
  position: sticky;
  top: 0;
  height: 100dvh;
  flex-shrink: 0;
}
.sidenav .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 18px;
  font-weight: 700;
  font-size: 17px;
}
.sidenav .brand img { width: 30px; height: 30px; }

.main {
  flex: 1;
  min-width: 0;
  padding: 16px 16px calc(var(--nav-h) + 28px + env(safe-area-inset-bottom));
  max-width: 1080px;
  margin: 0 auto;
  width: 100%;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 2px 14px;
}
.topbar h1 { font-size: 21px; font-weight: 700; }
.topbar .sub { color: var(--muted); font-size: 13px; margin-top: 1px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--hairline);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 30;
}
.tabbar button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 7px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
}
.tabbar button svg { width: 22px; height: 22px; }
.tabbar button.active { color: var(--accent); font-weight: 600; }

.navlink {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--ink-2);
  font-size: 14.5px;
  cursor: pointer;
  text-align: left;
}
.navlink svg { width: 20px; height: 20px; }
.navlink:hover { background: var(--surface-2); }
.navlink.active { background: var(--surface-2); color: var(--ink); font-weight: 600; }

@media (min-width: 860px) {
  .sidenav { display: flex; }
  .tabbar { display: none; }
  .main { padding: 24px 32px 48px; }
}

/* ---------------------------------------------------------------- components */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card + .card { margin-top: 12px; }
.card-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 10px;
}
.card-title .more { font-size: 13px; font-weight: 500; color: var(--accent); background: none; border: 0; cursor: pointer; padding: 2px 4px; }

.stack { display: flex; flex-direction: column; gap: 12px; }
.grid-2 { display: grid; gap: 12px; }
@media (min-width: 720px) { .grid-2 { grid-template-columns: 1fr 1fr; align-items: start; } }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}
.tile .t-label { font-size: 12px; color: var(--muted); }
.tile .t-value { font-size: 24px; font-weight: 700; margin-top: 2px; }
.tile .t-value small { font-size: 13px; font-weight: 500; color: var(--ink-2); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.icon { padding: 8px; }
.btn.icon svg { width: 18px; height: 18px; }
.btn:disabled { opacity: 0.5; cursor: default; }

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--nav-h) + 18px + env(safe-area-inset-bottom));
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(11, 11, 11, 0.25);
  z-index: 25;
}
@media (min-width: 860px) { .fab { bottom: 28px; } }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--ink-2);
  white-space: nowrap;
}
.chip .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.chip.outline { background: none; border: 1px solid var(--hairline); }

.tag {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  border: 1px solid var(--hairline);
  font-size: 12px;
  color: var(--ink-2);
}

.row-list { display: flex; flex-direction: column; }
.row-list > * { padding: 11px 2px; border-bottom: 1px solid var(--hairline); }
.row-list > *:last-child { border-bottom: 0; padding-bottom: 2px; }
.row-list > *:first-child { padding-top: 2px; }

.empty {
  padding: 26px 12px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

/* severity dots: ordinal blue ramp + number label (never color alone) */
.sev { display: inline-flex; align-items: center; gap: 5px; font-size: 12.5px; color: var(--ink-2); font-weight: 600; }
.sev .dots { display: inline-flex; gap: 2px; }
.sev .dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--hairline); }
.sev.s1 .dots i:nth-child(-n+1) { background: var(--sev-1); }
.sev.s2 .dots i:nth-child(-n+2) { background: var(--sev-2); }
.sev.s3 .dots i:nth-child(-n+3) { background: var(--sev-3); }
.sev.s4 .dots i:nth-child(-n+4) { background: var(--sev-4); }
.sev.s5 .dots i:nth-child(-n+5) { background: var(--sev-5); }

/* ---------------------------------------------------------------- forms */

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 13px; }
.field > label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field input[type="text"], .field input[type="date"], .field input[type="time"],
.field input[type="datetime-local"], .field input[type="password"], .field input[type="number"],
.field select, .field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  background: var(--surface);
  min-height: 42px;
}
.field textarea { resize: vertical; min-height: 74px; }
.field input:focus-visible, .field select:focus-visible, .field textarea:focus-visible,
.btn:focus-visible, .seg button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { font-size: 12px; color: var(--muted); }

.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button {
  border: 1px solid var(--hairline);
  background: var(--surface);
  color: var(--ink-2);
  border-radius: 999px;
  padding: 7px 13px;
  font-size: 13.5px;
  cursor: pointer;
}
.seg button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

.color-picker { display: flex; gap: 8px; flex-wrap: wrap; }
.color-picker button {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.color-picker button.on { border-color: var(--ink); box-shadow: inset 0 0 0 2px var(--surface); }

.time-list { display: flex; flex-direction: column; gap: 8px; }
.time-list .time-row { display: flex; gap: 8px; align-items: center; }
.time-list input[type="time"] {
  padding: 8px 10px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

/* ---------------------------------------------------------------- dialog */

dialog.sheet {
  border: 0;
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
  padding: 0;
  width: min(560px, calc(100vw - 24px));
  max-height: min(86dvh, 780px);
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.3);
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 10px;
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 2;
}
.sheet-head h2 { font-size: 17px; }
.sheet-head .x { border: 0; background: none; font-size: 22px; color: var(--muted); cursor: pointer; padding: 2px 6px; }
.sheet-body { padding: 4px 18px 18px; overflow-y: auto; }
.sheet-actions { display: flex; gap: 10px; justify-content: flex-end; padding-top: 6px; }
.sheet-actions .spacer { margin-right: auto; }

@media (max-width: 640px) {
  dialog.sheet {
    margin: auto 0 0;
    width: 100vw;
    max-width: 100vw;
    border-radius: 18px 18px 0 0;
    max-height: 92dvh;
  }
}

dialog.lightbox {
  border: 0;
  background: rgba(0, 0, 0, 0.92);
  padding: 0;
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  display: none;
  align-items: center;
  justify-content: center;
}
dialog.lightbox[open] { display: flex; }
dialog.lightbox img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }
dialog.lightbox .x {
  position: fixed;
  top: calc(10px + env(safe-area-inset-top));
  right: 14px;
  border: 0;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 22px;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
}

/* ---------------------------------------------------------------- photos */

.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.photo-grid .ph {
  position: relative;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--hairline);
  padding: 0;
  background: var(--surface-2);
  cursor: pointer;
}
.photo-grid .ph img { width: 100%; height: 100%; object-fit: cover; }
.photo-grid .ph .rm {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.photo-thumbs { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.photo-thumbs .ph {
  width: 56px; height: 56px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--hairline);
  padding: 0;
  background: var(--surface-2);
  cursor: pointer;
}
.photo-thumbs .ph img { width: 100%; height: 100%; object-fit: cover; }

/* ---------------------------------------------------------------- today view */

.dose-row { display: flex; align-items: center; gap: 12px; }
.dose-time { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 14px; width: 46px; flex-shrink: 0; }
.dose-info { flex: 1; min-width: 0; }
.dose-info .n { font-weight: 600; }
.dose-info .d { font-size: 12.5px; color: var(--muted); }
.dose-actions { display: flex; gap: 6px; flex-shrink: 0; }
.dose-actions button {
  border: 1px solid var(--hairline);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  color: var(--ink-2);
}
.dose-actions button.taken { background: var(--good); border-color: var(--good); color: #fff; font-weight: 600; }
.dose-actions button.skipped { background: var(--surface-2); color: var(--muted); font-weight: 600; text-decoration: line-through; }

.appt-row { display: flex; gap: 12px; align-items: flex-start; }
.appt-date {
  flex-shrink: 0;
  width: 52px;
  text-align: center;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  padding: 5px 2px;
}
.appt-date .m { font-size: 11px; color: var(--muted); }
.appt-date .d { font-size: 19px; font-weight: 700; line-height: 1.15; }
.appt-main { flex: 1; min-width: 0; }
.appt-main .h { font-weight: 600; }
.appt-main .s { font-size: 13px; color: var(--ink-2); }
.appt-main .meta { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.dday { font-size: 12px; font-weight: 700; color: var(--accent); white-space: nowrap; }
.dday.today { color: var(--danger); }
.appt-row.done { opacity: 0.55; }
.appt-row.done .h { text-decoration: line-through; }

.sym-row { display: flex; gap: 12px; }
.sym-main { flex: 1; min-width: 0; }
.sym-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sym-time { font-size: 12.5px; color: var(--muted); }
.sym-note { font-size: 13.5px; color: var(--ink-2); margin-top: 3px; overflow-wrap: anywhere; white-space: pre-wrap; }
.sym-tags { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 6px; }

/* ---------------------------------------------------------------- meds calendar */

.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.cal-title { font-weight: 700; font-size: 15px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.cal-wd { text-align: center; font-size: 11px; color: var(--muted); padding: 2px 0 6px; }
.cal-day {
  border: 0;
  background: none;
  cursor: pointer;
  border-radius: 10px;
  height: 46px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0;
  font-size: 13.5px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.cal-day:hover { background: var(--surface-2); }
.cal-day.today { font-weight: 700; color: var(--accent); }
.cal-day.sel { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.cal-day .ind { width: 6px; height: 6px; border-radius: 50%; background: transparent; flex-shrink: 0; }
.cal-day .ind.full { background: var(--good); }
.cal-day .ind.part { background: var(--warning); }
.cal-day .ind.miss { background: var(--danger); }
.cal-day .ind.due { background: var(--muted); }
.cal-day.sel .ind.due { background: var(--accent-ink); }
.cal-legend { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 10px; font-size: 12px; color: var(--muted); }
.cal-legend i { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 4px; }

.med-group { margin-top: 16px; }
.med-group-head { display: flex; align-items: center; gap: 8px; margin: 0 2px 8px; }
.med-group-head .cnt { font-size: 12.5px; color: var(--muted); }

/* ---------------------------------------------------------------- meds view */

.med-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.med-name { font-weight: 700; font-size: 15.5px; }
.med-sub { font-size: 13px; color: var(--ink-2); margin-top: 1px; }
.med-meta { font-size: 12.5px; color: var(--muted); margin-top: 6px; }
.med-week { display: flex; gap: 4px; margin-top: 10px; }
.med-week .day { flex: 1; text-align: center; }
.med-week .day .lbl { font-size: 10.5px; color: var(--muted); }
.med-week .day .cell {
  margin: 3px auto 0;
  width: 22px; height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  background: var(--surface-2);
  color: var(--muted);
}
.med-week .day .cell.full { background: var(--good); color: #fff; }
.med-week .day .cell.part { background: var(--warning); color: #1a1a19; }
.med-week .day .cell.miss { background: var(--surface-2); color: var(--danger); font-weight: 700; }
.med-week .day .cell.off { background: none; }
.med-inactive { opacity: 0.55; }

/* ---------------------------------------------------------------- chart */

.chart-wrap { overflow-x: auto; }
.chart svg { display: block; width: 100%; height: auto; }
.chart .bar { fill: var(--series-1); }
.chart .bar-hit { fill: transparent; cursor: pointer; }
.chart .axis-text { fill: var(--muted); font-size: 10.5px; }
.chart .gridline { stroke: var(--hairline); stroke-width: 1; }
.chart .baseline { stroke: var(--hairline); stroke-width: 1; }

.chart-tip {
  position: fixed;
  z-index: 60;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow), 0 4px 18px rgba(0, 0, 0, 0.18);
  padding: 8px 11px;
  font-size: 12.5px;
  pointer-events: none;
  min-width: 110px;
}
.chart-tip .t { color: var(--muted); margin-bottom: 2px; }
.chart-tip .v { font-weight: 700; }

/* ---------------------------------------------------------------- misc */

.filter-row { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 12px; scrollbar-width: none; }
.filter-row::-webkit-scrollbar { display: none; }
.filter-row .seg { flex-wrap: nowrap; }

.toast-region {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}
.toast {
  background: var(--ink);
  color: var(--page);
  border-radius: 999px;
  padding: 9px 18px;
  font-size: 13.5px;
  font-weight: 600;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.25);
  animation: toast-in 0.18s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

.login-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card { width: min(360px, 100%); text-align: center; }
.login-card img { width: 56px; height: 56px; margin: 0 auto 12px; }
.login-card h1 { font-size: 20px; margin-bottom: 4px; }
.login-card p { color: var(--muted); font-size: 13.5px; margin-bottom: 18px; }

.section-label {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 18px 2px 8px;
}

details.past summary { cursor: pointer; color: var(--muted); font-size: 13.5px; padding: 8px 2px; }
