/* =============================================
   Macro Kitchen — App Styles
   Mobile-first (390px base), Bootstrap 5 override layer
   ============================================= */

:root {
  --mk-green: #198754;
  --mk-green-light: #d1e7dd;
  --mk-bottom-nav-height: 60px;
  --mk-safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Alpine cloak — hide until Alpine initialises */
[x-cloak] { display: none !important; }

/* ---- Global ---- */
html {
  background-color: #f8f9fa;
}

body {
  font-size: 0.9375rem; /* 15px */
  -webkit-tap-highlight-color: transparent;
  background-color: #f8f9fa;
}

/* Push content above fixed bottom nav */
main {
  padding-bottom: calc(var(--mk-bottom-nav-height) + var(--mk-safe-bottom) + 1rem);
}

/* ---- Bottom Nav ---- */
.navbar.fixed-bottom {
  height: calc(var(--mk-bottom-nav-height) + var(--mk-safe-bottom));
  padding-bottom: var(--mk-safe-bottom);
  z-index: 1030;
}

.nav-tab {
  color: #6c757d;
  transition: color 0.15s ease;
}

.nav-tab:active,
.nav-tab:hover {
  color: var(--mk-green);
}

.nav-tab--active {
  color: var(--mk-green) !important;
}

.nav-tab__label {
  font-size: 0.65rem;
  line-height: 1;
  margin-top: 2px;
}

/* ---- Forms ---- */
.form-control-lg {
  min-height: 48px;
}

.btn-lg {
  min-height: 48px;
}

/* ---- Buttons — vertical-center content when min-height is set ---- */
.btn[style*="min-height"] {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ---- Cards ---- */
.card {
  border-radius: 0.75rem;
  border: 1px solid rgba(0, 0, 0, 0.06);
}

/* ---- Macro Pills ---- */
.macro-pill {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: 0.4rem 0.75rem;
  border-radius: 0.5rem;
  font-size: 0.75rem;
}

.macro-pill__value {
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.1;
}

/* ---- Page Load Progress Bar ---- */
#page-loader {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: var(--mk-green);
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
}

#page-loader.loading {
  opacity: 1;
  animation: page-load 3s ease-out forwards;
}

@keyframes page-load {
  0%   { width: 0; }
  3%   { width: 60%; }
  8%   { width: 80%; }
  20%  { width: 90%; }
  100% { width: 98%; }
}

/* ---- Search spinner: collapse when inactive so scan button stays flush ---- */
.input-group > .htmx-indicator.input-group-text {
  display: none;
}
.input-group > .htmx-indicator.input-group-text.htmx-request {
  display: flex;
}

/* ---- Barcode Scanner Overlay ---- */
.barcode-scanner-overlay {
  position: fixed;
  inset: 0;
  z-index: 1050;
  background: #000;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.barcode-scanner-header {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 14px);
  left: 16px;
  right: 72px;
  z-index: 1052;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.85);
}

.barcode-scanner-overlay .scanner-close-btn {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: 12px;
  z-index: 1053;
  min-height: 44px;
  min-width: 44px;
}

/* Flex child fills space under fixed header/close; min-height:0 avoids flex clip bugs */
.barcode-scanner-reader {
  flex: 1 1 0%;
  min-height: 0;
  width: 100%;
  overflow: hidden;
  background: #000;
}

#barcode-reader__scan_region {
  position: absolute !important;
  inset: 0 !important;
  width: 100% !important;
  height: 100% !important;
}

#barcode-reader__dashboard,
#barcode-reader__header_message {
  display: none !important;
}

#barcode-reader [id^="qr-shaded-region"] {
  display: none !important;
  background: transparent !important;
  border-width: 0 !important;
  border-color: transparent !important;
  box-shadow: none !important;
}

#barcode-reader [id*="qr-shaded-region"] {
  display: none !important;
  background: transparent !important;
  border-width: 0 !important;
  border-color: transparent !important;
  box-shadow: none !important;
}

/* Fill reader box; avoid position:absolute on video — Safari often composites camera incorrectly */
#barcode-reader video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#barcode-scanner-error {
  flex-shrink: 0;
  z-index: 1054;
  padding-bottom: max(12px, env(safe-area-inset-bottom, 0px)) !important;
  pointer-events: none;
}

.barcode-scan-guide {
  position: absolute;
  inset: 0;
  z-index: 1051;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.barcode-scan-guide__frame {
  position: relative;
  width: min(94vw, 520px);
  height: min(42vh, 320px);
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 12px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
  background: transparent;
}

.barcode-scan-guide__frame::before {
  content: "Align barcode this way";
  position: absolute;
  left: 50%;
  bottom: -30px;
  transform: translateX(-50%);
  color: rgba(255, 255, 255, 0.92);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-shadow: 0 1px 5px rgba(0, 0, 0, 0.9);
  white-space: nowrap;
}

/* ---- Offline Banner ---- */
#offline-banner {
  position: sticky;
  top: 0;
  z-index: 1040;
}

/* ---- AI Chat FAB ---- */
.chat-fab {
  position: fixed;
  bottom: calc(var(--mk-bottom-nav-height) + var(--mk-safe-bottom) + 16px);
  right: 16px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, #198754 0%, #0d6efd 100%);
  color: #fff;
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1031;
  box-shadow: 0 4px 16px rgba(25, 135, 84, 0.35);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  animation: chat-fab-entrance 0.4s ease-out;
}

.chat-fab:active {
  transform: scale(0.93);
  box-shadow: 0 2px 8px rgba(25, 135, 84, 0.25);
}

/* Locked variant — shown to free-tier users; opens upgrade modal */
.chat-fab--locked {
  background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
  box-shadow: 0 4px 16px rgba(108, 117, 125, 0.35);
  animation: none;
}

.chat-fab--locked::after {
  display: none;
}

.chat-fab__badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #ffc107;
  color: #000;
  font-size: 0.55rem;
  font-weight: 700;
  padding: 2px 5px;
  border-radius: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.chat-fab::after {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  background: linear-gradient(135deg, #198754 0%, #0d6efd 100%);
  opacity: 0;
  z-index: -1;
  animation: chat-fab-pulse 3s ease-in-out infinite;
}

@keyframes chat-fab-entrance {
  from { transform: scale(0) translateY(20px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}

@keyframes chat-fab-pulse {
  0%, 100% { opacity: 0; transform: scale(1); }
  50%      { opacity: 0.15; transform: scale(1.25); }
}

/* ---- Chat Overlay ---- */
.chat-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  z-index: 1045;
  background: #fff;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

.chat-overlay.chat-open {
  transform: translateY(0);
}

#chat-panel-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.chat-overlay__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(env(safe-area-inset-top, 0px) + 12px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
  background: linear-gradient(135deg, #198754 0%, #0d6efd 100%);
  color: #fff;
  min-height: 56px;
}

.chat-overlay__title {
  font-weight: 700;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-overlay__header-btn {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  cursor: pointer;
  min-height: 44px;
  min-width: 44px;
}

.chat-overlay__header-btn:active {
  background: rgba(255, 255, 255, 0.25);
}

.chat-overlay__messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
  background: #f8f9fa;
}

.chat-overlay__input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  background: #fff;
}

.chat-overlay__input {
  flex: 1;
  border: 1px solid #dee2e6;
  border-radius: 24px;
  padding: 10px 16px;
  font-size: 0.9375rem;
  outline: none;
  min-height: 44px;
  resize: none;
  font-family: inherit;
  line-height: 1.4;
  max-height: 120px;
  overflow-y: auto;
}

.chat-overlay__input:focus {
  border-color: var(--mk-green);
  box-shadow: 0 0 0 2px rgba(25, 135, 84, 0.15);
}

.chat-overlay__send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, #198754 0%, #0d6efd 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
  cursor: pointer;
  transition: opacity 0.15s;
}

.chat-overlay__send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ---- Chat Messages ---- */
.chat-msg {
  display: flex;
  margin-bottom: 12px;
  gap: 8px;
  max-width: 85%;
}

.chat-msg--user {
  margin-left: auto;
  flex-direction: row-reverse;
}

.chat-msg--assistant {
  margin-right: auto;
}

.chat-msg__avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #198754 0%, #0d6efd 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 0.7rem;
  flex-shrink: 0;
  align-self: flex-end;
}

.chat-msg__bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 0.9375rem;
  line-height: 1.45;
  word-break: break-word;
}

.chat-msg--user .chat-msg__bubble {
  background: var(--mk-green);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-msg--assistant .chat-msg__bubble {
  background: #fff;
  color: #212529;
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-bottom-left-radius: 4px;
}

/* ---- Chat formatted content ---- */
.chat-msg__bubble p { margin: 0; }
.chat-msg__bubble p + p { margin-top: 0.5em; }
.chat-msg__bubble strong { font-weight: 600; }
.chat-msg__bubble .chat-list {
  margin: 0.4em 0;
  padding-left: 1.2em;
  list-style: none;
}
.chat-msg__bubble ol.chat-list { padding-left: 0; }
.chat-msg__bubble .chat-list li {
  margin-bottom: 0.25em;
  line-height: 1.4;
}

/* ---- Chat Typing Indicator ---- */
.chat-typing {
  display: flex;
  gap: 8px;
  max-width: 85%;
  margin-bottom: 12px;
}

.chat-typing__dots {
  display: flex;
  gap: 4px;
  padding: 12px 14px;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 18px;
  border-bottom-left-radius: 4px;
}

.chat-typing__dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #adb5bd;
  animation: chat-typing-bounce 1.4s infinite ease-in-out;
}

.chat-typing__dots span:nth-child(2) { animation-delay: 0.16s; }
.chat-typing__dots span:nth-child(3) { animation-delay: 0.32s; }

@keyframes chat-typing-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40%           { transform: scale(1); opacity: 1; }
}

/* ---- Chat Recipe Suggestion Cards ---- */
.chat-suggestion-cards {
  margin-left: 36px;       /* align with assistant bubble (avatar 28px + gap 8px) */
  margin-top: -4px;
  margin-bottom: 12px;
  max-width: calc(85% - 36px);
}

.chat-carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  gap: 0;
  scrollbar-width: none;        /* Firefox */
}
.chat-carousel::-webkit-scrollbar { display: none; }

.chat-carousel__nav {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  padding-top: 8px;
}

.chat-carousel__arrow {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid #dee2e6;
  background: #fff;
  color: #495057;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.chat-carousel__arrow:active {
  background: #e9ecef;
}

.chat-carousel__arrow:disabled {
  opacity: 0.3;
  cursor: default;
}

.chat-carousel__counter {
  font-size: 0.75rem;
  font-weight: 600;
  color: #6c757d;
  min-width: 32px;
  text-align: center;
}

.chat-suggestion-context {
  font-size: 0.7rem;
  color: #6c757d;
  padding: 4px 0 8px 0;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}

.chat-suggestion-context__pill {
  background: #e8f5e9;
  color: #2e7d32;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.chat-recipe-card {
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  padding: 10px 12px;
  flex: 0 0 100%;
  scroll-snap-align: start;
  min-width: 0;
}

.chat-recipe-card__thumb img,
.chat-recipe-card__placeholder {
  width: 40px;
  height: 40px;
  object-fit: cover;
}

.chat-recipe-card__placeholder {
  background: #f0f0f0;
  color: #adb5bd;
  font-size: 1rem;
}

.chat-recipe-card__macros {
  font-size: 0.8rem;
  line-height: 1.3;
}

.chat-recipe-card__macros .text-muted {
  font-size: 0.7rem;
}

.chat-recipe-card__macro-detail {
  font-size: 0.7rem;
  color: #6c757d;
}

.chat-recipe-card__btn {
  min-height: 36px;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- Chat Suggestions (empty state) ---- */
.chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 16px;
  height: 100%;
}

.chat-welcome__icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: linear-gradient(135deg, #198754 0%, #0d6efd 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 1.75rem;
  margin-bottom: 16px;
}

.chat-welcome__title {
  font-weight: 700;
  font-size: 1.25rem;
  margin-bottom: 4px;
  color: #212529;
}

.chat-welcome__subtitle {
  font-size: 0.875rem;
  color: #6c757d;
  margin-bottom: 24px;
}

.chat-suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 280px;
}

.chat-suggestion-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid rgba(25, 135, 84, 0.2);
  background: #fff;
  color: #212529;
  font-size: 0.85rem;
  font-weight: 500;
  min-height: 44px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
  text-align: left;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

.chat-suggestion-btn i {
  color: var(--mk-green);
  font-size: 1rem;
  width: 20px;
  text-align: center;
  flex-shrink: 0;
}

.chat-suggestion-btn:active {
  background: var(--mk-green-light);
  border-color: var(--mk-green);
  box-shadow: none;
}

/* ---- Meal Builder Widget ---- */
.chat-meal-builder {
  margin: 8px 12px 8px 44px;
  padding: 16px;
  background: #fff;
  border-radius: 16px;
  border: 1px solid #e9ecef;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.chat-builder__label {
  font-size: 0.9rem;
  font-weight: 600;
  color: #212529;
  margin-bottom: 12px;
}

.chat-builder__label .text-muted {
  font-weight: 400;
  font-size: 0.8rem;
}

.chat-builder__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chat-builder__chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 20px;
  border: 1px solid #dee2e6;
  background: #fff;
  color: #495057;
  font-size: 0.85rem;
  min-height: 44px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.chat-builder__chip:active {
  background: var(--mk-green-light);
  border-color: var(--mk-green);
  color: var(--mk-green);
}

.chat-builder__chip--selected {
  background: var(--mk-green);
  border-color: var(--mk-green);
  color: #fff;
}

.chat-builder__chip--selected:active {
  background: #146c43;
  border-color: #146c43;
  color: #fff;
}

.chat-builder__chip--skip {
  border-style: dashed;
  color: #6c757d;
}

.chat-builder__input {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #dee2e6;
  border-radius: 10px;
  font-size: 0.85rem;
  margin-bottom: 8px;
  min-height: 44px;
  outline: none;
  transition: border-color 0.15s;
}

.chat-builder__input:focus {
  border-color: var(--mk-green);
  box-shadow: 0 0 0 2px rgba(25, 135, 84, 0.15);
}

.chat-builder__nav {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 8px;
}

.chat-builder__nav .btn {
  min-height: 44px;
  min-width: 80px;
}

.chat-builder__steps {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-top: 16px;
}

.chat-builder__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #dee2e6;
  transition: background 0.2s;
}

.chat-builder__dot--active {
  background: var(--mk-green);
}

/* ---- Admin chat history ---- */
.rotate-90 { transform: rotate(90deg); }
