/* Layout & Navigation - Mobile First */

.app-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: var(--bg-main);
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--spacing-md);
  height: 56px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  font-weight: 700;
  font-size: var(--font-size-base);
  color: var(--text-primary);
  text-decoration: none;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
}

.user-badge {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

/* Theme Toggle Button */
.theme-toggle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-xs);
  min-width: 36px;
  height: 36px;
  padding: 6px 10px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.theme-toggle-btn:hover {
  background: var(--bg-surface-hover);
  color: var(--text-primary);
}

/* iOS/Material Style Switch for Bottom Sheet */
.theme-switch {
  width: 46px;
  height: 26px;
  background: var(--bg-surface-hover);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-full);
  position: relative;
  cursor: pointer;
  padding: 0;
  outline: none;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.theme-switch .theme-switch-thumb {
  display: block;
  width: 20px;
  height: 20px;
  background: var(--text-muted);
  border-radius: 50%;
  position: absolute;
  top: 2px;
  left: 2px;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), background-color 0.2s ease;
}

.theme-switch.active {
  background: var(--accent-surface);
  border-color: var(--accent-primary);
}

.theme-switch.active .theme-switch-thumb {
  transform: translateX(20px);
  background: var(--accent-primary);
}

/* Main Content Container */
.app-main {
  flex: 1;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--spacing-md);
  padding-bottom: calc(var(--spacing-2xl) + 32px); /* Space for mobile bottom bar */
}

@media (min-width: 768px) {
  .app-main {
    padding: var(--spacing-lg);
    padding-bottom: var(--spacing-xl);
  }
}

/* Desktop Sidebar & Grid Layout */
@media (min-width: 1024px) {
  .app-wrapper {
    flex-direction: row;
  }

  .app-sidebar {
    width: 260px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    transition: background-color 0.2s ease, border-color 0.2s ease;
  }

  .sidebar-header {
    padding: var(--spacing-md);
    border-bottom: 1px solid var(--border-color);
  }

  .sidebar-nav {
    flex: 1;
    padding: var(--spacing-md) var(--spacing-sm);
    overflow-y: auto;
  }

  .nav-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    font-weight: 500;
    text-decoration: none;
    margin-bottom: 4px;
    transition: background-color 0.15s ease, color 0.15s ease;
  }

  .nav-item:hover {
    background: var(--bg-surface-hover);
    color: var(--text-primary);
    text-decoration: none;
  }

  .nav-item.active {
    background: var(--accent-surface);
    color: var(--accent-primary);
    font-weight: 600;
  }

  .sidebar-footer {
    padding: var(--spacing-md);
    border-top: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    gap: var(--spacing-xs);
  }

  .app-content-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
  }

  .mobile-bottom-nav {
    display: none !important;
  }

  .bottom-sheet-overlay {
    display: none !important;
  }
}

/* Mobile Bottom Navigation Bar */
@media (max-width: 1023px) {
  .app-sidebar {
    display: none;
  }

  .mobile-bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 50;
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 60px;
    background: var(--bg-surface);
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -2px 12px rgba(0,0,0,0.06);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transition: background-color 0.2s ease, border-color 0.2s ease;
  }

  .bottom-nav-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    flex: 1;
    height: 100%;
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 500;
    text-decoration: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0;
    transition: color 0.15s ease;
  }

  .bottom-nav-item:hover, .bottom-nav-item:active {
    color: var(--text-primary);
  }

  .bottom-nav-item.active {
    color: var(--accent-primary);
    font-weight: 600;
  }

  .bottom-nav-item .menu-indicator {
    position: absolute;
    top: 6px;
    right: calc(50% - 14px);
    width: 6px;
    height: 6px;
    background: var(--accent-primary);
    border-radius: 50%;
  }
}

/* Modals & General Dialogs */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 100;
  display: flex;
  align-items: flex-end; /* Bottom sheet by default on mobile */
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal-container {
  width: 100%;
  max-height: 90vh;
  background: var(--bg-surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: var(--spacing-lg);
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
  overflow-y: auto;
  border-top: 1px solid var(--border-color);
}

.modal-overlay.active .modal-container {
  transform: translateY(0);
}

@media (min-width: 640px) {
  .modal-overlay:not(.bottom-sheet-overlay) {
    align-items: center; /* Center standard dialogs on tablets/desktop */
    padding: var(--spacing-lg);
  }

  .modal-overlay:not(.bottom-sheet-overlay) .modal-container {
    max-width: 540px;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    transform: scale(0.96);
    box-shadow: var(--shadow-lg);
  }

  .modal-overlay:not(.bottom-sheet-overlay).active .modal-container {
    transform: scale(1);
  }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--spacing-md);
}

.modal-title {
  font-size: var(--font-size-lg);
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  padding: 6px;
  cursor: pointer;
  color: var(--text-muted);
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.modal-close:hover {
  background: var(--bg-surface-hover);
  color: var(--text-primary);
}

/* Specialized Mobile Menu Bottom Sheet */
.bottom-sheet-overlay {
  align-items: flex-end !important;
}

.bottom-sheet-container {
  width: 100%;
  max-width: 560px;
  max-height: 85vh;
  border-radius: 20px 20px 0 0;
  padding: 12px 16px 24px 16px;
  background: var(--bg-surface);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  padding-bottom: max(24px, env(safe-area-inset-bottom, 24px));
}

.sheet-drag-handle {
  width: 40px;
  height: 5px;
  background: var(--border-color);
  border-radius: 9999px;
  margin: 0 auto 12px auto;
  cursor: grab;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: 12px;
}

.sheet-user-card {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sheet-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent-surface);
  color: var(--accent-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.sheet-user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sheet-user-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}

.sheet-user-role {
  align-self: flex-start;
  font-size: 11px;
}

.sheet-close {
  background: var(--bg-surface-hover);
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  cursor: pointer;
}

.sheet-theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg-subtle);
  border-radius: var(--radius-md);
  margin-bottom: 12px;
  border: 1px solid var(--border-subtle);
}

.sheet-theme-label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
}

.sheet-nav-list {
  display: flex;
  flex-direction: column;
  gap: 3px;
  overflow-y: auto;
  max-height: 52vh;
  padding-right: 4px;
}

.sheet-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
  margin: 10px 6px 4px 6px;
}

.sheet-nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  text-decoration: none;
  color: var(--text-primary);
  transition: background-color 0.15s ease;
}

.sheet-nav-item:hover {
  background: var(--bg-surface-hover);
}

.sheet-nav-item.active {
  background: var(--accent-surface);
  color: var(--accent-primary);
}

.sheet-nav-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  background: var(--bg-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.sheet-nav-item.active .sheet-nav-icon {
  background: var(--accent-primary);
  color: #ffffff;
}

.sheet-nav-text {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.sheet-nav-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet-nav-desc {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet-nav-arrow {
  color: var(--text-muted);
  opacity: 0.6;
  flex-shrink: 0;
}

.sheet-footer {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border-subtle);
}

/* Toast Container */
.toast-container {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
  width: 90%;
  max-width: 400px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: var(--radius-md);
  font-size: var(--font-size-sm);
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toastIn 0.25s ease-out;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
}

.toast-success { border-left: 4px solid var(--success-text); }
.toast-error { border-left: 4px solid var(--danger-text); }
.toast-warning { border-left: 4px solid var(--warning-text); }
.toast-info { border-left: 4px solid var(--accent-primary); }