:root {
  --bg: #fbf7f5;
  --card: #ffffff;
  --text: #2a2226;
  --muted: #7d7177;
  --line: #ece3e0;
  --accent: #b6416a;
  --accent-soft: #f6e3ea;
  --warn: #b8691b;
  --warn-soft: #fbeedd;
  --ok: #2f7d5b;
  --radius: 16px;
  --tabbar-h: 58px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1618;
    --card: #262022;
    --text: #f3ecee;
    --muted: #a99ca2;
    --line: #3a3135;
    --accent: #e77fa3;
    --accent-soft: #452a34;
    --warn: #e9a55c;
    --warn-soft: #43321f;
    --ok: #6cc39b;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
[hidden] { display: none !important; }

h1, h2, h3 { margin: 0 0 .6rem; line-height: 1.2; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.error { color: #c0392b; min-height: 1.2em; }

/* layout */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
main {
  max-width: 560px; margin: 0 auto;
  padding: 4px max(16px, env(safe-area-inset-left)) calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row > input { flex: 1 1 100px; min-width: 0; }
.row > input[type="number"] { flex: 0 0 72px; }

/* controls: 16px text keeps iOS from zooming on focus */
input, textarea, select {
  font: inherit; font-size: 16px; color: inherit;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg);
  margin: 4px 0 10px;
}
textarea { resize: vertical; }
select { width: auto; flex: 1; margin: 0; }
button, .button {
  font: inherit; font-weight: 600;
  border: 0; border-radius: 12px;
  padding: 12px 16px; min-height: 44px;
  cursor: pointer; touch-action: manipulation;
  user-select: none; -webkit-user-select: none;
  text-decoration: none; text-align: center; display: inline-block;
}
button:disabled { opacity: .5; }
.primary { background: var(--accent); color: #fff; width: 100%; }
.secondary { background: var(--accent-soft); color: var(--accent); }
.icon { background: transparent; color: var(--accent); font-size: 1.6rem; padding: 4px 14px; }
button:active, .button:active { transform: scale(.97); }
@media (prefers-reduced-motion: reduce) { button:active, .button:active { transform: none; } }

fieldset { border: 0; padding: 0; margin: 0 0 8px; }
legend { font-size: .85rem; color: var(--muted); margin-bottom: 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip {
  font-weight: 500; font-size: .92rem;
  padding: 7px 12px; min-height: 36px; border-radius: 999px;
  background: var(--bg); color: var(--text); border: 1px solid var(--line);
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

/* pain bands */
.bands { display: grid; gap: 6px; margin-bottom: 12px; }
.band {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 10px 14px; min-height: 56px; border-radius: 12px; text-align: left;
  color: #fff; background: hsl(calc(140 - var(--n) * 14) 55% 46%);
  opacity: .55;
}
.band[aria-checked="true"] { opacity: 1; outline: 3px solid var(--text); outline-offset: 1px; }
.band-name { font-size: 1.05rem; font-weight: 700; }
.band-range { font-weight: 500; opacity: .85; margin-left: 4px; }
.band-desc { font-size: .88rem; font-weight: 400; }
.level-label { color: var(--muted); margin: -4px 0 6px; font-size: .88rem; }

.toggle { display: flex; align-items: center; gap: 10px; margin: 6px 0 10px; font-weight: 500; }
.toggle input { width: 22px; height: 22px; margin: 0; accent-color: var(--accent); }

.when summary { color: var(--muted); font-size: .9rem; margin-bottom: 8px; cursor: pointer; }

.photo-btn {
  display: block; text-align: center; padding: 18px;
  border: 2px dashed var(--line); border-radius: 12px; margin-bottom: 10px;
  color: var(--accent); font-weight: 600; cursor: pointer;
}
#visit-file-list li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#food-preview { width: 100%; max-height: 260px; object-fit: cover; border-radius: 12px; margin-bottom: 8px; }

/* day view */
.day-nav { display: flex; align-items: center; gap: 4px; margin-bottom: 10px; }
.day-nav input { margin: 0; text-align: center; }
.garmin-strip { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.pill {
  font-size: .82rem; padding: 4px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); white-space: nowrap;
}
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.plain { background: var(--card); color: var(--muted); border: 1px solid var(--line); }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 10px;
}
.entry-head { display: flex; align-items: baseline; gap: 8px; }
.entry-time { color: var(--muted); font-variant-numeric: tabular-nums; font-size: .9rem; }
.entry-title { font-weight: 600; flex: 1; }
.entry-del { background: none; color: var(--muted); padding: 0 4px; min-height: 32px; font-weight: 400; }
.entry-body { margin-top: 4px; }
.entry-body img { width: 100%; max-height: 220px; object-fit: cover; border-radius: 10px; margin: 6px 0; }
.visit-section { margin-top: 6px; font-size: .95rem; }
.visit-section .muted { font-size: .82rem; }
.visit-section ul { margin: 2px 0 0; padding-left: 1.2rem; }
.timeline .visit-section li { background: none; border: 0; border-radius: 0; padding: 0; margin: 2px 0; }
.visit-notes { margin-top: 6px; white-space: pre-wrap; overflow-wrap: anywhere; }
.visit-notes summary { color: var(--muted); font-size: .9rem; cursor: pointer; }
a.pill { text-decoration: none; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.entry-garmin { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.badge {
  display: inline-block; min-width: 2em; text-align: center;
  padding: 1px 8px; border-radius: 8px; color: #fff; font-weight: 700;
  background: hsl(calc(140 - var(--n) * 14) 55% 46%);
}
.empty { text-align: center; color: var(--muted); padding: 24px 0; }

.trend svg { width: 100%; height: 110px; display: block; }
.trend .legend { font-size: .8rem; color: var(--muted); display: flex; justify-content: space-between; }

/* insights */
.prose { line-height: 1.55; }
.prose h1, .prose h2, .prose h3 { margin: 1rem 0 .4rem; font-size: 1.05rem; }
.prose ul, .prose ol { padding-left: 1.2rem; margin: .4rem 0; }
.prose p { margin: .5rem 0; }
.prose > :first-child { margin-top: 0; }
.section-title { color: var(--muted); font-size: .9rem; font-weight: 600; margin: 18px 4px 8px; }
.history details { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; }
.history summary { cursor: pointer; font-weight: 500; }
.spinner::after { content: "…"; animation: dots 1.2s steps(4) infinite; display: inline-block; width: 1.2em; overflow: hidden; vertical-align: bottom; }
@keyframes dots { from { width: 0; } to { width: 1.2em; } }

.plain-list { list-style: none; padding: 0; margin: 0 0 8px; }
.plain-list li { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); }

/* tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  background: none; color: var(--muted); border-radius: 0;
  font-size: .72rem; font-weight: 600; padding: 6px 0 0;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
}
.tabbar button span { font-size: 1.25rem; line-height: 1; }
.tabbar button.active { color: var(--accent); }
.tabbar button:active { transform: none; }

.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 360px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-weight: 500; z-index: 20;
  transition: opacity .2s, transform .2s; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
