/**
 * Master layout — three-zone shell, #app-content scrollport, content defaults.
 * Loaded after base-layout.css and app-shell.css.
 */

html.app-layout {
  height: 100%;
  height: -webkit-fill-available;
  overflow: hidden;
  scroll-padding-top: 0;
  background-color: var(--bg-main);
}

html.app-layout body.app-layout-body {
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  margin: 0;
  background-color: var(--bg-main);
}

.app-root {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-main);
}

.app-skip-link {
  position: fixed;
  top: -120px;
  left: max(12px, env(safe-area-inset-left, 0px));
  z-index: 300;
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--btn-primary-bg);
  color: var(--btn-primary-text);
  font-weight: 600;
  font-size: 0.9rem;
  text-decoration: none;
  box-shadow: var(--shadow-container);
}

.app-skip-link:focus {
  top: calc(env(safe-area-inset-top, 0px) + 8px);
  outline: 2px solid var(--focus-outline-color);
  outline-offset: 2px;
}

/* Master zones 1–3 + scrollport: one canvas (same as .app-header) */
.app-zone-header.app-header,
.app-zone-sidebar.app-sidebar,
.app-zone-main,
.app-main-canvas,
.app-zone-footer.footer {
  background-color: var(--bg-main);
}

.app-zone-main,
.app-main-canvas {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.app-scroll-top-sentinel {
  flex-shrink: 0;
  width: 100%;
  height: 1px;
  margin: 0;
  padding: 0;
  pointer-events: none;
  visibility: hidden;
}

.app-main-viewport {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  background-color: var(--bg-main);
  /* Top inset is .app-main-offset (header clearance) — no extra scrollport padding above content */
  padding: 0
    max(var(--app-content-padding, 24px), env(safe-area-inset-right, 0px))
    var(--app-content-padding, 24px)
    max(var(--app-content-padding, 24px), env(safe-area-inset-left, 0px));
  scroll-padding-top: calc(var(--app-header-height, 56px) + env(safe-area-inset-top, 0px) + 12px);
}

@media (max-width: 767px) {
  .app-main-viewport {
    scroll-padding-top: calc(var(--mobile-topbar-offset) + 12px);
  }

  body.has-verification-banner .app-main-viewport {
    scroll-padding-top: calc(
      var(--app-header-height, 56px) + env(safe-area-inset-top, 0px) + var(--site-banner-height, 50px) + 12px
    );
  }

  /*
   * iOS Safari: nested overflow on a flex child often won't accept touch pan.
   * Let the document scroll on phones; fixed header + main-offset padding unchanged.
   */
  html.app-layout {
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    height: auto;
    min-height: 100%;
    min-height: -webkit-fill-available;
  }

  html.app-layout body.app-layout-body {
    overflow-x: hidden;
    overflow-y: visible;
    height: auto;
    max-height: none;
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
  }

  html.app-layout .app-root {
    flex: 1 0 auto;
    min-height: 100vh;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
  }

  html.app-layout #app-content.app-main-viewport {
    overflow-x: hidden;
    overflow-y: visible;
    flex: none;
    min-height: 0;
    height: auto;
    -webkit-overflow-scrolling: auto;
    touch-action: pan-y;
  }
}

body.has-verification-banner .app-main-viewport {
  scroll-padding-top: calc(
    var(--app-header-height, 56px) + env(safe-area-inset-top, 0px) + var(--site-banner-height, 50px) + 12px
  );
}

#app-content > .container.app-page-surface {
  margin-left: auto;
  margin-right: auto;
}

#app-content > .container {
  padding-top: 0;
  background-color: var(--bg-main);
}

/*
 * Settings / inbox form pages (.settings-page-surface): do not flex-grow the
 * page wrapper (app-shell gives #app-content > .container { flex: 1 }), or the
 * footer sits inside a stretched empty band and looks merged with the card.
 */
#app-content > .container.settings-page-surface {
  flex: 0 0 auto;
}

#app-content:has(.settings-page-surface) {
  gap: 1.25rem;
}

#app-content:has(.settings-page-surface) > .footer.app-zone-footer {
  margin-top: auto;
}

/* Optional full-bleed pages: {% block content_viewport %}…{% endblock %} with class on main via extra wrapper */
.app-main-viewport--bleed {
  padding: 0 !important;
}

ul.django-messages {
  list-style: none;
  padding: 0;
  margin: 0 0 1rem;
}

/* Default form controls inside the content window */
#app-content
  input:not([type='checkbox']):not([type='radio']):not([type='submit']):not([type='button']):not([type='hidden']):not([type='range']):not([type='file']):not([type='image']):not([type='reset']),
#app-content textarea,
#app-content select {
  background-color: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 12px;
  color: var(--text-primary);
  padding: 10px 14px;
  font-size: 1rem;
  font-family: inherit;
  line-height: 1.35;
  box-sizing: border-box;
  transition:
    border-color 0.18s ease,
    box-shadow 0.18s ease;
}

#app-content
  input:not([type='checkbox']):not([type='radio']):not([type='submit']):not([type='button']):not([type='hidden']):not([type='range']):not([type='file']):not([type='image']):not([type='reset']):focus,
#app-content textarea:focus,
#app-content select:focus {
  outline: none;
  border-color: var(--input-focus-border);
  box-shadow: var(--input-focus-shadow);
}

#app-content textarea {
  min-height: 6.5rem;
  resize: vertical;
}

/* Zone 3 buttons — match page canvas (--bg-main) */
#app-content .settings-submit,
#app-content .settings-submit-inline,
#app-content .settings-back-btn,
#app-content .app-table-filter-btn,
#app-content .produce-catalog-filters-shell__toggle {
  background-color: var(--bg-main);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  box-shadow: none;
}

#app-content .settings-submit:hover,
#app-content .settings-submit-inline:hover,
#app-content .settings-back-btn:hover,
#app-content .app-table-filter-btn:hover,
#app-content .produce-catalog-filters-shell__toggle:hover {
  background-color: var(--btn-icon-hover);
  color: var(--text-primary);
  border-color: var(--border-color);
}

@media (pointer: coarse) {
  #app-content .settings-submit:active,
  #app-content .settings-submit-inline:active,
  #app-content .settings-back-btn:active,
  #app-content .app-table-filter-btn:active,
  #app-content .produce-catalog-filters-shell__toggle:active {
    transform: scale(0.98);
    opacity: 0.92;
  }
}

/* Flash / notices: consistent corner radius inside main */
#app-content .htmx-alert {
  border-radius: 16px;
}

/* Scrollable table shell (legacy wrap for wide grids) */
#app-content .app-table-scroll {
  margin: 0.75rem 0 1rem;
  overflow-x: auto;
  overflow-y: visible;
  max-height: none;
  -webkit-overflow-scrolling: touch;
  border-radius: 16px;
  border: 1px solid var(--border-color);
  background: var(--input-bg);
  box-shadow: var(--shadow-soft);
}

#app-content .app-table-scroll table {
  margin: 0;
}

/* —— Universal data table card (Zone 3): card + toolbar + scroll + sticky thead —— */
#app-content .app-data-table-card {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  overflow: visible;
  margin: 0.75rem 0 1.25rem;
  box-shadow: var(--shadow-soft);
}

#app-content .app-data-table-card__toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 14px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-color);
  background: var(--input-bg);
}

#app-content .app-data-table-card__title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1.35;
  flex: 1 1 auto;
  min-width: 0;
}

#app-content .app-data-table-card__toolbar-field {
  flex: 0 1 min(100%, 260px);
  min-width: 11rem;
}

#app-content .app-data-table-card__toolbar-field-label {
  display: block;
  margin: 0 0 6px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
}

#app-content .app-data-table-card__toolbar-field .app-field-picker__glide {
  z-index: 12;
}

.app-table-filter-dropdown {
  position: relative;
  flex-shrink: 0;
}

.app-table-filter-btn {
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  background: var(--bg-main);
  color: var(--text-primary);
  font-size: 0.88rem;
  font-weight: 600;
  font-family: inherit;
  margin: 0;
  touch-action: manipulation;
  transition:
    background 0.15s ease,
    border-color 0.15s ease,
    color 0.15s ease,
    opacity 0.08s ease,
    transform 0.08s ease;
}

.app-table-filter-btn:hover {
  background: var(--btn-icon-hover);
  color: var(--text-primary);
}

.app-table-filter-btn:focus-visible {
  outline: 2px solid var(--focus-outline-color);
  outline-offset: 2px;
}

.app-table-filter-btn__label {
  line-height: 1.2;
}

.app-table-filter-btn__chevron {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
  transition: transform var(--dropdown-glide-duration) var(--dropdown-glide-ease);
}

.app-table-filter-btn__chevron i {
  font-size: 0.62rem;
  opacity: 0.95;
}

.app-table-filter-dropdown.is-open .app-table-filter-btn__chevron {
  transform: rotate(180deg);
}

/* §3 smooth dropdown — grid-template-rows 0fr → 1fr + synced opacity */
.app-table-filter-dropdown__glide {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: min(240px, calc(100vw - 2rem));
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows var(--dropdown-glide-duration) var(--dropdown-glide-ease);
  z-index: 8;
  pointer-events: none;
  will-change: grid-template-rows, opacity;
}

.app-table-filter-dropdown.is-open .app-table-filter-dropdown__glide {
  grid-template-rows: 1fr;
  pointer-events: auto;
}

.app-table-filter-dropdown__glide-inner {
  min-height: 0;
  overflow: hidden;
  opacity: 0;
  transition: opacity var(--dropdown-glide-duration) var(--dropdown-glide-ease);
}

.app-table-filter-dropdown.is-open .app-table-filter-dropdown__glide-inner {
  opacity: 1;
}

.app-table-filter-dropdown__panel {
  min-width: 0;
  max-height: min(52vh, 320px);
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  background: var(--input-bg);
  box-shadow: var(--dropdown-shadow);
}

.app-table-filter-option {
  display: block;
  width: 100%;
  margin: 0 0 4px;
  padding: 9px 12px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-primary);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  transition:
    background 0.12s ease,
    opacity 0.08s ease,
    transform 0.08s ease;
}

@media (pointer: coarse) {
  .app-table-filter-option:active {
    transform: scale(0.99);
    opacity: 0.92;
  }
}

.app-table-filter-option:last-child {
  margin-bottom: 0;
}

.app-table-filter-option:hover {
  background: var(--btn-icon-hover);
}

.app-table-filter-option.is-active {
  background: var(--header-hover-bg);
  font-weight: 600;
}

/* Keep picker panel above the table body when open. */
#app-content .app-data-table-card:has(.app-field-picker.is-open),
#app-content .app-data-table-card:has(.app-table-filter-dropdown.is-open) {
  overflow: visible;
}

#app-content .app-data-table-card:has(.app-field-picker.is-open) .app-data-table-card__toolbar,
#app-content .app-data-table-card:has(.app-table-filter-dropdown.is-open) .app-data-table-card__toolbar {
  position: relative;
  z-index: 20;
  overflow: visible;
}

#app-content .app-data-table-card:has(.app-field-picker.is-open) .app-field-picker.is-open .app-field-picker__glide,
#app-content .app-data-table-card:has(.app-table-filter-dropdown.is-open) .app-table-filter-dropdown.is-open .app-table-filter-dropdown__glide {
  z-index: 31;
}

@media (prefers-reduced-motion: reduce) {
  .app-table-filter-dropdown__glide,
  .app-table-filter-dropdown__glide-inner,
  .app-table-filter-btn__chevron {
    transition-duration: 0.01s;
  }

  .app-table-filter-dropdown__glide {
    will-change: auto;
  }

  .app-table-filter-btn {
    transition:
      background 0.15s ease,
      border-color 0.15s ease,
      color 0.15s ease;
  }

  .app-table-filter-option {
    transition: background 0.12s ease;
  }

  .app-table-filter-btn:active,
  .app-table-filter-option:active {
    transform: none;
    opacity: 1;
  }
}

/*
 * Horizontal scroll for wide grids only — vertical scroll is #app-content (§5 single scrollport).
 * Sticky thead sticks while the page scrolls, not inside a capped inner box.
 */
#app-content .app-data-table-card__scroll {
  overflow-x: auto;
  overflow-y: visible;
  max-height: none;
  -webkit-overflow-scrolling: touch;
}

#app-content .app-data-table-card__scroll table.app-data-table {
  display: table;
  width: max-content;
  min-width: 100%;
}

#app-content .app-data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

#app-content .app-data-table thead th {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--table-header-bg);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 0.8rem;
  text-align: left;
  vertical-align: bottom;
  box-shadow: inset 0 -1px 0 var(--border-color);
}

#app-content .app-data-table tbody tr:nth-child(odd) {
  background: var(--table-row-bg);
}

#app-content .app-data-table tbody tr:nth-child(even) {
  background: var(--table-row-zebra);
}

#app-content .app-data-table tbody tr:hover {
  background: var(--table-row-hover);
}

#app-content .app-data-table th,
#app-content .app-data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-color);
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
  word-break: break-word;
}

#app-content .app-data-table-card .price-table {
  margin-top: 0;
}

#app-content .app-data-table tbody tr.hidden-filtered,
#app-content .produce-line-card.hidden-filtered {
  display: none !important;
}

/* .cursorrules §5 — eliminate scroll trapping + universal table-to-card (phones & tablets) */
@media (max-width: 1024px) {
  #app-content .app-data-table-card {
    overflow: visible;
    height: auto;
  }

  /* Filter panel must stack above the table body (later sibling paints on top by default). */
  #app-content .app-data-table-card__toolbar {
    position: relative;
    z-index: 20;
    overflow: visible;
  }

  #app-content .app-data-table-card:has(.app-field-picker.is-open) .app-data-table-card__scroll,
  #app-content .app-data-table-card:has(.app-table-filter-dropdown.is-open) .app-data-table-card__scroll {
    position: relative;
    z-index: 0;
  }

  #app-content .app-data-table-card__toolbar .app-data-table-card__toolbar-field {
    flex: 1 1 100%;
    min-width: 0;
  }

  #app-content .app-data-table-card__toolbar .app-field-picker.is-open .app-field-picker__glide {
    z-index: 31;
    grid-template-rows: auto;
  }

  #app-content .app-data-table-card__toolbar .app-table-filter-dropdown {
    flex: 1 1 100%;
    min-width: 0;
  }

  #app-content .app-data-table-card__toolbar .app-table-filter-btn {
    width: 100%;
    justify-content: space-between;
  }

  .app-table-filter-dropdown.is-open {
    z-index: 30;
  }

  .app-table-filter-dropdown.is-open .app-table-filter-dropdown__glide {
    left: 0;
    right: 0;
    width: 100%;
    max-width: none;
    z-index: 31;
    grid-template-rows: auto;
  }

  #app-content .app-data-table-card__scroll {
    overflow-x: auto;
    overflow-y: visible !important;
    max-height: none !important;
  }

  #app-content .app-table-scroll {
    overflow-x: auto;
    overflow-y: visible !important;
    max-height: none !important;
  }

  /* Table-to-card: thead hidden, rows as stacked micro-cards */
  #app-content table.app-data-table thead {
    display: none;
  }

  #app-content table.app-data-table tbody tr {
    display: block;
    width: 100%;
    margin-bottom: 16px;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border-color);
    box-sizing: border-box;
  }

  #app-content table.app-data-table tbody tr:nth-child(odd) {
    background: var(--table-row-bg);
  }

  #app-content table.app-data-table tbody tr:nth-child(even) {
    background: var(--table-row-zebra);
  }

  #app-content table.app-data-table tbody tr:hover {
    background: var(--table-row-hover);
  }

  #app-content table.app-data-table tbody td {
    display: block;
    width: 100%;
    box-sizing: border-box;
    border-bottom: 1px solid var(--border-color);
    text-align: left;
  }

  #app-content table.app-data-table tbody td:last-child {
    border-bottom: none;
  }

  #app-content table.app-data-table tbody td:first-child {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text-primary);
    padding-top: 12px;
  }

  #app-content table.app-data-table tbody td[data-label]:not(:first-child) {
    display: grid;
    grid-template-columns: minmax(0, 7.5rem) minmax(0, 1fr);
    gap: 6px 12px;
    align-items: baseline;
    padding-top: 10px;
    padding-bottom: 10px;
  }

  #app-content table.app-data-table tbody td[data-label]:not(:first-child)::before {
    content: attr(data-label);
    color: var(--text-secondary);
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    line-height: 1.3;
  }
}

/* Table pages: do not add a second horizontal scroll on .container */
@media (min-width: 1025px) {
  #app-content > .container:has(.app-data-table-card),
  #app-content > .container:has(.app-table-scroll) {
    overflow-x: visible;
  }
}

/* Slightly tighter grid padding on tablet / small laptop (desktop table still visible) */
@media (min-width: 768px) and (max-width: 1024px) {
  #app-content .app-data-table-card__toolbar {
    padding: 10px 12px;
    gap: 8px;
  }

  #app-content .app-data-table thead th {
    font-size: 0.72rem;
    padding: 8px 10px;
  }

  #app-content .app-data-table th,
  #app-content .app-data-table td {
    padding: 8px 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-main-viewport {
    scroll-behavior: auto;
  }
}

/* Global top-rail progress (FarmazonUI lifecycle) */
.fz-global-progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--fz-progress-height);
  z-index: var(--fz-progress-z);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.fz-global-progress-bar__track {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--fz-progress-track-bg);
}

.fz-global-progress-bar__fill {
  height: 100%;
  width: 40%;
  background: var(--accent-gradient);
  transform: translateX(-100%);
  will-change: transform;
}

html.fz-ui-loading .fz-global-progress-bar {
  opacity: 1;
}

html.fz-ui-loading .fz-global-progress-bar__fill {
  animation: fz-global-progress-indeterminate 1.1s var(--dropdown-glide-ease) infinite;
}

html.fz-ui-loading #app-root {
  pointer-events: none;
}

@keyframes fz-global-progress-indeterminate {
  0% {
    transform: translateX(-100%);
  }

  100% {
    transform: translateX(250%);
  }
}

@media (prefers-reduced-motion: reduce) {
  html.fz-ui-loading .fz-global-progress-bar__fill {
    animation: none;
    transform: translateX(0);
    width: 100%;
    opacity: 0.85;
  }
}
