:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --text: #1f1d1a;
  --muted: #6b665e;
  --border: #ddd6cb;
  --accent: #b5462f;
  --accent-text: #ffffff;
  --ok: #2f7d4f;
  --warn: #b26a00;
  --bad: #b3261e;
  --info: #2a62a8;
  --radius: 12px;
  --tabbar-h: 60px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18;
    --surface: #252320;
    --surface-2: #2f2c28;
    --text: #ece8e1;
    --muted: #a39d93;
    --border: #3b3833;
    --accent: #e07a5f;
    --accent-text: #1b1a18;
    --ok: #6cc28f;
    --warn: #f0b252;
    --bad: #f2867e;
    --info: #85b2ec;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.15rem; margin: 0; }

main {
  max-width: 720px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  outline: none;
}

.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: var(--surface);
  border-top: 1px solid var(--border);
}
.tabbar a {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .75rem;
}
.tabbar a .ico { font-size: 1.25rem; line-height: 1; }
.tabbar a[aria-current="page"] { color: var(--accent); font-weight: 600; }
.badge {
  position: absolute; top: 6px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--bad); color: #fff; font-size: .7rem; line-height: 18px; text-align: center;
}

.muted { color: var(--muted); }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; margin: 0 0 12px;
}
button, .btn {
  font: inherit; cursor: pointer;
  min-height: 44px; padding: 0 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--bad); }

/* ---------- Topbar actions ---------- */
#topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border-radius: 22px;
  font-size: 1.4rem; line-height: 1; text-decoration: none; color: var(--accent);
  background: transparent; border: 0;
}
.icon-btn:active { background: var(--surface-2); }
.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.btn.small, button.small { min-height: 36px; padding: 0 12px; font-size: .9rem; }
button.block, .btn.block { width: 100%; }
button:disabled { opacity: .5; cursor: not-allowed; }
.danger-solid { background: var(--bad); border-color: var(--bad); color: #fff; }
a.link, .back { color: var(--accent); text-decoration: none; }
.back { display: inline-block; margin: 0 0 12px; padding: 6px 0; }
.center { text-align: center; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 2px; }

/* ---------- Month navigation ---------- */
.month-nav { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; margin: 0 0 8px; }
.month-nav .icon-btn { font-size: 2rem; }
.month-title { position: relative; text-align: center; font-size: 1.3rem; font-weight: 650; cursor: pointer; }
.month-title input {
  position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer;
}

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0 0 16px; padding: 0; display: grid; gap: 8px; }
.row-card {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 60px; padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  color: inherit; text-decoration: none;
}
.row-card:active { background: var(--surface-2); }
.row-main { min-width: 0; }
.row-title { font-weight: 600; overflow-wrap: anywhere; }
.row-sub, .row-note { font-size: .875rem; color: var(--muted); }
.row-note { font-style: italic; }
.row-side { flex: none; text-align: right; max-width: 55%; }
.chev { color: var(--muted); font-size: 1.4rem; }
h2.section { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 0 8px; }

/* ---------- Status badges ---------- */
.status { display: inline-block; font-size: .85rem; line-height: 1.3; }
.status b { font-weight: 600; }
.status-detail { display: block; font-size: .8rem; color: var(--muted); }
.status-ok b { color: var(--ok); }
.status-info b { color: var(--info); }
.status-bad b { color: var(--bad); }
.status-muted b { color: var(--muted); }

/* ---------- Empty states ---------- */
.empty { text-align: center; padding: 40px 16px; }
.empty-ico { font-size: 3rem; }
.empty h2 { margin: 8px 0 4px; font-size: 1.2rem; }
.empty p { max-width: 34ch; margin: 0 auto 20px; }

/* ---------- Detail ---------- */
.facts { margin: 0; display: grid; gap: 8px; }
.facts div { display: grid; grid-template-columns: 7rem 1fr; gap: 8px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.notes { white-space: pre-wrap; margin: 12px 0 0; padding-top: 12px; border-top: 1px solid var(--border); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 0 0 16px; }
.stats div {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 4px; text-align: center; display: flex; flex-direction: column;
}
.stats b { font-size: 1.3rem; }
.stats span { font-size: .72rem; color: var(--muted); }
.stats .bad b { color: var(--bad); }

/* ---------- Forms ---------- */
.form { display: grid; gap: 16px; }
.field { display: grid; gap: 6px; margin: 0; padding: 0; border: 0; min-width: 0; }
.field > span, .field > legend { font-weight: 600; font-size: .95rem; padding: 0; }
.field .opt { font-weight: 400; font-style: normal; color: var(--muted); font-size: .85rem; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.hint { color: var(--muted); font-size: .8rem; }
input, textarea, select {
  font: inherit; font-size: 16px; /* 16px prevents iOS zoom on focus */
  width: 100%; min-height: 44px; padding: 10px 12px;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px;
}
textarea { resize: vertical; }
input[type="radio"] { width: auto; min-height: 0; }
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; pointer-events: none; }
.segmented span {
  display: flex; align-items: center; justify-content: center; min-height: 44px;
  background: var(--surface); cursor: pointer; font-size: .95rem;
}
.segmented label + label span { border-left: 1px solid var(--border); }
.segmented input:checked + span { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.segmented input:focus-visible + span { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: -3px; }
.form-errors { color: var(--bad); margin: 0; }
.danger-zone { margin-top: 24px; }
.danger-zone .card p { margin-top: 0; }

/* ---------- Dialog ---------- */
.dialog {
  width: min(92vw, 420px); padding: 20px; border: 1px solid var(--border); border-radius: 16px;
  background: var(--surface); color: var(--text);
}
.dialog::backdrop { background: rgb(0 0 0 / .45); }
.dialog h2 { margin: 0 0 8px; font-size: 1.15rem; }
.dialog form { display: grid; gap: 12px; }
.dialog p { margin: 0; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

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

@media (min-width: 720px) {
  .tabbar { max-width: 720px; margin: 0 auto; border: 1px solid var(--border); border-bottom: 0; border-radius: 16px 16px 0 0; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
