/* ===== Sprout Mini App — стили ===== */

:root {
  /* Фолбэки на случай разработки/просмотра вне Telegram — внутри Telegram
     эти переменные сразу перезаписываются реальными themeParams через JS */
  --tg-theme-bg-color: #ffffff;
  --tg-theme-text-color: #1c1c1e;
  --tg-theme-hint-color: #8e8e93;
  --tg-theme-link-color: #1faa59;
  --tg-theme-button-color: #1faa59;
  --tg-theme-button-text-color: #ffffff;
  --tg-theme-secondary-bg-color: #f2f2f7;
  --tg-theme-section-bg-color: #ffffff;
  --tg-theme-section-separator-color: #e5e5ea;
  --tg-theme-subtitle-text-color: #8e8e93;
  --tg-theme-destructive-text-color: #d14b4b;

  /* Бренд Sprout: один намеренный зелёный — и для акцентов, и для "хорошей" оценки,
     это и есть идентичность приложения ("зелёный = растёт, полезно") */
  --sprout-green: #2ba84a;
  --sprout-green-soft: rgba(43, 168, 74, 0.14);
  --sprout-yellow: #e8a33d;
  --sprout-yellow-soft: rgba(232, 163, 61, 0.14);
  --sprout-red: #d14b4b;
  --sprout-red-soft: rgba(209, 75, 75, 0.14);
  --sprout-gray: #9a9aa0;
  --sprout-gray-soft: rgba(154, 154, 160, 0.14);

  --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;
  height: 100%;
  background: var(--tg-theme-bg-color);
  color: var(--tg-theme-text-color);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ---------- Вкладки-экраны ---------- */
#views {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.view {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  overflow-y: auto;
}

.view.active {
  display: flex;
}

/* ---------- Скан ---------- */
#view-scan {
  background: #000;
}

#camera-feed-container {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

#camera-feed-container video {
  width: 100% !important;
  height: 100% !important;
  object-fit: cover;
}

.scan-frame {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 78%;
  max-width: 340px;
  aspect-ratio: 1.5;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.scan-corner {
  position: absolute;
  width: 28px;
  height: 28px;
  border: 3px solid var(--sprout-green);
}
.scan-corner.tl { top: 0; left: 0; border-right: none; border-bottom: none; border-radius: 10px 0 0 0; }
.scan-corner.tr { top: 0; right: 0; border-left: none; border-bottom: none; border-radius: 0 10px 0 0; }
.scan-corner.bl { bottom: 0; left: 0; border-right: none; border-top: none; border-radius: 0 0 0 10px; }
.scan-corner.br { bottom: 0; right: 0; border-left: none; border-top: none; border-radius: 0 0 10px 0; }

.scan-line {
  position: absolute;
  left: 4%;
  right: 4%;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--sprout-green), transparent);
  box-shadow: 0 0 8px var(--sprout-green);
  animation: scan-sweep 2.2s ease-in-out infinite;
}

@keyframes scan-sweep {
  0% { top: 6%; opacity: 0; }
  10% { opacity: 1; }
  50% { top: 94%; }
  90% { opacity: 1; }
  100% { top: 94%; opacity: 0; }
}

#scan-hint {
  position: absolute;
  bottom: calc(110px + var(--safe-bottom));
  left: 0;
  right: 0;
  text-align: center;
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
  padding: 0 24px;
}

/* ---------- История ---------- */
#view-history,
#view-settings {
  padding: calc(20px + var(--safe-top)) 16px 16px;
  background: var(--tg-theme-bg-color);
}

#view-history h1,
#view-settings h1 {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 16px;
}

.history-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  background: var(--tg-theme-section-bg-color);
  border: none;
  border-bottom: 1px solid var(--tg-theme-section-separator-color);
  text-align: left;
  font-family: inherit;
  color: var(--tg-theme-text-color);
  cursor: pointer;
}
.history-row:first-child { border-radius: 14px 14px 0 0; }
.history-row:last-child { border-radius: 0 0 14px 14px; border-bottom: none; }

.history-thumb,
.history-thumb.placeholder {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--tg-theme-secondary-bg-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.history-info {
  flex: 1;
  min-width: 0;
}

.history-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-meta {
  font-size: 13px;
  color: var(--tg-theme-hint-color);
  margin-top: 2px;
}

.history-score {
  flex-shrink: 0;
  min-width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
}
.history-score.good { background: var(--sprout-green-soft); color: var(--sprout-green); }
.history-score.medium { background: var(--sprout-yellow-soft); color: var(--sprout-yellow); }
.history-score.bad { background: var(--sprout-red-soft); color: var(--sprout-red); }
.history-score.unknown { background: var(--sprout-gray-soft); color: var(--sprout-gray); }

/* ---------- Настройки ---------- */
.settings-card {
  background: var(--tg-theme-section-bg-color);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 12px;
}
.settings-card .label {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
}
.settings-card .value {
  font-size: 14px;
  color: var(--tg-theme-hint-color);
}

/* ---------- Нижняя навигация ---------- */
#tabbar {
  display: flex;
  background: var(--tg-theme-secondary-bg-color);
  border-top: 1px solid var(--tg-theme-section-separator-color);
  padding-bottom: var(--safe-bottom);
}

.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 8px 0 6px;
  background: none;
  border: none;
  color: var(--tg-theme-hint-color);
  font-family: inherit;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
}

.tab svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tab.active {
  color: var(--sprout-green);
}

/* ---------- Нижний лист с результатом ---------- */
#sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 10;
}
#sheet-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

#result-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 82vh;
  background: var(--tg-theme-bg-color);
  border-radius: 20px 20px 0 0;
  transform: translateY(105%);
  transition: transform 0.35s cubic-bezier(0.2, 0.8, 0.2, 1);
  z-index: 11;
  padding-bottom: calc(16px + var(--safe-bottom));
  overflow-y: auto;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.2);
}
#result-sheet.open {
  transform: translateY(0);
}

#sheet-close-zone {
  padding-top: 10px;
}

.sheet-drag-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--tg-theme-section-separator-color);
  margin: 0 auto 16px;
}

#sheet-content {
  padding: 0 20px 4px;
}

.product-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.product-thumb,
.product-thumb.placeholder {
  width: 52px;
  height: 52px;
  border-radius: 12px;
  object-fit: cover;
  background: var(--tg-theme-secondary-bg-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  flex-shrink: 0;
}

.product-title {
  font-size: 17px;
  font-weight: 700;
  line-height: 1.3;
}

.score-block {
  position: relative;
  width: 132px;
  height: 132px;
  margin: 4px auto 18px;
}

.score-ring {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.ring-bg {
  fill: none;
  stroke: var(--tg-theme-secondary-bg-color);
  stroke-width: 10;
}

.ring-fg {
  fill: none;
  stroke-width: 10;
  stroke-linecap: round;
}
.ring-fg.good { stroke: var(--sprout-green); }
.ring-fg.medium { stroke: var(--sprout-yellow); }
.ring-fg.bad { stroke: var(--sprout-red); }
.ring-fg.unknown { stroke: var(--sprout-gray); }

.score-number {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 38px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.badges {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-bottom: 16px;
}

.badge {
  background: var(--tg-theme-secondary-bg-color);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--tg-theme-hint-color);
}
.badge b {
  color: var(--tg-theme-text-color);
  font-size: 14px;
}

.source-note {
  font-size: 12px;
  color: var(--tg-theme-hint-color);
  text-align: center;
  margin-bottom: 12px;
}

.description {
  font-size: 15px;
  line-height: 1.5;
  margin: 0;
}

.empty-state,
.loading-state {
  text-align: center;
  padding: 12px 8px 24px;
}

.empty-emoji {
  font-size: 40px;
  margin-bottom: 10px;
}

.hint {
  color: var(--tg-theme-hint-color);
  font-size: 14px;
}

.btn-primary {
  margin-top: 14px;
  background: var(--sprout-green);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 13px 22px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

/* ---------- "Росток" — лоадер вместо обычного спиннера ---------- */
.sprout-spinner {
  width: 48px;
  height: 48px;
  margin: 0 auto 12px;
  display: block;
}
.sprout-spinner .leaf {
  fill: var(--sprout-green);
  opacity: 0;
  transform-origin: 24px 40px;
  animation: sprout-grow 1.6s ease-in-out infinite;
}
.sprout-spinner .leaf-right {
  animation-delay: 0.25s;
}
.sprout-spinner .stem {
  stroke: var(--sprout-green);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-dasharray: 26;
  stroke-dashoffset: 26;
  animation: sprout-stem 1.6s ease-in-out infinite;
}
@keyframes sprout-grow {
  0% { opacity: 0; transform: scale(0.3); }
  35% { opacity: 1; transform: scale(1); }
  70% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.3); }
}
@keyframes sprout-stem {
  0% { stroke-dashoffset: 26; }
  50% { stroke-dashoffset: 0; }
  100% { stroke-dashoffset: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .scan-line,
  .sprout-spinner .leaf,
  .sprout-spinner .stem {
    animation: none !important;
  }
  .ring-fg {
    transition: none !important;
  }
}
