/*
 * Hello Pantry Mini App and web app.
 *
 * The palette, the type scale, the spacing grid, the radii, the shadows and the motion curves are
 * not here: they are in design-system.css, which is the group's design system copied in by
 * scripts/sync-design-system.sh rather than transcribed. This file is only what the app adds on top
 * of it — the screens and the tab bar.
 *
 * Where this file departs from the system it says so on the line, with the reason. The only place
 * is the fields: they are 16px because anything smaller makes iOS zoom the page when the field is
 * focused.
 *
 * What moves is in motion.css; the hover and press states of the controls are here, with the
 * controls, as the system's own components have them.
 */

:root {
  --tabbar-height: 64px;
  --column-max: 560px;
  --fab-size: 56px;

  /* what sits over what: the bar and the round buttons, then the sheets, then the line after an
     add, then the notice, which has to be readable over a sheet (it used to be under it) */
  --z-tabs: 10;
  --z-sheet: 20;
  --z-toast: 30;
  --z-notice: 40;

  /* Telegram writes the height of the visible part of the window here, on the root element, as it
     changes; this is its value outside Telegram */
  --tg-viewport-stable-height: 100vh;

  /* the line after an add, the design system's snackbar: the inverted surface of the scheme */
  --snackbar-bg: var(--ink-900);
  --snackbar-text: var(--on-ink-text);
  --snackbar-action: var(--cyan-400);

  /* the dimmed part behind a sheet */
  --scrim: rgba(2, 6, 23, 0.55);

  /* where a lot stands against its expiry. They name the design system's own semantic colours
     rather than copy them, so the dark scheme (data-theme or the device's) carries over: the
     system redefines --danger, --highlight and --success for it. The sheets and the toast sit on
     --surface, which the system also switches. */
  --state-expired: var(--danger);
  --state-expired-soft: var(--danger-soft);
  --state-expiring: var(--highlight);
  --state-expiring-soft: var(--highlight-soft);
  --state-ok: var(--success);
  --state-ok-soft: var(--success-soft);
  --state-none: var(--border-strong);
  --state-none-soft: var(--surface);
}

/* the snackbar is light on dark and dark on light: the scheme is the Telegram client's
   (data-theme, set by app.js) or, with none said, the device's */
:root[data-theme="dark"] {
  --snackbar-bg: var(--ink-100);
  --snackbar-text: var(--ink-900);
  --snackbar-action: var(--cyan-700);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --snackbar-bg: var(--ink-100);
    --snackbar-text: var(--ink-900);
    --snackbar-action: var(--cyan-700);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs-body);
  line-height: var(--lh-body);
}

[hidden] { display: none !important; }

.eyebrow {
  font-family: var(--font-mono);
  font-size: var(--fs-mono-label);
  letter-spacing: var(--ls-label);
  text-transform: uppercase;
  color: var(--text-muted);
}

/* ------------------------------------------------------------------------ the notice line */

/* centred with auto margins and not with a translate, so that its entrance can use the transform */
.notice {
  position: fixed;
  top: var(--space-3);
  left: var(--space-4);
  right: var(--space-4);
  margin-inline: auto;
  max-width: calc(var(--column-max) - 2 * var(--space-4));
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--surface);
  border: var(--border-hairline) solid var(--border);
  border-left: 3px solid var(--accent);
  box-shadow: var(--shadow-md);
  color: var(--text);
  z-index: var(--z-notice);
}

.notice--error { border-left-color: var(--danger); }

/* ------------------------------------------------------------- landing, login and expired */

.landing {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
}

.landing__card {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-6);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

.landing__title {
  margin: 0;
  font-size: var(--fs-h2);
  line-height: var(--lh-heading);
}

.landing__text {
  margin: 0;
  color: var(--text-muted);
}

.login__error {
  padding: var(--space-3);
  border-radius: var(--radius-md);
  background: var(--danger-soft);
  color: var(--danger);
}

.login__divider {
  text-align: center;
  color: var(--text-subtle);
  font-size: var(--fs-body-sm);
}

/* -------------------------------------------------------------------------------- controls */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px;
  padding: 0 var(--space-4);
  border: 1px solid var(--accent);
  border-radius: var(--radius-md);
  background: var(--accent);
  color: var(--text-on-accent);
  font: inherit;
  font-weight: var(--fw-medium);
  text-decoration: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition:
    background var(--dur) var(--ease-out),
    border-color var(--dur) var(--ease-out),
    color var(--dur) var(--ease-out),
    opacity var(--dur) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

/* hover is for a pointer: on a touch screen it would stay on the button after the finger has gone */
@media (hover: hover) {
  .button:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
}

/* a press, which a touch screen has instead of a hover: the system's own */
.button:active:not(:disabled) {
  transform: translateY(0.5px) scale(0.992);
  background: var(--accent-press);
  border-color: var(--accent-press);
}

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

.button--ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border-strong);
}

@media (hover: hover) {
  .button--ghost:hover { background: var(--surface-2); border-color: var(--border-strong); }
}

.button--ghost:active:not(:disabled) { background: var(--surface-2); border-color: var(--border-strong); }
.button--small { min-height: 36px; font-size: var(--fs-body-sm); }
/* a button that is only an icon: square, the touch target of the design system */
.button--icon { padding: 0; min-width: 44px; min-height: 44px; }
.button--selected, .button--selected:active:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }

/* a chip, the system's: a pill 34px high, in a row that scrolls sideways when it is longer than
   the screen. It is a small ghost button that has been given the shape */
.chip {
  flex: none;
  min-height: 34px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-pill);
  font-size: var(--fs-caption);
}

.chips {
  display: flex;
  gap: var(--space-2);
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
  /* the row runs to the edges of the screen while its first chip lines up with the title */
  margin-inline: calc(-1 * var(--space-4));
  padding-inline: var(--space-4);
}

.chips::-webkit-scrollbar { display: none; }

.field { display: flex; flex-direction: column; gap: var(--space-1); margin-bottom: var(--space-3); }

.field__label {
  font-size: var(--fs-body-sm);
  font-weight: var(--fw-medium);
  color: var(--text-muted);
}

.input {
  min-height: 44px;
  padding: 0 var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  /* 16px and not the system's body size: below it iOS zooms the page when a field is focused */
  font: inherit;
  font-size: 16px;
}

.input:focus-visible, .button:focus-visible, .tab:focus-visible, .lot__open:focus-visible,
.suggestion:focus-visible, .fab:focus-visible, .button:focus-within, .chip:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus);
}

.choice { display: flex; gap: var(--space-2); flex-wrap: wrap; }

/* ------------------------------------------------------------------------- app and screens */

#app {
  max-width: var(--column-max);
  margin: 0 auto;
  min-height: 100vh;
}

.screens {
  padding: var(--space-4);
  padding-bottom: calc(var(--tabbar-height) + var(--space-6) + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.screen { display: flex; flex-direction: column; gap: var(--space-4); }

.screen__title {
  margin: 0;
  font-size: var(--fs-h2);
  line-height: var(--lh-heading);
}

.screen__text { margin: 0; color: var(--text-muted); }

.card {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

/* ---------------------------------------------------------------------------- the tab bar */

/* the system's bottom navigation: an icon for each tab, and the one on show is a pill that also
   carries its name. The others are the icon alone, with the name kept for a screen reader; five
   names side by side did not fit a phone ("Impostazioni" was cut), five icons and one name do */
.tabs {
  position: fixed;
  left: 0;
  right: 0;
  margin-inline: auto;
  bottom: 0;
  max-width: var(--column-max);
  height: calc(var(--tabbar-height) + env(safe-area-inset-bottom));
  padding: 0 var(--space-1) env(safe-area-inset-bottom);
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: var(--surface);
  border-top: var(--border-hairline) solid var(--border);
  z-index: var(--z-tabs);
}

.tab {
  position: relative;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-width: 44px;
  height: 44px;
  padding: 0;
  border: var(--border-hairline) solid transparent;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: var(--fs-caption);
  font-weight: var(--fw-medium);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition:
    color var(--dur-fast) var(--ease-out),
    background var(--dur) var(--ease-out),
    border-color var(--dur) var(--ease-out);
}

.tab__icon {
  flex: none;
  width: 22px;
  height: 22px;
  transition: transform var(--dur-fast) var(--ease-out);
}

.tab:active .tab__icon { transform: scale(0.88); }

/* a label that does not fit is shortened rather than pushing the other tabs off the screen */
.tab__label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab[aria-current="page"] {
  flex: 0 1 auto;
  min-width: 0;
  padding: 0 var(--space-3);
  background: var(--accent-soft);
  border-color: var(--accent-soft-border);
  color: var(--accent);
}

.tab:not([aria-current="page"]) .tab__label {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

/* ------------------------------------------------------------------------ pantry and expiry */

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

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

.group { display: flex; flex-direction: column; gap: var(--space-2); }

.group__title {
  margin: 0;
  font-family: var(--font-mono);
  font-size: var(--fs-mono-label);
  font-weight: var(--fw-medium);
  letter-spacing: var(--ls-label);
  text-transform: uppercase;
  color: var(--text-muted);
}

.lot {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  background: var(--state-none-soft);
  border: 1px solid var(--border);
  border-left: 4px solid var(--state-none);
  border-radius: var(--radius-md);
}

.lot--expired { background: var(--state-expired-soft); border-left-color: var(--state-expired); }
.lot--expiring { background: var(--state-expiring-soft); border-left-color: var(--state-expiring); }
.lot--ok { background: var(--state-ok-soft); border-left-color: var(--state-ok); }

.lot__open {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform var(--dur-fast) var(--ease-out);
}

.lot__open:active { transform: scale(0.99); }

.lot__info { display: flex; align-items: center; gap: var(--space-3); }
.lot__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }

.thumb {
  flex: none;
  width: 46px;
  height: 46px;
  border-radius: var(--radius-md);
  object-fit: cover;
  background: var(--surface-2);
}
/* a picture that is on its way is not there yet; motion.css shows it once it has arrived */
img.thumb {
  opacity: 0;
  transition: opacity var(--dur) var(--ease-out);
}
.thumb--empty {
  display: flex;
  align-items: center;
  justify-content: center;
  border: var(--border-hairline) solid var(--border);
  color: var(--text-subtle);
}
/* an inline SVG icon has no size of its own: inside a button it would shrink to nothing */
.icon { flex: none; width: 1.25em; height: 1.25em; }
.thumb--empty .icon { width: 50%; height: 50%; }
.thumb--large { width: 160px; height: 160px; max-width: 100%; border-radius: var(--radius-lg); }
.detail__picture { display: flex; justify-content: center; margin-bottom: var(--space-3); }
.lot__name { font-weight: var(--fw-medium); overflow-wrap: anywhere; }
.lot__meta { font-size: var(--fs-body-sm); color: var(--text-muted); }
.lot__actions { display: flex; gap: var(--space-2); flex-wrap: wrap; }

.fab {
  position: fixed;
  right: max(var(--space-4), calc(50% - var(--column-max) / 2 + var(--space-4)));
  bottom: calc(var(--tabbar-height) + var(--space-4) + env(safe-area-inset-bottom));
  z-index: var(--z-tabs);
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--fab-size);
  height: var(--fab-size);
  padding: 0;
  border: 0;
  /* the system's button is a rounded square, not a circle */
  border-radius: var(--radius-lg);
  background: var(--accent);
  color: var(--text-on-accent);
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition:
    background var(--dur) var(--ease-out),
    transform var(--dur-fast) var(--ease-out),
    box-shadow var(--dur) var(--ease-out);
}

@media (hover: hover) {
  .fab:hover { background: var(--accent-hover); }
}

.fab:active { transform: translateY(0.5px) scale(0.98); background: var(--accent-press); }

.fab .icon {
  width: 24px;
  height: 24px;
  transition: transform var(--dur) var(--ease-out);
}

.fab:active .icon { transform: scale(0.86); }
.fab:not(.fab--second):active .icon { transform: scale(0.86) rotate(90deg); }

/* ---------------------------------------------------------------- sheets and the toast */

/* a sheet is dimmed and below the screen until motion.js opens it (is-open, in motion.css), and
   it is that state it goes back to, over the quicker of the two curves, when it closes */
.sheet {
  position: fixed;
  inset: 0;
  z-index: var(--z-sheet);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: var(--scrim);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur) var(--ease-in-out);
}

.sheet__card {
  width: 100%;
  max-width: var(--column-max);
  /* the part of the window Telegram says is visible, not the one the keyboard or the client's own
     bar is covering */
  max-height: calc(var(--tg-viewport-stable-height) * 0.92);
  overflow-y: auto;
  /* dragging the end of a sheet must not drag the page behind it */
  overscroll-behavior: contain;
  padding: var(--space-3) var(--space-5) calc(var(--space-6) + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  background: var(--surface);
  border-top: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  box-shadow: var(--shadow-xl);
  transform: translateY(100%);
  transition: transform var(--dur) var(--ease-in-out);
}

/* the grab handle, the system's: 36 by 4 */
.sheet__card::before {
  content: "";
  flex: none;
  align-self: center;
  width: 36px;
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--border-strong);
}

.sheet__head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); }
.sheet__title { margin: 0; font-size: var(--fs-h3); line-height: var(--lh-heading); }

.stepper { display: flex; gap: var(--space-2); align-items: stretch; }
.stepper__value { flex: 1; min-width: 0; text-align: center; }
.pair { display: grid; grid-template-columns: 1fr 96px; gap: var(--space-2); }

.suggestions {
  list-style: none;
  margin: var(--space-1) 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.suggestion {
  width: 100%;
  min-height: 44px;
  padding: 0 var(--space-3);
  border: 0;
  border-bottom: 1px solid var(--border-faint);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-align: left;
  overflow-wrap: anywhere;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--dur-fast) var(--ease-out);
}

@media (hover: hover) {
  .suggestion:hover { background: var(--surface-2); }
}

.suggestion:active { background: var(--surface-2); }
.suggestion--new { color: var(--accent); font-weight: var(--fw-medium); }

.toast {
  position: fixed;
  left: var(--space-4);
  right: var(--space-4);
  margin-inline: auto;
  bottom: calc(var(--tabbar-height) + var(--space-3) + env(safe-area-inset-bottom));
  max-width: calc(var(--column-max) - 2 * var(--space-4));
  z-index: var(--z-toast);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: 48px;
  padding: 10px var(--space-4);
  border-radius: var(--radius-md);
  background: var(--snackbar-bg);
  color: var(--snackbar-text);
  font-size: var(--fs-body-sm);
  box-shadow: var(--shadow-lg);
}

/* the action of a snackbar is a word, not a button: no fill, no border, the accent of its scheme */
.toast .button {
  min-height: 36px;
  padding: 0 var(--space-2);
  background: transparent;
  border-color: transparent;
  border-radius: var(--radius-sm);
  color: var(--snackbar-action);
  font-weight: var(--fw-semibold);
}

@media (hover: hover) {
  .toast .button:hover { background: color-mix(in oklab, var(--snackbar-text) 10%, transparent); border-color: transparent; }
}

.toast .button:active:not(:disabled) {
  background: color-mix(in oklab, var(--snackbar-text) 16%, transparent);
  border-color: transparent;
}

/* ------------------------------------------------------- the lot sheet and the settings */

.button--danger { background: var(--danger); border-color: var(--danger); color: var(--text-on-accent); }
.button--quiet-danger { color: var(--danger); border-color: var(--danger); }

@media (hover: hover) {
  .button--quiet-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
  .button--danger:hover { background: var(--danger); border-color: var(--danger); opacity: 0.9; }
}

.button--quiet-danger:active:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); }
.button--danger:active:not(:disabled) { background: var(--danger); border-color: var(--danger); opacity: 0.85; }

.card--danger { border-color: var(--danger); }

/* a confirm step: the destructive button and the way out each on their own row */
.danger { display: flex; flex-direction: column; gap: var(--space-2); }

.detail { display: flex; flex-direction: column; gap: var(--space-1); }
.detail__line { margin: 0; overflow-wrap: anywhere; }
.actions { display: flex; flex-direction: column; gap: var(--space-2); }

.switch { display: flex; align-items: center; gap: var(--space-3); min-height: 44px; }
/* the system's switch: a 48 by 28 track and a thumb that slides, on the checkbox itself */
.switch input {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: none;
  width: 48px;
  height: 28px;
  margin: 0;
  border: var(--border-hairline) solid var(--border-strong);
  border-radius: var(--radius-pill);
  background: var(--surface-sunken);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--dur) var(--ease-out), border-color var(--dur) var(--ease-out);
}

.switch input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: var(--radius-circle);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  transition: transform var(--dur) var(--ease-out);
}

.switch input:checked { background: var(--accent); border-color: var(--accent); }
.switch input:checked::after { transform: translateX(20px); background: var(--text-on-accent); }
.switch input:focus-visible { outline: none; box-shadow: var(--shadow-focus); }

.place {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.place__row { display: flex; align-items: center; gap: var(--space-2); }
.place__name { flex: 1; min-width: 0; overflow-wrap: anywhere; font-weight: var(--fw-medium); }

/* ---------------------------------------------------------------------- barcode scanning */

.fab--second {
  right: max(calc(var(--space-4) + var(--fab-size) + var(--space-3)), calc(50% - var(--column-max) / 2 + var(--space-4) + var(--fab-size) + var(--space-3)));
  background: var(--surface);
  color: var(--text);
  border: var(--border-hairline) solid var(--border-strong);
}

.fab--second:active { background: var(--surface-2); }

@media (hover: hover) {
  .fab--second:hover { background: var(--surface-2); }
}

.scan__video {
  width: 100%;
  max-height: 40vh;
  object-fit: cover;
  border-radius: var(--radius-md);
  background: var(--surface-2);
}

.off {
  display: flex;
  gap: var(--space-3);
  align-items: center;
}

.off__image {
  width: 64px;
  height: 64px;
  object-fit: contain;
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.off__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.off__name { font-weight: var(--fw-medium); overflow-wrap: anywhere; }
