:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f2f5);
  --section-bg: var(--tg-theme-section-bg-color, #ffffff);
  --header-bg: var(--tg-theme-header-bg-color, var(--section-bg));
  --destructive: var(--tg-theme-destructive-text-color, #e53935);
  --radius: 16px;
  --radius-sm: 11px;
  --gap: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--secondary-bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  overscroll-behavior-y: none;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-top) + 8px) 12px 8px;
  background: var(--header-bg);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 18%, transparent);
}

.tabs {
  display: flex;
  gap: 4px;
  flex: 1;
  background: var(--secondary-bg);
  padding: 4px;
  border-radius: 12px;
}

.tab {
  flex: 1;
  padding: 8px 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--hint);
  border-radius: 9px;
  transition: background .15s, color .15s;
  white-space: nowrap;
}
.tab.is-active { background: var(--section-bg); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.lang-btn {
  width: 40px; height: 40px;
  border-radius: 12px;
  font-size: 18px;
  background: var(--secondary-bg);
  display: grid; place-items: center;
  flex-shrink: 0;
}

/* ---------- Layout ---------- */
.view {
  padding: var(--gap);
  padding-bottom: calc(96px + var(--safe-bottom));
  max-width: 720px;
  margin: 0 auto;
  min-height: 60vh;
}

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 10px;
  margin-bottom: 4px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  background: var(--section-bg);
  border: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
}
.chip.is-active { background: var(--button); color: var(--button-text); border-color: transparent; }

.search {
  width: 100%;
  padding: 11px 14px;
  margin-bottom: var(--gap);
  font-size: 15px;
  color: var(--text);
  background: var(--section-bg);
  border: 1px solid color-mix(in srgb, var(--hint) 20%, transparent);
  border-radius: var(--radius-sm);
  outline: none;
}
.search::placeholder { color: var(--hint); }

/* ---------- Product grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--gap);
}
@media (min-width: 560px) { .grid { grid-template-columns: repeat(3, 1fr); } }

.card {
  background: var(--section-bg);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.card-img {
  aspect-ratio: 1 / 1;
  background: var(--secondary-bg);
  display: grid;
  place-items: center;
  overflow: hidden;
}
.card-img img { width: 100%; height: 100%; object-fit: cover; }
.card-img .ph { font-size: 34px; font-weight: 700; color: var(--hint); opacity: .5; }

.card-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.card-name { font-weight: 600; font-size: 14px; line-height: 1.25; }
.card-sku { font-size: 11px; color: var(--hint); }
.card-price { font-size: 14px; font-weight: 700; margin-top: 2px; }
.card-price .unit { font-size: 12px; font-weight: 500; color: var(--hint); }
.card-foot { margin-top: auto; padding-top: 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 14px;
  font-size: 14px; font-weight: 600;
  border-radius: var(--radius-sm);
  transition: transform .08s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--button); color: var(--button-text); width: 100%; }
.btn-ghost { background: var(--secondary-bg); color: var(--text); }
.btn-danger { background: color-mix(in srgb, var(--destructive) 14%, transparent); color: var(--destructive); }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .5; pointer-events: none; }

.add-btn { width: 100%; background: color-mix(in srgb, var(--button) 14%, transparent); color: var(--button); }

/* qty stepper */
.stepper {
  display: grid;
  grid-template-columns: 38px 1fr 38px;
  align-items: center;
  background: var(--secondary-bg);
  border-radius: var(--radius-sm);
  height: 40px;
  overflow: hidden;
}
.stepper button { font-size: 20px; font-weight: 600; height: 100%; color: var(--button); }
.stepper .val { text-align: center; font-weight: 700; font-size: 15px; }

/* ---------- Cart bar ---------- */
.cartbar {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px calc(10px + var(--safe-bottom));
  background: var(--section-bg);
  border-top: 1px solid color-mix(in srgb, var(--hint) 16%, transparent);
  box-shadow: 0 -2px 16px rgba(0,0,0,.08);
}
.cartbar.hidden { display: none; }
.cartbar-info { display: flex; flex-direction: column; line-height: 1.2; }
.cartbar-count { font-size: 12px; color: var(--hint); }
.cartbar-total { font-size: 18px; font-weight: 700; }
.cartbar .btn-primary { width: auto; flex: 1; max-width: 60%; }

/* ---------- Orders ---------- */
.section-title { font-size: 13px; font-weight: 600; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; margin: 4px 2px 10px; }
.order {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.05);
}
.order-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.order-code { font-weight: 700; }
.order-total { font-weight: 700; }
.order-items { margin: 8px 0 0; padding: 0; list-style: none; }
.order-items li { font-size: 13px; color: var(--hint); padding: 2px 0; }
.badge {
  font-size: 11px; font-weight: 700; padding: 4px 9px; border-radius: 999px;
  white-space: nowrap;
  background: color-mix(in srgb, var(--hint) 16%, transparent); color: var(--text);
}
.badge.pending { background: #ffb30022; color: #b07b00; }
.badge.ok { background: #34c75922; color: #1a8a3a; }
.badge.bad { background: #ff3b3022; color: #c0271d; }
.order-actions { display: flex; gap: 8px; margin-top: 12px; }
.order-actions .btn { flex: 1; }

/* ---------- Sheet / modal ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; }
.sheet-backdrop.hidden, .sheet.hidden { display: none; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  background: var(--section-bg);
  border-radius: 20px 20px 0 0;
  padding: 8px 18px calc(20px + var(--safe-bottom));
  max-width: 720px; margin: 0 auto;
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-grip { width: 38px; height: 4px; border-radius: 3px; background: var(--hint); opacity: .4; margin: 8px auto 14px; }
.sheet h3 { margin: 0 0 6px; font-size: 18px; }
.sheet p { margin: 0 0 16px; color: var(--hint); font-size: 14px; }
.sheet .field { width: 100%; padding: 12px 14px; margin-bottom: 12px; font-size: 16px; border-radius: var(--radius-sm); border: 1px solid color-mix(in srgb, var(--hint) 22%, transparent); background: var(--secondary-bg); color: var(--text); outline: none; }
.lang-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 6px; }
.lang-list button { padding: 14px; border-radius: var(--radius-sm); background: var(--secondary-bg); font-size: 15px; font-weight: 600; text-align: left; }
.lang-list button.is-active { background: color-mix(in srgb, var(--button) 16%, transparent); color: var(--button); }

/* ---------- Misc ---------- */
.note { width: 100%; padding: 11px 14px; margin-bottom: 12px; font-size: 14px; border-radius: var(--radius-sm); border: 1px solid color-mix(in srgb, var(--hint) 20%, transparent); background: var(--section-bg); color: var(--text); resize: none; outline: none; }
.empty { text-align: center; color: var(--hint); padding: 60px 20px; font-size: 15px; }
.spinner { width: 26px; height: 26px; border: 3px solid color-mix(in srgb, var(--hint) 30%, transparent); border-top-color: var(--button); border-radius: 50%; animation: spin .8s linear infinite; margin: 60px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom));
  transform: translateX(-50%);
  background: rgba(0,0,0,.85); color: #fff;
  padding: 11px 18px; border-radius: 12px; font-size: 14px; font-weight: 500;
  z-index: 60; max-width: 86%; text-align: center;
  transition: opacity .2s, transform .2s;
}
.toast.hidden { opacity: 0; transform: translateX(-50%) translateY(8px); pointer-events: none; }
.login-msg { color: var(--hint); font-size: 13px; margin-top: 8px; text-align: center; }
.tg-login { display: flex; justify-content: center; min-height: 44px; margin-bottom: 6px; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--hint); font-size: 12px; margin: 14px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: color-mix(in srgb, var(--hint) 25%, transparent); }
.hidden { display: none; }
