Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart
92.4 KB
Raw Blame
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
import '../services/api_service.dart';
import '../services/audio_player_service.dart';
import '../services/screen_wake.dart';
import '../services/volume_key_service.dart';
import '../services/chromecast_service.dart';
import '../services/home_widget_service.dart';
import '../services/server_task_tracker.dart';
import '../services/socket_service.dart';
import '../services/sleep_timer_service.dart';
import '../services/audiobookshelf_update_service.dart';
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart'
    show ImageRenderMethodForWeb;
import 'package:palette_generator/palette_generator.dart';
import '../utils/cover_accent.dart';
import '../main.dart'
    show snappyTransitionsNotifier, einkModeNotifier, coverSchemeNotifier, rootNavigatorKey, applyOrientationLock, applyEinkModeTheme;
import '../services/scoped_prefs.dart';
import 'dart:convert';
import 'admin_screen.dart';
import 'admin_api_keys_screen.dart';
import 'admin_rmab_screen.dart';
import 'book_requests_screen.dart';
import '../widgets/rmab_config_sheet.dart'
    show showRmabConfigSheet, kRmabBaseUrlKey, kRmabApiTokenKey, kRmabLegacyUrlKey;
import '../widgets/rmab_search_results_sheet.dart';
import 'admin_email_screen.dart';
import 'admin_libraries_screen.dart';
import 'admin_server_logs_screen.dart';
import 'admin_auth_settings_screen.dart';
import 'admin_server_settings_screen.dart';
import 'admin_sessions_screen.dart';
import 'admin_stats_screen.dart';
import 'admin_upload_screen.dart';
import 'admin_users_screen.dart';
import 'bookorbit_admin_screen.dart';
import 'bookorbit_email_screen.dart';
import 'bookorbit_libraries_screen.dart';
import 'bookorbit_users_screen.dart';
import 'bookmarks_screen.dart';
import 'downloads_screen.dart';
import 'upcoming_releases_screen.dart';
import 'server_notifications_screen.dart';
import '../services/bookorbit_notification_socket.dart';
import '../services/server_notifications.dart';
import '../services/ebook_cache.dart';
import '../widgets/action_pill.dart';
import '../widgets/book_detail_sheet.dart' show showBookDetailSheet;
import '../widgets/ebook_router.dart';
import '../widgets/nav_hold_options.dart';
import '../widgets/sleep_timer_sheet.dart';
import '../l10n/app_localizations.dart';
import '../services/wording.dart';
import '../services/android_auto_service.dart';
import '../services/carplay_service.dart';
import '../widgets/expanded_card.dart';
import '../widgets/desktop_account_button.dart';
import '../widgets/desktop_collapsible_sidebar.dart';
import '../widgets/desktop_now_playing_bar.dart';
import '../widgets/admin_task_indicator.dart';
import 'absorbing_screen.dart';
import 'home_screen.dart';
import 'library_screen.dart';
import 'stats_screen.dart';
import 'settings_screen.dart';
import 'app_shell_navigation_policy.dart';
import '../widgets/library_picker_sheet.dart';
import '../widgets/beta_logging_notice.dart';
import '../widgets/welcome_sheet.dart';
import '../widgets/whats_new_prompt.dart';
import '../services/review_service.dart';
import '../services/settings_sync_service.dart';
import '../services/update_checker_service.dart';
import '../widgets/update_dialog.dart';
import '../widgets/overlay_toast.dart';
import '../utils/app_platform.dart';
import '../utils/desktop_workspace.dart';

class AppShell extends StatefulWidget {
  final bool startOnAbsorbing;

  const AppShell({super.key, this.startOnAbsorbing = false});

  /// Navigate to the Absorbing tab using BuildContext (ancestor lookup).
  static void goToAbsorbing(BuildContext context) {
    final state = context.findAncestorStateOfType<_AppShellState>();
    state?._switchToAbsorbing();
  }

  /// Navigate to the Absorbing tab without needing a context.
  static void goToAbsorbingGlobal() {
    _AppShellState._instance?._switchToAbsorbing();
  }

  /// Track when expanded card is opened/closed externally (e.g. chevron tap).
  static void setExpandedOpen(bool open) {
    _AppShellState._instance?._expandedIsOpen = open;
  }

  static ServerTaskTracker? serverTaskTrackerOf(BuildContext context) =>
      context.findAncestorStateOfType<_AppShellState>()?._serverTaskTracker;

  /// Switch to the Library tab and focus the search bar. Used by the
  /// app-icon "Search" shortcut. Returns false when the shell isn't mounted
  /// yet so callers can retry during cold start.
  static bool openSearchGlobal() {
    final inst = _AppShellState._instance;
    if (inst == null) return false;
    inst._openSearch();
    return true;
  }

  /// Switch to the Library tab and apply a tag filter. Used by the book
  /// detail sheet's tag chip so tapping a tag jumps the user to the library
  /// view filtered by that tag. Returns false when the shell or library
  /// state isn't mounted yet.
  static bool openLibraryWithTagFilterGlobal(String tag) =>
      _applyLibraryFilterGlobal((s) => s.applyTagFilter(tag));

  /// Switch to the Library tab and apply a genre filter. Mirrors the tag
  /// version above; used by the genre chip in book detail.
  static bool openLibraryWithGenreFilterGlobal(String genre) =>
      _applyLibraryFilterGlobal((s) => s.applyGenreFilter(genre));

  static bool _applyLibraryFilterGlobal(
    void Function(LibraryScreenState) apply,
  ) {
    final inst = _AppShellState._instance;
    if (inst == null) return false;
    unawaited(inst._applyLibraryFilterAfterPaneReset(apply));
    return true;
  }

  /// Called by Home (callingTab=0) and Library (callingTab=1) after their
  /// first frame. Lets the AppShell re-sync the bottom-nav listener to the
  /// right notifier — handles both "screen state didn't exist on initial
  /// attach" and "lazy attach attached to the wrong tab during a fade
  /// transition" (LibraryProvider notify can rebuild AppShell mid-fade and
  /// schedule a postFrame that fires before _currentIndex transitions).
  static void notifyScreenReady(int callingTab) {
    _AppShellState._instance?._reattachIfNeeded(callingTab);
  }

  @override
  State<AppShell> createState() => _AppShellState();
}

class _PaneNavigatorObserver extends NavigatorObserver {
  _PaneNavigatorObserver(this.onStackChanged);

  final VoidCallback onStackChanged;

  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);
    onStackChanged();
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPop(route, previousRoute);
    onStackChanged();
  }

  @override
  void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didRemove(route, previousRoute);
    onStackChanged();
  }

  @override
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    onStackChanged();
  }
}

class _AppShellState extends State<AppShell>
    with WidgetsBindingObserver, TickerProviderStateMixin {
  static _AppShellState? _instance;
  static const _desktopSidebarPinnedKey = 'desktop_sidebar_pinned';

  // Tabs: 0=Home, 1=Library, 2=Absorbing (default), 3=Stats, 4=Settings
  int _currentIndex = 2; // overridden by user preference in initState

  // Dedicated Podcasts tab (optional 6th destination, display order Home,
  // Library, Podcasts, Absorbing, Stats, Settings). Stack pages keep their
  // historic 0-4 indexes: the Podcasts and Library destinations share page 1,
  // and which one is highlighted is derived from the selected library.
  bool _podcastTabEnabled = false;
  String _podcastTabLibraryId = '';
  final _homeKey = GlobalKey<HomeScreenState>();
  final _libraryKey = GlobalKey<LibraryScreenState>();
  final _settingsKey = GlobalKey<SettingsScreenState>();
  final _player = AudioPlayerService();
  final _cast = ChromecastService();
  bool _playerHadBook = false;
  bool _wasPlaying = false;
  bool _lifecycleBackgrounded = false;
  String? _lastItemId;
  bool _expandedIsOpen = false;
  bool _wasCasting = false;
  DateTime? _lastBackPress;
  // Tracks which item's cover we derived the scheme from.
  String? _lastCoverItemId;

  // ── Scroll-to-hide bottom nav (driven by Library screen) ──
  late final AnimationController _navBarAnimController = AnimationController(
    vsync: this,
    duration: const Duration(milliseconds: 250),
    value: 1.0,
  );
  VoidCallback? _navBarListener;

  // Lazily build tabs so startup on Absorbing does not initialize Home/Library
  // work until the user actually visits those tabs.
  final List<Widget?> _pages = List<Widget?>.filled(5, null, growable: false);

  // Desktop workspace content-pane navigator: full-page pushes land here so
  // the sidebar and now-playing bar stay visible around them.
  final _paneNavigatorKey = GlobalKey<NavigatorState>();
  late final _PaneNavigatorObserver _paneNavigatorObserver;
  bool _paneRouteOpen = false;
  bool _adminRouteOpen = false;
  bool _desktopWorkspaceWasActive = false;
  bool _desktopCollapseAllowed = false;
  bool _desktopCollapseCheckScheduled = false;
  bool _desktopSidebarPinned = false;
  int _desktopSidebarPreferenceGeneration = 0;
  ServerTaskTracker? _serverTaskTracker;
  Timer? _serverTaskRefreshTimer;
  bool _refreshingServerTasks = false;
  Object? _serverTaskPollingApi;
  bool _serverTaskPollingEnabled = false;
  bool _serverTaskPollingSyncScheduled = false;
  final AudiobookshelfUpdateController _serverUpdateController =
      AudiobookshelfUpdateController.instance;

  Future<void> _loadDesktopSidebarPreference() async {
    final generation = _desktopSidebarPreferenceGeneration;
    final prefs = await SharedPreferences.getInstance();
    final pinned = prefs.getBool(_desktopSidebarPinnedKey) ?? false;
    if (!mounted ||
        generation != _desktopSidebarPreferenceGeneration ||
        pinned == _desktopSidebarPinned) {
      return;
    }
    setState(() => _desktopSidebarPinned = pinned);
  }

  void _setDesktopSidebarPinned(bool pinned) {
    _desktopSidebarPreferenceGeneration++;
    if (pinned == _desktopSidebarPinned) return;
    setState(() => _desktopSidebarPinned = pinned);
    unawaited(_saveDesktopSidebarPreference(pinned));
  }

  Future<void> _saveDesktopSidebarPreference(bool pinned) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_desktopSidebarPinnedKey, pinned);
  }

  void _startServerTaskRefreshTimer() {
    _serverTaskRefreshTimer?.cancel();
    _serverTaskRefreshTimer = null;
    if (!mounted || !AppPlatform.lacksPhonePlugins) return;
    final auth = context.read<AuthProvider>();
    if (!auth.isAdmin || auth.apiService == null || _serverTaskTracker == null) {
      return;
    }
    unawaited(_refreshServerTasks());
    _serverTaskRefreshTimer = Timer.periodic(const Duration(seconds: 3), (_) {
      // The socket already delivers task start, progress and finish. Polling
      // every 3s regardless cost each open admin tab ~1200 requests an hour;
      // only poll fast while something runs or the socket is down, and
      // otherwise catch up once a minute.
      final tracker = _serverTaskTracker;
      final busy = tracker != null && tracker.runningCount > 0;
      final last = _lastServerTaskRefresh;
      final due = last == null ||
          DateTime.now().difference(last) >= const Duration(minutes: 1);
      if (busy || !SocketService().isConnected || due) {
        unawaited(_refreshServerTasks());
      }
    });
  }

  DateTime? _lastServerTaskRefresh;

  void _syncServerTaskPolling({
    required bool isAdmin,
    required Object? api,
  }) {
    final enabled = isAdmin && api != null && _serverTaskTracker != null;
    if (enabled == _serverTaskPollingEnabled &&
        identical(api, _serverTaskPollingApi)) {
      return;
    }
    _serverTaskPollingEnabled = enabled;
    _serverTaskPollingApi = api;
    if (_serverTaskPollingSyncScheduled) return;
    _serverTaskPollingSyncScheduled = true;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _serverTaskPollingSyncScheduled = false;
      if (!mounted) return;
      if (_serverTaskPollingEnabled && !_lifecycleBackgrounded) {
        _startServerTaskRefreshTimer();
      } else {
        _serverTaskRefreshTimer?.cancel();
        _serverTaskRefreshTimer = null;
      }
    });
  }

  Future<void> _refreshServerTasks() async {
    if (!mounted || _refreshingServerTasks) return;
    final tracker = _serverTaskTracker;
    final api = context.read<AuthProvider>().apiService;
    if (tracker == null || api == null) return;
    _refreshingServerTasks = true;
    _lastServerTaskRefresh = DateTime.now();
    try {
      await tracker.refresh(api);
    } finally {
      _refreshingServerTasks = false;
    }
  }

  void _openSearch() => unawaited(_openSearchAfterPaneReset());

  Future<void> _openSearchAfterPaneReset() async {
    if (!await _preparePaneForNavigation()) return;
    // If the user triggered Search while a pushed route (Downloads, Bookmarks,
    // Settings pages, etc.) is on top of the shell, pop back so the shell's
    // Library tab actually becomes visible.
    final nav = rootNavigatorKey.currentState;
    if (nav != null && nav.canPop()) {
      nav.popUntil((r) => r.isFirst);
    }
    final lib = context.read<LibraryProvider>();
    final podcastsShown = _podcastsShown(lib);
    if (podcastsShown) _syncTabLibrary(1, true);
    _navigateToPrepared(1);
    // The Library tab can take a few hundred ms to mount after the switch.
    // Retry on a timer, not on post-frame callbacks: those don't schedule a
    // frame, so once the page went idle the chain stopped and Search never
    // focused the box on a fresh load.
    int attempts = 0;
    void tryFocus() {
      if (!mounted) return;
      final state = _libraryKey.currentState;
      if (state != null) {
        state.focusSearch();
        return;
      }
      if (++attempts < 60) {
        Future.delayed(const Duration(milliseconds: 50), tryFocus);
      }
    }

    WidgetsBinding.instance.addPostFrameCallback((_) => tryFocus());
  }

  void _switchToAbsorbing() => unawaited(_switchToAbsorbingAfterPaneReset());

  Future<void> _switchToAbsorbingAfterPaneReset() async {
    if (!mounted || !await _navigateToAfterPaneReset(2)) return;
    // Scroll to the currently playing book after the tab switch
    WidgetsBinding.instance.addPostFrameCallback((_) {
      AbsorbingScreen.scrollToActive();
    });
  }

  void _navigateTo(int index) => unawaited(_navigateToAfterPaneReset(index));

  Future<bool> _preparePaneForNavigation() async {
    if (!mounted ||
        !await DesktopWorkspaceNavigator.prepareForPaneReset() ||
        !mounted) {
      return false;
    }
    // A tab click while a pushed page fills the pane should reveal the tab.
    _paneNavigatorKey.currentState?.popUntil((r) => r.isFirst);
    return true;
  }

  Future<bool> _navigateToAfterPaneReset(int index) async {
    if (!await _preparePaneForNavigation()) return false;
    _navigateToPrepared(index);
    return true;
  }

  void _navigateToPrepared(int index) {
    if (index == _currentIndex) {
      // Already on this tab — handle re-tap actions
      if (index == 2) {
        // Absorbing tab: scroll to first card
        AbsorbingScreen.scrollToFirst();
      }
      return;
    }
    _ensurePageBuilt(index);
    _syncNavBarListener(index);
    if (snappyTransitionsNotifier.value || einkModeNotifier.value) {
      setState(() => _currentIndex = index);
      _syncPlayerOnScreen();
    } else {
      _fadeController.reverse().then((_) {
        if (!mounted) return;
        setState(() {
          _currentIndex = index;
        });
        _syncPlayerOnScreen();
        _fadeController.forward();
      });
    }
  }

  void _syncPlayerOnScreen() {
    PlayerScreenWake.onScreen.value = _currentIndex == 2 || _expandedIsOpen;
  }

  Future<void> _applyLibraryFilterAfterPaneReset(
    void Function(LibraryScreenState) apply,
  ) async {
    if (!await _preparePaneForNavigation()) return;
    // If the full-screen expanded player is on top of the shell, pop it
    // first. Otherwise switching to the Library tab leaves the player
    // covering the filtered library underneath. Caller (e.g. the book
    // detail sheet) has already popped its own modal route at this point.
    if (_expandedIsOpen && mounted) {
      await Navigator.of(context, rootNavigator: true).maybePop();
      if (!mounted) return;
    }
    _navigateToPrepared(1);
    // The retry budget has to survive the fade transition (~200ms by
    // default, see `_fadeController` and `_navigateTo`) plus the library
    // widget's own mount + first build. On cold start that easily takes
    // 300-500ms before `_libraryKey.currentState` becomes non-null. ~3s
    // worth of frames is generous enough to cover slow devices and stingy
    // enough to give up if something is genuinely broken.
    const maxAttempts = 180; // ~3s at 60fps
    var attempts = 0;
    void tryApply() {
      if (!mounted) return;
      final state = _libraryKey.currentState;
      if (state != null) {
        apply(state);
        return;
      }
      if (++attempts < maxAttempts) {
        WidgetsBinding.instance.addPostFrameCallback((_) => tryApply());
      }
    }

    WidgetsBinding.instance.addPostFrameCallback((_) => tryApply());
  }

  /// Subscribe to the active screen's barsRevealNotifier when on Home or
  /// Library tab, and ensure the nav bar is visible on all other tabs.
  void _syncNavBarListener(int index) {
    _detachNavBarListener();
    // Snap visible immediately on every tab change so a partial-hide state
    // from another tab can never bleed into the new tab.
    _navBarAnimController.value = 1.0;
    ValueListenable<double>? notifier;
    if (index == 0) {
      notifier = _homeKey.currentState?.barsRevealNotifier;
    } else if (index == 1) {
      notifier = _libraryKey.currentState?.barsRevealNotifier;
    }
    if (notifier != null) {
      _activeBarNotifier = notifier;
      // Mirror the screen's continuous 0..1 reveal value directly onto the
      // controller so the bottom nav slides in lockstep with the header.
      _navBarListener = () {
        final v = notifier!.value.clamp(0.0, 1.0);
        // Skip no-op controller writes. AnimationController.value setter
        // notifies listeners (and triggers SizeTransition + Scaffold layout)
        // even when the value didn't change, which causes scroll jank when
        // the bar is already fully open/closed.
        if ((_navBarAnimController.value - v).abs() < 0.005) return;
        _navBarAnimController.value = v;
      };
      notifier.addListener(_navBarListener!);
      _navBarListener!();
    }
  }

  ValueListenable<double>? _activeBarNotifier;

  void _detachNavBarListener() {
    if (_navBarListener != null && _activeBarNotifier != null) {
      _activeBarNotifier!.removeListener(_navBarListener!);
    }
    _navBarListener = null;
    _activeBarNotifier = null;
  }

  /// Hook called by Home/Library when their state finishes mounting so we can
  /// pick up (or correct) the listener attach. Re-syncs unconditionally when
  /// the calling tab matches the current tab, even if a listener is already
  /// attached — that listener may have been attached to the wrong tab by the
  /// lazy-attach race during a fade transition.
  void _reattachIfNeeded(int callingTab) {
    if (!mounted) return;
    // Only act when the screen calling us is actually the active one. If the
    // user has navigated away in the meantime, leave the existing attachment
    // to whatever screen they're on.
    if (_currentIndex != callingTab) return;
    _syncNavBarListener(_currentIndex);
  }

  void _ensurePageBuilt(int index) {
    if (_pages[index] != null) return;
    switch (index) {
      case 0:
        _pages[index] = HomeScreen(key: _homeKey);
        break;
      case 1:
        _pages[index] = LibraryScreen(key: _libraryKey);
        break;
      case 2:
        _pages[index] = AbsorbingScreen(key: AbsorbingScreen.globalKey);
        break;
      case 3:
        _pages[index] = const StatsScreen();
        break;
      case 4:
        _pages[index] = SettingsScreen(key: _settingsKey);
        break;
    }
  }

  late final AnimationController _fadeController = AnimationController(
    vsync: this,
    duration: const Duration(milliseconds: 200),
    value: 1.0,
  );

  void _loadStartScreen() {
    PlayerSettings.getStartScreen().then((idx) {
      if (mounted && idx != _currentIndex && idx >= 0 && idx <= 4) {
        setState(() => _currentIndex = idx);
        _syncPlayerOnScreen();
        _ensurePageBuilt(idx);
        _restoreBookLibraryForCurrentTab(restoreLibraryPage: true);
      }
    });
  }

  @override
  void initState() {
    super.initState();
    _instance = this;
    _incomingNotifications =
        BookOrbitNotificationSocket.instance.incoming.listen(showIncomingNotificationToast);
    EreaderVolumeNav.ensureHandler();
    if (AppPlatform.lacksPhonePlugins) {
      _serverTaskTracker = ServerTaskTracker();
      _serverUpdateController.addListener(_onServerUpdateChanged);
    }
    _paneNavigatorObserver = _PaneNavigatorObserver(_syncPaneRouteState);
    DesktopWorkspaceNavigator.register(() => _paneNavigatorKey.currentState);
    DesktopWorkspaceNavigator.exitGuardChanges.addListener(
      _onDesktopExitGuardChanged,
    );
    unawaited(_loadDesktopSidebarPreference());
    if (!AppPlatform.lacksPhonePlugins) PlayerScreenWake.start();
    if (AppPlatform.lacksPhonePlugins) {
      _currentIndex = 0;
    } else if (!widget.startOnAbsorbing) {
      _loadStartScreen();
    }
    _ensurePageBuilt(_currentIndex);
    _syncPlayerOnScreen();
    _playerHadBook = _player.hasBook;
    _wasPlaying = _player.isPlaying;
    _lastItemId = _player.currentItemId;
    WidgetsBinding.instance.addObserver(this);
    AudioPlayerService.setOnEpisodePlayStartedCallback(
      AppShell.goToAbsorbingGlobal,
    );
    _player.addListener(_onPlayerChanged);
    _wasCasting = _cast.isCasting;
    _cast.addListener(_onCastChanged);
    // Try immediately; _onLibraryChanged picks it up once data loads.
    // Deferred to post-frame so Theme.of(context) inside _deriveCoverScheme
    // doesn't establish an inherited-widget dependency during initState.
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) _deriveCoverScheme();
    });
    context.read<LibraryProvider>().addListener(_onLibraryChanged);
    _loadPodcastTabPrefs();
    PlayerSettings.settingsChanged.addListener(_loadPodcastTabPrefs);
    if (!AppPlatform.lacksPhonePlugins) _showLaunchPrompts();
    _checkForUpdate();
  }

  /// One after another, so they never land on top of each other.
  Future<void> _showLaunchPrompts() async {
    await WhatsNewPrompt.showIfUpdated(context);
    if (!mounted) return;
    await WelcomeSheet.showIfNeeded(context);
    if (!mounted) return;
    await BetaLoggingNotice.showIfPending(context);
  }

  Future<void> _loadPodcastTabPrefs() async {
    final enabled = await PlayerSettings.getPodcastTabEnabled();
    final libId = await PlayerSettings.getPodcastTabLibraryId();
    if (!mounted) return;
    if (enabled != _podcastTabEnabled || libId != _podcastTabLibraryId) {
      setState(() {
        _podcastTabEnabled = enabled;
        _podcastTabLibraryId = libId;
      });
    }
    _restoreBookLibraryForCurrentTab(restoreLibraryPage: true);
  }

  bool _podcastsShown(LibraryProvider lib) =>
      _podcastTabEnabled &&
      _podcastTabLibraryId.isNotEmpty &&
      lib.libraries.any((l) => l['id'] == _podcastTabLibraryId);

  void _restoreBookLibraryForCurrentTab({bool restoreLibraryPage = false}) {
    final lib = context.read<LibraryProvider>();
    final podcastsShown = _podcastsShown(lib);
    if (shouldRestoreBookLibrary(
      pageIndex: _currentIndex,
      podcastsShown: podcastsShown,
      selectedLibraryId: lib.selectedLibraryId,
      podcastLibraryId: _podcastTabLibraryId,
      restoreLibraryPage: restoreLibraryPage,
    )) {
      _syncTabLibrary(_currentIndex, podcastsShown);
    }
  }

  // Display-destination index <-> stack page mapping when the Podcasts tab
  // is shown (it sits at display slot 2 and shares stack page 1).
  int _pageForDest(int dest, bool podcastsShown) {
    if (!podcastsShown) return dest;
    if (dest <= 1) return dest;
    if (dest == 2) return 1;
    return dest - 1;
  }

  int _selectedDest(LibraryProvider lib, bool podcastsShown) {
    if (!podcastsShown) return _currentIndex;
    if (_currentIndex == 0) return 0;
    if (_currentIndex == 1) {
      return lib.selectedLibraryId == _podcastTabLibraryId ? 2 : 1;
    }
    return _currentIndex + 1;
  }

  /// Keep the selected library in step with the destination being entered:
  /// Podcasts pins its library; Home/Library return to the remembered book
  /// library. Uses the light switch so playback and cached shelves survive.
  void _syncTabLibrary(int dest, bool podcastsShown) {
    if (!podcastsShown) return;
    final lib = context.read<LibraryProvider>();
    if (dest == 2) {
      if (lib.selectedLibraryId != _podcastTabLibraryId) {
        lib.selectLibraryLight(_podcastTabLibraryId);
      }
    } else if (dest == 0 || dest == 1) {
      if (lib.selectedLibraryId == _podcastTabLibraryId) {
        lib.lastBookLibraryId().then((bookLib) {
          if (mounted && bookLib != null && bookLib != _podcastTabLibraryId) {
            lib.selectLibraryLight(bookLib);
          }
        });
      }
    }
  }

  static const _isGithubBuild = bool.fromEnvironment('GITHUB_BUILD');

  void _checkForUpdate() async {
    if (AppPlatform.lacksPhonePlugins || !_isGithubBuild) return;
    final includePreReleases = await PlayerSettings.getIncludePreReleases();
    final info = await UpdateCheckerService.check(
      includePreReleases: includePreReleases,
    );
    if (info == null || !mounted) return;
    await UpdateDialog.show(context, info);
  }

  void _onServerUpdateChanged() {
    if (mounted) setState(() {});
  }

  StreamSubscription<ServerNotification>? _incomingNotifications;

  @override
  void dispose() {
    _incomingNotifications?.cancel();
    _fadeController.dispose();
    _detachNavBarListener();
    _navBarAnimController.dispose();
    _player.removeListener(_onPlayerChanged);
    _cast.removeListener(_onCastChanged);
    _serverTaskRefreshTimer?.cancel();
    _serverTaskTracker?.dispose();
    if (AppPlatform.lacksPhonePlugins) {
      _serverUpdateController.removeListener(_onServerUpdateChanged);
    }
    PlayerSettings.settingsChanged.removeListener(_loadPodcastTabPrefs);
    try {
      context.read<LibraryProvider>().removeListener(_onLibraryChanged);
    } catch (_) {}
    DesktopWorkspaceNavigator.exitGuardChanges.removeListener(
      _onDesktopExitGuardChanged,
    );
    if (_instance == this) {
      _instance = null;
      DesktopWorkspaceNavigator.unregister();
    }
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  void _onLibraryChanged() {
    if (!mounted) return;
    // Re-derive cover scheme whenever absorbing list changes so the app
    // theme always reflects the current [0] book.
    _deriveCoverScheme();
    _restoreBookLibraryForCurrentTab();
  }

  /// Attempt to derive cover scheme. Returns true if successful.
  bool _deriveCoverScheme() {
    if (!mounted) return false;
    // Use player's current item, or fall back to absorbing list's first item
    var itemId = _player.currentItemId;
    if (itemId == null) {
      final lib = context.read<LibraryProvider>();
      final ids = lib.absorbingBookIds;
      if (ids.isNotEmpty) {
        final key = ids.first;
        // Composite keys are "itemId-episodeId"; extract the item ID
        itemId = key.length > 36 ? key.substring(0, 36) : key;
      }
    }
    if (itemId == null) {
      return false;
    }
    if (itemId == _lastCoverItemId) return true;

    final lib = context.read<LibraryProvider>();
    final coverUrl = lib.getCoverUrl(itemId, width: 400);
    if (coverUrl == null) {
      return false;
    }
    _lastCoverItemId = itemId;

    final ImageProvider provider;
    if (coverUrl.startsWith('/')) {
      provider = FileImage(File(coverUrl));
    } else {
      provider = CachedNetworkImageProvider(imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet, 
        coverUrl,
        headers: lib.mediaHeaders,
      );
    }

    final brightness = Theme.of(context).brightness;
    PaletteGenerator.fromImageProvider(provider, maximumColorCount: 16)
        .then((palette) {
          final seedColor = accentFromCoverPalette(palette);
          if (seedColor == null) {
            _lastCoverItemId = null;
            return;
          }
          final scheme = ColorScheme.fromSeed(
            seedColor: seedColor,
            brightness: brightness,
          );
          coverSchemeNotifier.value = scheme;
          PlayerSettings.setCoverSeedColor(seedColor.toARGB32());
        })
        .catchError((_) {
          _lastCoverItemId = null;
        });
    return true; // cover URL found, image load in progress
  }

  void _onPlayerChanged() {
    final hasBook = _player.hasBook;
    final playing = _player.isPlaying;
    final itemId = _player.currentItemId;

    // Detect playback starting: new book loaded, play resumed, or item changed
    final newBook = hasBook && !_playerHadBook;
    final playStarted = playing && !_wasPlaying;
    final itemChanged = itemId != null && itemId != _lastItemId;

    _playerHadBook = hasBook;
    _wasPlaying = playing;
    _lastItemId = itemId;

    if (itemChanged || newBook) _deriveCoverScheme();

    if ((newBook || playStarted || itemChanged) && !_expandedIsOpen) {
      _maybeAutoExpand();
    }
  }

  void _onCastChanged() {
    final casting = _cast.isCasting;
    if (casting && !_wasCasting) {
      _switchToAbsorbing();
    }
    _wasCasting = casting;
  }

  Future<void> _maybeAutoExpand() async {
    if (AppPlatform.lacksPhonePlugins) return;
    // Only auto-open the full-screen player from the Absorbing tab - starting
    // playback from elsewhere (another tab, the nav long-press) shouldn't
    // yank the user into the player.
    if (_currentIndex != 2) return;
    // The ebook reader is a route above this shell, so the tab check can't
    // see it: audio started from the reader (Find in audiobook with "Keep
    // reading", volume-key play) must not pop the full player underneath it.
    if (context.read<LibraryProvider>().readerQuiet) return;
    final enabled = await PlayerSettings.getFullScreenPlayer();
    if (!enabled) return;
    await _openExpandedPlayer();
  }

  Future<void> _openExpandedPlayer() async {
    if (!mounted || !_player.hasBook || _expandedIsOpen) return;

    // Synthesize item data from player state
    final itemId = _player.currentItemId;
    if (itemId == null) return;

    final lib = context.read<LibraryProvider>();
    // Try to find the real item data from the library
    Map<String, dynamic>? item;
    for (final section in lib.personalizedSections) {
      for (final e in (section['entities'] as List<dynamic>? ?? [])) {
        if (e is Map<String, dynamic> && e['id'] == itemId) {
          item = e;
          break;
        }
      }
      if (item != null) break;
    }
    // Fallback: synthesize from player data
    item ??= {
      'id': itemId,
      'libraryId': _player.currentLibraryId,
      'media': {
        'metadata': {
          'title': _player.currentTitle ?? 'Unknown',
          'authorName': _player.currentAuthor ?? '',
        },
        'duration': _player.totalDuration,
        'chapters': _player.chapters,
      },
    };
    if (_player.currentEpisodeId != null) {
      item['recentEpisode'] = {
        'id': _player.currentEpisodeId,
        'title': _player.currentEpisodeTitle ?? _player.currentTitle,
        'duration': _player.totalDuration,
      };
    }

    _expandedIsOpen = true;
    _syncPlayerOnScreen();
    final nav = Navigator.of(context, rootNavigator: true);
    await nav.push(
      ExpandedCardRoute(
        child: ExpandedCard(item: item, player: _player),
      ),
    );
    // Route was popped — expanded view closed
    _expandedIsOpen = false;
    _syncPlayerOnScreen();
  }

  @override
  void didHaveMemoryPressure() {
    AudioPlayerService.onMemoryPressure();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        // Before the backgrounded guard: when the activity attaches to an
        // engine that has never drawn (born from a headless Android Auto /
        // media-button start, kept alive because audio is playing), nothing
        // schedules a frame for the new surface and the launch sits on the
        // splash screen - and that engine never saw a backgrounded state.
        WidgetsBinding.instance.scheduleForcedFrame();
        _handleAppForegrounded();
        break;
      case AppLifecycleState.hidden:
      case AppLifecycleState.paused:
        // `hidden` fires on desktop (macOS/Windows) when the window is
        // minimized or hidden, where `paused` often never arrives - without
        // handling it, every background timer and the socket keep running at
        // full foreground cadence forever and drain the battery. On mobile
        // `hidden` precedes `paused`; the _lifecycleBackgrounded guard makes
        // the second call a no-op (and stops `hidden` on the way back up from
        // flapping the socket).
        _handleAppBackgrounded();
        break;
      case AppLifecycleState.detached:
        final cast = ChromecastService();
        if (cast.isConnected) cast.disconnect();
        break;
      case AppLifecycleState.inactive:
        break;
    }
  }

  void _handleAppForegrounded() {
    if (!_lifecycleBackgrounded) return;
    _lifecycleBackgrounded = false;
    _startServerTaskRefreshTimer();
    // The rotation lock lives on the Activity, and Android can recreate the
    // activity over the still-running cached engine (swipe away from recents
    // while playback keeps the service alive). main() doesn't re-run then, so
    // re-apply the saved lock on every return to the foreground.
    applyOrientationLock();
    // Belt-and-suspenders: never let the nav bar come back hidden after a
    // resume. The screens snap their own driver back to shown on next scroll
    // anyway, but mirror it here in case the user resumes onto a stale tab.
    _navBarAnimController.value = 1.0;
    unawaited(context.read<AuthProvider>().adoptCompanionTokens());
    context.read<LibraryProvider>().onAppForegrounded();
    SleepTimerService().onAppForegrounded();
    AudioPlayerService.onAppForegrounded();
    if (!AppPlatform.lacksPhonePlugins) {
      HomeWidgetService().onAppForegrounded();
      ReviewService.onAppForegrounded();
    }
    unawaited(SettingsSyncService().onAppForegrounded());
    _refreshDataForTab(_currentIndex);
    // Check auto sleep in case we resumed into the window
    SleepTimerService().checkAutoSleep();
    _checkForUpdate();
  }

  void _handleAppBackgrounded() {
    if (_lifecycleBackgrounded) return;
    _lifecycleBackgrounded = true;
    _serverTaskRefreshTimer?.cancel();
    _serverTaskRefreshTimer = null;
    context.read<LibraryProvider>().onAppBackgrounded();
    SleepTimerService().onAppBackgrounded();
    AudioPlayerService.onAppBackgrounded();
    if (!AppPlatform.lacksPhonePlugins) HomeWidgetService().onAppBackgrounded();
    unawaited(SettingsSyncService().onAppBackgrounded());
  }

  @override
  void didChangeMetrics() {
    // Orientation change, software keyboard, anything that resizes the window:
    // make sure the bottom nav isn't stuck partway hidden.
    _navBarAnimController.value = 1.0;
    _homeKey.currentState?.resetReveal();
    _libraryKey.currentState?.resetReveal();
  }

  void _onDesktopExitGuardChanged() {
    if (!mounted || !_desktopWorkspaceWasActive || _desktopCollapseAllowed) {
      return;
    }
    _scheduleDesktopWorkspaceCollapseCheck();
  }

  void _syncPaneRouteState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (!mounted) return;
      final paneRouteOpen = _paneNavigatorKey.currentState?.canPop() ?? false;
      if (paneRouteOpen != _paneRouteOpen) {
        setState(() => _paneRouteOpen = paneRouteOpen);
      }
    });
  }

  void _scheduleDesktopWorkspaceCollapseCheck() {
    if (_desktopCollapseCheckScheduled) return;
    _desktopCollapseCheckScheduled = true;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      unawaited(_checkDesktopWorkspaceCollapse());
    });
  }

  Future<void> _checkDesktopWorkspaceCollapse() async {
    if (!mounted) return;
    final view = View.of(context);
    final width = view.physicalSize.width / view.devicePixelRatio;
    if (width >= kDesktopWorkspaceBreakpoint) {
      _desktopCollapseCheckScheduled = false;
      return;
    }
    final allowed = await DesktopWorkspaceNavigator.prepareForPaneReset();
    if (!mounted) return;
    _desktopCollapseCheckScheduled = false;
    final currentView = View.of(context);
    final currentWidth =
        currentView.physicalSize.width / currentView.devicePixelRatio;
    if (allowed && currentWidth < kDesktopWorkspaceBreakpoint) {
      setState(() => _desktopCollapseAllowed = true);
    }
  }

  DateTime? _lastRefresh;
  static const _refreshCooldown = Duration(minutes: 1);

  void _refreshDataForTab(int tabIndex) {
    final now = DateTime.now();
    final lib = context.read<LibraryProvider>();

    // Always sync local progress (cheap, no network)
    lib.refreshLocalProgress();

    // Tabs that do not need full personalized shelf rebuilds.
    if (tabIndex == 1 || tabIndex == 2 || tabIndex == 3) {
      unawaited(lib.refreshProgressOnly());
      return;
    }

    // Only do a full server refresh if enough time has passed
    if (_lastRefresh == null ||
        now.difference(_lastRefresh!) > _refreshCooldown) {
      _lastRefresh = now;
      lib.refresh();
      // Keep Android Auto / CarPlay browse tree in sync
      if (!AppPlatform.lacksPhonePlugins) {
        AndroidAutoService().refresh();
        CarPlayService().refreshTemplates();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    if (AppPlatform.lacksPhonePlugins) {
      final taskAccess = context.select<AuthProvider, (bool, Object?)>(
        (auth) => (auth.isAdmin, auth.apiService),
      );
      _syncServerTaskPolling(isAdmin: taskAccess.$1, api: taskAccess.$2);
    }
    return LayoutBuilder(
      builder: (context, constraints) {
        final workspaceTier = AppPlatform.lacksPhonePlugins
            ? workspaceLayoutTierForWidth(constraints.maxWidth)
            : WorkspaceLayoutTier.mobile;
        final desktopRequested = workspaceTier != WorkspaceLayoutTier.mobile;
        var useDesktopWorkspace = desktopRequested;
        if (desktopRequested) {
          _desktopWorkspaceWasActive = true;
          _desktopCollapseAllowed = false;
        } else if (_desktopWorkspaceWasActive && !_desktopCollapseAllowed) {
          useDesktopWorkspace = true;
          _scheduleDesktopWorkspaceCollapseCheck();
        } else {
          _desktopWorkspaceWasActive = false;
          _desktopCollapseAllowed = false;
        }
        return PopScope(
          canPop: false,
          onPopInvokedWithResult: _handleBack,
          child: useDesktopWorkspace
              ? _buildDesktopWorkspace(
                  context,
                  allowPinnedSidebar: desktopRequested &&
                      usesExtendedDesktopSidebar(workspaceTier),
                )
              : Scaffold(
                  body: _buildPageStack(context),
                  bottomNavigationBar: _buildBottomNav(context),
                ),
        );
      },
    );
  }

  void _handleBack(bool didPop, Object? _) {
    if (didPop) return;

    if (AppPlatform.lacksPhonePlugins) {
      final pane = _paneNavigatorKey.currentState;
      if (pane != null && pane.canPop()) {
        unawaited(pane.maybePop());
        return;
      }
      if (_currentIndex == 1 &&
          _libraryKey.currentState?.isSearchActive == true) {
        _libraryKey.currentState?.clearSearch();
        return;
      }
      if (_currentIndex != 0) _navigateTo(0);
      return;
    }

    if (_currentIndex == 1 &&
        _libraryKey.currentState?.isSearchActive == true) {
      _libraryKey.currentState?.clearSearch();
      return;
    }

    if (_currentIndex == 2) {
      final now = DateTime.now();
      if (_lastBackPress != null &&
          now.difference(_lastBackPress!) < const Duration(seconds: 2)) {
        SystemChannels.platform.invokeMethod('SystemNavigator.pop', true);
        return;
      }
      _lastBackPress = now;
      showOverlayToast(
        context,
        AppLocalizations.of(context)!.appShellPressBackToExit,
        icon: Icons.exit_to_app_rounded,
      );
      return;
    }

    _switchToAbsorbing();
  }

  Widget _buildPageStack(BuildContext context) {
    return FadeTransition(
      opacity: _fadeController,
      child: IndexedStack(
        index: _currentIndex,
        children: List<Widget>.generate(
          _pages.length,
          (i) => _pages[i] ?? const SizedBox.shrink(),
        ),
      ),
    );
  }

  Widget _buildDesktopWorkspace(
    BuildContext context, {
    required bool allowPinnedSidebar,
  }) {
    final lib = context.watch<LibraryProvider>();
    final podcastsShown = _podcastsShown(lib);
    final destinations = _buildDestinations(context, podcastsShown);
    final sidebarPinned = allowPinnedSidebar && _desktopSidebarPinned;

    return Scaffold(
      body: DesktopSidebarLayout(
        pinned: sidebarPinned,
        canPin: allowPinnedSidebar,
        onPinnedChanged: _setDesktopSidebarPinned,
        sidebarBuilder: (sidebarContext, extended) => _buildDesktopSidebar(
          sidebarContext,
          lib: lib,
          podcastsShown: podcastsShown,
          destinations: destinations,
          extended: extended,
        ),
        child: LayoutBuilder(
          // The MediaQuery override sits outside the pane Navigator so pushed
          // pages also lay out against the pane, not the window. The tab stack
          // is declarative so it keeps rebuilding below imperative routes.
          builder: (context, constraints) => MediaQuery(
            data: MediaQuery.of(context).copyWith(
              size: Size(constraints.maxWidth, constraints.maxHeight),
            ),
            child: DesktopWorkspaceScope(
              child: ClipRect(
                child: Navigator(
                  key: _paneNavigatorKey,
                  observers: [_paneNavigatorObserver],
                  onDidRemovePage: (page) {},
                  pages: [
                    MaterialPage(child: _buildPageStack(context)),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
      bottomNavigationBar: shouldShowDesktopNowPlayingBar(
        _currentIndex,
        paneRouteOpen: _paneRouteOpen,
      )
          ? DesktopNowPlayingBar(
              player: _player,
              library: lib,
              onOpenNowPlaying: _switchToAbsorbing,
            )
          : null,
    );
  }

  Widget _buildDesktopSidebar(
    BuildContext context, {
    required LibraryProvider lib,
    required bool podcastsShown,
    required List<NavigationDestination> destinations,
    required bool extended,
  }) {
    final theme = Theme.of(context);
    final cs = theme.colorScheme;
    final l = AppLocalizations.of(context)!;
    final auth = context.watch<AuthProvider>();
    final libraryName =
        lib.selectedLibrary?['name'] as String? ?? l.libraryFallback;
    final username = auth.username?.trim();
    final accountName = username == null || username.isEmpty
        ? 'Account'
        : username;
    final serverUrl = auth.serverUrl ?? '';
    final parsedServer = Uri.tryParse(serverUrl);
    final serverLabel = parsedServer?.host.isNotEmpty == true
        ? parsedServer!.host
        : serverUrl;
    final serverVersion = auth.serverVersion?.trim();
    if (serverVersion != null &&
        serverVersion.isNotEmpty &&
        _serverUpdateController.shouldCheck(serverVersion)) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (mounted) {
          unawaited(
            _serverUpdateController.check(currentVersion: serverVersion),
          );
        }
      });
    }
    final taskTracker = _serverTaskTracker;
    void openSearch() => _openSearch();

    Widget buildBranding(List<ServerTask> tasks) {
      final showTasks = auth.isAdmin && tasks.isNotEmpty && taskTracker != null;
      return DesktopSidebarBranding(
        extended: extended,
        leading: showTasks
            ? AdminTaskIndicator(
                tasks: tasks,
                onPressed: () => showAdminTasksSheet(context, taskTracker),
              )
            : Container(
                width: 38,
                height: 38,
                decoration: BoxDecoration(
                  color: cs.primaryContainer,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Icon(
                  Icons.graphic_eq_rounded,
                  color: cs.onPrimaryContainer,
                ),
              ),
        title: l.appTitle,
        titleStyle: theme.textTheme.titleMedium?.copyWith(
          fontWeight: FontWeight.w800,
          letterSpacing: 1.4,
        ),
      );
    }

    // BookOrbit admins get their own pages; Audiobookshelf admins the
    // server admin screen.
    final showAdmin = auth.isBookOrbit
        ? auth.bookOrbitAdmin?.any == true
        : auth.isAdmin;
    void openAdmin() {
      if (_adminRouteOpen) return;
      final navigator = _paneNavigatorKey.currentState;
      if (navigator == null) return;
      _adminRouteOpen = true;
      unawaited(navigator
          .push(MaterialPageRoute<void>(
            builder: (_) => auth.isBookOrbit
                ? const BookOrbitAdminScreen()
                : AdminScreen(taskTracker: _serverTaskTracker),
          ))
          .whenComplete(() => _adminRouteOpen = false));
    }

    void openAccountMenu() {
      final wasBuilt = _pages[4] != null;
      _ensurePageBuilt(4);

      final state = _settingsKey.currentState;
      if (state != null) {
        state.showAccountMenu();
        return;
      }

      if (!wasBuilt) setState(() {});
      var attempts = 0;
      void tryOpen() {
        if (!mounted) return;
        final settings = _settingsKey.currentState;
        if (settings != null) {
          settings.showAccountMenu();
          return;
        }
        if (++attempts < 10) {
          WidgetsBinding.instance.addPostFrameCallback((_) => tryOpen());
        }
      }

      WidgetsBinding.instance.addPostFrameCallback((_) => tryOpen());
    }

    return SafeArea(
      child: SizedBox(
        width: extended
            ? DesktopCollapsibleSidebar.expandedWidth
            : DesktopCollapsibleSidebar.collapsedWidth,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Padding(
              padding: extended
                  ? const EdgeInsets.fromLTRB(12, 13, 60, 7)
                  : const EdgeInsets.fromLTRB(12, 13, 12, 7),
              child: auth.isAdmin && taskTracker != null
                  ? ListenableBuilder(
                      listenable: taskTracker,
                      builder: (_, __) => buildBranding(
                        taskTracker.visibleTasks,
                      ),
                    )
                  : buildBranding(const []),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(12, 4, 12, 10),
              child: Tooltip(
                message: l.switchLibraryTooltip,
                child: InkWell(
                  onTap: lib.libraries.length > 1
                      ? () => showLibraryPickerSheet(context, lib)
                      : null,
                  borderRadius: BorderRadius.circular(12),
                  child: Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 12,
                      vertical: 10,
                    ),
                    decoration: BoxDecoration(
                      color: cs.surfaceContainerLow,
                      borderRadius: BorderRadius.circular(12),
                      border: Border.all(
                        color: cs.outlineVariant.withValues(alpha: 0.45),
                      ),
                    ),
                    child: Row(
                      mainAxisAlignment: extended
                          ? MainAxisAlignment.start
                          : MainAxisAlignment.center,
                      children: [
                        Icon(
                          Icons.library_books_rounded,
                          size: 20,
                          color: cs.primary,
                        ),
                        if (extended) ...[
                          const SizedBox(width: 10),
                          Expanded(
                            child: Text(
                              libraryName,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: theme.textTheme.labelLarge,
                            ),
                          ),
                          if (lib.libraries.length > 1)
                            const Icon(Icons.expand_more_rounded, size: 20),
                        ],
                      ],
                    ),
                  ),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12),
              child: extended
                  ? FilledButton.tonalIcon(
                      onPressed: openSearch,
                      icon: const Icon(Icons.search_rounded, size: 20),
                      label: Text(l.search),
                      style: FilledButton.styleFrom(
                        alignment: Alignment.centerLeft,
                        minimumSize: const Size.fromHeight(44),
                      ),
                    )
                  : Tooltip(
                      message: l.search,
                      child: IconButton.filledTonal(
                        onPressed: openSearch,
                        icon: const Icon(Icons.search_rounded, size: 20),
                      ),
                    ),
            ),
            const SizedBox(height: 8),
            Expanded(
              child: NavigationRail(
                extended: extended,
                minWidth: 72,
                minExtendedWidth: 248,
                groupAlignment: -1,
                backgroundColor: Colors.transparent,
                selectedIndex: _selectedDest(lib, podcastsShown),
                onDestinationSelected: (dest) =>
                    _selectDestination(dest, podcastsShown),
                destinations: [
                  for (final destination in destinations)
                    NavigationRailDestination(
                      icon: destination.icon,
                      selectedIcon: destination.selectedIcon,
                      label: Text(destination.label),
                    ),
                ],
              ),
            ),
            if (showAdmin)
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12),
                child: extended
                    ? TextButton.icon(
                        onPressed: openAdmin,
                        icon: const Icon(Icons.admin_panel_settings_outlined),
                        label: Text(l.serverAdmin),
                        // Same height as the collapsed IconButton. The rail
                        // expands on hover, and the footer is bottom-anchored,
                        // so any height change here moved the rows away from
                        // the icon the pointer was already on - a click on
                        // the shield landed on empty space.
                        style: TextButton.styleFrom(
                          alignment: Alignment.centerLeft,
                          minimumSize: const Size.fromHeight(48),
                        ),
                      )
                    : Tooltip(
                        message: l.serverAdmin,
                        child: IconButton(
                          onPressed: openAdmin,
                          icon: const Icon(
                            Icons.admin_panel_settings_outlined,
                          ),
                        ),
                      ),
              ),
            if (serverVersion != null && serverVersion.isNotEmpty)
              // One slot for both layouts (two-line collapsed, one-row
              // expanded) so the rows above stay put when the rail expands.
              SizedBox(
                height: 58,
                child: Center(
                  child: _buildDesktopServerVersion(
                    context,
                    version: serverVersion,
                    extended: extended,
                  ),
                ),
              ),
            Divider(
              height: 1,
              color: cs.outlineVariant.withValues(alpha: 0.45),
            ),
            DesktopAccountButton(
              accountName: accountName,
              serverLabel: serverLabel,
              extended: extended,
              onPressed: openAccountMenu,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDesktopServerVersion(
    BuildContext context, {
    required String version,
    required bool extended,
  }) {
    final theme = Theme.of(context);
    final cs = theme.colorScheme;
    final l = AppLocalizations.of(context)!;
    final checkedFor = _serverUpdateController.checkedFor;
    final isCurrentVersion = checkedFor == version;
    final update = isCurrentVersion ? _serverUpdateController.update : null;
    final isChecking = isCurrentVersion && _serverUpdateController.isChecking;
    final versionLabel = l.serverVersionLabel(version);
    final updateLabel = update == null
        ? null
        : l.serverUpdateAvailable(update.latestVersion);
    final tooltip = updateLabel == null
        ? versionLabel
        : '$versionLabel\n$updateLabel';

    void openUpdate() {
      if (update == null) return;
      unawaited(
        launchUrl(
          Uri.parse(update.releaseUrl),
          mode: LaunchMode.externalApplication,
        ),
      );
    }

    return Padding(
      padding: const EdgeInsets.fromLTRB(8, 2, 8, 6),
      child: Tooltip(
        message: tooltip,
        child: Material(
          color: update == null
              ? Colors.transparent
              : cs.primaryContainer.withValues(alpha: 0.45),
          borderRadius: BorderRadius.circular(10),
          clipBehavior: Clip.antiAlias,
          child: InkWell(
            onTap: update == null ? null : openUpdate,
            borderRadius: BorderRadius.circular(10),
            child: Padding(
              padding: extended
                  ? const EdgeInsets.symmetric(horizontal: 12, vertical: 8)
                  : const EdgeInsets.symmetric(horizontal: 4, vertical: 7),
              child: extended
                  ? Row(
                      children: [
                        Icon(
                          update == null
                              ? Icons.dns_outlined
                              : Icons.system_update_alt_rounded,
                          size: 18,
                          color: update == null
                              ? cs.onSurfaceVariant
                              : cs.primary,
                        ),
                        const SizedBox(width: 10),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                versionLabel,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: theme.textTheme.bodySmall?.copyWith(
                                  color: cs.onSurfaceVariant,
                                ),
                              ),
                              if (updateLabel != null)
                                Text(
                                  updateLabel,
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: theme.textTheme.labelSmall?.copyWith(
                                    color: cs.primary,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                            ],
                          ),
                        ),
                        if (isChecking)
                          SizedBox(
                            width: 14,
                            height: 14,
                            child: CircularProgressIndicator(
                              strokeWidth: 2,
                              color: cs.onSurfaceVariant,
                            ),
                          )
                        else if (update != null)
                          Icon(
                            Icons.open_in_new_rounded,
                            size: 16,
                            color: cs.primary,
                          ),
                      ],
                    )
                  : Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        if (isChecking)
                          SizedBox(
                            width: 18,
                            height: 18,
                            child: CircularProgressIndicator(
                              strokeWidth: 2,
                              color: cs.onSurfaceVariant,
                            ),
                          )
                        else
                          Icon(
                            update == null
                                ? Icons.dns_outlined
                                : Icons.system_update_alt_rounded,
                            size: 18,
                            color: update == null
                                ? cs.onSurfaceVariant
                                : cs.primary,
                          ),
                        const SizedBox(height: 3),
                        Text(
                          version,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: theme.textTheme.labelSmall?.copyWith(
                            color: update == null
                                ? cs.onSurfaceVariant
                                : cs.primary,
                            fontSize: 10,
                            fontWeight: update == null
                                ? FontWeight.w500
                                : FontWeight.w700,
                          ),
                        ),
                      ],
                    ),
            ),
          ),
        ),
      ),
    );
  }

  void _selectDestination(int dest, bool podcastsShown) =>
      unawaited(_selectDestinationAfterPaneReset(dest, podcastsShown));

  Future<void> _selectDestinationAfterPaneReset(
    int dest,
    bool podcastsShown,
  ) async {
    if (!await _preparePaneForNavigation()) return;
    final lib = context.read<LibraryProvider>();
    final page = _pageForDest(dest, podcastsShown);
    if (page == 1 &&
        _currentIndex == 1 &&
        dest == _selectedDest(lib, podcastsShown) &&
        _libraryKey.currentState?.isSearchActive == true) {
      _libraryKey.currentState?.clearSearch();
      return;
    }
    _syncTabLibrary(dest, podcastsShown);
    _navigateToPrepared(page);
    if (page >= 0 && page <= 3) _refreshDataForTab(page);
  }

  Widget _buildBottomNav(BuildContext context) {
    // Determine the *correct* notifier for the active tab so we can detect
    // both "no listener" and "listener attached to the wrong tab" — the
    // second happens when LibraryProvider.notify() rebuilds AppShell mid-fade
    // and the lazy attach captures the pre-fade _currentIndex.
    final isHomeOrLibrary = _currentIndex == 0 || _currentIndex == 1;
    ValueListenable<double>? correctNotifier;
    if (_currentIndex == 0) {
      correctNotifier = _homeKey.currentState?.barsRevealNotifier;
    } else if (_currentIndex == 1) {
      correctNotifier = _libraryKey.currentState?.barsRevealNotifier;
    }
    final wrongAttachment =
        isHomeOrLibrary &&
        _navBarListener != null &&
        correctNotifier != null &&
        !identical(_activeBarNotifier, correctNotifier);
    final missingAttachment = isHomeOrLibrary && _navBarListener == null;

    if (missingAttachment || wrongAttachment) {
      final scheduledIndex = _currentIndex;
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (!mounted || _currentIndex != scheduledIndex) return;
        _syncNavBarListener(_currentIndex);
        if (_navBarListener == null && _currentIndex == scheduledIndex) {
          WidgetsBinding.instance.addPostFrameCallback((_) {
            if (mounted && _currentIndex == scheduledIndex) {
              _syncNavBarListener(_currentIndex);
            }
          });
        }
      });
    } else if (!isHomeOrLibrary && _navBarListener != null) {
      // Stale listener left over from a fade transition — detach and snap the
      // nav bar visible so it doesn't get hidden by the previous tab's notifier.
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (!mounted) return;
        if (_currentIndex != 0 &&
            _currentIndex != 1 &&
            _navBarListener != null) {
          _detachNavBarListener();
          _navBarAnimController.value = 1.0;
        }
      });
    }
    // On phone landscape, shrink the nav bar so it doesn't eat ~20% of the
    // shorter screen height. Tablets keep the full-size bar in any orientation.
    final mq = MediaQuery.of(context);
    final isTablet = mq.size.shortestSide >= 600;
    final isPhoneLandscape =
        !isTablet && mq.orientation == Orientation.landscape;
    final lib = context.watch<LibraryProvider>();
    final podcastsShown = _podcastsShown(lib);
    final destinations = _buildDestinations(context, podcastsShown);
    return SizeTransition(
      sizeFactor: _navBarAnimController,
      axisAlignment: 1.0,
      // NavigationBar has no per-destination long-press, so map the press x
      // to a destination slot. Plain taps pass through untouched.
      child: GestureDetector(
        onLongPressStart: (details) =>
            _onNavLongPress(details.localPosition.dx, destinations.length),
        child: NavigationBar(
          selectedIndex: _selectedDest(lib, podcastsShown),
          height: isPhoneLandscape ? 56 : null,
          labelBehavior: isPhoneLandscape
              ? NavigationDestinationLabelBehavior.alwaysHide
              : NavigationDestinationLabelBehavior.alwaysShow,
          onDestinationSelected: (dest) =>
              _selectDestination(dest, podcastsShown),
          destinations: destinations,
        ),
      ),
    );
  }

  void _onNavLongPress(double dx, int destCount) {
    final width = MediaQuery.sizeOf(context).width;
    if (width <= 0 || destCount <= 0) return;
    final slot = (dx / (width / destCount)).floor().clamp(0, destCount - 1);
    HapticFeedback.mediumImpact();
    unawaited(_runNavHold(_tabForSlot(slot, destCount)));
  }

  /// Slot index to stable tab key. The Podcasts tab only exists sometimes, so
  /// everything after it shifts by one without this.
  String _tabForSlot(int slot, int destCount) {
    final hasPodcasts = destCount >= 6;
    if (slot == 0) return 'home';
    if (slot == 1) return 'library';
    if (hasPodcasts && slot == 2) return 'podcasts';
    final rest = hasPodcasts ? slot - 3 : slot - 2;
    return switch (rest) {
      0 => 'absorbing',
      1 => 'stats',
      _ => 'settings',
    };
  }

  bool _rmabConfigured = false;
  bool _rmabWeb = false;
  bool _bookRequests = false;

  Future<void> _loadRmabAvailability() async {
    final base = await ScopedPrefs.getString(kRmabBaseUrlKey);
    final token = await ScopedPrefs.getString(kRmabApiTokenKey);
    final legacy = await ScopedPrefs.getString(kRmabLegacyUrlKey);
    _rmabConfigured =
        (base?.isNotEmpty ?? false) && (token?.isNotEmpty ?? false);
    // The site opens from the legacy URL when there is one, otherwise the
    // API base's origin - same rule the setup sheet's button uses.
    _rmabWeb = (legacy?.isNotEmpty ?? false) || (base?.isNotEmpty ?? false);
    if (!mounted) return;
    final api = context.read<AuthProvider>().apiService;
    _bookRequests = api != null && (await api.bookRequestAccess()).canRequest;
  }

  Future<void> _runNavHold(String tab) async {
    final key = navHoldPrefKey(tab);
    var id = navHoldResolve(await ScopedPrefs.getString(key), tab);
    if (!mounted) return;
    final admin = _navHoldAdmin();
    await _loadRmabAvailability();
    if (!mounted) return;
    // A choice that can't act on this account - admin pages after losing
    // admin, ReadMeABook before it is set up - asks again instead of doing
    // nothing.
    if (id != null &&
        !navHoldIdAvailable(id,
            admin: admin,
            rmabConfigured: _rmabConfigured,
            rmabWeb: _rmabWeb, bookRequests: _bookRequests)) {
      id = null;
    }
    if (id == null) {
      final chosen = await _showNavHoldPicker(tab, admin);
      if (chosen == null || !mounted) return;
      await ScopedPrefs.setString(key, chosen);
      if (!mounted) return;
      id = chosen;
      debugPrint('[NavHold] $tab set to $id');
    }
    if (id == 'menu') {
      // Always-show-menu: the saved choice stays 'menu' forever - what gets
      // tapped here runs once and is deliberately NOT written back, otherwise
      // the first pick would quietly replace the menu.
      final picked = await _showNavHoldPicker(tab, admin, forRun: true);
      if (picked == null || !mounted) return;
      debugPrint('[NavHold] $tab menu ran $picked (still menu)');
      _runNavHoldAction(picked);
      return;
    }
    debugPrint('[NavHold] $tab ran $id');
    _runNavHoldAction(id);
  }

  /// The action grid. [forRun] is launcher mode - the sheet is the menu itself
  /// rather than a one-time setup, so the entries that only make sense as a
  /// saved choice are left out.
  NavHoldAdmin _navHoldAdmin() {
    final auth = context.read<AuthProvider>();
    return NavHoldAdmin.forAccount(isAdmin: auth.isAdmin, bookOrbit: auth.bookOrbitAdmin);
  }

  Future<String?> _showNavHoldPicker(String tab, NavHoldAdmin admin,
      {bool forRun = false}) async {
    final l = AppLocalizations.of(context)!;
    // Launcher mode shows the user's own arrangement, which they can edit in
    // place; setup mode always offers everything.
    var ids = forRun ? await _launcherIds(admin) : <String>[];
    if (!mounted) return null;
    final picked = await showModalBottomSheet<String>(
      context: context,
      showDragHandle: true,
      isScrollControlled: true,
      builder: (ctx) => StatefulBuilder(
        builder: (ctx, setSheetState) {
          final tt = Theme.of(ctx).textTheme;
          final cs = Theme.of(ctx).colorScheme;
          return SafeArea(
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      forRun
                          ? navHoldTabLabel(tab, l)
                          : l.navHoldPickTitle(navHoldTabLabel(tab, l)),
                      style: tt.titleMedium,
                    ),
                    const SizedBox(height: 6),
                    Text(
                      forRun ? l.navHoldEditHint : l.navHoldPickBody,
                      style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant),
                    ),
                    const SizedBox(height: 14),
                    if (forRun)
                      ActionPillGrid(items: [
                        for (final id in ids)
                          ActionPillData(
                            icon: _iconForId(id),
                            label: navHoldLabel(id, l,
                                libraryName: _libraryNameOf),
                            onTap: () => Navigator.pop(ctx, id),
                            onLongPress: () async {
                              final next = await _editMenuItem(ids, id);
                              if (next != null) setSheetState(() => ids = next);
                            },
                          ),
                        ActionPillData(
                          icon: Icons.add_rounded,
                          label: l.navHoldAdd,
                          onTap: () async {
                            final next = await _addMenuItem(ids, admin);
                            if (next != null) setSheetState(() => ids = next);
                          },
                        ),
                      ])
                    else
                      ActionPillGrid(items: [
                        for (final o in navHoldOptions)
                          if (navHoldIdAvailable(o.id,
                              admin: admin,
                              rmabConfigured: _rmabConfigured,
                              rmabWeb: _rmabWeb, bookRequests: _bookRequests))
                            ActionPillData(
                              icon: o.icon,
                              label: navHoldLabel(o.id, l),
                              onTap: () => Navigator.pop(ctx, o.id),
                            ),
                      ]),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
    if (picked == null || !mounted) return null;
    // Folders open a second sheet; anything else is the answer.
    if (picked == 'admin') return _showNavHoldSubSheet(_adminSubItems(admin));
    if (picked == 'scan') return _showNavHoldSubSheet(_scanSubItems());
    return picked;
  }

  /// The hold menu's contents: the user's saved arrangement, minus anything
  /// that no longer applies, or the default set when untouched.
  Future<List<String>> _launcherIds(NavHoldAdmin admin) async {
    final raw = await ScopedPrefs.getString(navHoldMenuPrefKey);
    List<String>? saved;
    if (raw != null && raw.isNotEmpty) {
      try {
        saved = (jsonDecode(raw) as List<dynamic>).cast<String>();
      } catch (_) {}
    }
    final ids = saved ??
        navHoldDefaultMenu(
          admin: admin,
          rmabConfigured: _rmabConfigured,
          rmabWeb: _rmabWeb, bookRequests: _bookRequests,
        );
    return [
      for (final id in ids)
        if (navHoldIdAvailable(id,
            admin: admin,
            rmabConfigured: _rmabConfigured,
            rmabWeb: _rmabWeb, bookRequests: _bookRequests))
          id,
    ];
  }

  Future<void> _saveLauncherIds(List<String> ids) =>
      ScopedPrefs.setString(navHoldMenuPrefKey, jsonEncode(ids));

  IconData _iconForId(String id) {
    if (id.startsWith('admin:')) return navHoldAdminIcon(id.substring(6));
    if (id.startsWith('scan:')) {
      return id == 'scan:all' ? Icons.sync_rounded : Icons.folder_rounded;
    }
    for (final o in navHoldOptions) {
      if (o.id == id) return o.icon;
    }
    return Icons.bolt_rounded;
  }

  String? _libraryNameOf(String libraryId) {
    for (final lib in context.read<LibraryProvider>().libraries) {
      if (lib['id'] == libraryId) return lib['name'] as String?;
    }
    return null;
  }

  /// Hold a menu pill: shuffle it along or drop it. Returns the new order, or
  /// null when nothing changed.
  Future<List<String>?> _editMenuItem(List<String> ids, String id) async {
    final l = AppLocalizations.of(context)!;
    final i = ids.indexOf(id);
    if (i < 0) return null;
    final action = await showModalBottomSheet<String>(
      context: context,
      showDragHandle: true,
      builder: (ctx) => SafeArea(
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Padding(
            padding: const EdgeInsets.fromLTRB(24, 0, 24, 10),
            child: Text(navHoldLabel(id, AppLocalizations.of(ctx)!,
                libraryName: _libraryNameOf)),
          ),
          if (i > 0)
            ListTile(
              leading: const Icon(Icons.arrow_back_rounded),
              title: Text(l.navHoldMoveLeft),
              onTap: () => Navigator.pop(ctx, 'left'),
            ),
          if (i < ids.length - 1)
            ListTile(
              leading: const Icon(Icons.arrow_forward_rounded),
              title: Text(l.navHoldMoveRight),
              onTap: () => Navigator.pop(ctx, 'right'),
            ),
          ListTile(
            leading: Icon(Icons.remove_circle_outline_rounded,
                color: Theme.of(ctx).colorScheme.error),
            title: Text(l.navHoldRemoveFromMenu,
                style: TextStyle(color: Theme.of(ctx).colorScheme.error)),
            onTap: () => Navigator.pop(ctx, 'remove'),
          ),
          const SizedBox(height: 8),
        ]),
      ),
    );
    if (action == null) return null;
    final next = [...ids];
    switch (action) {
      case 'left':
        next
          ..removeAt(i)
          ..insert(i - 1, id);
      case 'right':
        next
          ..removeAt(i)
          ..insert(i + 1, id);
      case 'remove':
        next.removeAt(i);
    }
    await _saveLauncherIds(next);
    return next;
  }

  /// Add anything not already in the menu, admin pages and library scans
  /// included, so a page like Users can sit on the front grid.
  Future<List<String>?> _addMenuItem(List<String> ids, NavHoldAdmin admin) async {
    final l = AppLocalizations.of(context)!;
    final all = navHoldAllIds(
      admin: admin,
      libraryIds: [for (final lib in _scannableLibraries()) lib['id'] as String],
      rmabConfigured: _rmabConfigured,
      rmabWeb: _rmabWeb, bookRequests: _bookRequests,
    ).where((id) =>
        id != 'menu' && id != 'none' && !ids.contains(id)).toList();
    if (all.isEmpty) return null;
    final picked = await _showNavHoldSubSheet([
      for (final id in all)
        (id, _iconForId(id), navHoldLabel(id, l, libraryName: _libraryNameOf)),
    ]);
    if (picked == null) return null;
    final next = [...ids, picked];
    await _saveLauncherIds(next);
    return next;
  }

  List<(String, IconData, String)> _adminSubItems(NavHoldAdmin admin) {
    final l = AppLocalizations.of(context)!;
    return [
      for (final p in admin.pages)
        ('admin:$p', navHoldAdminIcon(p), navHoldAdminPageLabel(p, l)),
    ];
  }

  /// The libraries a server scan can start on. BookOrbit's Book Dock isn't
  /// one of its libraries and rescans under its own permission.
  List<Map<String, dynamic>> _scannableLibraries() => [
        for (final lib in context.read<LibraryProvider>().libraries)
          if ((lib['id'] as String?)?.isNotEmpty == true && lib['id'] != ApiService.bookDockLibraryId)
            lib.cast<String, dynamic>(),
      ];

  List<(String, IconData, String)> _scanSubItems() {
    final l = AppLocalizations.of(context)!;
    return [
      ('scan:all', Icons.sync_rounded, l.navHoldScanAll),
      for (final lib in _scannableLibraries())
        (
          'scan:${lib['id']}',
          Icons.folder_rounded,
          (lib['name'] as String?) ?? '',
        ),
    ];
  }

  Future<String?> _showNavHoldSubSheet(List<(String, IconData, String)> items) {
    return showModalBottomSheet<String>(
      context: context,
      showDragHandle: true,
      isScrollControlled: true,
      builder: (ctx) => SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
            child: ActionPillGrid(items: [
              for (final (id, icon, label) in items)
                ActionPillData(
                  icon: icon,
                  label: label,
                  onTap: () => Navigator.pop(ctx, id),
                ),
            ]),
          ),
        ),
      ),
    );
  }

  void _runNavHoldAction(String id) {
    if (id.startsWith('admin:')) {
      unawaited(_openAdminPage(id.substring(6)));
      return;
    }
    if (id.startsWith('scan:')) {
      unawaited(_runServerScan(id.substring(5)));
      return;
    }
    switch (id) {
      case 'search':
        final lib = context.read<LibraryProvider>();
        if (_podcastsShown(lib)) _syncTabLibrary(1, true);
        _openSearch();
      case 'switchLibrary':
        final lib = context.read<LibraryProvider>();
        if (lib.libraries.length < 2) return;
        showLibraryPickerSheet(context, lib);
      case 'bookmarks':
        Navigator.push(context,
            MaterialPageRoute(builder: (_) => const BookmarksScreen()));
      case 'downloads':
        Navigator.push(context,
            MaterialPageRoute(builder: (_) => const DownloadsScreen()));
      case 'admin':
        unawaited(_openAdminPage('home'));
      case 'sleep':
        showSleepTimerSheet(context, Theme.of(context).colorScheme.primary);
      case 'eink':
        final v = !PlayerSettings.einkMode;
        PlayerSettings.setEinkMode(v);
        applyEinkModeTheme(v);
        context.read<LibraryProvider>().applyEinkMode(v);
      case 'playPause':
        // Nothing loaded at all: start the front card rather than doing
        // nothing, so the shortcut always plays something.
        if (!_player.hasBook) {
          unawaited(_playFrontCard());
          return;
        }
        if (_player.isPlaying) {
          _player.pause();
        } else {
          _player.play(fromUi: true);
        }
      case 'stop':
        if (!_player.hasBook) return;
        unawaited(_player.stop());
      case 'queue':
        // The Absorbing page is built lazily, and the sheet lives on it - if
        // it isn't up yet, switch to the tab and open the sheet once it is.
        if (!AbsorbingScreen.openQueueManager(context)) {
          _switchToAbsorbing();
          WidgetsBinding.instance.addPostFrameCallback((_) {
            if (mounted) AbsorbingScreen.openQueueManager(context);
          });
        }
      case 'offline':
        final lib = context.read<LibraryProvider>();
        final going = !lib.isOffline;
        unawaited(lib.setManualOffline(going));
        showOverlayToast(
          context,
          going
              ? AppLocalizations.of(context)!.navHoldOfflineOn
              : AppLocalizations.of(context)!.navHoldOfflineOff,
          icon: going ? Icons.cloud_off_rounded : Icons.cloud_done_rounded,
        );
      case 'readBook':
        unawaited(_openCurrentEbook());
      case 'bookDetails':
        // Playing book if there is one, otherwise the front Absorbing card,
        // so this works before anything has been started.
        final itemId =
            _player.currentItemId ?? _frontAbsorbingItem()?['id'] as String?;
        if (itemId == null) {
          showOverlayToast(
              context, AppLocalizations.of(context)!.navHoldNothingPlaying,
              icon: Icons.info_outline_rounded);
          return;
        }
        showBookDetailSheet(context, itemId);
      case 'scanSeries':
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (_) =>
                    const UpcomingReleasesScreen(openScanChooser: true)));
      case 'rmabSearch':
        showRmabSearchResultsSheet(context, initialQuery: '');
      case 'rmabRequests':
        // The setup sheet opens on its My Requests tab.
        showRmabConfigSheet(context,
            isAdminContext: context.read<AuthProvider>().isAdmin);
      case 'rmabWeb':
        unawaited(_openRmabSite());
      case 'bookRequests':
        unawaited(BookRequestsScreen.open(context));
      case 'none':
        break;
    }
  }

  /// Push an Admin page straight from the nav bar. Most of them need the user
  /// or library lists the Admin hub normally loads, so fetch just those first
  /// - the hub itself stays the fallback if a fetch comes back empty.
  Future<void> _openAdminPage(String page) async {
    final api = context.read<AuthProvider>().apiService;
    if (api == null) return;
    Widget? screen;
    if (api is BookOrbitApiService) {
      screen = switch (page) {
        'users' => const BookOrbitUsersScreen(),
        'libraries' => const BookOrbitLibrariesScreen(),
        'email' => const BookOrbitEmailScreen(initialTab: BoEmailTab.servers),
        _ => const BookOrbitAdminScreen(),
      };
      await Navigator.push(context, MaterialPageRoute(builder: (_) => screen!));
      return;
    }
    switch (page) {
      case 'settings':
        screen = const AdminServerSettingsScreen();
      case 'auth':
        screen = const AdminAuthSettingsScreen();
      case 'logs':
        screen = const AdminServerLogsScreen();
      case 'stats':
        screen = const AdminStatsScreen();
      case 'users':
        final r = await Future.wait(
            [api.getUsers(), api.getOnlineUsers(), api.getLibraries()]);
        screen = AdminUsersScreen(
            users: r[0], onlineUsers: r[1], libraries: r[2]);
      case 'sessions':
        screen = AdminSessionsScreen(users: await api.getUsers());
      case 'email':
        screen = AdminEmailScreen(users: await api.getUsers());
      case 'apikeys':
        screen = AdminApiKeysScreen(users: await api.getUsers());
      case 'libraries':
        screen = AdminLibrariesScreen(libraries: await api.getLibraries());
      case 'upload':
        if (!mounted) return;
        screen = AdminUploadScreen(
          libraries: await api.getLibraries(),
          initialLibraryId: context.read<AuthProvider>().defaultLibraryId,
          apiService: api,
        );
      default:
        screen = const AdminScreen();
    }
    if (!mounted) return;
    await Navigator.push(
        context, MaterialPageRoute(builder: (_) => screen!));
  }

  /// Open the ReadMeABook site in the in-app browser: the legacy URL when one
  /// is set, otherwise the API base's origin.
  Future<void> _openRmabSite() async {
    final legacy = await ScopedPrefs.getString(kRmabLegacyUrlKey);
    var target = legacy ?? '';
    if (target.isEmpty) {
      final base = await ScopedPrefs.getString(kRmabBaseUrlKey) ?? '';
      final uri = Uri.tryParse(base.trim());
      target = (uri != null && uri.hasScheme && uri.host.isNotEmpty)
          ? uri.origin
          : '';
    }
    if (!mounted || target.isEmpty) return;
    await Navigator.push(
        context, MaterialPageRoute(builder: (_) => AdminRmabScreen(url: target)));
  }

  /// Kick off a server-side scan of one library, or every library.
  Future<void> _runServerScan(String target) async {
    final l = AppLocalizations.of(context)!;
    final api = context.read<AuthProvider>().apiService;
    if (api == null) return;
    final ids = target == 'all'
        ? [for (final lib in _scannableLibraries()) lib['id'] as String]
        : [target];
    if (ids.isEmpty) return;
    var ok = false;
    for (final id in ids) {
      if (await api.scanLibrary(id)) ok = true;
    }
    if (!mounted) return;
    showOverlayToast(
      context,
      ok ? l.navHoldScanStarted : l.navHoldScanFailed,
      icon: ok ? Icons.sync_rounded : Icons.error_outline_rounded,
    );
  }

  /// The item behind the front Absorbing card - what the user sees first when
  /// nothing is playing. The absorbing order is the card order.
  Map<String, dynamic>? _frontAbsorbingItem() {
    final lib = context.read<LibraryProvider>();
    for (final key in lib.absorbingBookIds) {
      final item = lib.absorbingItemCache[key];
      if (item != null) return item;
    }
    return null;
  }

  /// Start the front Absorbing card. Podcast episodes carry their episode in
  /// the cached entry; books need the full item for chapters and duration.
  Future<void> _playFrontCard() async {
    final l = AppLocalizations.of(context)!;
    final item = _frontAbsorbingItem();
    final api = context.read<AuthProvider>().apiService;
    final itemId = item?['id'] as String?;
    if (item == null || itemId == null || api == null) {
      showOverlayToast(context, l.navHoldNothingPlaying,
          icon: Icons.play_disabled_rounded);
      return;
    }
    final lib = context.read<LibraryProvider>();
    final coverUrl = lib.getCoverUrl(itemId);
    final episode = item['recentEpisode'] as Map<String, dynamic>?;
    if (episode != null) {
      final media = item['media'] as Map<String, dynamic>? ?? {};
      final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
      final error = await _player.playItem(
        api: api,
        itemId: itemId,
        title: episode['title'] as String? ?? '',
        author: metadata['title'] as String? ?? '',
        coverUrl: coverUrl,
        totalDuration: (episode['duration'] as num?)?.toDouble() ?? 0,
        chapters: const [],
        episodeId: episode['id'] as String?,
        episodeTitle: episode['title'] as String?,
        libraryId: item['libraryId'] as String?,
        fromUi: true,
      );
      if (mounted && error != null) showOverlayToast(context, error, icon: Icons.error_outline_rounded);
      return;
    }
    final full = await api.getLibraryItem(itemId) ?? item;
    if (!mounted) return;
    final media = full['media'] as Map<String, dynamic>? ?? {};
    final metadata = media['metadata'] as Map<String, dynamic>? ?? {};
    final error = await _player.playItem(
      api: api,
      itemId: itemId,
      title: metadata['title'] as String? ?? '',
      author: metadata['authorName'] as String? ?? '',
      coverUrl: coverUrl,
      totalDuration: (media['duration'] as num?)?.toDouble() ?? 0,
      chapters: (media['chapters'] as List<dynamic>?) ?? const [],
      libraryId: full['libraryId'] as String?,
      fromUi: true,
    );
    if (mounted && error != null) showOverlayToast(context, error, icon: Icons.error_outline_rounded);
  }

  /// Open the playing (or last-played) book's ebook, falling back to the front
  /// Absorbing card when nothing is loaded. The reader cache is checked first
  /// so this works with no signal, same as the player card's Read button.
  Future<void> _openCurrentEbook() async {
    final l = AppLocalizations.of(context)!;
    final front = _player.hasBook ? null : _frontAbsorbingItem();
    final itemId = _player.currentItemId ?? front?['id'] as String?;
    var title = _player.currentTitle ?? '';
    if (front != null) {
      final metadata = (front['media'] as Map<String, dynamic>?)?['metadata']
          as Map<String, dynamic>?;
      title = metadata?['title'] as String? ?? title;
    }
    if (itemId == null) {
      showOverlayToast(context, l.noEbookFileFound,
          icon: Icons.menu_book_outlined);
      return;
    }
    var ebookFile = await cachedEbookFileFor(itemId);
    if (!mounted) return;
    if (ebookFile == null) {
      final api = context.read<AuthProvider>().apiService;
      if (api != null) {
        try {
          final item = await api.getLibraryItem(itemId);
          ebookFile = resolveEbookFile(item);
        } catch (_) {}
      }
    }
    if (!mounted) return;
    if (ebookFile == null) {
      showOverlayToast(context, l.noEbookFileFound,
          icon: Icons.menu_book_outlined);
      return;
    }
    await openEbookReader(
      context,
      itemId: itemId,
      title: title,
      ebookFile: ebookFile,
    );
  }

  List<NavigationDestination> _buildDestinations(
    BuildContext context,
    bool podcastsShown,
  ) {
    final l = AppLocalizations.of(context)!;
    final lib = context.watch<LibraryProvider>();
    // With the dedicated Podcasts tab, Home/Library always wear their book
    // labels - the podcast library being selected must not morph them.
    final isPodcast = !podcastsShown && lib.isPodcastLibrary;

    // tooltip: '' everywhere: the default label Tooltip registers its own
    // long-press recognizer, which silently wins the gesture arena over the
    // bar-level long-press handler (quick actions per slot).
    return [
      NavigationDestination(
        icon: Icon(isPodcast ? Icons.explore_outlined : Icons.home_outlined),
        selectedIcon: Icon(
          isPodcast ? Icons.explore_rounded : Icons.home_rounded,
        ),
        label: isPodcast ? l.appShellDiscoverTab : l.appShellHomeTab,
        tooltip: '',
      ),
      NavigationDestination(
        icon: Icon(
          isPodcast ? Icons.podcasts_outlined : Icons.library_books_outlined,
        ),
        selectedIcon: Icon(
          isPodcast ? Icons.podcasts_rounded : Icons.library_books_rounded,
        ),
        label: isPodcast ? l.appShellShowsTab : l.appShellLibraryTab,
        tooltip: '',
      ),
      if (podcastsShown)
        NavigationDestination(
          icon: const Icon(Icons.podcasts_outlined),
          selectedIcon: const Icon(Icons.podcasts_rounded),
          label: l.appShellPodcastsTab,
          tooltip: '',
        ),
      NavigationDestination(
        icon: const _AnimatedWaveIcon(size: 24, active: false),
        selectedIcon: const _AnimatedWaveIcon(size: 24, active: true),
        label: Wording.of(context).appShellAbsorbingTab,
        tooltip: '',
      ),
      NavigationDestination(
        icon: const Icon(Icons.bar_chart_rounded),
        selectedIcon: const Icon(Icons.bar_chart_rounded),
        label: l.appShellStatsTab,
        tooltip: '',
      ),
      NavigationDestination(
        icon: const Icon(Icons.settings_outlined),
        selectedIcon: const Icon(Icons.settings_rounded),
        label: l.appShellSettingsTab,
        tooltip: '',
      ),
    ];
  }
}

// ─── Animated wave icon for nav bar matching notification icon ────
class _AnimatedWaveIcon extends StatefulWidget {
  final double size;
  final bool active;

  const _AnimatedWaveIcon({required this.size, required this.active});

  @override
  State<_AnimatedWaveIcon> createState() => _AnimatedWaveIconState();
}

class _AnimatedWaveIconState extends State<_AnimatedWaveIcon>
    with SingleTickerProviderStateMixin {
  late final AnimationController _ctrl;
  final _player = AudioPlayerService();

  @override
  void initState() {
    super.initState();
    _ctrl = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1400),
    );
    _player.addListener(_onPlayerChanged);
    _syncAnimation();
  }

  @override
  void dispose() {
    _ctrl.dispose();
    _player.removeListener(_onPlayerChanged);
    super.dispose();
  }

  void _onPlayerChanged() {
    _syncAnimation();
    if (mounted) setState(() {});
  }

  void _syncAnimation() {
    if (_player.isPlaying) {
      if (!_ctrl.isAnimating) _ctrl.repeat();
    } else {
      if (_ctrl.isAnimating) _ctrl.stop();
    }
  }

  @override
  Widget build(BuildContext context) {
    final cs = Theme.of(context).colorScheme;
    final playing = _player.isPlaying;
    // Painted rather than an Icon, so the nav bar's icon theme passes it by.
    // That only matters in e-ink mode, where the selected pill is solid black
    // and the theme is what turns a selected icon white - without this the
    // wave paints black on black and the tab looks empty.
    final einkColor =
        PlayerSettings.einkMode ? IconTheme.of(context).color : null;

    return AnimatedBuilder(
      animation: _ctrl,
      builder: (_, __) => CustomPaint(
        size: Size(widget.size, widget.size),
        painter: _NavWavePainter(
          phase: _ctrl.value,
          color: einkColor ??
              (widget.active ? cs.primary : cs.onSurfaceVariant),
          playing: playing,
        ),
      ),
    );
  }
}

class _NavWavePainter extends CustomPainter {
  final double phase;
  final Color color;
  final bool playing;

  _NavWavePainter({
    required this.phase,
    required this.color,
    required this.playing,
  });

  static const _barHeights = [0.35, 0.6, 1.0, 0.6, 0.35];
  static const _barCount = 5;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 2.5
      ..strokeCap = StrokeCap.round;

    final totalWidth = size.width * 0.6;
    final startX = (size.width - totalWidth) / 2;
    final spacing = totalWidth / (_barCount - 1);
    final midY = size.height / 2;
    final maxHalf = size.height * 0.38;

    for (int i = 0; i < _barCount; i++) {
      final x = startX + spacing * i;
      final baseRatio = _barHeights[i];

      if (playing) {
        final barPhase = phase * 2 * math.pi + i * 1.2;
        final ratio = (baseRatio * (0.5 + 0.5 * math.sin(barPhase))).clamp(
          0.2,
          1.0,
        );
        final half = maxHalf * ratio;
        canvas.drawLine(Offset(x, midY - half), Offset(x, midY + half), paint);
      } else {
        final half = maxHalf * baseRatio;
        canvas.drawLine(Offset(x, midY - half), Offset(x, midY + half), paint);
      }
    }
  }

  @override
  bool shouldRepaint(_NavWavePainter old) =>
      old.phase != phase || old.playing != playing || old.color != color;
}