:root {
  --bg: #f3f6f2;
  --card: #ffffff;
  --ink: #1c2620;
  --muted: #687369;
  --line: #e1e7e0;
  --soft: #f6f9f5;
  --soft-2: #eaf0e8;
  --primary: #2e8b57;
  --primary-hover: #237045;
  --primary-soft: #e3f3e9;
  --danger: #cf4152;
  --danger-soft: #fbe3e6;
  --urgent: #e8590c;
  --urgent-soft: #fff0e6;
  --tip: #fff7dc;
  --radius: 16px;
  --shadow: 0 4px 18px rgba(30, 60, 40, 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121714;
    --card: #1c231e;
    --ink: #e7ede8;
    --muted: #97a39a;
    --line: #2e3831;
    --soft: #222a24;
    --soft-2: #2a332c;
    --primary: #3fae6f;
    --primary-hover: #4cc07f;
    --primary-soft: #1f3a2a;
    --danger: #ef6b7b;
    --danger-soft: #3d2227;
    --urgent: #ff8a3d;
    --urgent-soft: #3a2618;
    --tip: #333020;
    --shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 17px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); margin: 8px 0 0; }

/* --- Кнопки --- */
button { font-family: inherit; cursor: pointer; color: inherit; }

.btn-primary {
  width: 100%;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 17px;
  font-weight: 600;
  transition: background 0.15s;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 15px;
}
.btn-ghost:hover { background: var(--soft-2); }

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 15px;
  font-weight: 600;
}

/* --- Вход --- */
#login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.login-card .logo { font-size: 52px; }
.login-card h1 { margin: 8px 0 2px; font-size: 24px; }
.login-card form { text-align: left; margin-top: 18px; }

/* --- Поля --- */
label {
  display: block;
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 12px;
  font-weight: 500;
}
input[type="text"], input[type="password"] {
  width: 100%;
  margin-top: 5px;
  padding: 12px;
  font-size: 16px; /* 16px — чтобы iPhone не увеличивал страницу при вводе */
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--soft);
  color: var(--ink);
}
input:focus { outline: 2px solid var(--primary); border-color: var(--primary); }

label.checkbox-field {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: var(--ink);
  font-size: 16px;
}
label.checkbox-field input {
  width: 22px; height: 22px; margin: 0;
  accent-color: var(--urgent);
  cursor: pointer;
}

/* --- Шапка --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-size: 19px; font-weight: 700; white-space: nowrap; }
.topbar-actions { display: flex; gap: 8px; min-width: 0; }
#name-btn { max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.offline-bar {
  background: var(--tip);
  text-align: center;
  font-size: 14px;
  padding: 8px 12px;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px 120px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 18px;
}

/* --- Добавление --- */
.add-row { display: flex; gap: 10px; align-items: stretch; }
.add-input-wrap { flex: 1; position: relative; min-width: 0; }
#add-name {
  margin-top: 0;
  font-size: 18px;
  padding: 14px;
  height: 100%;
}
.add-btn {
  flex-shrink: 0;
  width: 54px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-size: 26px;
  font-weight: 600;
}
.add-btn:hover { background: var(--primary-hover); }

.suggest {
  position: absolute;
  left: 0; right: 0;
  top: calc(100% + 6px);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  z-index: 20;
  overflow: hidden;
  max-height: 320px;
  overflow-y: auto;
}
.suggest-row {
  display: flex;
  align-items: center;
}
.suggest-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  background: transparent;
  border: none;
  padding: 12px 14px;
  font-size: 16px;
  min-width: 0;
}
.suggest-item .s-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggest-item .s-extra { font-size: 13px; color: var(--muted); white-space: nowrap; }
.suggest-row.active, .suggest-row:hover { background: var(--soft-2); }
.suggest-forget {
  border: none; background: transparent;
  color: var(--muted); font-size: 16px;
  padding: 12px 14px;
}
.suggest-forget:hover { color: var(--danger); }
.suggest-row.on-list .s-name::after { content: " · уже в списке"; color: var(--muted); font-size: 13px; }

.detected {
  font-size: 14px;
  color: var(--muted);
  margin-top: 8px;
}

.more-toggle {
  margin-top: 10px;
  background: transparent;
  border: none;
  padding: 4px 0;
  font-size: 14px;
  color: var(--primary);
  font-weight: 600;
}
.more-toggle .chevron { display: inline-block; transition: transform 0.2s; }
.more-toggle[aria-expanded="true"] .chevron { transform: rotate(180deg); }
.add-more { margin-top: 12px; }

.frequent { margin-top: 14px; }
.frequent .chip { font-size: 14px; padding: 7px 11px; }

.field { margin-bottom: 14px; }
.field-label {
  display: block;
  font-size: 14px;
  color: var(--muted);
  font-weight: 500;
  margin-bottom: 8px;
}
.row { display: flex; gap: 12px; }
.row .grow { flex: 1; }

/* --- Чипсы --- */
.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--soft);
  font-size: 15px;
  transition: background 0.12s, border-color 0.12s;
}
.chip:hover { border-color: var(--primary); }
.chip.selected { background: var(--primary); color: #fff; border-color: var(--primary); }
.chip .dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.chip .count { font-size: 12px; opacity: 0.75; font-weight: 600; }

/* --- Вкладки --- */
.tabs { display: flex; gap: 8px; margin-bottom: 12px; }
.tab {
  flex: 1;
  padding: 11px;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
}
.tab.active { background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* --- Фильтр по магазину --- */
.shop-filter-wrap { margin-bottom: 14px; }
.filter-bar {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.filter-bar::-webkit-scrollbar { display: none; }
.filter-bar .chip { flex-shrink: 0; font-size: 14px; padding: 7px 12px; }

/* --- Список --- */
.list { display: flex; flex-direction: column; gap: 14px; }

.group-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 0 4px 6px;
}
.group-title .count { font-weight: 500; text-transform: none; letter-spacing: 0; }
.group-items {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 12px 12px 14px;
  border-top: 1px solid var(--line);
}
.item:first-child { border-top: none; }
.item.urgent { background: var(--urgent-soft); }

.check {
  flex-shrink: 0;
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 2px solid var(--primary);
  background: transparent;
  display: grid;
  place-items: center;
  font-size: 18px;
  color: #fff;
  padding: 0;
}
.check:hover { background: var(--primary-soft); }
.item.done .check { background: var(--primary); }
.item.done .title { text-decoration: line-through; color: var(--muted); }
.item.done { opacity: 0.75; }

.item .info { flex: 1; min-width: 0; cursor: pointer; }
.item .title { font-weight: 600; overflow-wrap: anywhere; }
.item .qty { font-weight: 600; color: var(--primary); margin-left: 4px; white-space: nowrap; }
.item .nl { font-size: 14px; color: var(--muted); font-style: italic; }
.item .sub { font-size: 13px; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }
.item .note { font-size: 14px; margin-top: 2px; overflow-wrap: anywhere; }
.flag-urgent { color: var(--urgent); font-weight: 700; font-size: 13px; }

.shop-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  border-radius: 6px;
  padding: 1px 6px;
  margin-right: 4px;
  vertical-align: 1px;
}

.icon-btn {
  flex-shrink: 0;
  border: none;
  background: var(--soft-2);
  border-radius: 10px;
  width: 40px; height: 40px;
  font-size: 16px;
  display: grid;
  place-items: center;
}
.icon-btn:hover { background: var(--primary-soft); }
.icon-btn.readd { width: auto; padding: 0 12px; font-size: 14px; font-weight: 600; color: var(--primary); }

.other-shops {
  text-align: center;
  font-size: 14px;
  color: var(--muted);
}
.other-shops button {
  background: none; border: none; padding: 0;
  color: var(--primary); font-weight: 600; font-size: 14px;
  text-decoration: underline;
}

.basket-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.empty { text-align: center; color: var(--muted); padding: 40px 0; font-size: 18px; }

/* --- Модальное окно --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 20, 14, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 16px;
  overflow-y: auto;
  z-index: 100;
}
.modal { width: 100%; max-width: 560px; margin: auto; padding: 20px; }
.modal h2 { margin: 0 0 16px; font-size: 20px; }
.modal-actions { display: flex; gap: 10px; margin-top: 8px; }
.modal-actions .btn-ghost { flex: 1; padding: 14px; font-size: 15px; }
.modal-actions .btn-primary { flex: 2; width: auto; }

/* --- Всплывающее сообщение --- */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: calc(100% - 32px);
  max-width: 480px;
  background: #1c2620;
  color: #fff;
  border-radius: 14px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 16px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.25);
  z-index: 200;
}
.toast.error { background: var(--danger); }
.toast button {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.5);
  color: #fff;
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 15px;
  font-weight: 600;
  flex-shrink: 0;
}

@media (max-width: 420px) {
  body { font-size: 16px; }
  .brand { font-size: 17px; }
  #name-btn { max-width: 110px; }
  .modal-actions { flex-wrap: wrap; }
  .modal-actions .btn-danger { flex-basis: 100%; order: 3; }
}

/* На телефонах «наведение» залипает после касания — отключаем его. */
@media (hover: none) {
  .chip:hover { border-color: var(--line); }
  .chip.selected:hover { border-color: var(--primary); }
  .check:hover { background: transparent; }
  .item.done .check:hover { background: var(--primary); }
  .btn-ghost:hover, .icon-btn:hover { background: inherit; }
  .icon-btn:hover { background: var(--soft-2); }
}
