:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --text: #1f2421;
  --muted: #69716c;
  --line: #e1dfd7;
  --accent: #2f7d4f;
  --accent-soft: #e3f1e8;
  --danger: #b3261e;
  --breakfast: #e9a23b;
  --lunch: #3b82c4;
  --dinner: #8a5cc2;
  --snacks: #2f9e8f;
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151816;
    --surface: #1f2321;
    --surface-2: #282d2a;
    --text: #ecefed;
    --muted: #9aa39e;
    --line: #343a37;
    --accent: #5cc187;
    --accent-soft: #23382b;
    --danger: #ff8a80;
  }
}

* { 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, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}

button, input, textarea { font: inherit; color: inherit; }

button { cursor: pointer; }

button:disabled { cursor: default; opacity: 0.45; }

/* --- Kopfzeile --- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  padding: calc(env(safe-area-inset-top) + 8px) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
}

.topbar-row { display: flex; align-items: center; gap: 8px; }

.topbar-row + .topbar-row { margin-top: 6px; }

.app-title { font-size: 1.25rem; margin: 0; flex: 1; }

.view-switch {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 3px;
}

.view-switch button {
  border: 0;
  background: transparent;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.9rem;
}

.view-switch button[aria-pressed="true"] {
  background: var(--surface);
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.15);
  font-weight: 600;
}

.nav .period { flex: 1; text-align: center; min-width: 0; }

.kw {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 0.8rem;
  padding: 2px 8px;
  border-radius: 999px;
  margin-right: 6px;
}

.kw:empty { display: none; }

#period-label { font-weight: 600; }

.icon-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.5rem;
  line-height: 1;
}

.today-btn {
  border: 1px solid var(--accent);
  color: var(--accent);
  background: transparent;
  padding: 7px 12px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.9rem;
}

main { padding: 12px max(16px, env(safe-area-inset-left)) 80px; }

/* --- Wochenansicht: Smartphone = Tagesliste --- */

.week { display: flex; flex-direction: column; gap: 12px; }

.day {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  scroll-margin-top: 110px;
}

.day.today { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }

.day.past { opacity: 0.7; }

.day-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0 0 4px;
  font-size: 1.05rem;
}

.day-head .wd-short { display: none; }

.day-head .date { color: var(--muted); font-weight: 500; }

.badge {
  margin-left: auto;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  padding: 1px 8px;
  border-radius: 999px;
}

.meal { border-top: 1px solid var(--line); padding: 6px 0; }

.meal-head { display: flex; align-items: center; gap: 6px; }

.meal-label {
  flex: 1;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}

.meal-label::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: 1px;
  background: var(--dot);
}

.meal-breakfast { --dot: var(--breakfast); }
.meal-lunch { --dot: var(--lunch); }
.meal-dinner { --dot: var(--dinner); }
.meal-snacks { --dot: var(--snacks); }

.add-btn {
  border: 1px dashed var(--line);
  background: transparent;
  color: var(--accent);
  width: 34px;
  height: 30px;
  border-radius: 8px;
  font-size: 1.2rem;
  line-height: 1;
}

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

.entry {
  display: flex;
  align-items: stretch;
  background: var(--surface-2);
  border-left: 3px solid var(--dot);
  border-radius: 8px;
  overflow: hidden;
}

.entry-btn {
  flex: 1;
  min-width: 0;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 7px 10px;
}

.entry-title { display: block; font-weight: 500; overflow-wrap: break-word; hyphens: auto; -webkit-hyphens: auto; }

.entry-notes {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.entry-link {
  display: flex;
  align-items: center;
  padding: 0 12px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 700;
  border-left: 1px solid var(--line);
}

/* --- Wochenansicht: Tablet hochkant = Tage in zwei Spalten --- */

@media (min-width: 600px) and (max-width: 999px) {
  .week {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
  }
}

/* --- Wochenansicht: Tablet quer/PC = Raster 7 Tage nebeneinander --- */

@media (min-width: 1000px) {
  main { padding-left: 16px; padding-right: 16px; }

  .week {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    grid-template-rows: auto repeat(4, auto);
    gap: 0 8px;
  }

  .day {
    grid-row: span 5;
    display: grid;
    grid-template-rows: subgrid;
    padding: 8px;
  }

  .day-head { flex-wrap: wrap; font-size: 0.95rem; }
  .day-head .badge { margin-left: 0; }

  .meal { padding: 6px 0 8px; }
  .meal-label { font-size: 0.7rem; }
  .entry-btn { padding: 5px 7px; font-size: 0.9rem; }
  .entry-link { padding: 0 7px; }
  .add-btn { width: 28px; height: 26px; }
}

@media (min-width: 1000px) and (max-width: 1199px) {
  .day-head .wd-long { display: none; }
  .day-head .wd-short { display: inline; }
}

/* --- Monatsansicht --- */

.month {
  display: grid;
  grid-template-columns: 36px repeat(7, minmax(0, 1fr));
  gap: 4px;
}

.month-head {
  text-align: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  padding: 4px 0;
}

.kw-btn {
  border: 0;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 0.8rem;
  border-radius: 8px;
}

.month-day {
  position: relative;
  min-height: 64px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 4px;
  text-align: left;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  overflow: hidden;
}

.month-day.other-month { opacity: 0.4; }
.month-day.past:not(.other-month) { background: var(--surface-2); }
.month-day.today { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.month-day.today .month-num { background: var(--accent); color: #fff; border-radius: 999px; padding: 0 6px; }

.month-num { font-weight: 600; font-size: 0.9rem; }

.month-count {
  position: absolute;
  right: 4px;
  bottom: 4px;
  min-width: 20px;
  text-align: center;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0 5px;
}

.month-titles { display: none; list-style: none; margin: 2px 0 0; padding: 0; width: 100%; }

.month-titles li {
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.month-titles .more { color: var(--muted); }

@media (min-width: 760px) {
  .month { grid-template-columns: 44px repeat(7, minmax(0, 1fr)); gap: 6px; }
  .month-day { min-height: 96px; padding: 6px; }
  .month-titles { display: block; }
  .month-count { display: none; }
}

/* --- Dialog --- */

dialog {
  border: 0;
  border-radius: 16px;
  padding: 0;
  width: min(480px, calc(100vw - 24px));
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.3);
}

dialog::backdrop { background: rgb(0 0 0 / 0.35); }

dialog form { padding: 18px; display: grid; gap: 6px; }

dialog h2 { margin: 0 0 6px; font-size: 1.1rem; }

dialog label { font-weight: 600; font-size: 0.9rem; margin-top: 6px; }

.optional { color: var(--muted); font-weight: 400; }

dialog input,
dialog textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--bg);
  font-size: 16px; /* verhindert Zoom auf dem iPhone */
}

dialog input:focus,
dialog textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }

.field-error { color: var(--danger); margin: 0; font-size: 0.85rem; }

.suggest-wrap { position: relative; }

.suggestions {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  z-index: 2;
  list-style: none;
  margin: 0;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
  max-height: 240px;
  overflow-y: auto;
}

.suggestions button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
}

.suggestions button:hover,
.suggestions button:focus { background: var(--accent-soft); }

.has-link { color: var(--accent); font-weight: 700; }

.dialog-actions { display: flex; gap: 8px; margin-top: 12px; align-items: center; }

.dialog-actions .spacer { flex: 1; }

.dialog-actions button {
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 9px 14px;
  border-radius: 10px;
}

.dialog-actions .primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.dialog-actions .danger { color: var(--danger); border-color: transparent; background: transparent; padding-left: 0; }

/* --- Statuszeile --- */

.status {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 32px);
  background: var(--text);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 0.9rem;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.25);
}

.status[hidden] { display: none; }

.status.error { background: var(--danger); color: #fff; border-radius: 14px; }

.status button {
  border: 0;
  background: rgb(255 255 255 / 0.25);
  color: inherit;
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 600;
  white-space: nowrap;
}
