:root {
  --bg: #f3f6f6;
  --surface: #ffffff;
  --surface-2: #e9eeee;
  --text: #1a2427;
  --muted: #63727a;
  --border: #d9e1e2;
  --accent: #e03e3e;
  --accent-soft: #fdeaea;
  --accent-text: #ffffff;
  --tile: #ffffff;
  --tile-on: #e03e3e;
  --tile-on-text: #ffffff;
  --tile-recent: #e4eaeb;
  --done: #2f9e44;
  --danger: #c92a2a;
  --shadow: 0 1px 2px rgba(20, 40, 45, .06), 0 2px 8px rgba(20, 40, 45, .05);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f181a;
    --surface: #182326;
    --surface-2: #223034;
    --text: #edf3f3;
    --muted: #93a4a8;
    --border: #2b3a3e;
    --accent: #ff5a5a;
    --accent-soft: #3a1c1e;
    --accent-text: #1a0808;
    --tile: #1d2a2d;
    --tile-on: #e5484d;
    --tile-on-text: #ffffff;
    --tile-recent: #172124;
    --done: #51cf66;
    --danger: #ff6b6b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- Kopf */

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

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.1rem; }

.sync {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted);
  opacity: .6;
}
.sync.online { background: var(--done); opacity: 1; }
.sync.offline { background: var(--danger); opacity: 1; }

.tabs {
  display: flex;
  gap: 4px;
  margin-left: auto;
  padding: 3px;
  background: var(--surface-2);
  border-radius: 999px;
}
.tabs button {
  border: 0;
  background: transparent;
  padding: 6px 14px;
  border-radius: 999px;
  font-weight: 600;
  color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.who {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: .9rem;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

main { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* ------------------------------------------------------------- Buttons */

.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 14px;
  font-weight: 600;
  background: var(--surface-2);
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  width: 38px; height: 38px;
  border-radius: 10px;
  font-size: 1.1rem;
  line-height: 1;
  display: inline-grid;
  place-items: center;
}

/* ------------------------------------------------------------ Eingabe */

.add-bar {
  position: sticky;
  top: calc(62px + env(safe-area-inset-top));
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px 6px 4px 14px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.add-bar:focus-within { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); }
.add-icon { color: var(--accent); font-size: 1.5rem; font-weight: 700; line-height: 1; }
.add-bar input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  padding: 10px 0;
  font-size: 1.05rem;
  outline: none;
}
.add-bar input::-webkit-search-cancel-button { display: none; }
.add-clear { border: 0; background: var(--surface-2); border-radius: 50%; width: 30px; height: 30px; color: var(--muted); }

/* --------------------------------------------------------------- Blöcke */

.block { margin-bottom: 22px; }
.block-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.block-head h2, .card h2 { margin: 0; font-size: 1.1rem; display: flex; align-items: center; gap: 8px; }

.count-badge {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: .78rem;
  font-weight: 700;
}
.count-badge:empty { display: none; }

.hint { color: var(--muted); font-size: .85rem; margin: 10px 0 0; }
.empty { color: var(--muted); font-size: .9rem; margin: 4px 0; }
.done-empty {
  grid-column: 1 / -1;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 18px;
  text-align: center;
  font-size: 1rem;
}

/* --------------------------------------------------------------- Kacheln */

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(98px, 1fr));
  gap: 8px;
}
.tiles-small { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); margin-top: 10px; }

.tile {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--border);
  background: var(--tile);
  border-radius: 12px;
  padding: 8px 6px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-width: 0;
  text-align: center;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  transition: transform .12s ease, background-color .15s ease, opacity .3s ease;
  overflow: hidden;
}
.tile:active { transform: scale(.95); }
.tile:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 1px; }

.tile.on {
  background: var(--tile-on);
  border-color: var(--tile-on);
  color: var(--tile-on-text);
  box-shadow: 0 2px 6px color-mix(in srgb, var(--tile-on) 35%, transparent);
}
.tile.recent { background: var(--tile-recent); border-color: transparent; color: var(--muted); }
.tile.recent .tile-name { text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--muted) 60%, transparent); }
.tile.new { border-style: dashed; border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.tile-name {
  font-size: .82rem;
  font-weight: 600;
  line-height: 1.15;
  max-width: 100%;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.tile-spec {
  font-size: .72rem;
  line-height: 1.1;
  opacity: .85;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Wegstreichen: Linie quer über die Kachel, dann ausblenden */
.tile::after {
  content: "";
  position: absolute;
  left: 10%;
  top: 50%;
  width: 80%;
  height: 3px;
  border-radius: 2px;
  background: currentColor;
  transform: scaleX(0) rotate(-12deg);
  transform-origin: left center;
  transition: transform .25s ease;
  pointer-events: none;
}
.tile.striking::after { transform: scaleX(1) rotate(-12deg); }
.tile.striking { opacity: .45; transition-delay: 0s, 0s, .15s; }

/* Bild: Foto, Symbol oder Anfangsbuchstabe */
.pic {
  width: 44px; height: 44px;
  display: grid;
  place-items: center;
  flex: none;
}
.pic img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; }
.pic .emoji { font-size: 2rem; line-height: 1; }
.pic .letter {
  width: 38px; height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1.2rem;
  background: color-mix(in srgb, currentColor 14%, transparent);
}
.pic.pic-sm { width: 30px; height: 30px; }
.pic.pic-sm .emoji { font-size: 1.35rem; }
.pic.pic-sm .letter { width: 26px; height: 26px; font-size: .85rem; color: var(--muted); }
.pic.pic-sm img { border-radius: 7px; }
.pic.pic-lg { width: 52px; height: 52px; }
.pic.pic-lg .emoji { font-size: 2.4rem; }

/* -------------------------------------------------------------- Katalog */

.catalog { display: flex; flex-direction: column; gap: 10px; }
.cat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.cat summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
}
.cat summary::-webkit-details-marker { display: none; }
.cat summary::after {
  content: "›";
  margin-left: auto;
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  transition: transform .15s ease;
}
.cat[open] summary::after { transform: rotate(90deg); }
.cat-emoji { font-size: 1.3rem; }
.cat .tiles { padding: 0 10px 12px; }

/* --------------------------------------------------------------- Historie */

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.stat b { display: block; font-size: 1.6rem; line-height: 1.1; }
.stat span { color: var(--muted); font-size: .85rem; }

.history-grid { display: grid; grid-template-columns: minmax(240px, 1fr) 2fr; gap: 16px; align-items: start; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.search input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 9px 12px;
}
.search input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); }

.timeline { margin-top: 10px; }
.month { margin: 18px 0 6px; font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.month:first-child { margin-top: 6px; }
.log-day { display: grid; grid-template-columns: 84px 1fr; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.log-date { font-weight: 700; font-size: .9rem; }
.log-date small { display: block; color: var(--muted); font-weight: 500; }
.log-date .log-count { font-size: .78rem; margin-top: 2px; }
.log-items { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.log-item { display: flex; align-items: center; gap: 10px; min-width: 0; }
.log-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.log-name strong { font-weight: 600; }
.log-name .spec { color: var(--muted); font-size: .88rem; }
.log-name small { display: block; color: var(--muted); font-size: .76rem; line-height: 1.2; }
.log-by { margin: 4px 0 0; color: var(--muted); font-size: .8rem; }
.readd {
  flex: none;
  width: 30px; height: 30px;
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 8px;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
}
.readd:hover { color: var(--accent); border-color: var(--accent); }

.more { width: 100%; margin-top: 12px; }
.app-version { text-align: center; color: var(--muted); font-size: .8rem; margin: 24px 0 8px; }

/* ----------------------------------------------------------- Änderungen */

.user-filter { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.activity { padding: 6px 14px; }
.act-day { margin: 14px 0 4px; font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.act-day:first-child { margin-top: 8px; }
.act { display: flex; gap: 10px; align-items: flex-start; padding: 9px 0; border-bottom: 1px solid var(--border); }
.act:last-child { border-bottom: 0; }
.act-body { flex: 1; min-width: 0; }
.act-body p { margin: 0; overflow-wrap: anywhere; }
.act-body .what { color: var(--muted); }
.act-changes { margin: 4px 0 0; padding-left: 16px; font-size: .86rem; color: var(--muted); }
.act-changes li { overflow-wrap: anywhere; }
.act time { flex: none; color: var(--muted); font-size: .82rem; padding-top: 2px; }
.avatar {
  flex: none;
  width: 30px; height: 30px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700; font-size: .85rem;
  color: #fff;
  background: hsl(var(--hue, 0) 50% 46%);
}

/* --------------------------------------------------------------- Dialoge */

dialog {
  border: 0;
  border-radius: 18px;
  padding: 0;
  width: min(520px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(5, 15, 18, .5); }

#item-form, #name-form { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
#name-form .hint { margin: 0; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.dialog-head h2 { margin: 0; font-size: 1.2rem; overflow-wrap: anywhere; }
.dialog-title { display: flex; align-items: center; gap: 10px; min-width: 0; }

.field { display: flex; flex-direction: column; gap: 5px; border: 0; padding: 0; margin: 0; min-width: 0; }
.field > span, .field > legend { font-weight: 600; font-size: .9rem; padding: 0; margin-bottom: 5px; }
.field input:not([type="file"]), .field select {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 9px 11px;
  width: 100%;
}
.field input:focus, .field select:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 0; }

.tag-picker { display: flex; flex-wrap: wrap; gap: 6px; }
#spec-chips { margin-top: 8px; }
.tag-toggle {
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 999px;
  padding: 4px 11px;
  font-size: .85rem;
  font-weight: 600;
  color: var(--muted);
}
.tag-toggle:hover { color: var(--text); }
.tag-toggle[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.pic-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.file-btn { position: relative; cursor: pointer; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; }
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  gap: 4px;
  margin-top: 8px;
  max-height: 172px;
  overflow-y: auto;
  padding: 2px;
}
.emoji-btn {
  border: 1px solid transparent;
  background: var(--surface-2);
  border-radius: 10px;
  height: 42px;
  font-size: 1.4rem;
  line-height: 1;
}
.emoji-btn[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }

.meal-meta { margin: 0; font-size: .82rem; color: var(--muted); }
.meal-meta:empty { display: none; }
.form-error { color: var(--danger); margin: 0; font-size: .9rem; }
.form-error:empty { display: none; }

#login-dialog { width: min(400px, calc(100vw - 24px)); }
#login-dialog::backdrop { background: var(--bg); }
#login-form { padding: 24px 20px 20px; display: flex; flex-direction: column; gap: 14px; }
.login-head { text-align: center; }
.login-head img { border-radius: 14px; }
.login-head h2 { margin: 8px 0 0; font-size: 1.3rem; }
.login-head .hint { margin: 4px 0 0; }
.login-btn { padding: 11px; font-size: 1rem; }
.login-btn:disabled { opacity: .6; }
.account-form { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.account-form p { margin: 0; }

.bell { padding: 5px 9px; margin-left: 4px; }
.bell.off { opacity: .45; }
.topbar .bell + .who { margin-left: 0; }
.notify-form .hint { margin: 0; }
.notify-form .dialog-head h2 { overflow-wrap: normal; hyphens: auto; min-width: 0; }
.notify-form .dialog-head .icon-btn { flex: none; }
#notify-dialog { overflow-x: hidden; }
.steps { margin: 0; padding-left: 22px; display: flex; flex-direction: column; gap: 14px; }
.steps li { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.steps li::marker { font-weight: 700; color: var(--accent); }
.steps small { color: var(--muted); font-size: .82rem; }
.steps a:not(.btn) { color: var(--accent); font-weight: 600; }
.steps a.btn { text-decoration: none; }
.steps li > * { max-width: 100%; }
.copy-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; width: 100%; font-size: .85rem; }
.copy-row > span { flex: 0 0 100%; color: var(--muted); }
.copy-row code {
  flex: 1 1 140px;
  min-width: 0;
  overflow-wrap: anywhere;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: .8rem;
}
.copy-row .btn { padding: 5px 9px; font-size: .8rem; }
.switch-row { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.switch-row input { width: 20px; height: 20px; accent-color: var(--accent); }

.dialog-foot { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 12px;
  display: flex;
  gap: 14px;
  align-items: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .25);
  z-index: 50;
  width: max-content;
  max-width: calc(100vw - 24px);
}
.toast button { border: 0; background: none; color: var(--accent); font-weight: 700; padding: 0; }

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px; }
  .brand span:not(.sync) { font-size: 1rem; }
  .tabs { order: 3; width: 100%; margin-left: 0; }
  .tabs button { flex: 1; padding: 6px 4px; font-size: .88rem; }
  .who { margin-left: auto; }
  main { padding: 12px 12px 32px; }
  .add-bar { top: calc(100px + env(safe-area-inset-top)); }
  .tiles { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; }
  .pic { width: 38px; height: 38px; }
  .pic .emoji { font-size: 1.75rem; }
  .history-grid { grid-template-columns: 1fr; }
  .stats { gap: 8px; }
  .stat { padding: 10px; }
  .stat b { font-size: 1.3rem; }
}
