/*
 * Grocery Mini App — household shopping list (Phase 9.3).
 *
 * Mobile-first, Google-Keep-like: an add row at the top, tap-to-tick rows,
 * and checked items greyed + struck through (they sink to the bottom). Colours
 * come from Telegram's themeParams; the fallbacks below keep a sensible light
 * theme in a normal browser. script.js mirrors themeParams onto
 * documentElement so themeChanged updates apply immediately.
 */

:root {
  --tg-theme-bg-color: #ffffff;
  --tg-theme-text-color: #1a1a1a;
  --tg-theme-hint-color: #707579;
  --tg-theme-link-color: #2481cc;
  --tg-theme-button-color: #2481cc;
  --tg-theme-button-text-color: #ffffff;
  --tg-theme-secondary-bg-color: #f1f1f4;
  --tg-theme-section-bg-color: #ffffff;
  --tg-theme-section-separator-color: #e6e6e8;
  --tg-theme-destructive-text-color: #d1453b;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  min-height: var(--tg-viewport-stable-height, 100vh);
  background-color: var(--tg-theme-bg-color);
  color: var(--tg-theme-text-color);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  padding: calc(16px + env(safe-area-inset-top)) 16px
    calc(16px + env(safe-area-inset-bottom));
}

.app {
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.app__header {
  padding: 4px 2px;
}

.app__title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.app__identity {
  margin: 4px 0 0;
  font-size: 14px;
  color: var(--tg-theme-hint-color);
}

.card {
  background-color: var(--tg-theme-section-bg-color);
  border: 1px solid var(--tg-theme-section-separator-color);
  border-radius: 12px;
  padding: 20px;
}

.card__title {
  margin: 0 0 8px;
  font-size: 18px;
  font-weight: 600;
}

.muted {
  margin: 0;
  color: var(--tg-theme-hint-color);
}

/* buttons */
.button {
  appearance: none;
  border: 1px solid var(--tg-theme-button-color);
  background-color: var(--tg-theme-button-color);
  color: var(--tg-theme-button-text-color);
  font: inherit;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 10px;
  cursor: pointer;
}

.button--secondary {
  background-color: transparent;
  color: var(--tg-theme-link-color);
  border-color: var(--tg-theme-section-separator-color);
  font-weight: 500;
}

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

.button:active:not(:disabled) {
  opacity: 0.85;
}

/* add row */
.add {
  background-color: var(--tg-theme-section-bg-color);
  border: 1px solid var(--tg-theme-section-separator-color);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.add__row {
  display: flex;
  gap: 8px;
}

.add__name {
  flex: 1 1 auto;
  min-width: 0;
  border: none;
  border-bottom: 1px solid var(--tg-theme-section-separator-color);
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 17px;
  padding: 6px 2px;
}

.add__meta {
  flex: 1 1 0;
  min-width: 0;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 14px;
  padding: 2px;
}

.add__meta::placeholder,
.add__name::placeholder {
  color: var(--tg-theme-hint-color);
}

.add__name:focus,
.add__meta:focus {
  outline: none;
}

.add__name:focus {
  border-bottom-color: var(--tg-theme-button-color);
}

.notice {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
  background-color: var(--tg-theme-secondary-bg-color);
  color: var(--tg-theme-hint-color);
}

.notice[data-kind="error"] {
  color: var(--tg-theme-destructive-text-color);
}

/* list */
.list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.group {
  background-color: var(--tg-theme-section-bg-color);
  border: 1px solid var(--tg-theme-section-separator-color);
  border-radius: 12px;
  overflow: hidden;
}

.group__title {
  margin: 0;
  padding: 10px 14px 4px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--tg-theme-hint-color);
}

.items {
  list-style: none;
  margin: 0;
  padding: 0;
}

.item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 14px;
  border-top: 1px solid var(--tg-theme-section-separator-color);
}

.items .item:first-child {
  border-top: none;
}

/* the tap target: a round checkbox, Keep-style */
.item__check {
  flex: 0 0 auto;
  appearance: none;
  border: none;
  background: transparent;
  padding: 0;
  margin-top: 1px;
  cursor: pointer;
  /* a comfortable tap target without a visually huge box */
  min-width: 32px;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.item__box {
  position: relative;
  display: block;
  width: 22px;
  height: 22px;
  border: 2px solid var(--tg-theme-hint-color);
  border-radius: 50%;
}

.item__check[aria-checked="true"] .item__box {
  background-color: var(--tg-theme-button-color);
  border-color: var(--tg-theme-button-color);
}

.item__check[aria-checked="true"] .item__box::after {
  content: "";
  position: absolute;
  left: 7px;
  top: 2px;
  width: 4px;
  height: 10px;
  border: solid var(--tg-theme-button-text-color);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.item__check:focus-visible .item__box {
  outline: 2px solid var(--tg-theme-button-color);
  outline-offset: 2px;
}

/* the drag handle: only on not-yet-bought rows */
.item__grip {
  flex: 0 0 auto;
  appearance: none;
  border: none;
  background: transparent;
  padding: 0;
  margin: 0;
  min-width: 26px;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  color: var(--tg-theme-hint-color);
  /* The gesture owns the touch, so the list must not scroll while dragging.
     touch-action: none is what stops iOS/WKWebView hijacking the swipe. */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.item__grip:active {
  cursor: grabbing;
}

.item__grip:focus-visible {
  outline: 2px solid var(--tg-theme-button-color);
  outline-offset: 2px;
  border-radius: 8px;
}

.item__grip-dots {
  width: 12px;
  height: 18px;
  background-image: radial-gradient(currentColor 1.4px, transparent 1.6px);
  background-size: 6px 6px;
  opacity: 0.75;
}

/* the row currently being dragged — visibly lifted off the list. The transform
   is visual only (it does not change layout), so the drop-position maths is
   unaffected; the groups stop clipping while a drag is live so the lifted row
   can overlap its neighbours instead of being cut off at the group edge. */
.item--dragging {
  position: relative;
  z-index: 5;
  background-color: var(--tg-theme-section-bg-color);
  border-radius: 10px;
  outline: 2px solid var(--tg-theme-button-color);
  outline-offset: -2px;
  transform: scale(1.035);
  box-shadow:
    0 14px 30px rgba(0, 0, 0, 0.32),
    0 4px 10px rgba(0, 0, 0, 0.22);
  transition: transform 0.14s ease, box-shadow 0.14s ease;
  will-change: transform;
  cursor: grabbing;
}

body.is-dragging .group {
  overflow: visible;
}

body.is-dragging {
  -webkit-user-select: none;
  user-select: none;
}

/* the shop group the dragged row would land in — including another store,
   which is how a cross-store move is signalled before the drop */
.group--drop-target {
  outline: 2px dashed var(--tg-theme-button-color);
  outline-offset: -2px;
  background-color: var(--tg-theme-secondary-bg-color);
}

.item__body {
  flex: 1 1 auto;
  min-width: 0;
}

.item__name {
  display: block;
  overflow-wrap: anywhere;
}

.item__meta {
  display: block;
  font-size: 13px;
  color: var(--tg-theme-hint-color);
}

/* Google-Keep checked state: greyed + struck through */
.item--checked .item__name {
  text-decoration: line-through;
  color: var(--tg-theme-hint-color);
}

.item--checked .item__meta {
  opacity: 0.7;
}

.item__remove {
  flex: 0 0 auto;
  appearance: none;
  border: none;
  background: transparent;
  color: var(--tg-theme-hint-color);
  font-size: 20px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 8px;
  cursor: pointer;
}

.item__remove:hover,
.item__remove:active {
  color: var(--tg-theme-destructive-text-color);
}

/* the "Done" separator before the checked block */
.divider {
  margin: 6px 2px 0;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--tg-theme-hint-color);
}

.empty {
  margin: 0;
  padding: 4px 2px;
}

/* reorder discoverability hint (hidden unless there is something to reorder) */
.hint {
  margin: 0 2px;
  font-size: 13px;
  line-height: 1.4;
}

.hint__grip {
  letter-spacing: -1px;
}

.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 2px;
}

.counts {
  margin: 0;
  font-size: 14px;
  color: var(--tg-theme-hint-color);
}

.noscript {
  max-width: 480px;
  margin: 24px auto;
  padding: 0 16px;
  color: #707579;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --tg-theme-bg-color: #17212b;
    --tg-theme-text-color: #ffffff;
    --tg-theme-hint-color: #7d8b99;
    --tg-theme-link-color: #5aa7e0;
    --tg-theme-button-color: #5aa7e0;
    --tg-theme-button-text-color: #ffffff;
    --tg-theme-secondary-bg-color: #232e3c;
    --tg-theme-section-bg-color: #232e3c;
    --tg-theme-section-separator-color: #2c3a47;
    --tg-theme-destructive-text-color: #ff6259;
  }
}
