/* ---------- Kolory i zmienne ---------- */
:root {
  color-scheme: light dark;
  --bg: #fbf7f2;
  --surface: #ffffff;
  --surface-2: #f4ede5;
  --text: #1f1a17;
  --muted: #655c55;
  --border: #e5dbd0;
  --primary: #c2410c;
  --primary-hover: #9a3412;
  --primary-text: #ffffff;
  --primary-soft: #fde7da;
  --success: #15803d;
  --danger: #b91c1c;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 6px 20px rgb(0 0 0 / 0.04);
  --nav-h: 64px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171412;
    --surface: #221e1b;
    --surface-2: #2c2723;
    --text: #f3ede8;
    --muted: #b3a9a1;
    --border: #3b342f;
    --primary: #fb8c4c;
    --primary-hover: #fdba8c;
    --primary-text: #1a0f08;
    --primary-soft: #3b2417;
    --success: #4ade80;
    --danger: #f87171;
    --shadow: none;
  }
}

/* ---------- Podstawy ---------- */
*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 1rem;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}

h1, h2, h3 { line-height: 1.2; margin: 0; }
h2 { font-size: 1.4rem; margin: 8px 0 16px; }
h3 { font-size: 1.05rem; margin: 24px 0 10px; }
.card h3 { margin: 0 0 4px; }
p { margin: 0; }

[hidden] { display: none !important; }

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

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
  border-radius: 6px;
}

.skip-link {
  position: absolute;
  left: 8px;
  top: -100px;
  background: var(--primary);
  color: var(--primary-text);
  padding: 8px 12px;
  border-radius: 8px;
  z-index: 100;
}
.skip-link:focus { top: 8px; }

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 8px 16px 24px;
}
main:focus { outline: none; }

.loading { text-align: center; color: var(--muted); padding: 48px 0; }

/* ---------- Pasek górny ---------- */
.topbar {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(12px + env(safe-area-inset-top)) 16px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.25rem; }
.brand img { border-radius: 8px; }
.topbar-user { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.95rem; }

/* ---------- Zakładki (na telefonie na dole ekranu) ---------- */
.tabs {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--nav-h);
  color: var(--muted);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 600;
}
.tabs a[aria-current="page"] { color: var(--primary); }
.tabs a[aria-current="page"]::before {
  content: "";
  position: absolute;
  top: 0;
  left: 25%;
  right: 25%;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: var(--primary);
}
.tab-icon { font-size: 1.2rem; line-height: 1; }
.tab-badge {
  position: absolute;
  top: 8px;
  left: calc(50% + 8px);
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--primary);
  color: var(--primary-text);
  font-size: 0.72rem;
  line-height: 20px;
  text-align: center;
}

@media (min-width: 720px) {
  body { padding-bottom: 32px; }
  .tabs {
    position: sticky;
    top: 0;
    inset: auto;
    margin: 0 0 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    padding: 0;
  }
  .tabs a { flex-direction: row; gap: 8px; min-height: 52px; font-size: 0.95rem; }
  .tabs a[aria-current="page"] { background: var(--primary-soft); }
  .tabs a[aria-current="page"]::before { display: none; }
  .tab-icon { font-size: 1rem; }
  .tab-badge { position: static; }
}

/* ---------- Karty i formularze ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }

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

input, select {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
input::placeholder { color: var(--muted); opacity: 0.8; }
input:focus-visible, select:focus-visible { outline-offset: 0; border-color: var(--primary); }
input[aria-invalid="true"] { border-color: var(--danger); }

.form-row { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; }
.form-row .grow { flex: 1 1 180px; }
.form-row .w-sm { flex: 0 1 110px; }
.form-row .full { flex-basis: 100%; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 380px) {
  .form-grid { grid-template-columns: 1fr; }
}

.form-error { color: var(--danger); font-size: 0.9rem; font-weight: 600; }
.form-error:empty { display: none; }

.login { max-width: 400px; margin: 32px auto; }
.login form { display: grid; gap: 14px; margin-top: 12px; }

/* ---------- Przyciski ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 18px;
  border: 1px solid transparent;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.15s, border-color 0.15s;
}
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--primary-soft); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { border-color: var(--primary); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { min-height: 36px; padding: 4px 12px; font-size: 0.9rem; }
.btn-block { width: 100%; }

.icon-btn {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn-danger:hover { color: var(--danger); }
.icon-btn-sm { width: 32px; height: 32px; font-size: 0.85rem; }
.icon-btn-add { background: var(--primary-soft); color: var(--primary); width: 40px; height: 40px; }
.icon-btn-add:hover { background: var(--primary); color: var(--primary-text); }

/* ---------- Listy ---------- */
.list { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 8px; }

.item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 6px 6px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.item input[type="checkbox"] {
  flex: none;
  width: 24px;
  height: 24px;
  min-height: 0;
  margin: 0;
  accent-color: var(--success);
  cursor: pointer;
}
.item-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 6px 4px;
  cursor: pointer;
}
div.item-main { cursor: default; }
.item-title { font-weight: 600; overflow-wrap: anywhere; }
.item-meta { color: var(--muted); font-size: 0.85rem; overflow-wrap: anywhere; }
.item-amount { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.item-link { font-size: 0.85rem; color: var(--primary); font-weight: 600; white-space: nowrap; padding: 10px 6px; }
.item-link i { font-size: 0.75em; }

.item.is-done { background: var(--surface-2); }
.item.is-done .item-title { text-decoration: line-through; color: var(--muted); font-weight: 500; }

.item.is-settlement { border-style: dashed; }

.tag {
  display: inline-block;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  margin-right: 6px;
  background: var(--surface-2);
  color: var(--muted);
}
.tag-p3 { background: var(--primary-soft); color: var(--primary); }

.empty { text-align: center; color: var(--muted); padding: 24px 0; }

.toolbar { display: flex; align-items: center; gap: 10px; margin: 8px 0 12px; }
.toolbar label { font-weight: 600; color: var(--muted); font-size: 0.9rem; }
.toolbar select { width: auto; min-width: 160px; }

/* ---------- Saldo i statystyki ---------- */
.balance { text-align: center; display: grid; gap: 6px; justify-items: center; }
.balance-label { text-transform: uppercase; letter-spacing: 0.08em; font-size: 0.75rem; font-weight: 700; color: var(--muted); }
.balance-value { font-size: 1.3rem; font-weight: 700; }
.balance .btn { margin-top: 6px; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; text-align: center; }
.stat-value { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Jadłospis ---------- */
.week-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
}
.week-label { text-align: center; }
.week-range { font-weight: 700; font-size: 1.1rem; }

.week-summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 0 16px;
}
.week-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.days { display: grid; gap: 12px; }
.day { margin: 0; padding: 14px; }
.day.is-today { border-color: var(--primary); }
.day-title { display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px; margin: 0 0 10px; font-size: 1rem; }
.day-date { color: var(--muted); font-weight: 500; font-size: 0.9rem; }
.day-empty { margin-bottom: 10px; }

.meals { list-style: none; margin: 0 0 10px; padding: 0; display: grid; gap: 10px; }
.meal {
  padding: 6px 6px 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
}
.meal-head { display: flex; align-items: center; gap: 8px; }
.meal-title { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; font-weight: 700; overflow-wrap: anywhere; }
.meal-title i { color: var(--primary); }

.ingredients { list-style: none; margin: 2px 0 8px; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.ingredient {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding-left: 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.9rem;
}
.ing-name { overflow-wrap: anywhere; }
.ing-qty { color: var(--muted); margin-left: 6px; }
.ingredient.is-added { background: var(--surface-2); }
.ingredient.is-added .ing-name { color: var(--muted); }
.ing-status { color: var(--success); font-size: 0.8rem; }

.inline-form { display: flex; align-items: center; gap: 8px; }
.inline-form input { flex: 1; min-width: 0; min-height: 40px; }
.inline-form .qty-input { flex: 0 0 90px; }
.ing-form { padding-right: 4px; }
.ing-form input { min-height: 38px; font-size: 0.95rem; }
.meal-push { margin-top: 8px; }

@media (max-width: 380px) {
  .hide-xs { display: none; }
  .inline-form .qty-input { flex-basis: 70px; }
}

/* ---------- Okno dialogowe ---------- */
dialog {
  width: min(440px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog h2 { font-size: 1.15rem; margin: 0; overflow-wrap: anywhere; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- Komunikaty ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 12px);
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
.toast.is-error { background: var(--danger); color: #fff; }
@media (min-width: 720px) { .toast { bottom: 24px; } }

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