:root {
  --bg: #f7f3f1;
  --card: #ffffff;
  --ink: #23161f;
  --muted: #7a6973;
  --line: #eadfe3;
  --brand: #5b2148;
  --brand-ink: #ffffff;
  --brand-soft: #f3e6ee;
  --minus: #b3261e;
  --minus-soft: #fbe9e7;
  --plus: #1d7a46;
  --plus-soft: #e3f4ea;
  --warn: #9a6200;
  --warn-soft: #fff3d6;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(35, 22, 31, .06), 0 4px 16px rgba(35, 22, 31, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17111a;
    --card: #221a25;
    --ink: #f3ecf1;
    --muted: #ab9aa6;
    --line: #362a39;
    --brand: #d58bbd;
    --brand-ink: #23101e;
    --brand-soft: #3a2236;
    --minus: #ff8a80;
    --minus-soft: #3d1f1e;
    --plus: #6fd49b;
    --plus-soft: #17352a;
    --warn: #ffc860;
    --warn-soft: #3b2f14;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
img { display: block; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
code { background: var(--brand-soft); padding: 2px 6px; border-radius: 6px; font-size: 13px; }

/* ---------- Buttons & forms ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card);
  border-radius: 12px; padding: 12px 16px; font-weight: 600; cursor: pointer;
  min-height: 46px; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.sm { min-height: 36px; padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.btn.minus { color: var(--minus); border-color: color-mix(in srgb, var(--minus) 35%, transparent); background: var(--minus-soft); }
.btn.plus { color: var(--plus); border-color: color-mix(in srgb, var(--plus) 35%, transparent); background: var(--plus-soft); }

.form { display: grid; gap: 14px; }
.form label { display: grid; gap: 6px; font-weight: 600; font-size: 14px; }
input, select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 12px; background: var(--card);
  padding: 12px 14px; min-height: 46px; font-weight: 400; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }

.alert { padding: 12px 14px; border-radius: 12px; font-size: 14px; }
.alert.err { background: var(--minus-soft); color: var(--minus); }
.alert.good { background: var(--plus-soft); color: var(--plus); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }

/* ---------- Login / setup ---------- */
body.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 400px; background: var(--card); border-radius: 20px; padding: 28px 22px; box-shadow: var(--shadow); display: grid; gap: 18px; }
.auth-card h1 { font-size: 22px; margin: 0; }
.auth-card h2 { font-size: 17px; margin: 6px 0 0; }
.brand { display: flex; gap: 12px; align-items: center; }
.brand b { display: block; font-size: 18px; }
.brand span { color: var(--muted); font-size: 13px; }
.brand.small b { font-size: 16px; }
.checks { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.checks li { display: grid; gap: 2px; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.checks li.good { background: var(--plus-soft); }
.checks li.bad { background: var(--minus-soft); }
.checks li span { color: var(--muted); word-break: break-word; }

/* ---------- App shell ---------- */
body.app { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
.top {
  position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); display: flex; justify-content: space-between; align-items: center;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top)); border-bottom: 1px solid var(--line);
}
.who { border: 0; background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: 14px; cursor: pointer; box-shadow: var(--shadow); }
.role { font-size: 11px; color: var(--muted); margin-left: 4px; }
#view { max-width: 680px; margin: 0 auto; padding: 14px 16px; }

.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 6; display: flex; background: var(--card);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; border: 0; background: none; padding: 8px 4px 10px; font-size: 12px; color: var(--muted);
  display: grid; justify-items: center; gap: 2px; position: relative; cursor: pointer; font-weight: 600;
}
.tabs svg { width: 24px; height: 24px; fill: currentColor; }
.tabs button.on { color: var(--brand); }
.badge { position: absolute; top: 4px; left: 55%; background: var(--minus); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; line-height: 18px; min-width: 18px; }

.searchbar { position: sticky; top: 57px; z-index: 4; background: var(--bg); padding: 2px 0 10px; }
.searchbar input { font-size: 16px; padding-left: 42px; background: var(--card) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%238a7a85'%3E%3Cpath d='M10.5 3a7.5 7.5 0 0 1 5.9 12.1l4.3 4.3-1.4 1.4-4.3-4.3A7.5 7.5 0 1 1 10.5 3Zm0 2a5.5 5.5 0 1 0 0 11 5.5 5.5 0 0 0 0-11Z'/%3E%3C/svg%3E") no-repeat 13px center / 20px; }

.list { display: grid; gap: 10px; }
.row {
  display: grid; grid-template-columns: 56px 1fr auto; gap: 12px; align-items: center; background: var(--card);
  border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow); border: 1px solid transparent; cursor: pointer; text-align: left; width: 100%;
}
.row:active { border-color: var(--brand); }
.thumb { width: 56px; height: 70px; border-radius: 10px; object-fit: cover; background: var(--brand-soft); }
.row .t { font-weight: 600; font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.row .s { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tag { display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 999px; background: var(--warn-soft); color: var(--warn); margin-left: 4px; vertical-align: 1px; }
.qty { text-align: center; min-width: 52px; }
.qty b { display: block; font-size: 22px; line-height: 1.1; }
.qty span { font-size: 11px; color: var(--muted); }
.qty.zero b { color: var(--minus); }

.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.empty b { display: block; color: var(--ink); font-size: 16px; margin-bottom: 4px; }
.loading { text-align: center; color: var(--muted); padding: 28px; }
.spin { width: 22px; height: 22px; border: 3px solid var(--brand-soft); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; margin: 0 auto 8px; }
@keyframes spin { to { transform: rotate(360deg); } }

.section-h { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin: 4px 0 12px; flex-wrap: wrap; }
.section-h h2 { margin: 0; font-size: 18px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 600; white-space: nowrap; cursor: pointer; }
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

/* ---------- Cards (trials, logs, users) ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); }
.trial { display: grid; grid-template-columns: 48px 1fr; gap: 12px; }
.trial .thumb { width: 48px; height: 60px; }
.trial .t { font-weight: 600; font-size: 14px; }
.trial .s { color: var(--muted); font-size: 12px; }
.trial .acts { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.group-h { font-size: 13px; font-weight: 700; color: var(--muted); margin: 16px 2px 8px; text-transform: uppercase; letter-spacing: .04em; }

.log { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; }
.log .t { font-weight: 600; font-size: 14px; }
.log .s { color: var(--muted); font-size: 12px; grid-column: 1 / -1; }
.delta { font-weight: 800; font-size: 16px; text-align: right; }
.delta.m { color: var(--minus); }
.delta.p { color: var(--plus); }
.log.failed { border-left: 4px solid var(--minus); }
.log .err { grid-column: 1 / -1; color: var(--minus); font-size: 12px; }

.user { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.user .s { color: var(--muted); font-size: 12px; }
.user.off { opacity: .55; }

/* ---------- Bottom sheet ---------- */
.sheet-back { position: fixed; inset: 0; background: rgba(20, 10, 18, .45); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--card);
  border-radius: 22px 22px 0 0; max-height: 92vh; overflow-y: auto; padding: 8px 16px calc(18px + env(safe-area-inset-bottom));
  max-width: 680px; margin: 0 auto; animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(40px); } }
.grab { width: 40px; height: 4px; background: var(--line); border-radius: 4px; margin: 4px auto 12px; }
.sheet h3 { margin: 0 0 4px; font-size: 17px; }
.p-head { display: grid; grid-template-columns: 72px 1fr; gap: 12px; align-items: center; margin-bottom: 14px; }
.p-head .thumb { width: 72px; height: 90px; }
.now { display: flex; gap: 10px; margin: 6px 0 14px; }
.now div { flex: 1; background: var(--brand-soft); border-radius: 12px; padding: 10px; text-align: center; }
.now b { display: block; font-size: 24px; }
.now span { font-size: 12px; color: var(--muted); }
.acts-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.acts-grid .btn { min-height: 54px; font-size: 14px; flex-direction: column; gap: 0; line-height: 1.2; }
.acts-grid .btn small { font-weight: 500; font-size: 11px; opacity: .8; }
.stepper { display: grid; grid-template-columns: 56px 1fr 56px; gap: 8px; align-items: center; }
.stepper input { text-align: center; font-size: 22px; font-weight: 700; }
.stepper .btn { font-size: 24px; min-height: 52px; }
.preview { text-align: center; font-size: 15px; padding: 12px; border-radius: 12px; background: var(--bg); }
.preview b { font-size: 20px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 30;
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-weight: 600; font-size: 14px;
  max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}
.toast.err { background: var(--minus); color: #fff; }
.toast.good { background: var(--plus); color: #fff; }

@media (min-width: 720px) {
  .sheet { bottom: auto; top: 50%; transform: translateY(-50%); border-radius: 22px; left: 16px; right: 16px; max-width: 520px; animation: none; }
}
