/* Mobile first: the base styles are for phones, wider screens get extras at the bottom. */
:root {
  color-scheme: light dark;
  --primary: #1f8f55;
  --on-primary: #ffffff;
  --primary-container: #c6f0d6;
  --on-primary-container: #00210f;
  --bg: #f6faf6;
  --surface: #ffffff;
  --surface-2: #eaf2ec;
  --text: #171d19;
  --muted: #5b665e;
  --outline: #c3cdc5;
  --danger: #ba1a1a;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.12);
  --radius: 16px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --primary: #7fdba0;
    --on-primary: #00391c;
    --primary-container: #00522b;
    --on-primary-container: #c6f0d6;
    --bg: #0f1511;
    --surface: #171d19;
    --surface-2: #222b25;
    --text: #dfe5df;
    --muted: #a0aba3;
    --outline: #3f4942;
    --danger: #ffb4ab;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
  -webkit-tap-highlight-color: transparent;
}
body.has-composer { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 88px); }

h1, h2, h3 { margin: 0; line-height: 1.25; }
button, input { font: inherit; color: inherit; }
svg { width: 24px; height: 24px; fill: currentColor; flex: none; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

/* ---- Top bar ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 1.5rem; font-weight: 600; }

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

/* ---- Journal ---- */
.day { margin-top: 8px; }
.day-head {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--primary);
  padding: 12px 4px 6px;
}
.entries { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.entries li + li { border-top: 1px solid var(--outline); }
.entry {
  display: flex;
  gap: 14px;
  width: 100%;
  min-height: 56px;
  padding: 12px 16px;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}
.entry:active { background: var(--surface-2); }
.entry-time { color: var(--muted); font-variant-numeric: tabular-nums; padding-top: 1px; }
.entry-main { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; min-width: 0; }
.entry-text { overflow-wrap: anywhere; }
.chip {
  font-size: 0.75rem;
  padding: 1px 8px;
  border-radius: 8px;
  background: var(--primary-container);
  color: var(--on-primary-container);
}

.empty { text-align: center; padding: 48px 24px; color: var(--muted); }
.empty img { border-radius: 24px; }
.empty h2 { color: var(--text); margin: 16px 0 8px; font-size: 1.25rem; }

.banner {
  margin: 8px 0;
  padding: 12px 16px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--danger) 15%, var(--surface));
}

/* ---- Composer ---- */
.composer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-b));
  z-index: 6;
  padding: 10px 12px;
  background: var(--surface);
  border-top: 1px solid var(--outline);
}
.composer-row, .when-row { display: flex; align-items: center; gap: 8px; max-width: 720px; margin: 0 auto; }
.when-row { padding: 0 4px 8px; }
.when-row input { flex: 1; }
.composer input, .sheet input {
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--outline);
  border-radius: 24px;
  background: var(--bg);
  min-width: 0;
}
#what { flex: 1; font-size: 1rem; }
.composer input:focus, .sheet input:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }

.fab {
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 16px;
  background: var(--primary);
  color: var(--on-primary);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}

.icon-btn {
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: none;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: var(--muted);
  flex: none;
}
.icon-btn:active, .icon-btn.active { background: var(--surface-2); color: var(--primary); }

/* ---- Tab bar ---- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 7;
  display: flex;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--surface-2);
}
.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 500;
}
.tabbar a svg { padding: 2px 18px; width: 60px; height: 30px; border-radius: 15px; }
.tabbar a.active { color: var(--text); }
.tabbar a.active svg { background: var(--primary-container); color: var(--on-primary-container); }

/* ---- Week ---- */
.week-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 4px 0 8px;
}
.week-title { text-align: center; display: flex; flex-direction: column; align-items: center; }
.week-grid { display: grid; gap: 10px; }
.day-card { background: var(--surface); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.day-card h3 { font-size: 1rem; margin-bottom: 6px; }
.day-card h3 .muted { font-weight: 400; font-size: 0.875rem; }
.day-card p { margin: 0; }
.meal { display: flex; gap: 12px; padding: 4px 0; }
.meal-label { flex: 0 0 9.5em; font-size: 0.875rem; color: var(--muted); padding-top: 2px; }
.meal ul { list-style: none; margin: 0; padding: 0; min-width: 0; }
.meal .link { text-align: left; color: var(--text); overflow-wrap: anywhere; }

/* ---- Buttons & cards ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 20px;
  border-radius: 22px;
  border: 0;
  font-weight: 500;
  cursor: pointer;
  background: none;
}
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.filled { background: var(--primary); color: var(--on-primary); }
.btn.tonal { background: var(--primary-container); color: var(--on-primary-container); }
.btn.outline { border: 1px solid var(--outline); color: var(--primary); }
.btn.text { color: var(--primary); }
.btn.danger-text { color: var(--danger); padding: 0 12px; }
.btn.wide { width: 100%; margin: 16px 0 8px; }
.link { border: 0; background: none; padding: 0; color: var(--primary); cursor: pointer; font: inherit; }
a.link { text-decoration: none; font-weight: 500; }

.card { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow); }
.card h2 { font-size: 1.125rem; margin-bottom: 4px; }
.card p { margin: 0 0 12px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }

/* ---- Edit sheet (bottom sheet on phones) ---- */
.sheet {
  width: 100%;
  max-width: 560px;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 28px 28px 0 0;
  background: var(--surface);
  color: var(--text);
}
.sheet::backdrop { background: rgb(0 0 0 / 0.4); }
.sheet[open] { animation: slide-up 0.2s ease-out; }
.sheet form { display: flex; flex-direction: column; gap: 14px; padding: 24px 20px calc(16px + var(--safe-b)); }
.sheet h2 { font-size: 1.375rem; font-weight: 500; }
.sheet label { display: flex; flex-direction: column; gap: 6px; font-size: 0.875rem; color: var(--muted); }
.sheet input { color: var(--text); font-size: 1rem; border-radius: 12px; }
.sheet p { margin: -6px 0 0; }
.sheet-actions { display: flex; align-items: center; gap: 4px; }
.spacer { flex: 1; }

@keyframes slide-up { from { transform: translateY(100%); } to { transform: none; } }

/* ---- Snackbar ---- */
.snackbar {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 84px);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: 560px;
  margin: 0 auto;
  padding: 14px 16px;
  border-radius: 8px;
  background: #2f3631;
  color: #eff1ed;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.25);
}
.snackbar span { flex: 1; }
.snackbar .link { color: #7fdba0; font-weight: 600; }

/* ---- Larger screens ---- */
@media (min-width: 720px) {
  .sheet { margin: auto; border-radius: 28px; }
  .sheet[open] { animation: none; }
  .week-grid { grid-template-columns: 1fr 1fr; }
  .snackbar { bottom: 24px; }
}

/* ---- Print / PDF: only the week tables ---- */
.print-area { display: none; }

@media print {
  @page { size: A4 landscape; margin: 12mm; }
  body { background: #fff; color: #000; padding: 0; }
  body > :not(.print-area) { display: none !important; }
  .print-area { display: block; }
  .print-week { break-after: page; }
  .print-week:last-child { break-after: auto; }
  .print-week h2 { font-size: 14pt; margin-bottom: 8px; }
  .print-week table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 9.5pt; }
  .print-week th, .print-week td { border: 1px solid #000; padding: 6px 5px; vertical-align: top; text-align: left; overflow-wrap: anywhere; }
  .print-week thead th { background: #e8e8e8; }
  .print-week tbody th { width: 11%; }
  .print-week small { font-weight: 400; }
}
