:root {
  --bg: #f5f3ee;
  --card: #ffffff;
  --ink: #24292f;
  --muted: #6b7280;
  --line: #e5e1d8;
  --accent: #2e5d43;
  --accent-ink: #ffffff;
  --danger: #b3402a;
  --shadow: 0 1px 2px rgba(36, 41, 47, 0.06), 0 4px 14px rgba(36, 41, 47, 0.05);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b;
    --card: #1e2126;
    --ink: #e8e6e1;
    --muted: #9aa1ab;
    --line: #32363d;
    --accent: #6fae8b;
    --accent-ink: #10231a;
    --danger: #e07a5f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
/* the hidden attribute must always win, even over display:flex/grid rules */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main { max-width: 1080px; margin: 0 auto; padding: 1rem 1rem 4rem; }

/* ---------- header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap;
  padding: 0.6rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 0.45rem; font-weight: 700; font-size: 1.15rem; letter-spacing: 0.02em; }
.brand-icon { font-size: 1.35rem; }
.tabs { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.tab {
  border: 0; background: transparent; color: var(--muted);
  padding: 0.45rem 0.8rem; border-radius: 999px;
  font: inherit; font-weight: 600; cursor: pointer;
}
.tab:hover { color: var(--ink); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.tab.active { background: var(--accent); color: var(--accent-ink); }

/* ---------- cards / layout ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.view > * + * { margin-top: 0.9rem; }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.1rem; }
.list-head h2 { margin: 0; font-size: 1.15rem; }
.muted { color: var(--muted); font-size: 0.9rem; }
.empty { padding: 2.2rem 1rem; text-align: center; color: var(--muted); }

/* ---------- toolbar / filters ---------- */
.toolbar { padding: 0.7rem 0.9rem; }
.search-row { display: flex; gap: 0.6rem; }
.search-row input { flex: 1; }
#btn-filters { display: none; white-space: nowrap; }
.filters { display: flex; gap: 0.7rem; flex-wrap: wrap; align-items: end; margin-top: 0.7rem; }
.filters label { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.78rem; font-weight: 600; color: var(--muted); }
select, input, textarea {
  font: inherit; color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line); border-radius: 7px;
  padding: 0.42rem 0.55rem;
  min-width: 0;
}
.filters select, .filters input { width: 100%; }
select:focus, input:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 0; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border-radius: 8px; border: 1px solid var(--line);
  padding: 0.45rem 0.9rem;
  background: var(--card); color: var(--ink);
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.icon-btn {
  border: 0; background: transparent; cursor: pointer;
  font-size: 0.95rem; padding: 0.25rem 0.35rem; border-radius: 6px;
  color: var(--muted);
}
.icon-btn:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--ink); }
.icon-btn.danger:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

/* ---------- summary cards ---------- */
.summary { display: grid; grid-template-columns: repeat(auto-fill, minmax(148px, 1fr)); gap: 0.6rem; }
.summary:empty { display: none; }
.scard { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.6rem 0.75rem; box-shadow: var(--shadow); }
.scard-label { font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.scard-value { font-size: 1.25rem; font-weight: 700; margin-top: 0.1rem; }
.scard-sub { font-size: 0.78rem; color: var(--muted); margin-top: 0.1rem; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.data th {
  text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.data td { padding: 0.55rem 0.75rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.data tbody tr:last-child td { border-bottom: 0; }
.data tbody tr:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.num, td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.actions-col { width: 4.5rem; }
td.actions { white-space: nowrap; text-align: right; }
.date-cell { white-space: nowrap; font-variant-numeric: tabular-nums; }
.pre-line { white-space: pre-line; }
.undated { color: var(--muted); font-style: italic; }

/* ---------- photos ---------- */
.photo-row { display: flex; align-items: center; gap: 0.9rem; }
.photo-preview { width: 76px; height: 76px; object-fit: cover; border-radius: 12px; border: 1px solid var(--line); flex: 0 0 auto; }
.photo-preview.round { border-radius: 50%; }
.photo-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.avatar { width: 24px; height: 24px; border-radius: 50%; object-fit: cover; vertical-align: -6px; margin-right: 0.35rem; }
.thumb { width: 42px; height: 42px; border-radius: 10px; object-fit: cover; border: 1px solid var(--line); display: block; }
.thumb-ph {
  width: 42px; height: 42px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent); font-weight: 700; font-size: 1.05rem;
}
.thumb-col { width: 3.2rem; }
.scard.horse { display: flex; align-items: center; gap: 0.7rem; }
.scard.horse img, .scard.horse .thumb-ph { width: 52px; height: 52px; border-radius: 12px; object-fit: cover; flex: 0 0 auto; }
.scard.horse .scard-value { font-size: 1.1rem; }

/* ---------- bottom nav & fab (shown on mobile) ---------- */
.bottom-nav { display: none; }
.fab { display: none; }

/* ---------- type badges ---------- */
.badge {
  display: inline-block; padding: 0.12rem 0.55rem; border-radius: 999px;
  font-size: 0.78rem; font-weight: 600; white-space: nowrap;
  background: color-mix(in srgb, var(--b, #8a8f98) 16%, transparent);
  color: color-mix(in srgb, var(--b, #8a8f98) 80%, var(--ink));
  border: 1px solid color-mix(in srgb, var(--b, #8a8f98) 30%, transparent);
}
.b-training { --b: #3574c4; }
.b-jumping { --b: #5b5bd6; }
.b-competition { --b: #9350c9; }
.b-hoof { --b: #b78124; }
.b-deworming { --b: #3f9142; }
.b-vaccination { --b: #1d9a8f; }
.b-medical { --b: #c94f3d; }
.b-maintenance { --b: #8a8f98; }

/* ---------- dialogs ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--ink);
  width: min(560px, calc(100vw - 2rem));
  padding: 1.1rem 1.2rem;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(15, 18, 20, 0.45); backdrop-filter: blur(2px); }
dialog h3 { margin: 0 0 0.9rem; }
dialog form { display: flex; flex-direction: column; gap: 0.7rem; }
dialog label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.82rem; font-weight: 600; color: var(--muted); }
dialog select, dialog input, dialog textarea { width: 100%; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0.7rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 0.4rem; }

/* ---------- auth ---------- */
body:not(.authed) .topbar, body:not(.authed) main { display: none; }
#view-login { display: none; }
body.unauthed #view-login { display: grid; place-items: center; min-height: 100dvh; padding: 1rem; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 0.8rem; padding: 1.7rem 1.6rem; }
.login-card h1 { margin: 0 0 0.4rem; text-align: center; font-size: 1.35rem; letter-spacing: 0.02em; }
.login-brand { text-align: center; font-size: 2.4rem; line-height: 1; }
.login-card label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.82rem; font-weight: 600; color: var(--muted); }
.form-error { color: var(--danger); font-size: 0.85rem; }
#login-error { text-align: center; }
.push-right { margin-left: auto; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }
dialog hr { border: 0; border-top: 1px solid var(--line); margin: 1rem 0; }
dialog h4 { margin: 0 0 0.5rem; }
.space-between { justify-content: space-between; }

/* ---------- multi-user ---------- */
.chip {
  display: inline-block; padding: 0.05rem 0.5rem; margin-left: 0.25rem;
  border-radius: 999px; font-size: 0.72rem; font-weight: 700;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: color-mix(in srgb, var(--accent) 75%, var(--ink));
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  vertical-align: 0.1em;
}
.linklike {
  border: 0; background: transparent; color: var(--accent);
  font: inherit; font-size: 0.88rem; font-weight: 600; cursor: pointer;
  text-align: center; padding: 0.2rem;
}
.linklike:hover { text-decoration: underline; }
.field-note { font-weight: 400; font-size: 0.75rem; color: var(--muted); }
.check-row { flex-direction: row !important; align-items: center; gap: 0.5rem !important; }
.check-row input[type="checkbox"] { width: 1.05rem; height: 1.05rem; accent-color: var(--accent); }
.scard.welcome { grid-column: 1 / -1; display: flex; flex-direction: column; gap: 0.45rem; padding: 1rem; }
.small { font-size: 0.85rem; }
.share-list { margin: 0.6rem 0 1rem; max-height: 45vh; overflow-y: auto; }
.share-list .data td { padding: 0.5rem 0.75rem; }
.share-list:has(#shares-empty:not([hidden])) { border-style: dashed; }

/* ---------- dictation ---------- */
.label-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.mic-btn {
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
  border-radius: 999px; cursor: pointer;
  font-size: 0.95rem; line-height: 1.3; padding: 0.15rem 0.6rem;
}
.mic-btn:hover { border-color: var(--accent); }
.mic-btn.recording {
  background: var(--danger); border-color: var(--danger);
  animation: mic-pulse 1.1s ease-in-out infinite;
}
.mic-btn.busy { opacity: 0.55; pointer-events: none; }
@keyframes mic-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 1.4rem; transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 0.6rem 1.1rem; border-radius: 999px;
  font-size: 0.9rem; font-weight: 600;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
  z-index: 50; max-width: calc(100vw - 2rem);
}
.toast.error { background: var(--danger); color: #fff; }

/* ---------- mobile-first layout ---------- */
@media (max-width: 760px) {
  main { padding: 0.75rem 0.6rem calc(6.5rem + env(safe-area-inset-bottom)); }
  .topbar { padding: 0.5rem 0.75rem; justify-content: space-between; }
  .tabs { display: none; }

  /* bigger touch targets, and 16px inputs so iOS doesn't zoom on focus */
  select, input, textarea { font-size: 16px; padding: 0.55rem 0.6rem; }
  .icon-btn { font-size: 1.05rem; padding: 0.45rem 0.5rem; }
  .btn { padding: 0.55rem 1rem; }

  /* filters collapse behind the Filters button */
  #btn-filters { display: inline-block; }
  .toolbar:not(.filters-open) .filters { display: none; }
  .filters label { flex: 1 1 40%; }

  /* summary: horse identity spans full width, stat cards sit 2-up below (no sideways scroll) */
  .summary { grid-template-columns: 1fr 1fr; gap: 0.5rem; }
  .summary .scard.horse { grid-column: 1 / -1; }

  /* bottom navigation */
  .bottom-nav {
    display: flex; gap: 0.2rem;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: var(--card); border-top: 1px solid var(--line);
    padding: 0.3rem 0.5rem calc(0.35rem + env(safe-area-inset-bottom));
  }
  .bnav-btn {
    flex: 1 1 0; min-width: 0;
    border: 0; background: transparent; color: var(--muted); cursor: pointer;
    font: inherit; font-size: 0.66rem; font-weight: 600;
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    padding: 0.3rem 0; border-radius: 10px;
    white-space: nowrap;
  }
  .bnav-btn .bnav-ico { font-size: 1.3rem; line-height: 1.1; }
  .bnav-btn.active { color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }

  /* floating add button on the journal */
  body[data-view="journal"].authed .fab { display: flex; }
  .fab {
    position: fixed; right: 1rem; bottom: calc(4.6rem + env(safe-area-inset-bottom)); z-index: 29;
    width: 58px; height: 58px; border-radius: 50%; border: 0;
    background: var(--accent); color: var(--accent-ink);
    font-size: 2rem; line-height: 1; align-items: center; justify-content: center;
    box-shadow: 0 8px 22px rgba(0, 0, 0, 0.35); cursor: pointer;
  }
  #btn-add { display: none; }

  /* dialogs become bottom sheets */
  dialog {
    margin: auto 0 0; width: 100vw; max-width: 100vw;
    border-radius: 16px 16px 0 0; border-left: 0; border-right: 0; border-bottom: 0;
    max-height: 92dvh; overflow-y: auto;
    padding-bottom: calc(1.2rem + env(safe-area-inset-bottom));
  }
  .grid2 { grid-template-columns: 1fr; }

  /* events table stacks into cards */
  .events thead { display: none; }
  .events tbody tr { display: block; padding: 0.55rem 0.2rem; border-bottom: 1px solid var(--line); }
  .events tbody tr:last-child { border-bottom: 0; }
  .events td { display: flex; gap: 0.6rem; border: 0; padding: 0.12rem 0.55rem; }
  .events td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 82px; color: var(--muted);
    font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em;
    padding-top: 0.15rem;
  }
  .events td.num { text-align: left; }
  .events td.actions { justify-content: flex-end; }

  /* management tables (horses / people / types / users / invites) become cards */
  .manage thead { display: none; }
  .manage tbody tr {
    position: relative;
    display: block;
    padding: 0.6rem 5.6rem 0.6rem 0.85rem; /* right space clears the two action buttons */
    border-bottom: 1px solid var(--line);
  }
  .manage tbody tr:last-child { border-bottom: 0; }
  .manage tbody tr:has(.thumb-cell) { padding-left: 4rem; min-height: 3.5rem; } /* left space for thumbnail */
  .manage td { display: block; border: 0; padding: 0.06rem 0; overflow-wrap: anywhere; }
  .manage td.thumb-cell { position: absolute; left: 0.85rem; top: 0.6rem; padding: 0; }
  .manage td.thumb-cell .thumb, .manage td.thumb-cell .thumb-ph { width: 44px; height: 44px; }
  .manage td.name-cell { font-size: 1rem; margin-bottom: 0.1rem; }
  .manage td.actions {
    position: absolute; right: 0.5rem; top: 0.5rem;
    display: flex; gap: 0.1rem; padding: 0;
  }
  .manage td[data-label] { display: flex; gap: 0.5rem; align-items: baseline; font-size: 0.9rem; min-width: 0; }
  .manage td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 4.2rem; color: var(--muted);
    font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  }
  .manage td.num { text-align: left; }
  .manage td:empty { display: none; }
}
