/*
  Larder base stylesheet. Hand-written, no component library.
  Served under style-src 'self': never use inline style attributes in .razor
  files; add a class here instead. Phone first (iPhone Safari): safe-area
  insets, 100dvh layouts, 44 px tap targets and 16 px form controls so Safari
  does not zoom. No hover-only affordances: every :hover rule is paired with
  :focus-visible. Every colour is a token in the two blocks below, so the dark
  scheme covers every component by redefining the tokens only.
*/

/* ---------- Tokens ---------- */

:root {
  color-scheme: light dark;

  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #efede6;
  --text: #1d2321;
  --muted: #5d6763;
  --border: #d9dcd6;
  --accent: #2f6f4f;
  --accent-strong: #245a3f;
  --accent-contrast: #ffffff;
  --danger: #b3261e;
  --danger-bg: #fbe4e2;
  --warn: #7a4f00;
  --warn-bg: #fff1d6;
  --info-bg: #e3eef8;
  --info: #1d4f7a;
  --ok: #1d5a3a;
  --ok-bg: #dff1e6;
  --pending: #6b5a00;
  --pending-bg: #fdf6c9;
  --shadow: 0 4px 16px rgb(0 0 0 / 0.12);
  --scrim: rgb(0 0 0 / 0.4);
  --focus: #1d6fd1;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --radius: 10px;
  --radius-small: 6px;
  --radius-pill: 999px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-small: 0.875rem;

  --tap: 44px;
  --tabbar-height: 56px;
  --header-height: 48px;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  --safe-left: env(safe-area-inset-left);
  --safe-right: env(safe-area-inset-right);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --surface: #1c211f;
    --surface-2: #252b28;
    --text: #e8ece9;
    --muted: #a2aca7;
    --border: #343c38;
    --accent: #6fc39a;
    --accent-strong: #8ad3ae;
    --accent-contrast: #0c1a13;
    --danger: #f2b8b5;
    --danger-bg: #4a1e1b;
    --warn: #f5d38a;
    --warn-bg: #3d2e0b;
    --info-bg: #15293b;
    --info: #a9cdf0;
    --ok: #a6dfc1;
    --ok-bg: #163326;
    --pending: #f0e08a;
    --pending-bg: #3a3410;
    --shadow: 0 4px 16px rgb(0 0 0 / 0.5);
    --scrim: rgb(0 0 0 / 0.6);
    --focus: #8ab8f0;
  }
}

/* ---------- Base ---------- */

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100dvh;
  font-size: 16px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

h1,
h2,
h3 {
  margin: 0 0 var(--space-3);
  line-height: 1.2;
}

p {
  margin: 0 0 var(--space-3);
}

a {
  color: var(--accent);
}

svg {
  max-width: 100%;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ---------- App shell ---------- */

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: calc(var(--header-height) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) var(--space-4) 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.app-title {
  display: block;
  flex: 1;
  min-width: 0;
  min-height: 44px;
  overflow: hidden;
  color: var(--text);
  font-size: 1.125rem;
  font-weight: 700;
  line-height: 44px;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-main {
  flex: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: var(--space-4);
  padding-bottom: calc(var(--tabbar-height) + var(--space-5));
}

.app-main:first-child {
  padding-top: calc(var(--space-4) + env(safe-area-inset-top));
}

.page-title {
  font-size: 1.5rem;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.page-header .page-title {
  margin: 0;
}

/* ---------- Header menu (a details element: opens with no script and no circuit) ---------- */

.menu {
  position: relative;
  flex: none;
}

.menu summary {
  display: flex;
  align-items: center;
  min-height: 44px;
  min-width: 44px;
  padding: 0 var(--space-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  cursor: pointer;
  font-weight: 600;
  list-style: none;
  user-select: none;
}

.menu summary::-webkit-details-marker {
  display: none;
}

.menu[open] summary {
  background: var(--surface-2);
}

.menu-panel {
  position: absolute;
  top: calc(100% + var(--space-1));
  right: 0;
  z-index: 25;
  display: flex;
  flex-direction: column;
  min-width: 14rem;
  max-width: calc(100vw - 2 * var(--space-4));
  padding: var(--space-1);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.menu-panel a {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-small);
  color: var(--text);
  text-decoration: none;
}

.menu-panel a:hover,
.menu-panel a:focus-visible,
.menu-panel a.active {
  background: var(--surface-2);
}

/* ---------- Tab bar ---------- */

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  justify-content: space-around;
  min-height: calc(var(--tabbar-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 44px;
  min-width: 44px;
  padding: var(--space-1) 0;
  color: var(--muted);
  font-size: 0.75rem;
  text-decoration: none;
}

.tabbar a.active {
  color: var(--accent);
  font-weight: 600;
}

.tab-icon {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Connection lost (app.js sets data-connection on the root element) ---------- */

:root[data-connection="lost"] .app-main {
  opacity: 0.55;
}

:root[data-connection="lost"] .app-main :is(button, input, select, textarea, a, summary):not(.allow-offline) {
  pointer-events: none;
}

/* ---------- Forms and buttons ---------- */

input,
select,
textarea,
button {
  font: inherit;
  font-size: 16px;
  min-height: 44px;
  color: var(--text);
}

input,
select,
textarea {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
}

textarea {
  min-height: 5.5rem;
  resize: vertical;
}

input:disabled,
select:disabled,
textarea:disabled,
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

input[type="checkbox"],
input[type="radio"] {
  width: 24px;
  min-height: 24px;
  margin: 10px;
}

label {
  display: block;
  margin-bottom: var(--space-1);
  font-weight: 600;
}

.field {
  margin-bottom: var(--space-4);
}

.field-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.field-row > * {
  flex: 1 1 8rem;
  min-width: 0;
}

.field-hint {
  margin: var(--space-1) 0 0;
  color: var(--muted);
  font-size: var(--font-small);
}

.field-error {
  margin: var(--space-1) 0 0;
  color: var(--danger);
  font-size: var(--font-small);
  font-weight: 600;
}

.checkbox-row {
  display: flex;
  align-items: center;
  min-height: 44px;
  margin: 0;
  font-weight: 400;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px;
  min-width: 44px;
  padding: 0 var(--space-4);
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  text-decoration: none;
  cursor: pointer;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

.btn-primary:hover,
.btn-primary:focus-visible {
  background: var(--accent-strong);
}

.btn-danger {
  color: var(--danger);
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Layout helpers ---------- */

.stack > * + * {
  margin-top: var(--space-3);
}

.cluster {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.card {
  margin: 0 0 var(--space-4);
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.card > :last-child {
  margin-bottom: 0;
}

.empty {
  margin: 0 0 var(--space-4);
  padding: var(--space-5) var(--space-4);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  text-align: center;
}

.row-actions {
  display: flex;
  flex: none;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
}

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

/* ---------- Chips and segmented toggles (aria-pressed marks the chosen one) ---------- */

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin: 0 0 var(--space-3);
  padding: 0;
  list-style: none;
}

.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 0 var(--space-4);
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  cursor: pointer;
  text-decoration: none;
}

.chip:hover,
.chip:focus-visible {
  border-color: var(--accent);
}

.chip[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

.segmented {
  display: inline-flex;
  max-width: 100%;
  margin: 0 0 var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  overflow: hidden;
}

.segmented > button,
.segmented > a {
  display: inline-flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 0 var(--space-4);
  background: var(--surface);
  color: var(--text);
  border: 0;
  border-right: 1px solid var(--border);
  cursor: pointer;
  text-decoration: none;
}

.segmented > :last-child {
  border-right: 0;
}

.segmented > [aria-pressed="true"],
.segmented > [aria-current="true"],
.segmented > [aria-current="page"] {
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
}

/* ---------- List rows ---------- */

.list {
  margin: 0 0 var(--space-4);
  padding: 0;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 44px;
  padding: var(--space-2) var(--space-4);
  border-bottom: 1px solid var(--border);
}

.row:last-child {
  border-bottom: 0;
}

.row-main {
  flex: 1;
  min-width: 0;
}

.row-leading {
  flex: none;
}

.row-link,
.row-button {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-self: stretch;
  min-height: 44px;
  margin: calc(-1 * var(--space-2)) 0;
  padding: var(--space-2) 0;
  background: none;
  border: 0;
  color: inherit;
  font: inherit;
  text-align: left;
  text-decoration: none;
}

.row-button {
  cursor: pointer;
}

.row-link:hover .row-title,
.row-link:focus-visible .row-title,
.row-button:hover .row-title,
.row-button:focus-visible .row-title {
  text-decoration: underline;
}

.row-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-meta {
  display: block;
  color: var(--muted);
  font-size: var(--font-small);
}

.list-total {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin: 0 0 var(--space-4);
  padding: var(--space-2) var(--space-4);
  background: var(--surface-2);
  border-radius: var(--radius-small);
  color: var(--muted);
  font-size: var(--font-small);
  font-weight: 600;
}

/* ---------- Quick add (sticky above the tab bar) ---------- */

.quick-add {
  position: sticky;
  bottom: calc(var(--tabbar-height) + var(--safe-bottom));
  z-index: 5;
  display: flex;
  gap: var(--space-2);
  margin: var(--space-4) calc(-1 * var(--space-4)) 0;
  padding: var(--space-2) var(--space-4);
  background: var(--bg);
  border-top: 1px solid var(--border);
}

.quick-add input {
  flex: 1;
}

/* ---------- Quantity stepper ---------- */

.stepper {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  overflow: hidden;
}

.stepper button {
  width: 44px;
  min-height: 44px;
  padding: 0;
  background: var(--surface-2);
  border: 0;
  font-size: 1.25rem;
}

.stepper input {
  width: 4.5em;
  border: 0;
  border-radius: 0;
  text-align: center;
}

.stepper-unit {
  display: flex;
  align-items: center;
  align-self: stretch;
  padding: 0 var(--space-3);
  background: var(--surface-2);
  color: var(--muted);
}

/* ---------- Typeahead (combobox with a listbox of options) ---------- */

.typeahead {
  position: relative;
  flex: 1;
}

.typeahead-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 6;
  max-height: 50dvh;
  margin: var(--space-1) 0 0;
  padding: 0;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  box-shadow: var(--shadow);
  list-style: none;
}

.quick-add .typeahead-list {
  top: auto;
  bottom: 100%;
  margin: 0 0 var(--space-1);
}

.typeahead-option {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 44px;
  padding: var(--space-2) var(--space-3);
  cursor: pointer;
}

.typeahead-option:hover,
.typeahead-option:focus-visible,
.typeahead-option.is-active {
  background: var(--surface-2);
}

.typeahead-detail {
  color: var(--muted);
  font-size: var(--font-small);
}

/* ---------- Date field ---------- */

.date-field-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
}

.date-field-row input[type="date"] {
  flex: 1 1 10rem;
  width: auto;
  min-width: 0;
}

.date-field-text {
  min-width: 6rem;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

/* ---------- Bottom sheet ---------- */

.sheet-scrim {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: var(--scrim);
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 31;
  max-width: 560px;
  max-height: 85dvh;
  margin: 0 auto;
  overflow-y: auto;
  padding: var(--space-4);
  padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom));
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
}

/* Containers that are only focused by script (the sheet, the page heading after navigation) show no ring. */
[tabindex="-1"]:focus-visible {
  outline: none;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.sheet-title {
  margin: 0;
  font-size: 1.25rem;
}

.sheet-actions {
  position: sticky;
  bottom: 0;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  justify-content: flex-end;
  margin-top: var(--space-4);
  padding-top: var(--space-2);
  background: var(--surface);
}

@media (max-width: 640px) {
  .sheet-full {
    top: 0;
    max-width: none;
    max-height: none;
    height: 100dvh;
    padding-top: calc(var(--space-4) + env(safe-area-inset-top));
    border-radius: 0;
  }
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: var(--space-4);
  right: var(--space-4);
  bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-3));
  z-index: 40;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  max-width: 560px;
  min-height: 44px;
  margin: 0 auto;
  padding: var(--space-2) var(--space-4);
  background: var(--text);
  color: var(--bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.toast .btn {
  margin-left: auto;
  background: transparent;
  color: inherit;
  border-color: currentColor;
}

/* ---------- Banners ---------- */

.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
  margin: 0 0 var(--space-3);
  padding: var(--space-2) var(--space-4);
  background: var(--info-bg);
  color: var(--info);
  border-radius: var(--radius-small);
}

.banner a {
  color: inherit;
  font-weight: 600;
}

.banner-info {
  background: var(--info-bg);
  color: var(--info);
}

.banner-warning,
.banner-offline {
  background: var(--warn-bg);
  color: var(--warn);
}

.banner-planning {
  background: var(--ok-bg);
  color: var(--ok);
}

.banner-error {
  background: var(--danger-bg);
  color: var(--danger);
}

/* ---------- Badges, flags and the pending marker ---------- */

.badge,
.flag {
  display: inline-block;
  padding: 1px var(--space-2);
  border-radius: var(--radius-pill);
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.badge {
  background: var(--surface-2);
  color: var(--muted);
}

.badge-ok {
  background: var(--ok-bg);
  color: var(--ok);
}

.badge-info {
  background: var(--info-bg);
  color: var(--info);
}

.badge-warn {
  background: var(--warn-bg);
  color: var(--warn);
}

.badge-danger {
  background: var(--danger-bg);
  color: var(--danger);
}

/* Use-by date has passed. */
.flag-use-by {
  background: var(--danger-bg);
  color: var(--danger);
}

/* In the freezer for more than six months. */
.flag-old {
  background: var(--warn-bg);
  color: var(--warn);
}

/* An optimistic change not yet confirmed by the server ("Sending"). */
.pending {
  display: inline-block;
  padding: 1px var(--space-2);
  background: var(--pending-bg);
  color: var(--pending);
  border-radius: var(--radius-pill);
  font-size: 0.75rem;
  font-style: italic;
}

.row.is-pending {
  opacity: 0.75;
}

/* ---------- Week and night cards ---------- */

.week {
  display: grid;
  gap: var(--space-3);
  margin: 0 0 var(--space-4);
  padding: 0;
  list-style: none;
}

.night {
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  border-radius: var(--radius);
}

.night.is-tonight {
  border-left-color: var(--accent);
}

.night.is-cooked {
  border-left-color: var(--ok);
  background: var(--surface-2);
}

.night.is-missing {
  border-left-color: var(--danger);
}

.night-date {
  color: var(--muted);
  font-size: var(--font-small);
  font-weight: 600;
}

.night-title {
  margin: 0 0 var(--space-1);
  font-size: 1.125rem;
}

.night-links {
  margin: var(--space-2) 0 0;
  padding: 0;
  list-style: none;
}

.night-links .is-missing {
  color: var(--danger);
  font-weight: 600;
}

/* ---------- Utilities ---------- */

.muted {
  color: var(--muted);
}

.placeholder {
  padding-top: var(--space-4);
}

.qr {
  display: inline-block;
  margin: 0 0 var(--space-4);
  text-align: center;
}

.qr figcaption {
  color: var(--muted);
  font-size: var(--font-small);
}

/* ---------- Join, register and reset pages (static SSR) ---------- */

.join h2 {
  margin-top: var(--space-5);
  font-size: 1.125rem;
}

.join-invite {
  font-size: 1.125rem;
  font-weight: 600;
}

/* The larder:// fallback: closed until the person says the link opened in the browser (section 14). */
.app-fallback {
  margin-top: var(--space-5);
  padding-top: var(--space-3);
  border-top: 1px solid var(--border);
}

.app-fallback summary {
  display: list-item;
  min-height: 44px;
  padding: var(--space-2) 0;
  color: var(--accent);
  cursor: pointer;
}

.app-fallback[open] summary {
  margin-bottom: var(--space-2);
}
