:root {
  --primary: #2563eb;
  --primary-soft: #e8efff;
  --secondary: #0f172a;
  --accent: #f59e0b;
  --accent-soft: #fff5e0;
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #6b7789;
  --line: #e7eaf1;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 20px;
  --shadow: 0 10px 30px rgba(15, 23, 42, .06);
  --tabbar-h: 70px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body { min-height: 100dvh; }

h1, h2, h3 { margin: 0; color: var(--secondary); letter-spacing: -.02em; }
h1 { font-size: 18px; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }

.app-shell {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 18px 12px;
  background: rgba(246, 247, 251, .88);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.brand { display: flex; align-items: center; gap: 11px; }

.brand-mark {
  width: 38px; height: 38px;
  border-radius: 12px;
  background: linear-gradient(145deg, var(--secondary), #1e3a8a 70%, var(--primary));
  display: flex; align-items: center; justify-content: center; gap: 2.5px;
  box-shadow: 0 6px 16px rgba(37, 99, 235, .25);
}
.brand-mark span {
  display: block;
  width: 3px;
  border-radius: 3px;
  background: #fff;
  opacity: .92;
}
.brand-mark span:nth-child(1) { height: 9px; }
.brand-mark span:nth-child(2) { height: 18px; background: var(--accent); }
.brand-mark span:nth-child(3) { height: 12px; }

.brand-sub { margin: 0; font-size: 11px; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; }

.views {
  flex: 1;
  padding: 4px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 28px);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.view { display: flex; flex-direction: column; gap: 12px; animation: rise .22s ease; }
.view[hidden] { display: none; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.panel {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(231, 234, 241, .8);
}

.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.panel-head h3 { margin: 0; }

.view-title { font-size: 22px; }

.muted { color: var(--muted); margin: 0; }
.small { font-size: 12px; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 999px;
  background: var(--secondary);
  color: #fff;
  white-space: nowrap;
}
.pill.soft { background: var(--primary-soft); color: var(--primary); }

/* stats */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-card {
  background: #f8fafc;
  border-radius: 16px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  border: 1px solid var(--line);
}
.stat-card.accent { background: linear-gradient(160deg, #fffaf0, var(--accent-soft)); border-color: #fde9c0; }
.stat-value { font-size: 30px; font-weight: 700; color: var(--secondary); line-height: 1.1; letter-spacing: -.03em; }
.stat-card.accent .stat-value { color: #b45309; }
.stat-label { font-size: 12px; color: var(--muted); }

/* latest observation */
.obs-card {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 12px 12px 16px;
  display: flex;
  gap: 12px;
  background: #fff;
  overflow: hidden;
}
.obs-card + .obs-card { margin-top: 10px; }

.wave {
  position: relative;
  flex: 0 0 34px;
  border-radius: 10px;
  background: #f4f6fb;
  overflow: hidden;
}
.wave svg { display: block; width: 100%; height: 100%; }
.wave polyline {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}
.obs-card[data-outcome="High"] .wave { background: #fff1f0; }
.obs-card[data-outcome="Moderate"] .wave { background: #fff7ec; }
.obs-card[data-outcome="Mild"] .wave { background: #eff6ff; }
.obs-card[data-outcome="None"] .wave { background: #f1f5f9; }

.obs-main { flex: 1; min-width: 0; }
.obs-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.obs-trigger { font-weight: 650; color: var(--secondary); font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.obs-date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.obs-notes { margin: 6px 0 0; color: #475569; font-size: 13px; word-wrap: break-word; }
.obs-actions { display: flex; gap: 6px; margin-top: 10px; }

.outcome-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 650;
  padding: 3px 9px;
  border-radius: 999px;
}
.outcome-chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.o-High { background: #fee2e2; color: #b91c1c; }
.o-Moderate { background: #fef3c7; color: #b45309; }
.o-Mild { background: #dbeafe; color: #1d4ed8; }
.o-None { background: #e2e8f0; color: #475569; }

/* big latest detail */
.latest-hero { display: flex; align-items: center; gap: 14px; margin-bottom: 10px; }
.latest-wave { flex: 0 0 64px; height: 64px; border-radius: 16px; background: #f4f6fb; overflow: hidden; }
.latest-wave svg { width: 100%; height: 100%; display: block; }
.latest-trigger { font-size: 19px; font-weight: 700; color: var(--secondary); letter-spacing: -.02em; }
.latest-meta { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.latest-notes {
  margin: 0 0 12px;
  padding: 12px;
  border-radius: 12px;
  background: #f8fafc;
  color: #475569;
  font-size: 14px;
  border: 1px solid var(--line);
}
.latest-notes.empty { color: var(--muted); font-style: italic; }

.empty-state {
  text-align: center;
  padding: 26px 12px;
  color: var(--muted);
  font-size: 14px;
}
.empty-state strong { display: block; color: var(--secondary); margin-bottom: 4px; font-size: 15px; }

/* form */
.form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12px; font-weight: 650; color: #475569; letter-spacing: .01em; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  width: 100%;
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fbfcfe;
  outline: none;
  transition: border-color .15s, box-shadow .15s, background .15s;
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236b7789' stroke-width='1.8' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 13px center;
  padding-right: 36px;
}
textarea { resize: vertical; min-height: 74px; }
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  background: #fff;
  box-shadow: 0 0 0 4px rgba(37, 99, 235, .12);
}
input:user-invalid, select:user-invalid { border-color: var(--danger); }

.error { margin: 0; font-size: 12px; color: var(--danger); min-height: 0; display: none; }
.error.show { display: block; }

/* buttons */
.btn {
  font: inherit;
  border: none;
  cursor: pointer;
  border-radius: 12px;
  padding: 11px 18px;
  font-weight: 650;
  font-size: 14px;
  transition: transform .12s, background .15s, opacity .15s;
  touch-action: manipulation;
}
.btn:active { transform: scale(.975); }
.btn.primary { background: var(--primary); color: #fff; box-shadow: 0 8px 20px rgba(37, 99, 235, .28); }
.btn.primary:active { background: #1d4ed8; }
.btn.ghost { background: var(--primary-soft); color: var(--primary); }
.btn.danger { background: #fee2e2; color: var(--danger); }
.btn.small { padding: 8px 14px; font-size: 13px; border-radius: 10px; }
.btn.block { width: 100%; padding: 14px; font-size: 15px; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 12px;
  border: none;
  background: var(--secondary);
  color: #fff;
  font-size: 22px;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(15, 23, 42, .22);
}
.icon-btn.ghost { background: #f1f5f9; color: var(--secondary); box-shadow: none; }

/* log */
.sticky-panel { position: sticky; top: calc(env(safe-area-inset-top, 0px) + 64px); z-index: 10; }
#logSearch { margin-top: 10px; padding: 10px 12px; }
#logCount { margin-top: 8px; }

.group { margin-top: 4px; }
.group-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 4px 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: .06em;
  text-transform: uppercase;
}
.group-title .count { background: #eef1f7; color: #475569; padding: 1px 8px; border-radius: 999px; letter-spacing: 0; }

/* insights */
.highlight-card {
  border-radius: 16px;
  padding: 14px;
  background: linear-gradient(150deg, #eef4ff, #f8fbff);
  border: 1px solid #dbe7ff;
}
.highlight-card .hl-label { font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: var(--primary); font-weight: 700; }
.highlight-card .hl-trigger { font-size: 20px; font-weight: 700; color: var(--secondary); margin: 3px 0; letter-spacing: -.02em; }
.highlight-card .hl-meta { font-size: 13px; color: #475569; }

.donut-legend { display: flex; flex-wrap: wrap; gap: 10px 14px; margin-top: 12px; font-size: 11.5px; color: var(--muted); }
.donut-legend span { display: inline-flex; align-items: center; gap: 5px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot.high { background: #ef4444; }
.dot.moderate { background: var(--accent); }
.dot.mild { background: var(--primary); }
.dot.none { background: #94a3b8; }

.donut-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.donut-cell {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 8px;
  text-align: center;
  background: #fbfcfe;
}
.donut-cell.best { border-color: #fcd9a0; background: #fffbf2; }
.donut-wrap { position: relative; width: 96px; height: 96px; margin: 0 auto 6px; }
.donut-wrap svg { transform: rotate(-90deg); display: block; }
.donut-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 700; color: var(--secondary);
}
.donut-center .val { font-size: 20px; line-height: 1; }
.donut-center .of { font-size: 10px; color: var(--muted); font-weight: 600; }
.donut-name { font-size: 12.5px; font-weight: 650; color: var(--secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.donut-sub { font-size: 11px; color: var(--muted); }

/* profile */
.profile-hero { display: flex; align-items: center; gap: 14px; }
.avatar {
  width: 54px; height: 54px;
  flex: 0 0 54px;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--primary), #60a5fa);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 22px;
}
.profile-hero > div:nth-child(2) { flex: 1; min-width: 0; }

.trigger-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.trigger-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #f8fafc;
  border: 1px solid var(--line);
}
.trigger-list .t-name { flex: 1; font-weight: 600; font-size: 14px; color: var(--secondary); }
.trigger-list .t-count { font-size: 11px; color: var(--muted); }
.trigger-list .t-del {
  border: none; background: transparent; color: #94a3b8;
  font-size: 18px; cursor: pointer; padding: 0 2px; line-height: 1;
}
.trigger-list .t-del:active { color: var(--danger); }

.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form input { flex: 1; padding: 10px 12px; }

/* tabbar */
.tabbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 520px;
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  padding: 8px 8px calc(env(safe-area-inset-bottom, 0px) + 8px);
  background: rgba(255, 255, 255, .94);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--line);
  box-shadow: 0 -8px 24px rgba(15, 23, 42, .05);
}
.tab {
  font: inherit;
  border: none;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 2px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 10.5px;
  font-weight: 600;
  transition: color .15s, background .15s;
}
.tab-glyph { font-size: 16px; line-height: 1; }
.tab.active { color: var(--primary); background: var(--primary-soft); }

/* toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 16px);
  transform: translate(-50%, 20px);
  background: var(--secondary);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 80;
  max-width: 88vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 480px) {
  .donut-grid { grid-template-columns: repeat(3, 1fr); }
}
