/* MAYA9 Marketplace (C2C) — photo-first browse redesign.
 * Ported from the approved pilot (design/pilot-market-ui, 2026-10-26):
 * two-column browse (left sidebar + right card grid), cards = 1:1 photo with a
 * corner status pill, then bold price first, title one line, muted meta line.
 * Tokens-only (design/tokens.css via m9-ui.css): no hex colors, no new radii,
 * shadows, spacing or font values. Standard breakpoints 560 / 820 / 1120 / 1380.
 * Touch targets >= 44px, text >= 12px, no horizontal overflow at 320px. */

/* -------------------------------------------------------------------------- *
 * View shell — replace the 225px product-view rail with the market layout
 * -------------------------------------------------------------------------- */
#view-marketplace.is-active {
  display: block;
  animation: none;
}

body:has(#view-marketplace.is-active) main {
  width: 100%;
  max-width: 100%;
}

#view-marketplace .market-page-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
}

#view-marketplace .market-layout-container {
  width: 100%;
  max-width: var(--m9-content-max, 1720px);
  margin: var(--m9-space-0) auto;
  box-sizing: border-box;
  padding-inline: var(--m9-page-gutter, clamp(18px, 2.2vw, 36px));
  display: flex;
  align-items: flex-start;
  position: relative;
  min-width: 0;
}

/* -------------------------------------------------------------------------- *
 * Left sidebar (~340px, sticky) — Marketplace title, search pill, menu,
 * create CTA, categories and quick links
 * -------------------------------------------------------------------------- */
#view-marketplace .market-sidebar {
  width: 340px;
  min-width: 340px;
  max-width: 340px;
  background: var(--m9-surface);
  border-right: 1px solid var(--m9-border-soft);
  padding: var(--m9-space-4) var(--m9-space-4) var(--m9-space-8);
  position: sticky;
  top: var(--m9-topbar-height);
  max-height: calc(100vh - var(--m9-topbar-height));
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--m9-space-3);
  scrollbar-width: thin;
  z-index: 20;
}

#view-marketplace .market-sidebar__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--m9-space-2);
}

#view-marketplace .market-sidebar__title {
  font: var(--m9-type-page-title);
  color: var(--m9-brand-navy);
  margin: var(--m9-space-0);
  letter-spacing: -0.02em;
}

#view-marketplace .market-sidebar__subtitle {
  margin: calc(var(--m9-space-1) * -1) var(--m9-space-0) var(--m9-space-0);
  font: var(--m9-type-ui);
  color: var(--m9-text-secondary);
}

/* Pill search box */
#view-marketplace .market-search-pill {
  display: flex;
  align-items: center;
  background: var(--m9-surface-secondary);
  border-radius: var(--m9-radius-pill);
  padding: var(--m9-space-1-5) var(--m9-space-3-5);
  gap: var(--m9-space-2);
  min-height: 44px;
  border: 1px solid var(--m9-border-soft);
  transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}

#view-marketplace .market-search-pill:focus-within {
  border-color: var(--m9-primary);
  background: var(--m9-surface);
  box-shadow: 0 0 0 var(--m9-focus-width) var(--m9-focus-color);
}

#view-marketplace .market-search-pill svg {
  color: var(--m9-text-muted);
  flex-shrink: 0;
}

#view-marketplace .market-search-pill__submit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  padding: var(--m9-space-0);
  border: none;
  border-radius: var(--m9-radius-pill);
  background: transparent;
  cursor: pointer;
  color: var(--m9-text-muted);
}

#view-marketplace .market-search-pill__input {
  border: none;
  background: transparent;
  outline: none;
  width: 100%;
  min-width: 0;
  font: var(--m9-type-ui);
  color: var(--m9-text);
}

#view-marketplace .market-search-pill__input::placeholder {
  color: var(--m9-text-muted);
}

#view-marketplace .market-visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: var(--m9-space-0);
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: var(--m9-space-0);
}

/* Sidebar menu list (pill style) */
#view-marketplace .market-menu-list,
#view-marketplace .market-favorites-list {
  display: flex;
  flex-direction: column;
  gap: var(--m9-space-1);
}

#view-marketplace .market-menu-item {
  display: flex;
  align-items: center;
  gap: var(--m9-space-3);
  padding: var(--m9-space-2) var(--m9-space-3);
  border-radius: var(--m9-radius-pill);
  border: none;
  background: transparent;
  font: var(--m9-type-ui);
  font-weight: 600;
  color: var(--m9-text);
  text-decoration: none;
  cursor: pointer;
  width: 100%;
  text-align: left;
  min-height: 44px;
  transition: background 0.15s ease, color 0.15s ease;
}

#view-marketplace .market-menu-item:hover {
  background: var(--m9-surface-secondary);
}

#view-marketplace .market-menu-item.is-active {
  background: var(--m9-surface-accent);
  color: var(--m9-primary);
  font-weight: 700;
}

#view-marketplace .market-menu-item__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: var(--m9-radius-pill);
  background: var(--m9-surface-secondary);
  color: inherit;
  flex-shrink: 0;
}

#view-marketplace .market-menu-item.is-active .market-menu-item__icon {
  background: var(--m9-primary);
  color: var(--m9-on-primary);
}

#view-marketplace .market-menu-item__label {
  flex: 1;
  min-width: 0;
}

/* Primary CTA: + สร้างรายการขายใหม่ */
#view-marketplace .market-sidebar__action {
  margin: var(--m9-space-1) var(--m9-space-0);
}

#view-marketplace .market-btn-create-pill {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--m9-space-2);
  width: 100%;
  border-radius: var(--m9-radius-pill);
  min-height: 44px;
  font: var(--m9-type-card-title);
  text-decoration: none;
  box-shadow: var(--m9-shadow-cta);
  white-space: normal;
}

#view-marketplace .market-sidebar__divider {
  border: none;
  border-top: 1px solid var(--m9-border-soft);
  margin: var(--m9-space-1) var(--m9-space-0);
}

#view-marketplace .market-sidebar__section {
  display: flex;
  flex-direction: column;
  gap: var(--m9-space-2);
}

#view-marketplace .market-sidebar__section-title {
  font: var(--m9-type-label);
  color: var(--m9-text-muted);
  padding: var(--m9-space-0) var(--m9-space-2);
  letter-spacing: 0.02em;
}

/* Category filter list */
#view-marketplace .market-category-list {
  display: flex;
  flex-direction: column;
  gap: var(--m9-space-1);
}

#view-marketplace .market-cat-item {
  display: flex;
  align-items: center;
  gap: var(--m9-space-2-5);
  padding: var(--m9-space-2) var(--m9-space-3);
  border-radius: var(--m9-radius-pill);
  border: none;
  background: transparent;
  font: var(--m9-type-ui);
  color: var(--m9-text);
  cursor: pointer;
  text-align: left;
  min-height: 44px;
  width: 100%;
  transition: background 0.15s ease, color 0.15s ease;
}

#view-marketplace .market-cat-item:hover {
  background: var(--m9-surface-secondary);
  color: var(--m9-brand-navy);
}

#view-marketplace .market-cat-item.is-active {
  background: var(--m9-surface-accent);
  color: var(--m9-primary);
  font-weight: 700;
}

#view-marketplace .market-cat-item__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: var(--m9-radius-pill);
  background: var(--m9-surface-secondary);
  color: inherit;
  flex-shrink: 0;
}

#view-marketplace .market-cat-item.is-active .market-cat-item__icon {
  background: var(--m9-primary);
  color: var(--m9-on-primary);
}

/* -------------------------------------------------------------------------- *
 * Right main column — mobile chips, section header, card grid
 * -------------------------------------------------------------------------- */
#view-marketplace .market-main {
  flex: 1;
  min-width: var(--m9-space-0);
  padding: var(--m9-space-4) var(--m9-space-6) var(--m9-space-12);
  display: flex;
  flex-direction: column;
  gap: var(--m9-space-4);
}

#view-marketplace .market-main__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--m9-space-4);
  flex-wrap: wrap;
}

#view-marketplace .market-main__title-wrap {
  display: flex;
  align-items: baseline;
  gap: var(--m9-space-3);
  flex-wrap: wrap;
  min-width: 0;
}

#view-marketplace .market-main__heading {
  font: var(--m9-type-section-title);
  color: var(--m9-brand-navy);
  margin: var(--m9-space-0);
  letter-spacing: -0.015em;
}

#view-marketplace .market-main__count {
  font: var(--m9-type-caption);
  color: var(--m9-text-muted);
}

/* Mobile horizontal chips rows (menu + categories), shown <= 820px */
#view-marketplace .market-mobile-nav,
#view-marketplace .market-mobile-chips {
  display: none;
  overflow-x: auto;
  gap: var(--m9-space-2);
  padding: var(--m9-space-1) var(--m9-space-0);
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

#view-marketplace .market-mobile-nav::-webkit-scrollbar,
#view-marketplace .market-mobile-chips::-webkit-scrollbar {
  display: none;
}

#view-marketplace .m9-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--m9-space-2) var(--m9-space-3-5);
  border-radius: var(--m9-radius-pill);
  border: 1px solid var(--m9-border-soft);
  background: var(--m9-surface);
  color: var(--m9-text);
  font: var(--m9-type-ui);
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  min-height: 44px;
  text-decoration: none;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

#view-marketplace .m9-chip:hover {
  background: var(--m9-surface-secondary);
}

#view-marketplace .m9-chip.is-active {
  background: var(--m9-primary);
  border-color: var(--m9-primary);
  color: var(--m9-on-primary);
}

#view-marketplace .m9-chip--action {
  background: var(--m9-surface-accent);
  color: var(--m9-primary);
  border-color: var(--m9-primary);
}

/* -------------------------------------------------------------------------- *
 * Card grid — 5 cols > 1380 | 4 cols <= 1380 | 3 cols <= 1120 | 2 cols <= 820
 * (2 cols kept at <= 560 for the photo-first compact mobile grid)
 * -------------------------------------------------------------------------- */
#view-marketplace .listing-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: var(--m9-space-3-5);
}

@media (max-width: 1380px) {
  #view-marketplace .listing-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 1120px) {
  #view-marketplace .listing-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* -------------------------------------------------------------------------- *
 * Listing card — 1:1 photo (radius-sm) + corner status pill, then bold price,
 * one-line title, muted meta/location line. No heavy borders.
 * -------------------------------------------------------------------------- */
#view-marketplace .listing-card {
  background: var(--m9-surface);
  border: none;
  border-radius: var(--m9-radius-sm);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: inherit;
  box-shadow: none;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  cursor: pointer;
  min-width: 0;
}

#view-marketplace .listing-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--m9-shadow-surface);
}

#view-marketplace .listing-card:focus-within {
  outline: var(--m9-focus-width) solid var(--m9-focus-color);
  outline-offset: var(--m9-focus-offset);
}

#view-marketplace .listing-card__link {
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: inherit;
  height: 100%;
  width: 100%;
  min-width: 0;
  padding: var(--m9-space-0);
  border: none;
  background: transparent;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

#view-marketplace .listing-card__media {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--m9-surface-secondary);
  border-radius: var(--m9-radius-sm);
  overflow: hidden;
}

#view-marketplace .listing-card__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.25s ease;
}

#view-marketplace .listing-card:hover .listing-card__img {
  transform: scale(1.02);
}

#view-marketplace .listing-card__img-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--m9-space-1);
  background: var(--m9-gradient-selected);
  color: var(--m9-text-muted);
  font: var(--m9-type-caption);
}

/* Corner status pill over the photo */
#view-marketplace .listing-card__badge-pill {
  position: absolute;
  top: var(--m9-space-2);
  left: var(--m9-space-2);
  background: color-mix(in srgb, var(--m9-surface) 92%, transparent);
  border-radius: var(--m9-radius-pill);
  padding: var(--m9-space-0-5) var(--m9-space-2);
  font: var(--m9-type-caption);
  font-weight: 700;
  color: var(--m9-brand-navy);
  box-shadow: var(--m9-shadow-surface);
  pointer-events: none;
  display: inline-flex;
  align-items: center;
  gap: var(--m9-space-1);
  line-height: 1.3;
  max-width: calc(100% - var(--m9-space-4));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#view-marketplace .listing-card__content {
  padding: var(--m9-space-2) var(--m9-space-0) var(--m9-space-1);
  display: flex;
  flex-direction: column;
  gap: var(--m9-space-0-5);
  flex: 1;
  min-width: 0;
}

/* Line 1: price — the most prominent text on the card */
#view-marketplace .listing-card__price {
  font: var(--m9-type-section-title);
  color: var(--m9-brand-navy);
  letter-spacing: -0.01em;
}

/* Line 2: title, one line with ellipsis */
#view-marketplace .listing-card__title {
  font: var(--m9-type-ui);
  color: var(--m9-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin: var(--m9-space-0);
}

/* Line 3: location / meta, small and muted */
#view-marketplace .listing-card__location,
#view-marketplace .listing-card__meta {
  font: var(--m9-type-caption);
  color: var(--m9-text-muted);
  display: flex;
  align-items: center;
  gap: var(--m9-space-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

#view-marketplace .listing-card__meta svg {
  flex-shrink: 0;
}

#view-marketplace .listing-card__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--m9-space-1-5);
  padding: var(--m9-space-2) var(--m9-space-0) var(--m9-space-2-5);
  border-top: 1px solid var(--m9-border-soft);
}

#view-marketplace .listing-card__actions .m9-button {
  min-height: 44px;
  white-space: normal;
}

/* -------------------------------------------------------------------------- *
 * Loading skeleton grid (1:1 shimmer)
 * -------------------------------------------------------------------------- */
#view-marketplace .skeleton-card {
  background: var(--m9-surface);
  border-radius: var(--m9-radius-sm);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#view-marketplace .skeleton-box {
  background: var(--m9-disabled-bg);
  border-radius: var(--m9-radius-sm);
  position: relative;
  overflow: hidden;
}

#view-marketplace .skeleton-shimmer::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent 0%,
    color-mix(in srgb, var(--m9-surface) 60%, transparent) 50%,
    transparent 100%
  );
  animation: m9-mkt-shimmer 1.5s infinite;
}

@keyframes m9-mkt-shimmer {
  100% {
    transform: translateX(100%);
  }
}

#view-marketplace .skeleton-card__media {
  aspect-ratio: 1 / 1;
  width: 100%;
}

#view-marketplace .skeleton-card__body {
  padding: var(--m9-space-2) var(--m9-space-0);
  display: flex;
  flex-direction: column;
  gap: var(--m9-space-1-5);
}

#view-marketplace .skeleton-card__line-price {
  height: var(--m9-space-6);
  width: 45%;
}

#view-marketplace .skeleton-card__line-title {
  height: var(--m9-space-4);
  width: 85%;
}

#view-marketplace .skeleton-card__line-sub {
  height: var(--m9-space-3);
  width: 60%;
}

/* -------------------------------------------------------------------------- *
 * Honest empty / error / status panels (real API codes stay visible)
 * -------------------------------------------------------------------------- */
#view-marketplace .m9-empty-state {
  min-height: 240px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--m9-space-8) var(--m9-space-4);
  background: var(--m9-surface);
  border: var(--m9-border-default);
  border-radius: var(--m9-radius-xl);
  box-shadow: var(--m9-shadow-surface);
  gap: var(--m9-space-3);
}

#view-marketplace .m9-empty-state[role="alert"] {
  background: var(--m9-danger-surface);
  border-color: var(--m9-danger-strong);
}

#view-marketplace .m9-empty-state > svg {
  width: var(--m9-space-10);
  height: var(--m9-space-10);
  padding: var(--m9-space-3);
  border-radius: var(--m9-radius-pill);
  background: var(--m9-surface-secondary);
  color: var(--m9-text-muted);
  box-sizing: content-box;
}

#view-marketplace .m9-empty-state[role="alert"] > svg {
  color: var(--m9-danger);
  background: var(--m9-danger-bg);
}

#view-marketplace .m9-empty-state h3 {
  margin: var(--m9-space-0);
  font: var(--m9-type-section-title);
  color: var(--m9-brand-navy);
}

#view-marketplace .m9-empty-state p {
  margin: var(--m9-space-0);
  font: var(--m9-type-body);
  color: var(--m9-text-secondary);
  max-width: 460px;
}

#view-marketplace .m9-market-note {
  display: flex;
  align-items: center;
  gap: var(--m9-space-2);
  flex-wrap: wrap;
  padding: var(--m9-space-3) var(--m9-space-4);
  background: var(--m9-surface);
  border: var(--m9-border-default);
  border-radius: var(--m9-radius-lg);
  font: var(--m9-type-ui);
  color: var(--m9-text-secondary);
}

#view-marketplace .m9-market-note > svg {
  color: var(--m9-info);
  flex-shrink: 0;
}

#view-marketplace .m9-market-note > span {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

#view-marketplace .m9-market-note .m9-button {
  min-height: 44px;
}

/* Panels for the sell / mine / orders / saved tabs */
#view-marketplace .m9-surface {
  padding: var(--m9-space-4);
  border-radius: var(--m9-radius-xl);
}

#view-marketplace .checkout-panel {
  max-width: 720px;
}

#view-marketplace .m9-helper {
  font: var(--m9-type-caption);
  color: var(--m9-text-muted);
}

#view-marketplace .m9-inline-error {
  font: var(--m9-type-caption);
  color: var(--m9-danger);
  overflow-wrap: anywhere;
}

#view-marketplace .form-grid .m9-button,
#view-marketplace .order-actions .m9-button,
#view-marketplace .order-tabs button {
  min-height: 44px;
}

/* Listing detail modal content (photo-first) */
#view-marketplace .m9-market-detail {
  display: grid;
  gap: var(--m9-space-4);
}

#view-marketplace .m9-market-detail__media {
  max-width: 420px;
  width: 100%;
}

#view-marketplace .m9-market-detail__media .listing-card__media {
  border-radius: var(--m9-radius-lg);
}

#view-marketplace .m9-market-detail__price {
  font: var(--m9-type-kpi);
  color: var(--m9-brand-navy);
}

#view-marketplace .m9-market-detail__copy h2 {
  margin: var(--m9-space-1) var(--m9-space-0);
  font: var(--m9-type-page-title);
  color: var(--m9-brand-navy);
}

#view-marketplace .m9-market-detail__meta {
  margin: var(--m9-space-0) var(--m9-space-0) var(--m9-space-3);
  font: var(--m9-type-ui);
  color: var(--m9-text-muted);
}

/* -------------------------------------------------------------------------- *
 * Responsive: <= 820px sidebar becomes horizontal chips rows + 2-col grid
 * -------------------------------------------------------------------------- */
@media (max-width: 820px) {
  #view-marketplace .market-sidebar {
    display: none;
  }

  #view-marketplace .market-mobile-nav,
  #view-marketplace .market-mobile-chips {
    display: flex;
  }

  #view-marketplace .market-main {
    padding: var(--m9-space-3) var(--m9-space-3) calc(var(--m9-space-8) + var(--m9-topbar-height));
    gap: var(--m9-space-3);
  }

  #view-marketplace .listing-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--m9-space-3);
  }

  #view-marketplace .listing-card {
    min-width: 0;
    max-width: 100%;
  }

  #view-marketplace .m9-market-detail__media {
    max-width: 100%;
  }
}

@media (max-width: 560px) {
  #view-marketplace .listing-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--m9-space-2);
  }
  #view-marketplace .market-main {
    padding: var(--m9-space-2) var(--m9-space-2-5) calc(var(--m9-space-8) + var(--m9-topbar-height));
  }

  #view-marketplace .listing-grid {
    gap: var(--m9-space-2-5);
  }

  #view-marketplace .market-main__header {
    gap: var(--m9-space-2);
  }
}
