:root {
  --accent: #10b981;
  --accent-dark: #059669;
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1f2937;
  --text-soft: #6b7280;
  --border: #e5e7eb;
  --danger: #ef4444;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .07), 0 1px 2px rgba(0, 0, 0, .04);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding-bottom: env(safe-area-inset-bottom);
}

.hidden { display: none !important; }
.spacer { flex: 1; }

/* ---------- login ---------- */
.login-view {
  min-height: 100dvh;
  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 28px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.login-logo { font-size: 44px; }
.login-card h1 { margin: 0 0 6px; font-size: 22px; }
.login-card input {
  padding: 12px;
  font-size: 16px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-error { color: var(--danger); margin: 0; font-size: 14px; }

/* ---------- header / tabs ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 8px;
  max-width: 980px;
  margin: 0 auto;
}
.app-title { font-size: 20px; font-weight: 700; }
.header-actions { display: flex; gap: 4px; }

.btn-ghost {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-soft);
  padding: 6px 10px;
  border-radius: 8px;
}
.btn-ghost:hover { background: rgba(0, 0, 0, .05); color: var(--text); }

.tabs {
  display: flex;
  gap: 4px;
  padding: 0 16px 12px;
  max-width: 980px;
  margin: 0 auto;
}
.tab {
  flex: 1;
  padding: 9px;
  font-size: 15px;
  font-weight: 600;
  border: none;
  background: var(--card);
  color: var(--text-soft);
  cursor: pointer;
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.tab.active { background: var(--accent); color: #fff; }

.tab-panel {
  max-width: 980px;
  margin: 0 auto;
  padding: 0 16px 90px;
}

/* ---------- buttons ---------- */
.btn {
  background: #eceff3;
  color: var(--text);
  border: none;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 10px;
  transition: filter .15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:hover { filter: brightness(.96); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); filter: none; }
.btn-outline { background: var(--card); border: 1.5px solid var(--border); }
.btn-danger-outline { background: var(--card); border: 1.5px solid #fecaca; color: var(--danger); }
.btn-small { padding: 7px 12px; font-size: 13.5px; border-radius: 8px; }
.btn-block { width: 100%; }

/* ---------- search ---------- */
.search-bar { position: relative; margin-bottom: 14px; }
.search-bar > input {
  width: 100%;
  padding: 13px 16px;
  font-size: 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
}
.search-bar > input:focus { outline: none; border-color: var(--accent); }

.search-results {
  list-style: none;
  padding: 4px;
  margin: 6px 0 0;
  max-height: 320px;
  overflow-y: auto;
  border-radius: var(--radius);
  position: absolute;
  width: 100%;
  background: var(--card);
  z-index: 100;
  box-shadow: 0 10px 25px rgba(0, 0, 0, .12);
}
.search-results li {
  padding: 9px 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  border-radius: 8px;
}
.search-results li:hover, .search-results li:active { background: #f0f4f2; }
.thumb {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  flex: none;
}
img.thumb { object-fit: cover; }
.thumb-placeholder {
  background: #eef1f4;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
}
.result-text { display: flex; flex-direction: column; min-width: 0; }
.result-text .sub { font-size: 12.5px; color: var(--text-soft); }
.result-badge {
  margin-left: auto;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent-dark);
  background: #d1fae5;
  padding: 2px 8px;
  border-radius: 99px;
  flex: none;
}

/* ---------- plan selector ---------- */
.plan-select-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;
}
.plan-select-bar select {
  flex: 1;
  min-width: 0;
  padding: 11px 12px;
  font-size: 15px;
  font-weight: 600;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  box-shadow: var(--shadow);
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%236b7280' d='M6 8 0 0h12z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 34px;
}
.plan-select-bar select:focus { outline: none; border-color: var(--accent); }
.plan-menu-btn {
  flex: none;
  width: 44px;
  height: 44px;
  font-size: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.plan-menu-btn:hover { background: #f0f4f2; }

/* ---------- plan toolbar ---------- */
.plan-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.portions-control { display: flex; align-items: center; gap: 12px; font-weight: 600; font-size: 15px; }
.stepper {
  display: flex;
  align-items: center;
  background: var(--card);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.stepper button {
  border: none;
  background: none;
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 700;
  font-size: 24px;
  line-height: 1;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.stepper button:active { background: #ecfdf5; }
.stepper input {
  border: none;
  text-align: center;
  font-weight: 700;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper input:focus { outline: none; }

/* large stepper (portions) — big touch targets for mobile */
.stepper-lg button { width: 52px; height: 48px; }
.stepper-lg input { width: 56px; height: 48px; font-size: 19px; }

.plan-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* hide native number spinners everywhere (avoid tiny up/down arrows) */
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }

/* ---------- plan list ---------- */
.plan-list { margin-bottom: 14px; }

.plan-head {
  display: grid;
  grid-template-columns: 2.2fr 1fr 1fr .7fr .7fr .7fr .7fr 74px;
  gap: 8px;
  padding: 8px 14px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: .03em;
}

.plan-row {
  display: grid;
  grid-template-columns: 2.2fr 1fr 1fr .7fr .7fr .7fr .7fr 74px;
  gap: 8px;
  align-items: center;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  margin-bottom: 8px;
}

.row-name {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  cursor: pointer;
}
.row-name .texts { min-width: 0; }
.row-name .n { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-name .sub { font-size: 12px; color: var(--text-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.gram-input {
  width: 100%;
  max-width: 96px;
  min-height: 42px;
  padding: 9px 6px;
  font-size: 16px;
  text-align: center;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: #fbfcfd;
}
.gram-input:focus { outline: none; border-color: var(--accent); }
.gram-input.locked { border-color: var(--accent); background: #ecfdf5; }

.macro-cell { font-size: 14px; text-align: center; }
.macro-cell .u { display: none; }

.row-actions { display: flex; align-items: center; gap: 6px; justify-content: flex-end; }

.lock-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 17px;
  padding: 4px;
  border-radius: 8px;
  opacity: .45;
}
.lock-btn.locked { opacity: 1; }
.remove-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 15px;
  color: var(--text-soft);
  padding: 4px 6px;
  border-radius: 8px;
}
.remove-btn:hover { color: var(--danger); background: #fef2f2; }

.gram-label { display: none; font-size: 11px; color: var(--text-soft); font-weight: 600; }

/* ---------- totals bar ---------- */
.totals-bar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(12px + env(safe-area-inset-bottom));
  width: calc(100% - 24px);
  max-width: 948px;
  background: #111827;
  color: #fff;
  border-radius: 14px;
  padding: 11px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  z-index: 50;
  font-size: 14.5px;
}
.totals-label { font-weight: 700; color: #9ca3af; font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; flex: none; }
.totals-values { display: flex; gap: 14px; flex-wrap: wrap; }
.totals-values b { color: #34d399; }
.totals-values .cost b { color: #fbbf24; }

/* ---------- shopping list ---------- */
.shopping-rows { display: flex; flex-direction: column; gap: 2px; }
.shopping-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
}
.shopping-item:last-child { border-bottom: none; }
.shopping-item input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
.shopping-item .s-name { flex: 1; font-weight: 600; font-size: 15px; }
.shopping-item .s-name .sub { font-weight: 400; font-size: 12.5px; color: var(--text-soft); }
.shopping-item .s-amt { font-weight: 700; color: var(--accent-dark); font-size: 15px; white-space: nowrap; }
.shopping-item.checked .s-name { text-decoration: line-through; color: var(--text-soft); }

/* ---------- empty state ---------- */
.empty-state {
  text-align: center;
  color: var(--text-soft);
  padding: 40px 20px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}

/* ---------- library ---------- */
.lib-section { margin-bottom: 26px; }
.lib-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.lib-header h2 { margin: 0; font-size: 17px; }
.lib-list { display: grid; gap: 8px; grid-template-columns: 1fr; }

.lib-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  cursor: pointer;
}
.lib-card:hover { box-shadow: 0 3px 10px rgba(0,0,0,.1); }
.lib-card .texts { min-width: 0; flex: 1; }
.lib-card .n { font-weight: 600; font-size: 15px; }
.lib-card .sub { font-size: 12.5px; color: var(--text-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-card .macros { font-size: 12.5px; color: var(--text-soft); flex: none; text-align: right; }
.lib-card .thumb { width: 46px; height: 46px; }

/* ---------- modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, .5);
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal-card {
  background: var(--card);
  border-radius: 18px 18px 0 0;
  width: 100%;
  max-width: 520px;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 8px;
}
.modal-header h3 { margin: 0; font-size: 17px; }
.modal-body { padding: 6px 18px 12px; overflow-y: auto; }
.modal-footer {
  display: flex;
  gap: 8px;
  padding: 12px 18px calc(16px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}

.field { display: block; margin-bottom: 12px; }
.field span { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-soft); margin-bottom: 4px; }
.field input {
  width: 100%;
  padding: 10px 12px;
  font-size: 16px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
}
.field input:focus { outline: none; border-color: var(--accent); }

.macro-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.macro-grid .field { margin-bottom: 8px; }
.macro-grid input { padding: 10px 6px; text-align: center; }

.per100-note { font-size: 12.5px; font-weight: 700; color: var(--text-soft); text-transform: uppercase; letter-spacing: .03em; margin: 14px 0 8px; }
.hint { font-size: 12.5px; color: var(--text-soft); margin: 8px 0 0; }
.warning { font-size: 13px; color: #b45309; background: #fef3c7; border-radius: 8px; padding: 8px 10px; }

.img-field { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.img-preview-thumb {
  width: 74px;
  height: 74px;
  border-radius: 14px;
  background: #eef1f4;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  overflow: hidden;
  flex: none;
}
.img-preview-thumb img { width: 100%; height: 100%; object-fit: cover; }
.img-buttons { display: flex; gap: 8px; flex-wrap: wrap; }

.off-row { display: flex; gap: 8px; margin-bottom: 14px; align-items: flex-start; }
.off-row > button { flex: none; margin-top: 1px; }
.off-search { position: relative; flex: 1; min-width: 0; }
.off-search input {
  width: 100%;
  padding: 8px 10px;
  font-size: 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
}
.off-search input:focus { outline: none; border-color: var(--accent); }

/* meal editor rows */
.meal-ing-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }
.meal-ing-row select {
  flex: 1;
  min-width: 0;
  padding: 9px 8px;
  font-size: 14.5px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: var(--card);
}
.meal-ing-row input {
  width: 76px;
  padding: 9px 6px;
  font-size: 14.5px;
  text-align: center;
  border: 1.5px solid var(--border);
  border-radius: 8px;
}

/* ---------- cropper ---------- */
.crop-card { padding-bottom: 0; }
.crop-viewport {
  position: relative;
  width: min(84vw, 380px);
  height: min(84vw, 380px);
  margin: 8px auto;
  overflow: hidden;
  border-radius: 12px;
  background: #111;
  touch-action: none;
}
.crop-viewport img {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  user-select: none;
  -webkit-user-select: none;
  max-width: none;
}
.crop-frame {
  position: absolute;
  inset: 0;
  border: 2px solid rgba(255, 255, 255, .85);
  border-radius: 12px;
  pointer-events: none;
}
#crop-zoom { display: block; width: min(84vw, 380px); margin: 4px auto 8px; accent-color: var(--accent); }

/* ---------- scanner ---------- */
.scan-viewport {
  width: min(84vw, 420px);
  margin: 8px auto;
  border-radius: 12px;
  overflow: hidden;
  background: #111;
}
.scan-viewport video { width: 100%; display: block; }
#scan-status { text-align: center; padding-bottom: 16px; }

/* ---------- lightbox / toast ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .85);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}
.lightbox img { max-width: 92%; max-height: 92%; border-radius: 10px; }

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(80px + env(safe-area-inset-bottom));
  background: #111827;
  color: #fff;
  padding: 10px 18px;
  border-radius: 99px;
  font-size: 14px;
  z-index: 3000;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .3);
}

/* ================= mobile: plan rows become cards ================= */
@media (max-width: 719px) {
  .plan-head { display: none; }

  .plan-row {
    grid-template-columns: repeat(4, 1fr);
    grid-template-areas:
      "name name name actions"
      "pp   pp   tot  tot"
      "mkcal mp  mf   mc";
    row-gap: 10px;
    padding: 12px 14px;
  }
  .plan-row .row-name { grid-area: name; }
  .plan-row .cell-pp { grid-area: pp; }
  .plan-row .cell-tot { grid-area: tot; }
  .plan-row .row-actions { grid-area: actions; }
  .plan-row .m-kcal { grid-area: mkcal; }
  .plan-row .m-p { grid-area: mp; }
  .plan-row .m-f { grid-area: mf; }
  .plan-row .m-c { grid-area: mc; }

  .cell-pp, .cell-tot { display: flex; align-items: center; gap: 8px; }
  .gram-label { display: block; flex: none; }
  .gram-input { max-width: none; flex: 1; }

  .macro-cell { background: #f6f8fa; border-radius: 8px; padding: 5px 2px; font-size: 13px; }
  .macro-cell .u { display: block; font-size: 10.5px; color: var(--text-soft); font-weight: 600; }

  .plan-actions { width: 100%; }
  .plan-actions .btn { flex: 1; }

  .macro-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (min-width: 720px) {
  .modal { align-items: center; padding: 20px; }
  .modal-card { border-radius: 18px; max-height: 86vh; }
  .lib-list { grid-template-columns: 1fr 1fr; }
}
