:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f3f6);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --accent: var(--tg-theme-button-color, #2f7de1);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --border: rgba(127, 127, 127, 0.22);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 15px;
  background: var(--bg2);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

main { padding: 12px 12px 140px; }

.edit-banner {
  background: color-mix(in srgb, var(--accent) 16%, var(--bg));
  color: var(--text);
  border: 1px solid color-mix(in srgb, var(--accent) 40%, var(--border));
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 14px;
}
.edit-banner[hidden] { display: none; }

.tabs { display: flex; gap: 8px; margin-bottom: 12px; }
.tabs .chip { flex: 1; height: 38px; }

.tpl-list { margin-top: 12px; }
.tpl-row {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 10px 0;
  border-top: 1px solid var(--border);
}
.tpl-row:first-child { border-top: 0; }
.tpl-row .tpl-meta { flex: 1; min-width: 0; }
.tpl-row .tpl-name { color: var(--text); font-weight: 600; }
.tpl-row .tpl-info { font-size: 12px; color: var(--hint); margin-top: 2px; }
.tpl-row button { flex-shrink: 0; }

h2 { font-size: 16px; margin: 0 0 10px; }

.card {
  background: var(--bg);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--hint);
  margin-bottom: 10px;
}

input, select {
  font: inherit;
  color: var(--text);
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  width: 100%;
  outline: none;
}

input:focus, select:focus { border-color: var(--accent); }

.ac { position: relative; }
.ac-list {
  position: absolute;
  z-index: 20;
  left: 0; right: 0; top: calc(100% + 4px);
  margin: 0; padding: 4px 0;
  list-style: none;
  max-height: 280px;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.15);
}
.ac-list[hidden] { display: none; }
.ac-list li { padding: 8px 10px; cursor: pointer; color: var(--text); font-size: 14px; }
.ac-list li + li { border-top: 1px solid var(--border); }
.ac-list li.active, .ac-list li:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.ac-list .ac-full { color: var(--hint); font-size: 12px; margin-top: 2px; }
.ac-badge { color: var(--accent); font-size: 12px; margin-top: 4px; }

.search { display: flex; gap: 8px; margin-bottom: 12px; }
.search input { background: var(--bg); }

.chip {
  flex-shrink: 0;
  font: inherit;
  font-size: 13px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 8px;
  padding: 0 10px;
}
.chip.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.category {
  background: var(--bg);
  border-radius: 12px;
  margin-bottom: 10px;
  overflow: hidden;
}

.category > summary {
  list-style: none;
  cursor: pointer;
  padding: 13px 14px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.category > summary::-webkit-details-marker { display: none; }
.category > summary::after { content: "▾"; color: var(--hint); margin-left: 8px; }
.category:not([open]) > summary::after { content: "▸"; }

.badge {
  margin-left: auto;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  border-radius: 10px;
  padding: 1px 8px;
}
.badge:empty { display: none; }

.item { border-top: 1px solid var(--border); padding: 10px 14px; }
.item.selected { background: color-mix(in srgb, var(--accent) 8%, transparent); }

.item-head { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.item-head input[type="checkbox"] { width: 20px; height: 20px; margin: 1px 0 0; flex-shrink: 0; accent-color: var(--accent); }
.item-name { flex: 1; }
.item-price { color: var(--hint); font-size: 12px; margin-top: 2px; }

.item-controls { display: flex; gap: 10px; align-items: flex-end; margin: 10px 0 2px 30px; }
.item-controls[hidden] { display: none; }
.item-controls .field { margin: 0; }
.item-controls .price-field { flex: 1; }
.item-line-total { font-weight: 600; white-space: nowrap; padding-bottom: 9px; }

.stepper { display: flex; align-items: center; }
.stepper button {
  width: 36px; height: 38px;
  font-size: 18px;
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text);
}
.stepper button:first-child { border-radius: 8px 0 0 8px; }
.stepper button:last-child { border-radius: 0 8px 8px 0; }
.stepper input { width: 56px; text-align: center; border-radius: 0; border-left: 0; border-right: 0; padding: 9px 4px; }

#summary {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
}
.summary-row { display: flex; justify-content: space-between; align-items: baseline; }
#sum-total { font-size: 18px; }
#sum-breakdown { font-size: 12px; margin-top: 2px; min-height: 14px; }

.primary {
  width: 100%;
  margin-top: 8px;
  font: inherit;
  font-weight: 600;
  padding: 12px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
}

.link { font: inherit; font-size: 13px; background: none; border: 0; color: var(--accent); padding: 0; }
.link.danger { color: #e53935; }
.muted { color: var(--hint); }
.hidden { display: none !important; }
