/* Layout Utilities */

/* === APP SHELL === */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.app-sidebar {
  width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-neutral-300);
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: var(--z-sticky);
  overflow-y: auto;
  transition: transform var(--transition-base);
}

.app-main {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-x: hidden;
}

.app-topbar {
  height: var(--navbar-height);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(229, 231, 235, 0.8);
  display: flex;
  align-items: center;
  padding: 0 var(--space-6);
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
}

.app-content {
  flex: 1;
  padding: var(--space-6);
  max-width: var(--content-max-width);
  width: 100%;
  margin: 0 auto;
  overflow-x: hidden;
  overflow-y: visible;
}

.app-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--bottom-nav-height);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid rgba(229, 231, 235, 0.8);
  z-index: var(--z-sticky);
}

/* === CONTAINER === */
.container { max-width: var(--content-max-width); margin: 0 auto; padding: 0 var(--space-4); }
.container-sm { max-width: 640px; margin: 0 auto; padding: 0 var(--space-4); }
.container-md { max-width: 768px; margin: 0 auto; padding: 0 var(--space-4); }

/* === FLEXBOX === */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.items-end { align-items: flex-end; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.justify-end { justify-content: flex-end; }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-8); }

/* === GRID === */
.grid { display: grid; }
.grid-cols-1 { grid-template-columns: 1fr; }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }
.col-span-2 { grid-column: span 2; }
.col-span-3 { grid-column: span 3; }

/* === SPACING === */
.m-0 { margin: 0; }
.mt-2 { margin-top: var(--space-2); }
.mt-4 { margin-top: var(--space-4); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-6 { margin-bottom: var(--space-6); }
.mb-8 { margin-bottom: var(--space-8); }
.ml-2 { margin-left: var(--space-2); }
.ml-auto { margin-left: auto; }
.mr-2 { margin-right: var(--space-2); }

.p-2 { padding: var(--space-2); }
.p-3 { padding: var(--space-3); }
.p-4 { padding: var(--space-4); }
.p-6 { padding: var(--space-6); }
.p-8 { padding: var(--space-8); }
.px-4 { padding-left: var(--space-4); padding-right: var(--space-4); }
.px-6 { padding-left: var(--space-6); padding-right: var(--space-6); }
.py-2 { padding-top: var(--space-2); padding-bottom: var(--space-2); }
.py-3 { padding-top: var(--space-3); padding-bottom: var(--space-3); }
.py-4 { padding-top: var(--space-4); padding-bottom: var(--space-4); }

/* === TEXT === */
.text-xs { font-size: var(--text-xs); }
.text-sm { font-size: var(--text-sm); }
.text-base { font-size: var(--text-base); }
.text-md { font-size: var(--text-md); }
.text-lg { font-size: var(--text-lg); }
.text-xl { font-size: var(--text-xl); }
.text-2xl { font-size: var(--text-2xl); }
.text-bold { font-weight: var(--font-bold); }
.text-semibold { font-weight: var(--font-semibold); }
.text-medium { font-weight: var(--font-medium); }
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-muted { color: var(--color-neutral-600); }
.text-primary { color: var(--color-primary); }
.text-error { color: var(--color-error); }
.text-success { color: var(--color-success); }
.text-mono { font-family: var(--font-mono); }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* === VISIBILITY === */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }

/* === RESPONSIVE === */
@media (max-width: 1024px) {
  .app-sidebar {
    transform: translateX(-100%);
    /* Cast a faint shadow on the body when open so the user perceives a
       drawer, not a column reflow. */
    box-shadow: 4px 0 24px rgba(15, 23, 33, 0);
    transition: transform 240ms ease, box-shadow 240ms ease;
  }
  .app-sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(15, 23, 33, 0.18);
  }
  /* Semi-transparent backdrop that fades in while the drawer is open.
     The actual close-on-tap is handled by the navbar.js outside-click
     listener; the backdrop is here for visual feedback only. */
  .app-sidebar::after {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 33, 0);
    pointer-events: none;
    opacity: 0;
    transition: opacity 240ms ease;
    z-index: -1;
  }
  .app-sidebar.open::after {
    background: rgba(15, 23, 33, 0.32);
    opacity: 1;
  }
  .app-main { margin-left: 0; }
  .app-bottom-nav {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: space-around;
  }
  .app-content {
    padding-bottom: calc(var(--bottom-nav-height) + var(--space-4));
    overflow-x: hidden;
  }
  .hide-mobile { display: none !important; }
}

@media (max-width: 768px) {
  .grid-cols-2 { grid-template-columns: 1fr; }
  .grid-cols-3 { grid-template-columns: 1fr; }
  .grid-cols-4 { grid-template-columns: repeat(2, 1fr); }
  .app-content { padding: var(--space-4); padding-bottom: calc(var(--bottom-nav-height) + var(--space-4)); }
  .form-row { flex-direction: column; }

  /* Prevent overflow on cards and content */
  .card { overflow: hidden; word-break: break-word; }
  .kpi-card { overflow: hidden; }

  /* Bottom nav items more compact */
  .bottom-nav-item { min-width: 48px; font-size: 10px; }
}

@media (max-width: 480px) {
  .grid-cols-4 { grid-template-columns: repeat(2, 1fr); }
  .app-content { padding: var(--space-3); padding-bottom: calc(var(--bottom-nav-height) + var(--space-4)); }
  h2 { font-size: var(--text-lg); }
  .kpi-card .kpi-value { font-size: var(--text-xl); }
  .btn { font-size: var(--text-sm); padding: var(--space-2) var(--space-4); }
  .btn-lg { font-size: var(--text-base); }
}

@media (min-width: 1025px) {
  .show-mobile-only { display: none !important; }
  /* When sidebar is visible, constrain 4-col grid if viewport is tight */
  .app-content .grid-cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (min-width: 1025px) and (max-width: 1150px) {
  .app-content .grid-cols-4 { grid-template-columns: repeat(2, 1fr); }
}
