/* Colour roles:
   green   = primary actions and "done" (ticks, progress, Shop/Add/Save)
   ink     = selection (active tab) and text
   orange  = urgent only
   grey    = everything else */
:root {
  --green: #15803d;
  --green-dark: #166534;
  --green-light: #e7f6ec;
  --bg: #f4f6f5;
  --card: #ffffff;
  --chip: #eef2f0;
  --text: #1a2320;
  --muted: #5b6862;
  --icon: #8b9892;
  --border: #e1e7e4;
  --danger: #c0392b;
  --urgent: #d9661a;
  --urgent-text: #a84300;
  --urgent-soft: #fdf1e7;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Sinhala", "Iskoola Pota", "Nirmala UI", sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

/* touch-action: manipulation on every element disables double-tap zoom (iOS & Android)
   while keeping scrolling and pinch-zoom for accessibility. */
* { box-sizing: border-box; touch-action: manipulation; }
html, body { margin: 0; min-height: 100%; }
body { -webkit-tap-highlight-color: transparent; overscroll-behavior-y: contain; }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

.view { max-width: 640px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; }

/* Line icons */
.ic {
  width: 1.25em; height: 1.25em; flex: none; vertical-align: -.22em;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.urgent-ic { color: var(--urgent); }

/* ---------- Buttons & fields ---------- */
.btn {
  border: 1px solid var(--border); background: var(--card); padding: .7rem 1.1rem; min-height: 46px;
  border-radius: 12px; cursor: pointer; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.danger-outline { color: var(--danger); border-color: #e9c3be; }
.btn.full { width: 100%; margin-top: .25rem; }
.big-btn { width: 100%; padding: 1rem; font-size: 1.05rem; margin-top: 1rem; min-height: 54px; }
.icon-btn {
  border: none; background: transparent; font-size: 1.2rem; cursor: pointer; line-height: 1; color: var(--muted);
  min-width: 44px; min-height: 44px; border-radius: 12px; display: inline-grid; place-items: center; flex: none;
}
.icon-btn.small { font-size: 1.05rem; }
.icon-btn:active { background: var(--chip); }
.link-btn {
  border: none; background: none; color: var(--green-dark); font-weight: 600; cursor: pointer;
  text-decoration: underline; text-underline-offset: 3px; padding: .6rem .5rem; min-height: 44px; font-size: .95rem;
}
.link-btn.danger { color: var(--danger); }
.lang-btn {
  border: 1px solid var(--border); background: transparent; color: var(--text); min-height: 36px;
  border-radius: 999px; padding: .2rem .75rem; cursor: pointer; font-weight: 600; font-size: .9rem; flex: none;
}
.field { display: block; margin: .9rem 0; }
.field > span { display: block; font-size: .875rem; color: var(--muted); margin-bottom: .35rem; }
.field input, .field select {
  width: 100%; padding: .75rem .8rem; border: 1px solid var(--border);
  border-radius: 12px; background: var(--card); font-size: 1rem; min-height: 48px;
}
.field small { display: block; margin-top: .35rem; font-size: .8rem; line-height: 1.4; }
.field input:focus, .field select:focus, .search-box:focus-within, .qty-input:focus {
  outline: 2px solid var(--green); outline-offset: 1px;
}
.code-input { text-transform: uppercase; letter-spacing: .15em; font-family: ui-monospace, Consolas, monospace; }
.toggle { display: flex; align-items: center; gap: .6rem; margin: .6rem 0; cursor: pointer; font-weight: 600; min-height: 44px; }
.toggle input { width: 1.4rem; height: 1.4rem; accent-color: var(--urgent); }

/* ---------- Welcome ---------- */
#welcome { padding: 1rem; padding-top: max(1rem, env(safe-area-inset-top)); }
.welcome-card { background: var(--card); border-radius: 20px; padding: 1.4rem; box-shadow: 0 2px 12px rgba(0, 0, 0, .06); margin-top: .5rem; }
.welcome-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.logo { width: 56px; height: 56px; }
.panel { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-top: 1rem; }
.panel .btn { width: 100%; }
.create-link { text-align: center; margin: 1rem 0 0; }

/* ---------- Sticky header (top bar + search + tabs) ---------- */
.sticky-head { position: sticky; top: 0; z-index: 20; background: var(--bg); transition: transform .2s ease; }
.sticky-head.compact { transform: translateY(calc(-1 * var(--topbar-h, 56px))); }
.topbar {
  display: flex; align-items: center; gap: .35rem; background: var(--card); color: var(--text);
  border-bottom: 1px solid var(--border);
  padding: .4rem .4rem .4rem 1rem; padding-top: max(.4rem, env(safe-area-inset-top)); min-height: 56px;
}
.topbar .title { flex: 1; font-weight: 700; font-size: 1.08rem; display: flex; align-items: center; gap: .5rem; min-width: 0; }
.topbar .title #hhName, #shopTitle { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.title-ic { color: var(--green); }
.status { width: 8px; height: 8px; border-radius: 50%; background: #e0a800; flex: none; }
.status.online { background: #22c55e; }

.search-wrap { position: relative; padding: .6rem 1rem .5rem; }
.search-box {
  display: flex; align-items: center; gap: .5rem; background: var(--card); border: 1px solid var(--border);
  border-radius: 999px; padding: 0 .25rem 0 1rem; box-shadow: 0 1px 3px rgba(0, 0, 0, .04); min-height: 50px;
}
.search-icon { color: var(--icon); }
.search-box input {
  flex: 1; border: none; outline: none; padding: .7rem 0; font-size: 1.05rem; background: transparent; min-width: 0;
}
.search-box input::placeholder { color: var(--muted); }
.search-box input::-webkit-search-cancel-button { display: none; }
.results {
  list-style: none; margin: .4rem 0 0; padding: .3rem; background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 10px 28px rgba(0, 0, 0, .16); max-height: 55vh; max-height: 55dvh;
  overflow: auto; overscroll-behavior: contain; position: absolute; left: 1rem; right: 1rem; z-index: 25;
}
.results li { display: flex; align-items: center; gap: .75rem; padding: .65rem .7rem; min-height: 56px; border-radius: 10px; cursor: pointer; }
.results li.active { background: var(--green-light); }
.results li.r-header {
  min-height: 0; padding: .5rem .7rem .25rem; cursor: default; font-size: .75rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
.results .r-icon { font-size: 1.45rem; width: 1.8rem; text-align: center; flex: none; }
.results li.add-new .r-icon { color: var(--green); font-weight: 700; }
.results .r-text { flex: 1; min-width: 0; font-size: 1.02rem; }
.results .r-alt { font-size: .875rem; color: var(--muted); line-height: 1.45; }
.results .r-badge { font-size: .75rem; color: var(--green-dark); font-weight: 600; white-space: nowrap; }
.results li.add-new .r-text { color: var(--green-dark); font-weight: 600; }

/* Tabs (buckets) */
.tabs {
  display: flex; gap: .4rem; overflow-x: auto; margin: .55rem -1rem 0; padding: 0 1rem .1rem;
  scrollbar-width: none; scroll-snap-type: x proximity; scroll-padding-inline: 1rem;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs.fade-right { -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent); mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent); }
.tabs.fade-left { -webkit-mask-image: linear-gradient(to right, transparent, #000 32px); mask-image: linear-gradient(to right, transparent, #000 32px); }
.tabs.fade-left.fade-right {
  -webkit-mask-image: linear-gradient(to right, transparent, #000 32px, #000 calc(100% - 40px), transparent);
  mask-image: linear-gradient(to right, transparent, #000 32px, #000 calc(100% - 40px), transparent);
}
.tab {
  flex: none; display: inline-flex; align-items: center; gap: .4rem; border: 1px solid var(--border); scroll-snap-align: start;
  background: var(--card); border-radius: 999px; padding: .45rem .8rem; min-height: 40px; cursor: pointer; font-size: .92rem; white-space: nowrap;
}
.tab .count { font-size: .75rem; background: var(--chip); border-radius: 999px; padding: 0 .45rem; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--text); border-color: var(--text); color: var(--card); font-weight: 600; }
.tab.active .count { background: rgba(255, 255, 255, .2); color: inherit; }
.tab.is-empty { opacity: .55; }
.tab.edit { padding: .45rem .7rem; color: var(--muted); }

/* ---------- List ---------- */
.content { padding: .5rem 1rem calc(6.5rem + var(--safe-bottom)); }
.chips { display: flex; flex-wrap: wrap; gap: .45rem; }
.chip {
  border: 1px solid var(--border); background: var(--card); border-radius: 999px; padding: .45rem .9rem;
  min-height: 40px; cursor: pointer; font-size: .92rem;
}
.chip.on { background: var(--green-light); border-color: var(--green); color: var(--green-dark); font-weight: 600; }

.empty { text-align: center; padding: 2.5rem 1rem; }
.empty-icon { font-size: 3rem; }

.group { margin-bottom: 1rem; }
.group h3 { margin: .25rem .25rem .45rem; display: flex; gap: .4rem; align-items: center; }
.group h3.clickable { cursor: pointer; }
.items {
  list-style: none; margin: 0; padding: 0; background: var(--card); border-radius: var(--radius);
  overflow: hidden; border: 1px solid var(--border); position: relative;
}
.item {
  position: relative; z-index: 1; background: var(--card); transition: transform .18s ease;
  display: flex; align-items: center; gap: .65rem; padding: .55rem .2rem .55rem .85rem; min-height: 62px;
  border-bottom: 1px solid var(--border); cursor: pointer; -webkit-user-select: none; user-select: none;
  touch-action: pan-y; /* horizontal moves are handled by swipe-to-delete */
}
.item.swiping { transition: none; }
.item:last-child { border-bottom: none; }
.item:active { background: var(--green-light); }
.check {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid #b4c0ba; flex: none;
  display: grid; place-items: center; color: #fff; background: var(--card);
}
.check .ic { width: 16px; height: 16px; stroke-width: 3; }
.item.checked .check { background: var(--green); border-color: var(--green); }
.item-emoji { font-size: 1.4rem; width: 1.8rem; text-align: center; flex: none; line-height: 1; }
.item.checked .item-emoji { opacity: .45; filter: grayscale(1); }
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 600; font-size: 1.02rem; overflow-wrap: anywhere; line-height: 1.35; }
.item-name .qty {
  display: inline-block; font-weight: 700; font-size: .85rem; color: var(--text); background: var(--chip);
  border-radius: 999px; padding: .1rem .55rem; margin-left: .4rem; white-space: nowrap; vertical-align: 1px;
}
.item-name .qty[data-edit] { cursor: pointer; }
.item-sub { font-size: .875rem; color: var(--muted); margin-top: .15rem; line-height: 1.45; overflow-wrap: anywhere; }
.item.checked .item-name { text-decoration: line-through; color: var(--muted); }
.item.checked .qty { background: transparent; color: var(--muted); }
.edit-btn { color: var(--icon); font-size: 1.05rem; }
.item.flash { animation: flash 1.2s ease; }
@keyframes flash { 0%, 40% { background: #fff6c8; } 100% { background: var(--card); } }
.item.urgent { box-shadow: inset 4px 0 0 var(--urgent); }
.urgent-group h3 { color: var(--urgent-text); }
.urgent-group .items { border-color: #f1cfb2; }
.badge {
  display: inline-flex; align-items: center; font-size: .75rem; background: var(--chip); color: var(--muted);
  border-radius: 999px; padding: .05rem .5rem; margin-left: .35rem; white-space: nowrap; vertical-align: 1px;
}
.badge.repeat { padding: .15rem .35rem; }
.badge .ic { width: 12px; height: 12px; }

/* Swipe left to delete */
.swipe-bg {
  position: absolute; left: 0; right: 0; z-index: 0; list-style: none;
  display: flex; align-items: center; justify-content: flex-end; padding-right: 1.4rem;
  background: #d9534f; color: #fff;
}
.swipe-bg .ic { width: 1.5rem; height: 1.5rem; transition: transform .15s ease; }
.swipe-bg.armed { background: var(--danger); }
.swipe-bg.armed .ic { transform: scale(1.25); }

.bought { margin-top: 1.25rem; }
.bought summary { cursor: pointer; font-weight: 600; color: var(--muted); padding: .7rem .25rem; min-height: 44px; }
.bought .btn { margin-top: .75rem; width: 100%; }

/* Floating "Shop" button, in thumb reach */
.fab {
  position: fixed; z-index: 30; right: max(1rem, calc(50vw - 320px + 1rem)); bottom: calc(1rem + var(--safe-bottom));
  border: none; border-radius: 999px; background: var(--green); color: #fff; font-weight: 700; font-size: 1.05rem;
  padding: .95rem 1.4rem; min-height: 56px; box-shadow: 0 6px 18px rgba(21, 128, 61, .4); cursor: pointer;
  display: inline-flex; align-items: center; gap: .5rem;
}
.fab:active { transform: scale(.96); }
#main:has(#search:focus) .fab, #main:has(#results:not([hidden])) .fab { display: none; }

/* ---------- Shopping mode ---------- */
.finish-btn { min-height: 40px; padding: .4rem 1.1rem; }
.progress-wrap { padding: .75rem 1rem; }
.progress { height: 10px; background: var(--border); border-radius: 999px; overflow: hidden; }
.progress .bar { height: 100%; background: var(--green); width: 0; transition: width .3s ease; }
.progress-text { font-size: .875rem; color: var(--muted); margin-top: .35rem; text-align: center; font-weight: 600; }
.item.big { padding: .9rem .9rem; min-height: 70px; }
.item.big .check { width: 34px; height: 34px; }
.item.big .check .ic { width: 19px; height: 19px; }
.item.big .item-name { font-size: 1.15rem; }
.item.big .item-emoji { font-size: 1.6rem; }

/* ---------- Bottom sheets (all dialogs) ---------- */
dialog {
  border: none; padding: 0; color: var(--text); background: var(--card);
  margin: auto auto 0; width: 100%; max-width: 640px; max-height: 92vh; max-height: 92dvh;
  border-radius: 22px 22px 0 0; box-shadow: 0 -8px 40px rgba(0, 0, 0, .25); overscroll-behavior: contain;
}
dialog[open] { animation: sheet-up .22s ease-out; }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog > form { padding: .6rem 1.1rem calc(1.1rem + var(--safe-bottom)); }
dialog > form::before {
  content: ""; display: block; width: 42px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto .8rem;
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@media (min-width: 700px) {
  dialog { margin: auto; width: min(92vw, 460px); border-radius: 18px; }
  dialog > form::before { display: none; }
  dialog > form { padding-top: 1.25rem; }
  dialog[open] { animation: none; }
}
.dialog-actions { display: flex; gap: .5rem; align-items: center; margin-top: 1rem; flex-wrap: wrap; }
.spacer { flex: 1; }
.sheet-title { display: flex; gap: .75rem; align-items: center; margin-bottom: .25rem; }
.sheet-title h2 { margin: 0; }
.sheet-icon { font-size: 2.2rem; }
.notice { background: var(--urgent-soft); color: var(--urgent-text); border-radius: 10px; padding: .6rem .8rem; font-size: .9rem; margin: .75rem 0 0; }

/* Quantity stepper */
.stepper { display: flex; gap: .6rem; align-items: center; }
.step-btn {
  width: 54px; height: 54px; flex: none; border-radius: 50%; border: 1px solid var(--border); background: var(--card);
  color: var(--green); font-size: 1.7rem; font-weight: 700; cursor: pointer; line-height: 1;
}
.step-btn:active { background: var(--green-light); }
.qty-input {
  flex: 1; min-width: 0; text-align: center; font-size: 1.3rem; font-weight: 700; padding: .7rem .5rem; min-height: 54px;
  border: 1px solid var(--border); border-radius: 14px; background: var(--card);
}
.qty-input::placeholder { font-size: .95rem; font-weight: 400; color: var(--muted); }
.unit-chips { margin-top: .6rem; }
.unit-chips .chip { min-width: 52px; }

/* Settings & bucket manager */
.share-box { background: var(--green-light); border-radius: var(--radius); padding: .9rem; text-align: center; }
.code-display { font-family: ui-monospace, Consolas, monospace; font-size: 1.8rem; font-weight: 700; letter-spacing: .12em; color: var(--green-dark); margin: .25rem 0 .75rem; user-select: all; }
.row { display: flex; gap: .5rem; justify-content: center; flex-wrap: wrap; }
.section-title { margin-top: 1.1rem; display: flex; align-items: center; gap: .4rem; }
.leave-link { text-align: center; margin: .5rem 0 0; }
.bucket-rows, .recurring-list { list-style: none; margin: 0; padding: 0; }
.bucket-row, .recurring-row { display: flex; align-items: center; gap: .5rem; padding: .4rem 0; border-bottom: 1px solid var(--border); min-height: 56px; }
.bucket-row input, .bucket-add input {
  flex: 1; min-width: 0; padding: .6rem .7rem; border: 1px solid var(--border); border-radius: 10px; font-size: 1rem;
  background: transparent; min-height: 44px;
}
.bucket-row .count { font-size: .8rem; color: var(--muted); min-width: 1.5rem; text-align: center; }
.icon-select { font-size: 1.2rem; padding: .35rem; min-height: 44px; border: 1px solid var(--border); border-radius: 10px; background: transparent; }
.bucket-add { display: flex; gap: .5rem; margin-top: .75rem; align-items: center; }
.check-list { display: flex; flex-direction: column; gap: .4rem; margin: .5rem 0; }
.check-row {
  display: flex; align-items: center; gap: .7rem; padding: .7rem .8rem; min-height: 52px; border: 1px solid var(--border);
  border-radius: 12px; cursor: pointer;
}
.check-row input { width: 1.3rem; height: 1.3rem; accent-color: var(--green); }
.check-row span:nth-child(2) { flex: 1; }
.check-row .count { font-size: .8rem; color: var(--muted); }
.check-row:has(input:checked) { border-color: var(--green); background: var(--green-light); }

/* Toast (sits above the floating button) */
.toast {
  position: fixed; left: 50%; bottom: calc(5.5rem + var(--safe-bottom)); transform: translateX(-50%);
  background: #1a2320; color: #fff; padding: .55rem .6rem .55rem 1rem; border-radius: 12px; z-index: 100;
  display: flex; gap: .75rem; align-items: center; width: max-content; max-width: calc(100vw - 2rem); box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.toast button {
  background: none; border: none; color: #86efac; font-weight: 700; cursor: pointer; min-height: 40px;
  padding: 0 .5rem; white-space: nowrap; text-transform: uppercase; font-size: .85rem; letter-spacing: .03em;
}
/* No floating button in shopping mode, so the toast can sit lower. */
body:has(#shop:not([hidden])) .toast { bottom: calc(1.25rem + var(--safe-bottom)); }

/* Desktop / mouse niceties */
@media (hover: hover) {
  .btn:hover { filter: brightness(.95); }
  .icon-btn:hover { background: var(--chip); color: var(--text); }
  .results li:not(.r-header):hover { background: var(--green-light); }
  .chip:hover { border-color: var(--green); }
  .item:hover { background: #fafcfb; }
  .group h3.clickable:hover { color: var(--green-dark); }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1513; --card: #161e1b; --chip: #232d29; --text: #e6ece9; --muted: #a0aea7; --icon: #7d8b85;
    --border: #26302c; --green-light: #14301f; --green-dark: #86efac;
    --urgent-text: #ffa45c; --urgent-soft: #3a2614;
  }
  .check { background: transparent; border-color: #4d5c56; }
  .item.checked .check { border-color: var(--green); }
  .code-display { color: #86efac; }
  .urgent-group .items { border-color: #5a3a1e; }
  .item.flash { animation: none; }
  .toast { background: #e6ece9; color: #0f1513; }
  .toast button { color: var(--green); }
  @media (hover: hover) { .item:hover { background: #1b2420; } }
}
