:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --line: #d8dbe2;
  --text: #14171d;
  --muted: #626a78;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #c8342b;
  --good: #12855b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 19, 25, .07), 0 1px 8px rgba(16, 19, 25, .05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101319;
    --surface: #191d26;
    --surface-2: #232936;
    --line: #303747;
    --text: #eef1f6;
    --muted: #9aa4b6;
    --accent: #4c8dff;
    --accent-text: #08101f;
    --danger: #ff6b5e;
    --good: #3ddc9a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding: 0 max(12px, env(safe-area-inset-left)) calc(28px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-right));
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

/* ---------------------------------------------------------------- top bar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 -12px 14px;
  padding: max(8px, env(safe-area-inset-top)) 14px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(10px);
  border-bottom: 1px solid var(--line);
}

.topbar h1 { font-size: 19px; margin: 0; letter-spacing: -.01em; }

.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-right: auto;
  font-size: 12px;
  color: var(--muted);
}

.status .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: background .2s;
}

.status.online .dot { background: var(--good); }
.status.offline .dot { background: var(--danger); }
.status.offline { color: var(--danger); }

/* ---------------------------------------------------------------- buttons */

.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 14px;
  min-height: 44px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .08s ease, filter .15s ease;
}

.btn:active { transform: scale(.97); }
.btn-ghost { background: var(--surface); border-color: var(--line); }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-danger { background: transparent; color: var(--danger); border-color: currentColor; }
.btn-wide { display: block; width: 100%; padding: 15px; font-size: 16px; }

.btn-icon {
  min-width: 44px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
  background: var(--surface-2);
  color: var(--muted);
}

.btn-icon-danger { color: var(--danger); }
.btn-icon:disabled { opacity: .3; }

#edit-toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: transparent; }

/* ----------------------------------------------------------------- groups */

.group { margin: 0 0 26px; }

.group h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  margin: 0 4px 8px;
}

.items { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.empty { color: var(--muted); font-size: 14px; margin: 4px; }
.items:not(:empty) + .empty { display: none; }

.item {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
}

.item-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }

.name {
  font-size: 17px;
  font-weight: 650;
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.name-edit, .add-form input, .field input {
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
  min-height: 44px;
  width: 100%;
  min-width: 0;
}

.name-edit:focus, .add-form input:focus, .field input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

/* edit mode swaps the label for a rename box, and reveals the tools */
.name-edit, .edit-tools, .add-form { display: none; }
body.editing .name { display: none; }
body.editing .name-edit { display: block; flex: 1; }
body.editing .edit-tools { display: flex; gap: 6px; }
body.editing .add-form { display: flex; gap: 8px; margin-top: 10px; }
body.editing .bump-hint { visibility: hidden; }

/* --------------------------------------------------------------- tallies */

.counter-body { display: flex; gap: 10px; align-items: stretch; }

.bump {
  flex: 1;
  min-height: 96px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  transition: transform .06s ease, filter .1s ease;
}

.bump:active { transform: scale(.98); filter: brightness(1.08); }

.count { font-size: 44px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.bump-hint { font-size: 12px; opacity: .8; letter-spacing: .03em; }

.btn-minus {
  flex: 0 0 74px;
  font-size: 20px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
}

/* ------------------------------------------------------ start/end of day */

.reading-body { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.field { display: grid; gap: 5px; min-width: 0; }
.field > span { font-size: 12px; color: var(--muted); }
.field input { font-variant-numeric: tabular-nums; font-size: 18px; font-weight: 600; }

.change {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  border-top: 1px dashed var(--line);
  padding-top: 8px;
  font-size: 12px;
  color: var(--muted);
}

.change-value { font-size: 17px; color: var(--text); font-variant-numeric: tabular-nums; }
.change-value.up { color: var(--good); }
.change-value.down { color: var(--danger); }

/* ---------------------------------------------------------------- actions */

.actions { display: grid; gap: 10px; margin: 30px 0 10px; }
.note { color: var(--muted); font-size: 12px; text-align: center; margin: 0; }

/* ----------------------------------------------------------------- dialog */

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  width: min(560px, 92vw);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .35);
}

dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dialog-inner { padding: 16px; display: grid; gap: 12px; }
.dialog-inner h2 { margin: 0; font-size: 17px; }

#md-text {
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  width: 100%;
  resize: vertical;
}

.dialog-buttons { display: flex; gap: 8px; justify-content: flex-end; }

/* ------------------------------------------------------------------ toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: 11px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
  z-index: 50;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

@media (min-width: 640px) {
  body { max-width: 680px; margin: 0 auto; }
  .topbar { margin-inline: 0; border-radius: 0 0 var(--radius) var(--radius); }
}
