:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --card: var(--tg-theme-section-bg-color, var(--bg));
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --line: color-mix(in srgb, var(--hint) 22%, transparent);
  --soft: color-mix(in srgb, var(--hint) 12%, transparent);
  --ok: #10b981; --warn: #f59e0b;
  --radius: 16px;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --spring: cubic-bezier(.34, 1.4, .64, 1);
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #1c1c1e; --bg2: #000000; --card: #1c1c1e; --text: #f2f2f7; --hint: #8e8e93; --accent: #4f9cf0;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg2); color: var(--text); overscroll-behavior-y: none;
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.center { text-align: center; }

/* ---------- top bar */
.topbar { position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg2) 82%, transparent);
  backdrop-filter: saturate(1.6) blur(14px); -webkit-backdrop-filter: saturate(1.6) blur(14px); padding: 6px 12px; }
.view-title { margin: 0 auto; max-width: 640px; font-size: 22px; padding: 8px 4px 6px; }

/* ---------- views */
.views { position: relative; max-width: 640px; margin: 0 auto; padding: 4px 14px calc(var(--tabbar-h) + var(--safe-b) + 92px); overflow-x: clip; }
.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin: 10px 0; border: 0; display: block; width: 100%; text-align: left; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; gap: 8px; }
.pressable { transition: transform .18s var(--ease), filter .18s; }
.pressable:active { transform: scale(.97); filter: brightness(.96); }
.section-h { font-size: 13px; margin: 18px 4px 4px; color: var(--hint); font-weight: 700; text-transform: uppercase; letter-spacing: .4px; }
.empty { text-align: center; padding: 30px 12px; color: var(--hint); }
.empty .big-emoji { font-size: 40px; display: block; margin-bottom: 6px; }
.skeleton { height: 64px; border-radius: var(--radius); margin: 10px 0;
  background: linear-gradient(90deg, var(--card) 0%, var(--soft) 50%, var(--card) 100%); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- stock */
.search { margin-top: 2px !important; background: var(--card) !important; }
.chips-row { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 2px; scrollbar-width: none; }
.chips-row::-webkit-scrollbar { display: none; }
.chips-row .chip { flex: none; }
.strip { display: flex; flex-direction: column; gap: 0; border-left: 4px solid var(--warn); }
.strip.low { border-left-color: var(--danger); }
.strip-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 7px 0; border: 0; background: none;
  width: 100%; text-align: left; border-top: 1px solid var(--line); }
.strip-row:first-of-type { border-top: 0; }
.strip-row .when { white-space: nowrap; font-size: 13px; font-weight: 600; }
.when.bad { color: var(--danger); }
.when.warn { color: var(--warn); }
.group { padding: 4px 14px; }
.row { display: flex; align-items: center; gap: 10px; width: 100%; border: 0; background: var(--card); text-align: left;
  padding: 11px 0; border-top: 1px solid var(--line); touch-action: pan-y; position: relative; z-index: 1; }
.swipe-wrap:first-child .row, .row:first-child { border-top: 0; }
.row .emoji { font-size: 22px; width: 30px; text-align: center; flex: none; }
.row .main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row .main b { font-weight: 600; overflow-wrap: anywhere; }
.row .main small { color: var(--hint); font-size: 13px; overflow-wrap: anywhere; }
.row .side { text-align: right; font-size: 13px; white-space: nowrap; display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.row.out .main b { color: var(--hint); }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--soft); white-space: nowrap; }
.badge.warn { background: color-mix(in srgb, var(--warn) 24%, transparent); }
.badge.bad { background: color-mix(in srgb, var(--danger) 22%, transparent); }
.badge.ok { background: color-mix(in srgb, var(--ok) 22%, transparent); }
.lvl { display: inline-flex; gap: 2px; vertical-align: middle; }
.lvl i { width: 7px; height: 12px; border-radius: 2px; background: var(--soft); }
.lvl i.on { background: var(--ok); }
.lvl.low i.on { background: var(--danger); }

/* swipe rows (destructive / quick actions behind a swipe) */
.swipe-wrap { position: relative; overflow: hidden; }
.swipe-wrap.card-wrap { border-radius: var(--radius); margin: 10px 0; }
.swipe-wrap.card-wrap > .card { margin: 0; position: relative; z-index: 1; touch-action: pan-y; }
.swipe-bg { position: absolute; inset: 0; display: flex; align-items: center; justify-content: flex-end; padding: 0 18px; font-weight: 700;
  opacity: 0; background: var(--danger); color: #fff; }
.swipe-bg.use { background: var(--accent); }

/* ---------- inbox */
.sync-line { margin: 2px 4px 0; min-height: 18px; }
.receipt-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.line { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); width: 100%; border-left: 0;
  border-right: 0; border-bottom: 0; background: var(--card); text-align: left; touch-action: pan-y; position: relative; z-index: 1; }
.swipe-wrap:first-child > .line { border-top: 0; }
.line .st { width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; font-size: 13px; font-weight: 800; flex: none;
  background: var(--soft); color: var(--hint); }
.line .st.ok { background: color-mix(in srgb, var(--ok) 22%, transparent); color: var(--ok); }
.line .st.q { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.line .st.w { background: color-mix(in srgb, var(--warn) 24%, transparent); color: var(--warn); }
.line .st.x { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }
.line .main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.line .main b { font-weight: 600; overflow-wrap: anywhere; }
.line .main small { color: var(--hint); font-size: 12px; overflow-wrap: anywhere; }
.line .price { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.receipt-actions { display: flex; gap: 8px; margin-top: 10px; }
.receipt-actions > * { flex: 1; }
.changed { border-left: 4px solid var(--warn); }

/* ---------- item sheet */
.item-top { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.item-top .emoji { font-size: 30px; }
.item-top h3 { margin: 0 !important; }
.big-total { font-size: 26px; font-weight: 800; letter-spacing: -.4px; font-variant-numeric: tabular-nums; }
.action-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin: 12px 0; }
.action-grid button { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 12px 4px; border-radius: 14px; border: 0;
  background: var(--soft); font-weight: 600; font-size: 13px; transition: transform .15s var(--spring); }
.action-grid button span { font-size: 22px; }
.action-grid button:active { transform: scale(.94); }
.action-grid button.main { background: var(--accent); color: var(--accent-text); }
.levels { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin: 10px 0; }
.levels button { border: 0; border-radius: 12px; padding: 10px 2px 8px; background: var(--soft); display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; transition: transform .15s var(--spring), background .2s; }
.levels button:active { transform: scale(.92); }
.levels button.sel { background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--accent); }
.jar { width: 22px; height: 30px; border: 2px solid currentColor; border-radius: 4px 4px 6px 6px; position: relative; overflow: hidden; opacity: .9; }
.jar i { position: absolute; left: 0; right: 0; bottom: 0; background: currentColor; opacity: .55; }
.pack { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); width: 100%; border-left: 0; border-right: 0;
  border-bottom: 0; background: var(--bg); text-align: left; touch-action: pan-y; position: relative; z-index: 1; }
.swipe-wrap:first-child > .pack { border-top: 0; }
.pack .main { flex: 1; display: flex; flex-direction: column; }
.pack .main small { color: var(--hint); font-size: 12px; }
.bar { height: 6px; border-radius: 3px; background: var(--soft); overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; width: 0; background: var(--ok); border-radius: 3px; transition: width .7s var(--ease); }
details.settings { margin-top: 14px; }
details.settings summary { cursor: pointer; font-weight: 700; padding: 8px 0; color: var(--accent); list-style: none; }
details.settings summary::-webkit-details-marker { display: none; }
.hist { font-size: 13px; color: var(--hint); padding: 4px 0; border-top: 1px solid var(--line); display: flex; justify-content: space-between; gap: 8px; }
.hist:first-of-type { border-top: 0; }

/* ---------- cooking */
.cook-head { display: flex; gap: 8px; align-items: center; margin-top: 6px; flex-wrap: wrap; }
.cook-head .primary { flex: 1 1 100%; }
.cook-head .secondary { flex: 1; padding: 12px; }
.dish-card .top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.dish-card .cost { font-weight: 800; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cost-card { text-align: center; padding: 18px 10px; background: var(--soft); border-radius: 18px; margin: 8px 0 12px; }
.cost-card .big { font-size: 34px; font-weight: 800; letter-spacing: -.6px; }
.ing { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.ing:first-child { border-top: 0; }
.ing > span:nth-child(2) { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.ing input { width: 92px; margin: 0 !important; padding: 8px 10px !important; text-align: right; font-weight: 700; }
.ing .x { border: 0; background: none; color: var(--danger); font-size: 20px; width: 32px; }
.qrow { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px; }
.qrow:first-child { border-top: 0; }

/* ---------- shared controls */
.chip { border: 0; background: var(--soft); color: var(--text); border-radius: 999px; padding: 7px 12px; font-size: 13px; transition: transform .15s var(--ease); }
.chip:active { transform: scale(.94); }
.chip.sel { background: var(--accent); color: var(--accent-text); }
.chip.danger { color: var(--danger); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 12px; }
.fab { position: fixed; right: max(18px, calc(50% - 320px + 18px)); bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); z-index: 9;
  width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 45%, transparent);
  transition: transform .25s var(--spring), opacity .2s; }
.fab svg { width: 26px; height: 26px; stroke-width: 2.5; }
.fab:active { transform: scale(.9); }
.fab.hidden { transform: scale(0); opacity: 0; pointer-events: none; }
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; padding: 6px 10px calc(6px + var(--safe-b));
  background: color-mix(in srgb, var(--bg) 86%, transparent); backdrop-filter: saturate(1.6) blur(16px); -webkit-backdrop-filter: saturate(1.6) blur(16px);
  border-top: 1px solid var(--line); }
.tab { position: relative; z-index: 1; flex: 1; border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 0; color: var(--hint); font-size: 11px; font-weight: 600; transition: color .25s; }
.tab svg { width: 24px; height: 24px; transition: transform .35s var(--spring); }
.tab.active { color: var(--accent); }
.tab.active svg { transform: translateY(-1px) scale(1.08); }
.tab:active svg { transform: scale(.86); }
.tab-pill { position: absolute; top: 6px; height: 50px; border-radius: 14px; background: color-mix(in srgb, var(--accent) 13%, transparent);
  transition: transform .42s var(--spring), width .42s var(--spring); }
.tab-badge { position: absolute; top: 2px; left: 50%; margin-left: 6px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center; font-weight: 700; z-index: 2; }
.seg { position: relative; display: flex; background: var(--bg2); border-radius: 12px; padding: 3px; margin-top: 6px; }
.seg-btn { position: relative; z-index: 1; flex: 1; border: 0; background: none; padding: 9px 6px; border-radius: 9px; font-weight: 600; font-size: 14px; transition: color .25s, background .25s; }
.seg-btn.active { background: var(--accent); color: var(--accent-text); }
.form label, .sheet label { display: block; margin-bottom: 12px; font-weight: 600; font-size: 14px; }
input[type=text], input[type=number], input[type=search], input[type=date] {
  width: 100%; margin-top: 6px; padding: 12px 14px; border-radius: 12px; border: 1px solid transparent; background: var(--bg2);
  font-weight: 400; font-size: 16px; transition: border-color .2s, box-shadow .2s; -webkit-appearance: none; appearance: none; }
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.primary { background: var(--accent); color: var(--accent-text); border: 0; border-radius: 12px; padding: 14px 16px; font-weight: 700; }
.primary:disabled { opacity: .5; }
.secondary { background: var(--soft); border: 0; border-radius: 12px; padding: 14px 16px; font-weight: 600; }
.secondary.danger { color: var(--danger); }
.wide { width: 100%; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 label { margin: 0 0 12px; }
.actions { display: flex; gap: 10px; margin-top: 14px; }
.actions > * { flex: 1; }
.field-label { font-weight: 600; font-size: 14px; }
.switch-row { display: flex !important; gap: 12px; align-items: center; font-weight: 600 !important; margin: 12px 0; padding: 12px; border-radius: 12px; background: var(--bg2); }
.switch-row span { flex: 1; }
.switch { appearance: none; -webkit-appearance: none; width: 50px; height: 30px; border-radius: 15px; background: var(--line); position: relative; flex: none;
  transition: background .25s; margin: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .3s var(--spring); }
.switch:checked { background: var(--ok); }
.switch:checked::after { transform: translateX(20px); }
.stepper { display: flex; align-items: center; gap: 10px; }
.stepper button { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--soft); font-size: 22px; font-weight: 600; flex: none;
  transition: transform .15s var(--spring); touch-action: manipulation; }
.stepper button:active { transform: scale(.86); }
.stepper input { text-align: center; font-weight: 800; font-size: 20px; margin: 0 !important; }
.results { max-height: 40vh; overflow: auto; margin: 8px 0; }
.result { display: flex; justify-content: space-between; gap: 8px; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line);
  background: none; padding: 12px 4px; transition: background .15s; }
.result:active { background: var(--soft); }
.result.sel { color: var(--accent); font-weight: 700; }
.result.new { color: var(--accent); font-weight: 600; }
.unit-label { color: var(--hint); font-weight: 400; }
.hintbox { background: var(--bg2); border-radius: 12px; padding: 10px 12px; font-size: 13px; margin: 8px 0; }
.hintbox.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); }

/* ---------- sheet */
.sheet-backdrop { position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .45); }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 31; max-width: 640px; margin: 0 auto; max-height: 92vh; overflow-y: auto;
  background: var(--bg); border-radius: 22px 22px 0 0; padding: 0 16px calc(18px + var(--safe-b)); box-shadow: 0 -10px 40px rgba(0,0,0,.25); touch-action: pan-y; }
.sheet h3 { margin: 4px 0 12px; font-size: 19px; }
.sheet-handle { position: sticky; top: 0; display: flex; justify-content: center; padding: 10px 0 8px; background: var(--bg); z-index: 3; cursor: grab; touch-action: none; }
.sheet-handle span { width: 38px; height: 5px; border-radius: 3px; background: var(--line); }

/* ---------- toast */
#toast { position: fixed; left: 16px; right: 16px; bottom: calc(var(--tabbar-h) + var(--safe-b) + 14px); z-index: 50; max-width: 560px; margin: 0 auto;
  background: color-mix(in srgb, var(--text) 88%, transparent); color: var(--bg); padding: 12px 16px; border-radius: 14px; text-align: center; font-weight: 600;
  transform: translateY(20px) scale(.96); opacity: 0; pointer-events: none; transition: transform .35s var(--spring), opacity .25s; }
#toast.show { transform: none; opacity: 1; }
#toast.error { background: var(--danger); color: #fff; }
#toast.has-action { bottom: calc(var(--tabbar-h) + var(--safe-b) + 92px); pointer-events: auto; display: flex; align-items: center; justify-content: space-between; gap: 12px; text-align: left; }
.toast-action { border: 0; background: none; color: var(--accent); font-weight: 800; font-size: 15px; padding: 4px 2px; white-space: nowrap; }
.weight-card { display: flex; align-items: center; gap: 12px; }
.weight-icon { font-size: 24px; width: 40px; height: 40px; display: grid; place-items: center; border-radius: 12px; background: var(--soft); }
.weight-text { flex: 1; display: flex; flex-direction: column; }
.weight-text small { color: var(--hint); }
.chev { color: var(--hint); font-size: 24px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; animation-duration: 0s !important; }
}
