/*
 * ============================================================
 *  LAYOUT INFRASTRUCTURE v1.0
 *  Global app layout, containers, grid, stack, section,
 *  divider, responsive utilities.
 *
 *  DEPENDENCIES: design-tokens.css (must be loaded first)
 *  ============================================================
 */

/* ════════════════════════════════════════════════════════════
   APP LAYOUT — Root shell for every page
   ════════════════════════════════════════════════════════════ */
.app-layout {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  background: var(--color-bg, #F8F6F1);
}

.app-layout--admin {
  background: var(--color-bg, #F4F6FB);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: var(--z-nav, 1000);
}

.app-header--fixed {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

.app-body {
  display: flex;
  flex: 1;
  position: relative;
}

.app-body--with-sidebar {
  display: grid;
  grid-template-columns: var(--sidebar-w, 272px) 1fr;
  min-height: calc(100dvh - var(--nav-h, 72px));
}

.app-main {
  flex: 1;
  min-width: 0;
}

.app-main--sidebar {
  padding: var(--space-6, 24px);
  overflow-x: hidden;
}

.app-footer {
  margin-top: auto;
}

/* Admin layout overrides */
.app-layout--admin .app-body--with-sidebar {
  grid-template-columns: var(--sidebar-w, 272px) 1fr;
}

/* ════════════════════════════════════════════════════════════
   CONTAINER SYSTEM
   ════════════════════════════════════════════════════════════ */
.app-container {
  width: 100%;
  max-width: var(--container-max, 1280px);
  margin: 0 auto;
  padding: 0 var(--content-padding, clamp(1rem, 4vw, 2.5rem));
}

.app-container--narrow {
  max-width: var(--container-narrow, 1024px);
}

.app-container--wide {
  max-width: var(--container-wide, 1440px);
}

.app-container--small {
  max-width: 720px;
}

.app-container--fluid {
  max-width: none;
}

.app-container--reading {
  max-width: 680px;
}

/* ════════════════════════════════════════════════════════════
   PAGE WRAPPER
   ════════════════════════════════════════════════════════════ */
.page-wrapper {
  padding-top: var(--page-padding-top, 0);
}

.page-wrapper--with-hero {
  --page-padding-top: 0;
}

.page-wrapper--interior {
  --page-padding-top: var(--nav-h, 72px);
}

@media (max-width: 900px) {
  .page-wrapper--interior {
    --page-padding-top: 56px;
  }
}

.page-wrapper--with-sidebar {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-6, 24px);
}

@media (min-width: 1024px) {
  .page-wrapper--with-sidebar {
    grid-template-columns: 1fr 320px;
  }
}

/* ════════════════════════════════════════════════════════════
   CONTENT WRAPPER
   ════════════════════════════════════════════════════════════ */
.content-wrapper {
  display: flex;
  flex-direction: column;
  gap: var(--space-6, 24px);
}

.content-wrapper--sm {
  gap: var(--space-4, 16px);
}

.content-wrapper--lg {
  gap: var(--space-10, 40px);
}

.content-wrapper--xl {
  gap: var(--space-14, 56px);
}

/* ════════════════════════════════════════════════════════════
   GRID SYSTEM
   ════════════════════════════════════════════════════════════ */
.app-grid {
  display: grid;
  gap: var(--space-6, 24px);
}

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

.app-grid--3 {
  grid-template-columns: repeat(3, 1fr);
}

.app-grid--4 {
  grid-template-columns: repeat(4, 1fr);
}

.app-grid--auto {
  grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr));
}

.app-grid--auto-sm {
  grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr));
}

.app-grid--auto-lg {
  grid-template-columns: repeat(auto-fit, minmax(min(340px, 100%), 1fr));
}

.app-grid--sidebar {
  grid-template-columns: 2fr 1fr;
}

/* Grid gap variants */
.app-grid--gap-sm {
  gap: var(--space-4, 16px);
}

.app-grid--gap-lg {
  gap: var(--space-8, 32px);
}

.app-grid--gap-xl {
  gap: var(--space-12, 48px);
}

/* Responsive grid collapse */
@media (max-width: 900px) {
  .app-grid--sidebar {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .app-grid--2,
  .app-grid--3,
  .app-grid--4 {
    grid-template-columns: 1fr;
  }
}

/* ════════════════════════════════════════════════════════════
   STACK — Vertical and horizontal spacing primitive
   ════════════════════════════════════════════════════════════ */
.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-4, 16px);
}

.stack--xs {
  gap: var(--space-1, 4px);
}

.stack--sm {
  gap: var(--space-2, 8px);
}

.stack--md {
  gap: var(--space-4, 16px);
}

.stack--lg {
  gap: var(--space-6, 24px);
}

.stack--xl {
  gap: var(--space-8, 32px);
}

.stack--2xl {
  gap: var(--space-12, 48px);
}

.stack--3xl {
  gap: var(--space-16, 64px);
}

.stack--horizontal {
  flex-direction: row;
  align-items: center;
}

.stack--horizontal.stack--wrap {
  flex-wrap: wrap;
}

.stack--split {
  display: flex;
  flex-direction: column;
}

.stack--split > :last-child {
  margin-top: auto;
}

.stack--horizontal.stack--split > :last-child {
  margin-top: 0;
  margin-left: auto;
}

/* ════════════════════════════════════════════════════════════
   SECTION COMPONENT
   ════════════════════════════════════════════════════════════ */
.app-section {
  padding: var(--section-gap, clamp(4rem, 8vw, 8rem)) 0;
}

.app-section--sm {
  padding: var(--space-6, 24px) 0;
}

.app-section--md {
  padding: var(--space-12, 48px) 0;
}

.app-section--lg {
  padding: var(--space-20, 80px) 0;
}

.app-section--xl {
  padding: var(--space-28, 112px) 0;
}

.app-section--hero {
  padding: var(--space-20, 80px) 0;
}

/* Section backgrounds */
.app-section--dark {
  background: var(--color-primary, #0A2342);
  color: var(--color-white, #FFFFFF);
}

.app-section--warm {
  background: var(--color-bg-warm, #F5F2EB);
}

.app-section--accent {
  background: var(--color-accent, #C8A45C);
  color: var(--color-primary-dark, #091E23);
}

.app-section--muted {
  background: var(--color-bg-2, #EEF1F8);
}

/* Section header */
.app-section__header {
  text-align: center;
  max-width: 640px;
  margin: 0 auto var(--space-10, 40px);
}

.app-section__header--left {
  text-align: left;
  margin-left: 0;
}

.app-section__eyebrow {
  font-size: var(--text-xs, 11px);
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--color-accent, #C8A45C);
  margin-bottom: var(--space-2, 8px);
  display: block;
}

.app-section__title {
  font-family: var(--font-display, 'Playfair Display', serif);
  font-size: clamp(1.75rem, 4vw, 3rem);
  font-weight: 600;
  line-height: 1.08;
  margin-bottom: var(--space-4, 16px);
  text-wrap: balance;
}

.app-section__subtitle {
  font-size: var(--text-md, 18px);
  line-height: var(--lh-relaxed, 1.65);
  color: var(--color-text-secondary, #5B5B6B);
  max-width: 56ch;
  margin: 0 auto;
}

.app-section--dark .app-section__title {
  color: var(--color-white, #FFFFFF);
}

.app-section--dark .app-section__subtitle {
  color: rgba(255, 255, 255, 0.6);
}

.app-section__bar {
  width: 48px;
  height: 3px;
  background: var(--color-accent, #C8A45C);
  border-radius: 999px;
  margin: var(--space-3, 12px) auto var(--space-5, 20px);
}

.app-section__header--left .app-section__bar {
  margin-left: 0;
}

.app-section__body {
  display: flex;
  flex-direction: column;
  gap: var(--space-6, 24px);
}

/* ════════════════════════════════════════════════════════════
   DIVIDER
   ════════════════════════════════════════════════════════════ */
.app-divider {
  border: none;
  height: 1px;
  background: var(--color-border, rgba(0,0,0,0.08));
  margin: var(--space-6, 24px) 0;
}

.app-divider--sm {
  margin: var(--space-4, 16px) 0;
}

.app-divider--lg {
  margin: var(--space-10, 40px) 0;
}

.app-divider--labeled {
  display: flex;
  align-items: center;
  gap: var(--space-4, 16px);
  color: var(--color-text-muted, #8B8B9E);
  font-size: var(--text-xs, 11px);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border: none;
  height: auto;
  margin: var(--space-6, 24px) 0;
}

.app-divider--labeled::before,
.app-divider--labeled::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--color-border, rgba(0,0,0,0.08));
}

.app-divider--accent {
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--color-accent, #C8A45C), transparent);
}

/* ════════════════════════════════════════════════════════════
   RESPONSIVE UTILITY CLASSES
   ════════════════════════════════════════════════════════════ */
/* Show/hide by breakpoint */
.hide-mobile {
  display: none;
}

.show-mobile {
  display: block;
}

@media (max-width: 430px) {
  .hide-mobile--xs { display: none; }
  .show-mobile--xs { display: block; }
}

@media (max-width: 768px) {
  .hide-mobile--sm { display: none; }
  .show-mobile--sm { display: block; }
}

@media (min-width: 769px) {
  .hide-desktop { display: none; }
  .show-desktop { display: block; }
}

@media (min-width: 1025px) {
  .hide-desktop--lg { display: none; }
  .show-desktop--lg { display: block; }
}

/* Responsive visibility utilities */
@media (max-width: 430px) {
  .hide-mobile { display: none; }
  .show-mobile { display: block; }
}

@media (min-width: 431px) and (max-width: 768px) {
  .hide-tablet { display: none; }
  .show-tablet { display: block; }
}

@media (min-width: 431px) {
  .hide-desktop { display: none; }
}

/* Safe area utilities */
.safe-area-bottom {
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.safe-area-top {
  padding-top: env(safe-area-inset-top, 0px);
}

.safe-area-left {
  padding-left: env(safe-area-inset-left, 0px);
}

.safe-area-right {
  padding-right: env(safe-area-inset-right, 0px);
}

/* Aspect ratio utilities */
.aspect-square {
  aspect-ratio: 1;
}

.aspect-video {
  aspect-ratio: 16 / 9;
}

.aspect-portrait {
  aspect-ratio: 3 / 4;
}

.aspect-wide {
  aspect-ratio: 21 / 9;
}

/* Truncation */
.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.truncate--2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.truncate--3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Spacing helpers — margin */
.mt-auto { margin-top: auto; }
.mb-auto { margin-bottom: auto; }
.ms-auto { margin-inline-start: auto; }
.me-auto { margin-inline-end: auto; }
.mx-auto { margin-left: auto; margin-right: auto; }

/* Spacing helpers — padding */
.p-0 { padding: 0; }
.p-2 { padding: var(--space-2, 8px); }
.p-4 { padding: var(--space-4, 16px); }
.p-6 { padding: var(--space-6, 24px); }
.p-8 { padding: var(--space-8, 32px); }

.px-4 { padding-left: var(--space-4, 16px); padding-right: var(--space-4, 16px); }
.px-6 { padding-left: var(--space-6, 24px); padding-right: var(--space-6, 24px); }
.px-8 { padding-left: var(--space-8, 32px); padding-right: var(--space-8, 32px); }

.py-4 { padding-top: var(--space-4, 16px); padding-bottom: var(--space-4, 16px); }
.py-6 { padding-top: var(--space-6, 24px); padding-bottom: var(--space-6, 24px); }
.py-8 { padding-top: var(--space-8, 32px); padding-bottom: var(--space-8, 32px); }

/* Flex utility classes */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-row { flex-direction: row; }
.flex-wrap { flex-wrap: wrap; }
.flex-nowrap { flex-wrap: nowrap; }
.flex-1 { flex: 1; }
.flex-shrink-0 { flex-shrink: 0; }

.items-start { align-items: flex-start; }
.items-center { align-items: center; }
.items-end { align-items: flex-end; }
.items-stretch { align-items: stretch; }

.justify-start { justify-content: flex-start; }
.justify-center { justify-content: center; }
.justify-end { justify-content: flex-end; }
.justify-between { justify-content: space-between; }
.justify-around { justify-content: space-around; }

.gap-xs { gap: var(--space-1, 4px); }
.gap-sm { gap: var(--space-2, 8px); }
.gap-md { gap: var(--space-4, 16px); }
.gap-lg { gap: var(--space-6, 24px); }
.gap-xl { gap: var(--space-8, 32px); }

/* Overflow utilities */
.overflow-hidden { overflow: hidden; }
.overflow-auto { overflow: auto; }
.overflow-x-auto { overflow-x: auto; }
.overflow-y-auto { overflow-y: auto; }

/* Width utilities */
.w-full { width: 100%; }
.w-auto { width: auto; }

/* Height utilities */
.h-full { height: 100%; }

/* Position utilities */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.sticky { position: sticky; }

/* Cursor utilities */
.cursor-pointer { cursor: pointer; }
.cursor-not-allowed { cursor: not-allowed; }

/* User select */
.select-none { user-select: none; }

/* Pointer events */
.pointer-events-none { pointer-events: none; }
.pointer-events-auto { pointer-events: auto; }

/* Object fit */
.object-cover { object-fit: cover; }
.object-contain { object-fit: contain; }

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .app-section,
  .stack,
  .app-grid {
    transition: none;
  }
}
