:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1a1c1e;
  --text-dim: #5f6368;
  --accent: #0f766e;
  --accent-2: #115e59;
  --bubble: #d7f2ee;
  --bubble-text: #0b3b37;
  --border: #e2e5e9;
  --danger: #b42318;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --nav-h: 62px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --surface: #1f2226;
    --surface-2: #23262b;
    --text: #e6e8ea;
    --text-dim: #9aa0a6;
    --accent: #2dd4bf;
    --accent-2: #14b8a6;
    --bubble: #0f3d38;
    --bubble-text: #d7f2ee;
    --border: #2c3036;
    --danger: #f97066;
    --shadow: 0 1px 3px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, "Segoe UI", system-ui, Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
body { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }

/* Views */
.view { display: none; flex-direction: column; flex: 1; min-height: 0; }
.view.active { display: flex; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 14px 16px calc(14px) 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.topbar-title { font-weight: 700; font-size: 1.05rem; display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.event-switcher {
  font-weight: 700; font-size: 1.05rem; color: var(--text); background: transparent;
  border: none; padding: 2px 22px 2px 0; margin: 0; max-width: 100%; cursor: pointer;
  font-family: inherit; -webkit-appearance: none; appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%), linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: right 8px center, right 3px center; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat;
}
.event-switcher:focus { outline: 2px solid var(--accent); border-radius: 6px; }
.event-switcher option { color: var(--text); background: var(--surface); font-weight: 500; }
.topbar-sub { font-weight: 400; font-size: .78rem; color: var(--text-dim); margin-top: 2px; }
.ghost-btn {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 7px 12px; font-size: .82rem; cursor: pointer;
}
.ghost-btn:active { transform: scale(.97); }

/* Thread */
.thread {
  flex: 1; overflow-y: auto; padding: 16px 14px 20px; display: flex; flex-direction: column; gap: 10px;
  scroll-behavior: smooth;
}
.day-divider {
  align-self: center; background: var(--surface-2); color: var(--text-dim);
  font-size: .72rem; font-weight: 600; padding: 3px 12px; border-radius: 999px; margin: 8px 0 2px;
  text-transform: uppercase; letter-spacing: .04em;
}
.note {
  align-self: flex-end; max-width: 85%; background: var(--bubble); color: var(--bubble-text);
  padding: 9px 13px; border-radius: 16px 16px 4px 16px; box-shadow: var(--shadow);
  position: relative; word-wrap: break-word; white-space: pre-wrap;
}
.note.recovery { background: var(--surface-2); color: var(--text); border: 1px dashed var(--border); }
.note-text { font-size: .95rem; line-height: 1.4; }
.note-text .tag { color: var(--accent-2); font-weight: 600; }
.note-text .person { font-weight: 700; }
@media (prefers-color-scheme: dark) { .note-text .tag { color: var(--accent); } }
.note-meta {
  display: flex; align-items: center; gap: 10px; margin-top: 5px;
  font-size: .68rem; color: var(--text-dim); opacity: .85;
}
.note-meta button { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: .68rem; padding: 0; }
.note-meta button:hover { color: var(--danger); }
.note-meta .star-btn { font-size: .95rem; color: var(--text-dim); }
.note-meta .star-btn.on { color: #f5a623; }
.note-meta .star-btn:hover { color: #f5a623; }
.note.starred { box-shadow: var(--shadow), -3px 0 0 0 #f5a623 inset; }
.note-photo { display: block; max-width: 100%; width: 220px; max-height: 240px; object-fit: cover; border-radius: 10px; margin-bottom: 6px; cursor: zoom-in; background: var(--surface-2); }
.note-photo.missing { width: auto; padding: 10px 12px; font-size: .8rem; color: var(--text-dim); cursor: default; }
.lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.9); display: flex; align-items: center; justify-content: center; padding: 20px; cursor: zoom-out; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* Empty state */
.empty-state { margin: auto; text-align: center; color: var(--text-dim); padding: 24px; max-width: 460px; }
.empty-state.small { margin: 40px auto; }
.empty-emoji { font-size: 2.6rem; }
.empty-state h2 { color: var(--text); margin: 10px 0 6px; font-size: 1.25rem; }
.empty-state .example {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; text-align: left; font-size: .88rem; color: var(--text); margin: 14px 0;
}
.empty-state .hint { font-size: .82rem; }

/* Composer */
.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
.chip-row { display: flex; gap: 7px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: none; }
.chip-row::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 5px 12px; font-size: .8rem; cursor: pointer; white-space: nowrap;
}
.chip:active { background: var(--bubble); }
.compose-bar { display: flex; align-items: flex-end; gap: 8px; }
.compose-bar textarea {
  flex: 1; resize: none; border: 1px solid var(--border); background: var(--bg); color: var(--text);
  border-radius: 20px; padding: 11px 14px; font-size: 1rem; line-height: 1.35; max-height: 140px;
  font-family: inherit;
}
.compose-bar textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
.icon-btn, .send-btn {
  flex: 0 0 auto; width: 44px; height: 44px; border-radius: 50%; border: none; cursor: pointer;
  font-size: 1.15rem; display: grid; place-items: center;
}
.icon-btn { background: var(--surface-2); }
.icon-btn.recording { background: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100%{opacity:1;} 50%{opacity:.55;} }
.send-btn { background: var(--accent); color: #fff; font-size: 1.25rem; }
.send-btn:active { background: var(--accent-2); }
.mic-status { text-align: center; font-size: .78rem; color: var(--danger); padding-top: 6px; }

/* Report */
.report-wrap, .setup-wrap { flex: 1; overflow-y: auto; padding: 16px; }
.report-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.primary-btn, .secondary-btn, .danger-btn {
  border-radius: 10px; padding: 10px 16px; font-size: .9rem; font-weight: 600; cursor: pointer; border: 1px solid transparent;
}
.primary-btn { background: var(--accent); color: #fff; }
.primary-btn:active { background: var(--accent-2); }
.secondary-btn { background: var(--surface); color: var(--text); border-color: var(--border); }
.secondary-btn:disabled { opacity: .45; cursor: not-allowed; }
.danger-btn { background: transparent; color: var(--danger); border-color: var(--danger); }
.report-note { font-size: .8rem; color: var(--text-dim); margin: 12px 0; line-height: 1.45; }
.report-output {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 20px; margin-top: 8px; line-height: 1.55; font-size: .93rem;
}
.report-output h1 { font-size: 1.4rem; margin: 0 0 4px; }
.report-output h2 { font-size: 1.05rem; margin: 22px 0 8px; padding-bottom: 5px; border-bottom: 1px solid var(--border); color: var(--accent-2); }
@media (prefers-color-scheme: dark){ .report-output h2 { color: var(--accent); } }
.report-output h3 { font-size: .95rem; margin: 14px 0 4px; }
.report-output ul { margin: 6px 0; padding-left: 20px; }
.report-output li { margin: 3px 0; }
.report-output p { margin: 6px 0; }
.report-output table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: .86rem; }
.report-output th, .report-output td { border: 1px solid var(--border); padding: 6px 9px; text-align: left; }
.report-output code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: .85em; }
.report-output .muted { color: var(--text-dim); font-style: italic; }

/* Setup */
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field > span { font-size: .82rem; font-weight: 600; color: var(--text-dim); }
.field > span em { font-weight: 400; font-style: normal; }
.field input, .field select, .field textarea {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 9px; padding: 10px 12px; font-size: .95rem; font-family: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }
.setup-wrap hr { border: none; border-top: 1px solid var(--border); margin: 22px 0; }
.setup-wrap h3 { margin: 0 0 4px; }
.toggle-row { display: flex; align-items: center; gap: 10px; margin: 6px 0 14px; font-size: .9rem; cursor: pointer; }
.toggle-row input { width: 18px; height: 18px; accent-color: var(--accent); }

/* Toast */
#toast {
  position: fixed; bottom: calc(var(--nav-h) + 16px); left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: .85rem;
  opacity: 0; pointer-events: none; transition: all .25s; z-index: 50; box-shadow: var(--shadow); max-width: 90%;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Search bar + filter banner */
.searchbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border); }
.searchbar input { flex: 1; border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 999px; padding: 8px 14px; font-size: .9rem; font-family: inherit; }
.searchbar input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.search-clear { flex: 0 0 auto; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-dim); border-radius: 50%; width: 30px; height: 30px; cursor: pointer; font-size: .8rem; }
.filter-banner { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 14px; background: var(--bubble); color: var(--bubble-text); font-size: .8rem; }
.filter-banner button { background: none; border: none; color: inherit; font-weight: 700; cursor: pointer; }
.note-text .tag, .note-text .person { cursor: pointer; }

/* Edit-in-place */
.note.editing { align-self: stretch; max-width: 100%; background: var(--surface); border: 1px solid var(--accent); color: var(--text); }
.note .edit-area { width: 100%; border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 10px; padding: 8px 10px; font: inherit; font-size: .95rem; resize: vertical; min-height: 62px; }
.note.editing .note-meta button { color: var(--accent); font-weight: 600; }

/* Toast action button */
#toast { display: flex; align-items: center; gap: 14px; }
.toast-action { background: none; border: none; color: var(--accent); font-weight: 700; cursor: pointer; font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; }

/* Contacts */
.contacts-wrap { flex: 1; overflow-y: auto; padding: 14px; }
.contact-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.contact-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.contact-name { font-weight: 700; font-size: 1rem; color: var(--accent-2); }
.contact-count { font-size: .74rem; color: var(--text-dim); white-space: nowrap; }
.contact-snip { font-size: .88rem; margin: 8px 0; line-height: 1.42; color: var(--text); }
.contact-snip .tag { color: var(--accent-2); font-weight: 600; }
.contact-snip .person { font-weight: 700; }
.contact-tags { font-size: .78rem; color: var(--text-dim); margin-bottom: 6px; }
.contact-follow { font-size: .8rem; color: var(--danger); font-weight: 600; margin-bottom: 8px; }
.contact-actions { display: flex; gap: 8px; }
.contact-actions button { flex: 1; background: var(--surface-2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 8px; font-size: .8rem; font-weight: 600; cursor: pointer; }
.contact-actions button:active { background: var(--bubble); }
@media (prefers-color-scheme: dark) { .contact-name, .contact-snip .tag { color: var(--accent); } }

/* Dashboard */
.dashboard-wrap { flex: 1; overflow-y: auto; padding: 14px; }
.dash-goals { background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 10px; padding: 10px 12px; font-size: .86rem; line-height: 1.4; margin-bottom: 14px; }
.dash-tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.dash-tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px; text-align: center; box-shadow: var(--shadow); }
.tile-val { font-size: 1.8rem; font-weight: 800; color: var(--accent-2); line-height: 1; }
.tile-val .tile-sub { font-size: 1rem; font-weight: 600; color: var(--text-dim); }
.tile-label { font-size: .74rem; color: var(--text-dim); margin-top: 5px; text-transform: uppercase; letter-spacing: .03em; }
@media (prefers-color-scheme: dark) { .tile-val { color: var(--accent); } }
.dash-h { font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); margin: 20px 0 8px; display: flex; justify-content: space-between; align-items: baseline; }
.dash-h-sub { font-size: .72rem; font-weight: 400; text-transform: none; letter-spacing: 0; }
.dash-days { display: flex; flex-direction: column; gap: 6px; }
.dash-day { display: flex; align-items: center; gap: 10px; font-size: .82rem; }
.dash-day-label { flex: 0 0 92px; color: var(--text-dim); }
.dash-bar { flex: 1; height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.dash-bar-fill { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.dash-day-count { flex: 0 0 24px; text-align: right; font-weight: 700; }
.followups { display: flex; flex-direction: column; gap: 8px; }
.followup { display: flex; align-items: flex-start; gap: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font-size: .88rem; line-height: 1.4; cursor: pointer; }
.followup input { margin-top: 2px; width: 18px; height: 18px; flex: 0 0 auto; accent-color: var(--accent); }
.followup.done span { text-decoration: line-through; color: var(--text-dim); }
.followup .tag { color: var(--accent-2); font-weight: 600; }
@media (prefers-color-scheme: dark) { .followup .tag { color: var(--accent); } }

/* Bottom nav */
.bottom-nav {
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
  height: var(--nav-h); padding-bottom: env(safe-area-inset-bottom);
}
.nav-btn {
  flex: 1; background: none; border: none; color: var(--text-dim); font-size: .72rem; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; cursor: pointer;
}
.nav-btn span { font-size: 1.3rem; }
.nav-btn.active { color: var(--accent); }

@media (min-width: 720px) {
  body { max-width: 560px; margin: 0 auto; border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}
