/* ==========================================================================
 * layout.css -- App Shell Layout
 *
 * Implements the responsive app shell: sidebar, header, main content area,
 * bottom navigation, toast container, modal, and loading overlay.
 *
 * Breakpoints:
 *   Desktop: > 1024px  -- Sidebar fixed left, main offset by sidebar width
 *   Tablet:  768-1024px -- Sidebar overlay (slide from left), main full width
 *   Mobile:  < 768px   -- Sidebar hidden, bottom nav visible
 *
 * Design reference: design.md Section 2.4 Layout Shell (lines 941-1148)
 * ========================================================================== */


/* ==========================================================================
 * x-cloak: Hide Alpine.js elements until they are initialized
 * ========================================================================== */

[x-cloak] {
  display: none !important;
}


/* ==========================================================================
 * Sidebar
 * ========================================================================== */

.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  z-index: var(--z-sidebar);
  transform: translateX(0);
  transition: transform 0.2s ease;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar__header {
  display: flex;
  align-items: center;
  height: var(--header-height);
  padding: 0 var(--space-4);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.sidebar__logo {
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--color-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar__nav {
  flex: 1;
  padding: var(--space-2) 0;
}

.sidebar__link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  font-size: var(--font-size-sm);
  font-weight: 500;
  color: var(--color-text-secondary);
  transition: background-color var(--transition-fast), color var(--transition-fast);
  text-decoration: none;
  border-left: 3px solid transparent;
}

.sidebar__link:hover {
  background: var(--color-bg);
  color: var(--color-text);
}

.sidebar__link--active {
  color: var(--color-primary);
  background: rgba(37, 99, 235, 0.06);
  border-left-color: var(--color-primary);
  font-weight: 600;
}

.sidebar__icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}


/* ==========================================================================
 * Sidebar Backdrop (tablet overlay)
 * ========================================================================== */

.sidebar-backdrop {
  display: none;
}


/* ==========================================================================
 * Main Content Area
 * ========================================================================== */

.main {
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}


/* ==========================================================================
 * Header
 * ========================================================================== */

.header {
  display: flex;
  align-items: center;
  height: var(--header-height);
  padding: 0 var(--space-4);
  padding-top: env(safe-area-inset-top);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: var(--z-header);
  position: sticky;
  top: 0;
  flex-shrink: 0;
}

.header__menu-btn {
  display: none; /* Hidden on desktop, shown on tablet */
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  color: var(--color-text);
  margin-right: var(--space-2);
}

.header__menu-btn:hover {
  background: var(--color-bg);
}

.header__title {
  font-size: var(--font-size-lg);
  font-weight: 600;
  color: var(--color-text);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header__user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-left: auto;
  flex-shrink: 0;
}

.header__user-name {
  font-size: var(--font-size-sm);
  font-weight: 500;
  color: var(--color-text-secondary);
  white-space: nowrap;
}


/* ==========================================================================
 * Page Content
 * ========================================================================== */

.content {
  flex: 1;
  padding: var(--space-4);
}


/* ==========================================================================
 * Bottom Navigation (mobile only)
 * ========================================================================== */

.bottom-nav {
  display: none;
}

.bottom-nav__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  padding: var(--space-1);
  font-size: var(--font-size-xs);
  color: var(--color-text-secondary);
  text-decoration: none;
  transition: color var(--transition-fast);
  min-width: 48px;
}

.bottom-nav__item:hover,
.bottom-nav__item--active {
  color: var(--color-primary);
}

.bottom-nav__icon {
  width: 22px;
  height: 22px;
}


/* ==========================================================================
 * Toast Container
 * ========================================================================== */

.toast-container {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: 400px;
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  background: var(--color-surface);
  border-left: 4px solid;
}

.toast--success { border-left-color: var(--color-success); }
.toast--error   { border-left-color: var(--color-danger); }
.toast--warning { border-left-color: var(--color-warning); }
.toast--info    { border-left-color: var(--color-primary); }

.toast__icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.toast--success .toast__icon { color: var(--color-success); }
.toast--error   .toast__icon { color: var(--color-danger); }
.toast--warning .toast__icon { color: var(--color-warning); }
.toast--info    .toast__icon { color: var(--color-primary); }

.toast__message {
  flex: 1;
  font-size: var(--font-size-sm);
  color: var(--color-text);
}

.toast__dismiss {
  background: none;
  border: none;
  cursor: pointer;
  font-size: var(--font-size-lg);
  color: var(--color-text-secondary);
  flex-shrink: 0;
  line-height: 1;
}

.toast__dismiss:hover {
  color: var(--color-text);
}

/* Toast enter/leave transitions */
.toast-enter {
  animation: toast-slide-in 0.3s ease;
}

.toast-leave {
  animation: toast-slide-out 0.2s ease forwards;
}

@keyframes toast-slide-in {
  from {
    opacity: 0;
    transform: translateX(100%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes toast-slide-out {
  from {
    opacity: 1;
    transform: translateX(0);
  }
  to {
    opacity: 0;
    transform: translateX(100%);
  }
}


/* ==========================================================================
 * Modal
 * ========================================================================== */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal-backdrop);
  padding: var(--space-4);
}

.modal-content {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-content__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--color-border);
}

.modal-content__close {
  font-size: var(--font-size-xl);
  color: var(--color-text-secondary);
  cursor: pointer;
  line-height: 1;
}

.modal-content__close:hover {
  color: var(--color-text);
}

.modal-content__body {
  padding: var(--space-6);
}

.modal-content__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--color-border);
}


/* ==========================================================================
 * Loading Overlay
 * ========================================================================== */

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  z-index: var(--z-toast); /* Above modals but below tooltips */
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-overlay p {
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
  font-weight: 500;
}


/* ==========================================================================
 * Tablet (768px - 1024px)
 *
 * Sidebar slides in as overlay, main takes full width.
 * Hamburger button becomes visible in header.
 * ========================================================================== */

@media (max-width: 1024px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.sidebar--open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: calc(var(--z-sidebar) - 1);
  }

  .main {
    margin-left: 0;
  }

  .header__menu-btn {
    display: flex;
  }
}


/* ==========================================================================
 * Mobile (< 768px)
 *
 * Sidebar hidden entirely. Bottom nav visible.
 * Hamburger button hidden (no sidebar to toggle).
 * Content gets bottom padding for the fixed bottom nav.
 * ========================================================================== */

@media (max-width: 767px) {
  .sidebar {
    display: none;
  }

  .sidebar-backdrop {
    display: none;
  }

  .header__menu-btn {
    display: none;
  }

  .header__user-name {
    display: none; /* Save horizontal space on small screens */
  }

  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    z-index: var(--z-header);
    justify-content: space-around;
    align-items: center;
  }

  .content {
    padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + var(--space-4));
  }

  /* Toast container repositioned above bottom nav on mobile */
  .toast-container {
    top: auto;
    bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom) + var(--space-4));
    right: var(--space-2);
    left: var(--space-2);
    max-width: 100%;
  }

  /* Modal slides up from bottom on mobile */
  .modal-content {
    max-width: 100%;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 85vh;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }
}

/* ── Page Transitions ── */
.page-container {
  animation: page-enter var(--transition-page, 300ms) ease;
}
@keyframes page-enter {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Bottom Nav Labels ── */
.bottom-nav__label {
  display: block;
  font-size: var(--font-size-xs, 12px);
  margin-top: 2px;
  line-height: 1;
}

/* ── Offline Banner ── */
.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: var(--space-2) var(--space-4);
  padding-top: calc(var(--space-2) + env(safe-area-inset-top));
  background: var(--color-warning, #d97706);
  color: #fff;
  text-align: center;
  font-size: var(--font-size-sm, 14px);
  font-weight: 500;
  z-index: 700;
  transform: translateY(-100%);
  transition: transform var(--transition-normal, 250ms) ease;
}
.offline-banner--visible {
  transform: translateY(0);
}

/* ── Update Banner ── */
.update-banner {
  position: fixed;
  bottom: var(--space-4);
  right: var(--space-4);
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface, #ffffff);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg, 12px);
  box-shadow: var(--shadow-lg);
  z-index: 600;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-size: var(--font-size-sm, 14px);
}
@media (max-width: 767px) {
  .update-banner {
    bottom: calc(56px + env(safe-area-inset-bottom) + var(--space-4));
    left: var(--space-4);
  }
}
