<script lang="ts">
  import { invalidateAll } from '$app/navigation';
  import { goto } from '$app/navigation';
  import { page } from '$app/state';
  import Lean101Logo from '$lib/components/Lean101Logo.svelte';
  import { clientSession, hasModuleAccess, sessionHydrated } from '$lib/session';
  import { onMount, tick } from 'svelte';
  import packageInfo from '../../../package.json';

  type SearchItem = {
    href: string;
    label: string;
    description: string;
    keywords: string;
  };

  type NavItem = {
    href: string;
    label: string;
    shortLabel: string;
    icon?: 'home';
    moduleKey?: string;
  };

  const dashboardItem: NavItem = { href: '/', label: 'Dashboard', shortLabel: 'DB', icon: 'home', moduleKey: 'dashboard' };
  const workingDocumentItems: NavItem[] = [
    { href: '/raw-materials', label: 'Raw Materials', shortLabel: 'RM', moduleKey: 'raw_materials' },
    { href: '/mixes', label: 'Mix Master', shortLabel: 'MM', moduleKey: 'mix_master' },
    { href: '/products', label: 'Products', shortLabel: 'PR', moduleKey: 'products' },
    { href: '/scenarios', label: 'Scenarios', shortLabel: 'SC', moduleKey: 'scenarios' }
  ];
  const accessControlItem: NavItem = { href: '/client-access', label: 'Client Access', shortLabel: 'AC', moduleKey: 'client_access' };
  const navigation = [dashboardItem, ...workingDocumentItems, accessControlItem];

  const footerLinks = [
    { href: '/products', label: 'Delivered Pricing', shortLabel: 'DP' },
    { href: '/scenarios', label: 'Planning View', shortLabel: 'PV' }
  ];

  const searchItems: SearchItem[] = [
    {
      href: '/',
      label: 'Open Dashboard',
      description: 'Jump to the Hunter Premium Produce workspace summary.',
      keywords: 'hunter premium produce overview dashboard workspace home'
    },
    {
      href: '/raw-materials',
      label: 'Open Raw Materials',
      description: 'Review live input costs that feed the pricing model.',
      keywords: 'raw materials pricing inputs costs supplier'
    },
    {
      href: '/mixes',
      label: 'Open Mix Master',
      description: 'Browse saved mixes and their costing outputs.',
      keywords: 'mix master mixes recipes spreadsheet'
    },
    {
      href: '/mixes/new',
      label: 'Create New Mix',
      description: 'Start a new costing worksheet for Hunter Premium Produce.',
      keywords: 'new mix create worksheet hunter premium produce formula'
    },
    {
      href: '/products',
      label: 'Open Products',
      description: 'Review delivered product pricing and margins.',
      keywords: 'products pricing margins delivered outputs'
    },
    {
      href: '/settings',
      label: 'Open Workspace Settings',
      description: 'Review account details and workspace preferences.',
      keywords: 'settings account preferences profile workspace'
    },
    {
      href: '/scenarios',
      label: 'Open Scenarios',
      description: 'Inspect planning scenarios and overrides.',
      keywords: 'scenarios sandbox overrides compare planning'
    }
  ];

  let { children } = $props();
  const isRootRoute = $derived(page.url.pathname === '/');

  let paletteOpen = $state(false);
  let paletteQuery = $state('');
  let quickMenuOpen = $state(false);
  let userMenuOpen = $state(false);
  let navOpen = $state(false);
  let workingDocumentsExpanded = $state(true);
  let showBottomNav = $state(false);
  let isRestoringSession = $state(false);
  let restoredToken = $state<string | null>(null);
  let paletteInput: HTMLInputElement | null = $state(null);
  const appVersion = `v${packageInfo.version}`;
  const currentYear = new Date().getFullYear();
  const visibleDashboardItem = $derived(
    !$clientSession || !dashboardItem.moduleKey || hasModuleAccess($clientSession, dashboardItem.moduleKey) ? dashboardItem : null
  );
  const visibleWorkingDocumentItems = $derived(
    !$clientSession
      ? workingDocumentItems
      : workingDocumentItems.filter((item) => !item.moduleKey || hasModuleAccess($clientSession, item.moduleKey))
  );
  const visibleFooterLinks = $derived([
    ...footerLinks,
    ...(!$clientSession || !hasModuleAccess($clientSession, 'client_access', 'manage')
      ? []
      : [{ href: accessControlItem.href, label: accessControlItem.label, shortLabel: accessControlItem.shortLabel }])
  ]);
  const primaryBottomNavigation = $derived(
    [...(visibleDashboardItem ? [visibleDashboardItem] : []), ...visibleWorkingDocumentItems.slice(0, 3)]
  );

  function matchesRoute(href: string, pathname: string) {
    return href === '/' ? pathname === '/' : pathname.startsWith(href);
  }

  function pageTitle(pathname: string) {
    return navigation.find((item) => matchesRoute(item.href, pathname))?.label ?? 'Dashboard';
  }

  function pageDescription(pathname: string) {
    const descriptions: Record<string, string> = {
      '/': 'Hunter Premium Produce client workspace',
      '/raw-materials': 'Review source input costs and downstream exposure',
      '/mixes': 'Browse saved mix worksheets and costing outputs',
      '/mixes/new': 'Create a new mix worksheet for Hunter Premium Produce',
      '/products': 'Track delivered product pricing and margin views',
      '/settings': 'Review your workspace profile and application settings',
      '/scenarios': 'Compare alternate pricing and production assumptions',
      '/client-access': 'Manage user access, module permissions, and audit history'
    };

    return descriptions[pathname] ?? 'Hunter Premium Produce client workspace';
  }

  function openPalette(query = '') {
    paletteQuery = query;
    paletteOpen = true;
    quickMenuOpen = false;
    userMenuOpen = false;
    navOpen = false;
  }

  function syncViewport() {
    showBottomNav = window.innerWidth <= 1180;

    if (!showBottomNav) {
      navOpen = false;
    }
  }

  async function runSearchItem(item: SearchItem) {
    paletteOpen = false;
    paletteQuery = '';
    await goto(item.href);
  }

  async function openSettings() {
    quickMenuOpen = false;
    userMenuOpen = false;
    navOpen = false;
    await goto('/settings');
  }

  const filteredSearchItems = $derived(
    searchItems.filter((item) => {
      const haystack = `${item.label} ${item.description} ${item.keywords}`.toLowerCase();
      return haystack.includes(paletteQuery.trim().toLowerCase());
    })
  );

  $effect(() => {
    page.url.pathname;
    quickMenuOpen = false;
    userMenuOpen = false;
    paletteOpen = false;
    paletteQuery = '';
    navOpen = false;
  });

  $effect(() => {
    if (paletteOpen) {
      tick().then(() => paletteInput?.focus());
    }
  });

  $effect(() => {
    const hydrated = $sessionHydrated;
    const token = $clientSession?.token ?? null;

    if (!hydrated) {
      return;
    }

    if (!token) {
      isRestoringSession = false;
      restoredToken = null;
      return;
    }

    if (restoredToken === token) {
      return;
    }

    restoredToken = token;
    isRestoringSession = true;

    invalidateAll().finally(() => {
      if (restoredToken === token) {
        isRestoringSession = false;
      }
    });
  });

  $effect(() => {
    if ($sessionHydrated && !$clientSession && !isRootRoute) {
      goto('/', { replaceState: true });
    }
  });

  onMount(() => {
    syncViewport();

    const handleKeydown = (event: KeyboardEvent) => {
      const target = event.target as HTMLElement | null;
      const isTypingField =
        target instanceof HTMLInputElement ||
        target instanceof HTMLTextAreaElement ||
        target instanceof HTMLSelectElement ||
        target?.isContentEditable;

      if ((event.key === 'k' && (event.metaKey || event.ctrlKey)) || (!isTypingField && event.key === '/')) {
        event.preventDefault();
        openPalette();
      }

      if (event.key === 'Escape') {
        paletteOpen = false;
        quickMenuOpen = false;
        userMenuOpen = false;
        navOpen = false;
      }
    };

    window.addEventListener('keydown', handleKeydown);
    window.addEventListener('resize', syncViewport);

    return () => {
      window.removeEventListener('keydown', handleKeydown);
      window.removeEventListener('resize', syncViewport);
    };
  });
</script>

<svelte:head>
  <title>{pageTitle(page.url.pathname)} | Hunter Premium Produce</title>
</svelte:head>

{#if !$clientSession}
  <div class="signed-out-shell">
    {#if isRootRoute}
      {@render children()}
    {:else}
      <section class="locked-card loading-card signed-out-card">
        <p class="workspace-label">Checking Session</p>
        <h2>Returning to the client login screen.</h2>
        <p>Only authenticated client users can open workspace routes directly.</p>
      </section>
    {/if}
  </div>
{:else}
<div class="app-shell">
  {#if showBottomNav && navOpen}
    <button aria-label="Close navigation" class="nav-backdrop" type="button" onclick={() => (navOpen = false)}></button>
  {/if}

  {#if !showBottomNav}
    <aside class="sidebar">
      <div class="brand-row">
        <a class="brand" href="/">
          <Lean101Logo className="sidebar-logo" showTagline={false} />
        </a>
      </div>

      <div class="sidebar-body">
        <nav class="nav-list" aria-label="Client navigation">
          {#if visibleDashboardItem}
            <a class:active={matchesRoute(visibleDashboardItem.href, page.url.pathname)} href={visibleDashboardItem.href}>
              <span class="nav-icon">
                <svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
                  <path
                    d="M3.75 10.5 12 3.75l8.25 6.75"
                    stroke="currentColor"
                    stroke-width="1.85"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                  <path
                    d="M5.25 9.75v9h13.5v-9"
                    stroke="currentColor"
                    stroke-width="1.85"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                  <path
                    d="M10.125 18.75v-5.25h3.75v5.25"
                    stroke="currentColor"
                    stroke-width="1.85"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                </svg>
              </span>
              <span>{visibleDashboardItem.label}</span>
            </a>
          {/if}
        </nav>

        <div class="nav-group" aria-label="Working documents" hidden={!visibleWorkingDocumentItems.length}>
          <button
            aria-controls="working-documents-nav"
            aria-expanded={workingDocumentsExpanded}
            class="nav-group-toggle"
            type="button"
            onclick={() => (workingDocumentsExpanded = !workingDocumentsExpanded)}
          >
            <span class="nav-group-label">Working Documents</span>
            <span class:open={workingDocumentsExpanded} class="chevron"></span>
          </button>
          {#if workingDocumentsExpanded}
            <nav class="nav-sublist" id="working-documents-nav" aria-label="Working document pages">
              {#each visibleWorkingDocumentItems as item}
                <a class:active={matchesRoute(item.href, page.url.pathname)} href={item.href}>
                  <span class="nav-icon">{item.shortLabel}</span>
                  <span>{item.label}</span>
                </a>
              {/each}
            </nav>
          {/if}
        </div>

        <div class="sidebar-footer">
          {#each visibleFooterLinks as item}
            <a href={item.href}>
              <span class="nav-icon muted">{item.shortLabel}</span>
              <span>{item.label}</span>
            </a>
          {/each}
        </div>

        <div class="sidebar-meta">
          <span>{appVersion}</span>
          <small>&copy; {currentYear} Hunter Premium Produce</small>
        </div>
      </div>
    </aside>
  {/if}

  <div class:bottom-nav-layout={showBottomNav} class="main-shell">
    <header class="topbar">
      <div class="topbar-start">
        <div class="topbar-copy">
          <h1>{pageTitle(page.url.pathname)}</h1>
          <p>{pageDescription(page.url.pathname)}</p>
        </div>
      </div>

      <div class="topbar-middle">
        <button class="search-box topbar-search" type="button" aria-label="Search the workspace" onclick={() => openPalette()}>
          <span class="search-icon"></span>
          <span class="search-placeholder">Search pages, mixes, products, and settings...</span>
          <kbd>/</kbd>
        </button>
      </div>

      <div class="topbar-actions">
        <div class="menu-wrap">
          <button
            class="action-button"
            type="button"
            onclick={() => {
              quickMenuOpen = !quickMenuOpen;
              userMenuOpen = false;
            }}
          >
            Quick Actions
            <span class:open={quickMenuOpen} class="chevron"></span>
          </button>

          {#if quickMenuOpen}
            <div class="menu-panel">
              <a href="/mixes">Open mix costing</a>
              <a href="/mixes/new">Create mix worksheet</a>
              <a href="/products">Review delivered pricing</a>
              <button type="button" onclick={() => openPalette('')}>Search the workspace</button>
            </div>
          {/if}
        </div>

        <div class="menu-wrap user-menu-wrap">
          <button
            aria-expanded={userMenuOpen}
            class="user-trigger"
            type="button"
            onclick={() => {
              userMenuOpen = !userMenuOpen;
              quickMenuOpen = false;
            }}
          >
            <span class={`user-status-dot ${$clientSession ? 'live' : 'idle'}`}></span>
            <span class="user-trigger-copy">
              <span class="workspace-label">{$sessionHydrated ? ($clientSession ? 'Signed in' : 'Signed out') : 'Checking session'}</span>
              <strong>{$sessionHydrated ? ($clientSession ? $clientSession.email : 'Sign in required') : 'Restoring workspace access'}</strong>
            </span>
            <span class:open={userMenuOpen} class="chevron"></span>
          </button>

          {#if userMenuOpen}
            <div class="menu-panel user-menu-panel">
              <div class="user-menu-summary">
                <strong>
                  {$sessionHydrated
                    ? $clientSession
                      ? $clientSession.name || 'Client account'
                      : 'Client session inactive'
                    : 'Checking saved client session'}
                </strong>
                <span>
                  {$sessionHydrated
                    ? $clientSession
                      ? $clientSession.email
                      : 'Return to the dashboard page to sign in.'
                    : 'Waiting for the browser session check to complete.'}
                </span>
              </div>
              <button type="button" onclick={openSettings}>Change settings</button>
              {#if $clientSession}
                <button type="button" onclick={() => clientSession.clear()}>Log out</button>
              {:else if !$sessionHydrated}
                <button type="button" disabled>Checking session...</button>
              {:else}
                <a href="/">Go to sign-in</a>
              {/if}
            </div>
          {/if}
        </div>
      </div>
    </header>

    <main class="content">
      {#if !isRootRoute && isRestoringSession}
        <section class="locked-card loading-card">
          <p class="workspace-label">Checking Session</p>
          <h2>Restoring your client workspace.</h2>
          <p>Refreshing the current page with the saved browser session before deciding whether sign-in is required.</p>
        </section>
      {:else}
        {@render children()}
      {/if}
    </main>
  </div>
</div>

{#if showBottomNav}
  <nav class="bottom-nav" aria-label="Tablet navigation">
    {#each primaryBottomNavigation as item}
      <a class:active={matchesRoute(item.href, page.url.pathname)} href={item.href}>
        <span class="bottom-nav-icon">
          {#if item.icon === 'home'}
            <svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
              <path
                d="M3.75 10.5 12 3.75l8.25 6.75"
                stroke="currentColor"
                stroke-width="1.85"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
              <path
                d="M5.25 9.75v9h13.5v-9"
                stroke="currentColor"
                stroke-width="1.85"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
              <path
                d="M10.125 18.75v-5.25h3.75v5.25"
                stroke="currentColor"
                stroke-width="1.85"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
            </svg>
          {:else}
            {item.shortLabel}
          {/if}
        </span>
        <span>{item.label}</span>
      </a>
    {/each}

    <button aria-expanded={navOpen} class:active={navOpen} type="button" onclick={() => (navOpen = !navOpen)}>
      <span class="bottom-nav-icon muted">+</span>
      <span>More</span>
    </button>
  </nav>

  {#if navOpen}
    <section
      aria-label="Tablet navigation drawer"
      class="bottom-drawer"
      role="dialog"
      aria-modal="true"
      onclick={(event) => event.stopPropagation()}
    >
      <div class="drawer-handle"></div>

      <div class="drawer-header">
        <div>
          <p class="workspace-label">Workspace Drawer</p>
          <strong>Hunter Premium Produce</strong>
        </div>
        <button aria-label="Close drawer" class="nav-toggle" type="button" onclick={() => (navOpen = false)}>
          <span></span>
        </button>
      </div>

      <button class="search-box drawer-search" type="button" aria-label="Search the workspace" onclick={() => openPalette()}>
        <span class="search-icon"></span>
        <span class="search-placeholder">Search the workspace...</span>
        <kbd>/</kbd>
      </button>

      <div class="drawer-grid">
        <nav class="drawer-section" aria-label="All workspace pages">
          {#if visibleDashboardItem}
            <a class:active={matchesRoute(visibleDashboardItem.href, page.url.pathname)} href={visibleDashboardItem.href} onclick={() => (navOpen = false)}>
              <span class="nav-icon">
                <svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
                  <path
                    d="M3.75 10.5 12 3.75l8.25 6.75"
                    stroke="currentColor"
                    stroke-width="1.85"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                  <path
                    d="M5.25 9.75v9h13.5v-9"
                    stroke="currentColor"
                    stroke-width="1.85"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                  <path
                    d="M10.125 18.75v-5.25h3.75v5.25"
                    stroke="currentColor"
                    stroke-width="1.85"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                  />
                </svg>
              </span>
              <span>{visibleDashboardItem.label}</span>
            </a>
          {/if}

          <div class="drawer-group" hidden={!visibleWorkingDocumentItems.length}>
            <button
              aria-controls="drawer-working-documents-nav"
              aria-expanded={workingDocumentsExpanded}
              class="nav-group-toggle drawer-group-toggle"
              type="button"
              onclick={() => (workingDocumentsExpanded = !workingDocumentsExpanded)}
            >
              <span class="drawer-group-label">Working Documents</span>
              <span class:open={workingDocumentsExpanded} class="chevron"></span>
            </button>
            {#if workingDocumentsExpanded}
              <div id="drawer-working-documents-nav" class="drawer-sublist">
                {#each visibleWorkingDocumentItems as item}
                  <a class:active={matchesRoute(item.href, page.url.pathname)} href={item.href} onclick={() => (navOpen = false)}>
                    <span class="nav-icon">{item.shortLabel}</span>
                    <span>{item.label}</span>
                  </a>
                {/each}
              </div>
            {/if}
          </div>
        </nav>

        <div class="drawer-section drawer-actions">
          <a href="/mixes/new" onclick={() => (navOpen = false)}>
            <span class="nav-icon">NW</span>
            <span>Create mix worksheet</span>
          </a>
          <button type="button" onclick={openSettings}>
            <span class="nav-icon muted">ST</span>
            <span>Change settings</span>
          </button>
          <a href="/products" onclick={() => (navOpen = false)}>
            <span class="nav-icon muted">DP</span>
            <span>Review delivered pricing</span>
          </a>
          <button type="button" onclick={() => openPalette('')}>
            <span class="nav-icon muted">SR</span>
            <span>Search the workspace</span>
          </button>
          {#if $clientSession}
            <button type="button" onclick={() => clientSession.clear()}>
              <span class="nav-icon muted">SO</span>
              <span>Sign out</span>
            </button>
          {/if}
        </div>
      </div>

      <div class="drawer-footer">
        {#each visibleFooterLinks as item}
          <a href={item.href} onclick={() => (navOpen = false)}>
            <span>{item.label}</span>
            <small>{item.shortLabel}</small>
          </a>
        {/each}
      </div>
    </section>
  {/if}
{/if}

{/if}

{#if $clientSession && paletteOpen}
  <div class="palette-overlay" role="presentation" onclick={() => (paletteOpen = false)}>
    <div
      class="palette"
      role="dialog"
      aria-modal="true"
      aria-label="Workspace search"
      tabindex="-1"
      onclick={(event) => event.stopPropagation()}
      onkeydown={(event) => {
        if (event.key === 'Escape') {
          paletteOpen = false;
        }
      }}
    >
      <div class="palette-input-row">
        <span class="search-icon"></span>
        <input bind:this={paletteInput} bind:value={paletteQuery} placeholder="Search pages, workflows, and pricing views..." />
        <kbd>Esc</kbd>
      </div>

      <div class="palette-results">
        {#if filteredSearchItems.length}
          {#each filteredSearchItems as item}
            <button class="palette-item" type="button" onclick={() => runSearchItem(item)}>
              <div>
                <strong>{item.label}</strong>
                <span>{item.description}</span>
              </div>
              <small>{item.href}</small>
            </button>
          {/each}
        {:else}
          <div class="palette-empty">
            <strong>No results</strong>
            <span>Try searching for mixes, products, scenarios, or pricing.</span>
          </div>
        {/if}
      </div>
    </div>
  </div>
{/if}

<style>
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

  :global(:root) {
    --bg: #f4f7f5;
    --panel: #ffffff;
    --panel-soft: #f8fbf9;
    --line: #e5ece7;
    --line-strong: #d9e4dd;
    --text: #18231d;
    --muted: #6d7d74;
    --green: #22a95e;
    --green-deep: #148249;
    --green-soft: #eaf8ef;
    --blue-soft: #eef7ff;
    --shadow: 0 10px 30px rgba(15, 23, 17, 0.06);
  }

  :global(html, body) {
    margin: 0;
    min-height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, "Segoe UI", sans-serif;
  }

  :global(*) {
    box-sizing: border-box;
  }

  :global(h1, h2, h3, h4, h5, h6) {
    font-family: Inter, "Segoe UI", sans-serif;
    letter-spacing: -0.03em;
  }

  :global(button),
  :global(input),
  :global(select),
  :global(textarea) {
    font: inherit;
  }

  :global(a) {
    color: inherit;
    text-decoration: none;
  }

  .app-shell {
    display: grid;
    grid-template-columns: 228px minmax(0, 1fr);
    min-height: 100vh;
  }

  .signed-out-shell {
    min-height: 100vh;
    padding: 1.5rem;
  }

  .signed-out-card {
    margin: 0 auto;
  }

  .nav-backdrop {
    display: none;
  }

  .sidebar {
    display: flex;
    flex-direction: column;
    gap: 1.1rem;
    padding: 0.9rem;
    background: var(--panel);
    border-right: 1px solid var(--line);
    position: sticky;
    top: 0;
    height: 100vh;
    overflow: hidden;
  }

  .sidebar-body {
    min-height: 0;
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 1.1rem;
  }

  .brand-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.68rem;
  }

  .brand {
    display: block;
    width: 100%;
  }

  .nav-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #fff;
    background: linear-gradient(135deg, var(--green) 0%, var(--green-deep) 100%);
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.04em;
  }

  .sidebar :global(.sidebar-logo) {
    width: min(100%, 12.2rem);
  }

  .sidebar :global(.sidebar-logo .logo-copy strong) {
    font-size: 1.6rem;
  }

  .sidebar :global(.sidebar-logo .logo-mark) {
    width: 2.6rem;
  }

  .nav-toggle,
  .action-button,
  .menu-panel button {
    border: 1px solid var(--line);
    background: var(--panel);
    cursor: pointer;
  }

  .nav-toggle {
    width: 2.05rem;
    height: 2.05rem;
    border-radius: 0.68rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
  }

  .topbar-nav-button {
    flex-shrink: 0;
  }

  .nav-toggle span,
  .search-icon,
  .chevron {
    position: relative;
    display: inline-block;
  }

  .nav-toggle span,
  .nav-toggle span::before,
  .nav-toggle span::after {
    width: 0.88rem;
    height: 2px;
    background: currentColor;
    border-radius: 999px;
    content: '';
  }

  .nav-toggle span::before,
  .nav-toggle span::after {
    position: absolute;
    left: 0;
  }

  .nav-toggle span::before {
    top: -0.28rem;
  }

  .nav-toggle span::after {
    top: 0.28rem;
  }

  .search-box {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 0.64rem;
    width: 100%;
    padding: 0.72rem 0.82rem;
    border: 1px solid var(--line);
    border-radius: 0.82rem;
    background: var(--panel-soft);
    text-align: left;
    cursor: pointer;
  }

  .search-placeholder {
    color: #93a098;
  }

  .search-icon {
    width: 0.82rem;
    height: 0.82rem;
    border: 2px solid #98a59d;
    border-radius: 999px;
  }

  .search-icon::after {
    content: '';
    position: absolute;
    right: -0.28rem;
    bottom: -0.18rem;
    width: 0.42rem;
    height: 2px;
    border-radius: 999px;
    background: #98a59d;
    transform: rotate(45deg);
  }

  kbd {
    padding: 0.1rem 0.42rem;
    border: 1px solid var(--line-strong);
    border-radius: 0.42rem;
    color: var(--muted);
    background: #fff;
    font-size: 0.76rem;
  }

  .nav-list,
  .nav-sublist,
  .sidebar-footer {
    display: grid;
    gap: 0.3rem;
  }

  .nav-list a,
  .nav-sublist a,
  .sidebar-footer a {
    display: flex;
    align-items: center;
    gap: 0.68rem;
    padding: 0.72rem 0.68rem;
    border-radius: 0.82rem;
    color: #304038;
    transition: background-color 160ms ease;
  }

  .nav-list a:hover,
  .nav-sublist a:hover,
  .sidebar-footer a:hover,
  .nav-list a.active,
  .nav-sublist a.active {
    background: var(--green-soft);
  }

  .nav-list a.active,
  .nav-sublist a.active {
    color: var(--green-deep);
    font-weight: 600;
  }

  .nav-group {
    display: grid;
    gap: 0.55rem;
    padding-top: 0.15rem;
  }

  .nav-group-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    width: 100%;
    padding: 0 0.68rem;
    border: none;
    background: transparent;
    color: inherit;
    cursor: pointer;
  }

  .nav-group-label,
  .drawer-group-label {
    margin: 0;
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .nav-group-label {
    padding: 0;
  }

  .nav-sublist {
    position: relative;
    padding-left: 1rem;
  }

  .nav-sublist::before {
    content: '';
    position: absolute;
    top: 0.4rem;
    bottom: 0.4rem;
    left: 0.7rem;
    width: 1px;
    background: var(--line);
  }

  .nav-sublist a {
    position: relative;
  }

  .nav-icon {
    width: 1.56rem;
    height: 1.56rem;
    border-radius: 0.56rem;
  }

  .nav-icon svg,
  .bottom-nav-icon svg {
    width: 0.9rem;
    height: 0.9rem;
  }

  .nav-icon.muted {
    background: linear-gradient(135deg, #95a39b 0%, #6e7c73 100%);
  }

  .sidebar-footer {
    margin-top: auto;
    padding-top: 0.6rem;
    flex-shrink: 0;
  }

  .sidebar-meta {
    display: grid;
    gap: 0.2rem;
    padding: 0.85rem 0.3rem 0;
    color: var(--muted);
    font-size: 0.78rem;
    flex-shrink: 0;
  }

  .sidebar-meta small {
    font-size: 0.74rem;
  }

  .main-shell {
    min-width: 0;
    display: flex;
    flex-direction: column;
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 0.95fr) minmax(20rem, 1.1fr) auto;
    align-items: center;
    gap: 0.9rem;
    padding: 0.86rem 1.34rem;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
  }

  .topbar-start {
    min-width: 0;
    display: flex;
    align-items: flex-start;
    gap: 0.82rem;
  }

  .topbar-copy h1,
  .topbar-copy p {
    margin: 0;
  }

  .topbar-copy h1 {
    font-size: 1.62rem;
    font-weight: 700;
  }

  .topbar-copy p {
    margin-top: 0.22rem;
    color: var(--muted);
    font-size: 0.92rem;
  }

  .topbar-middle {
    min-width: 0;
  }

  .topbar-search {
    min-height: 3rem;
    background: #fff;
  }

  .topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.68rem;
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .workspace-chip {
    display: grid;
    gap: 0.14rem;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--line);
    border-radius: 0.92rem;
    background: var(--panel-soft);
  }

  .session-chip {
    cursor: pointer;
    text-align: left;
  }

  .workspace-label {
    color: var(--muted);
    font-size: 0.76rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .workspace-chip strong {
    font-size: 0.96rem;
  }

  .menu-wrap {
    position: relative;
  }

  .action-button {
    display: inline-flex;
    align-items: center;
    gap: 0.62rem;
    border-radius: 0.88rem;
    padding: 0.68rem 0.84rem;
    color: #304038;
  }

  .user-trigger {
    min-width: 14rem;
    display: inline-flex;
    align-items: center;
    gap: 0.72rem;
    padding: 0.64rem 0.84rem;
    border: 1px solid var(--line);
    border-radius: 0.96rem;
    background: var(--panel-soft);
    color: #304038;
    text-align: left;
    cursor: pointer;
  }

  .user-status-dot {
    width: 0.72rem;
    height: 0.72rem;
    flex-shrink: 0;
    border-radius: 999px;
    background: #b4c0ba;
    box-shadow: 0 0 0 0.24rem rgba(180, 192, 186, 0.2);
  }

  .user-status-dot.live {
    background: var(--green);
    box-shadow: 0 0 0 0.24rem rgba(34, 169, 94, 0.14);
  }

  .user-status-dot.idle {
    background: #c08b3d;
    box-shadow: 0 0 0 0.24rem rgba(192, 139, 61, 0.14);
  }

  .user-trigger-copy {
    min-width: 0;
    display: grid;
    flex: 1;
  }

  .user-trigger-copy strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.95rem;
  }

  .user-menu-wrap {
    min-width: 0;
  }

  .user-menu-panel {
    min-width: 16rem;
  }

  .user-menu-summary {
    display: grid;
    gap: 0.2rem;
    padding: 0.72rem 0.78rem;
    border-radius: 0.82rem;
    background: var(--panel-soft);
  }

  .user-menu-summary span {
    color: var(--muted);
    font-size: 0.82rem;
  }

  .chevron {
    width: 0.54rem;
    height: 0.54rem;
    border-right: 2px solid #7a8c82;
    border-bottom: 2px solid #7a8c82;
    transform: rotate(45deg);
    transition: transform 140ms ease;
  }

  .chevron.open {
    transform: rotate(-135deg);
  }

  .menu-panel {
    position: absolute;
    top: calc(100% + 0.45rem);
    right: 0;
    z-index: 20;
    min-width: 13rem;
    display: grid;
    gap: 0.18rem;
    padding: 0.4rem;
    border: 1px solid var(--line);
    border-radius: 0.96rem;
    background: rgba(255, 255, 255, 0.98);
    box-shadow: 0 18px 40px rgba(15, 23, 17, 0.1);
    backdrop-filter: blur(10px);
  }

  .menu-panel a,
  .menu-panel button {
    padding: 0.72rem 0.78rem;
    border-radius: 0.78rem;
    color: #304038;
    text-align: left;
    background: transparent;
    border: none;
  }

  .menu-panel a:hover,
  .menu-panel button:hover {
    background: var(--panel-soft);
  }

  .content {
    min-width: 0;
    padding: 1.34rem;
  }

  .locked-card {
    max-width: 42rem;
    padding: 1.25rem;
    border: 1px solid var(--line);
    border-radius: 1.25rem;
    background: var(--panel);
    box-shadow: var(--shadow);
  }

  .loading-card {
    min-height: 10rem;
  }

  .locked-card h2,
  .locked-card p {
    margin: 0;
  }

  .locked-card h2 {
    margin-top: 0.35rem;
    font-size: clamp(1.7rem, 3vw, 2.2rem);
  }

  .locked-card p:last-of-type {
    margin-top: 0.45rem;
    color: var(--muted);
  }

  .locked-card a {
    display: inline-flex;
    margin-top: 1rem;
    padding: 0.78rem 0.92rem;
    border-radius: 0.88rem;
    background: linear-gradient(135deg, var(--green) 0%, var(--green-deep) 100%);
    color: #fff;
    font-weight: 600;
  }

  .palette-overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: grid;
    place-items: start center;
    padding: 8vh 1rem 1rem;
    background: rgba(11, 18, 14, 0.3);
    backdrop-filter: blur(10px);
  }

  .palette {
    width: min(44rem, 100%);
    border: 1px solid rgba(217, 228, 221, 0.9);
    border-radius: 1.2rem;
    background: rgba(255, 255, 255, 0.98);
    box-shadow: 0 24px 60px rgba(15, 23, 17, 0.16);
    overflow: hidden;
  }

  .palette-input-row {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 0.8rem;
    padding: 0.95rem 1rem;
    border-bottom: 1px solid var(--line);
  }

  .palette-input-row input {
    border: none;
    outline: none;
    background: transparent;
    color: var(--text);
    font-size: 0.98rem;
  }

  .palette-results {
    max-height: 26rem;
    overflow: auto;
    padding: 0.5rem;
  }

  .palette-item,
  .palette-empty {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.88rem 0.92rem;
    border: none;
    border-radius: 0.92rem;
    text-align: left;
    background: transparent;
  }

  .palette-item {
    cursor: pointer;
  }

  .palette-item:hover {
    background: var(--panel-soft);
  }

  .palette-item strong,
  .palette-empty strong {
    display: block;
    font-size: 0.96rem;
  }

  .palette-item span,
  .palette-empty span,
  .palette-item small {
    color: var(--muted);
  }

  .palette-item span {
    display: block;
    margin-top: 0.18rem;
    font-size: 0.84rem;
  }

  .palette-item small {
    flex-shrink: 0;
    font-size: 0.76rem;
  }

  .palette-empty {
    justify-content: flex-start;
  }

  .bottom-nav,
  .bottom-drawer {
    display: none;
  }

  .main-shell.bottom-nav-layout .content {
    padding-bottom: 7.25rem;
  }

  @media (max-width: 1180px) {
    .app-shell {
      grid-template-columns: 1fr;
    }

    .nav-backdrop {
      position: fixed;
      inset: 0;
      z-index: 48;
      display: block;
      border: none;
      background: rgba(11, 18, 14, 0.28);
      backdrop-filter: blur(4px);
    }

    .bottom-nav {
      position: fixed;
      left: max(0.8rem, env(safe-area-inset-left));
      right: max(0.8rem, env(safe-area-inset-right));
      bottom: max(0.8rem, env(safe-area-inset-bottom));
      z-index: 45;
      display: grid;
      grid-template-columns: repeat(5, minmax(0, 1fr));
      gap: 0.5rem;
      padding: 0.6rem;
      border: 1px solid rgba(217, 228, 221, 0.92);
      border-radius: 1.35rem;
      background: rgba(255, 255, 255, 0.96);
      box-shadow: 0 20px 40px rgba(15, 23, 17, 0.16);
      backdrop-filter: blur(16px);
    }

    .bottom-nav a,
    .bottom-nav button {
      min-width: 0;
      display: grid;
      justify-items: center;
      gap: 0.34rem;
      padding: 0.62rem 0.38rem;
      border: none;
      border-radius: 1rem;
      background: transparent;
      color: #51635a;
      text-align: center;
      font-size: 0.74rem;
      font-weight: 700;
      cursor: pointer;
    }

    .bottom-nav a.active,
    .bottom-nav button.active {
      color: var(--green-deep);
      background: linear-gradient(180deg, #f4fbf7 0%, #e8f6ee 100%);
    }

    .bottom-nav-icon {
      width: 2.1rem;
      height: 2.1rem;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      border-radius: 0.78rem;
      color: #fff;
      background: linear-gradient(135deg, var(--green) 0%, var(--green-deep) 100%);
      font-size: 0.66rem;
      letter-spacing: 0.04em;
    }

    .bottom-nav-icon.muted {
      background: linear-gradient(135deg, #96a49c 0%, #718077 100%);
    }

    .bottom-drawer {
      position: fixed;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 50;
      display: grid;
      gap: 1rem;
      padding: 0.85rem 1rem calc(6.6rem + env(safe-area-inset-bottom));
      border-top: 1px solid var(--line);
      border-radius: 1.6rem 1.6rem 0 0;
      background:
        linear-gradient(180deg, rgba(248, 251, 249, 0.98) 0%, rgba(255, 255, 255, 0.98) 100%);
      box-shadow: 0 -20px 45px rgba(15, 23, 17, 0.16);
      backdrop-filter: blur(16px);
    }

    .drawer-handle {
      width: 3.5rem;
      height: 0.34rem;
      margin: 0 auto;
      border-radius: 999px;
      background: #c8d4ce;
    }

    .drawer-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 0.8rem;
    }

    .drawer-header strong {
      font-size: 1rem;
    }

    .drawer-search {
      background: #fff;
    }

    .drawer-grid {
      display: grid;
      gap: 0.9rem;
      grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr);
    }

    .drawer-section {
      display: grid;
      gap: 0.4rem;
    }

    .drawer-section a,
    .drawer-section button {
      display: flex;
      align-items: center;
      gap: 0.72rem;
      padding: 0.82rem 0.86rem;
      border: 1px solid var(--line);
      border-radius: 0.96rem;
      background: rgba(255, 255, 255, 0.88);
      color: #304038;
      text-align: left;
      cursor: pointer;
    }

    .drawer-section a.active {
      color: var(--green-deep);
      background: var(--green-soft);
    }

    .drawer-group {
      display: grid;
      gap: 0.4rem;
      padding-top: 0.35rem;
    }

    .drawer-group-toggle {
      padding: 0 0.2rem;
    }

    .drawer-group-label {
      padding: 0;
    }

    .drawer-sublist {
      display: grid;
      gap: 0.4rem;
    }

    .drawer-footer {
      display: grid;
      gap: 0.5rem;
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .drawer-footer a {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 0.75rem;
      padding: 0.82rem 0.9rem;
      border: 1px solid var(--line);
      border-radius: 0.96rem;
      background: rgba(255, 255, 255, 0.88);
      color: #304038;
      font-weight: 600;
    }

    .drawer-footer small {
      color: var(--muted);
      font-size: 0.72rem;
      letter-spacing: 0.05em;
      text-transform: uppercase;
    }

    .topbar {
      position: sticky;
      top: 0;
      z-index: 30;
      grid-template-columns: minmax(0, 1fr);
      padding: 0.95rem 1rem;
      background: rgba(255, 255, 255, 0.96);
      backdrop-filter: blur(12px);
    }

    .topbar-middle {
      order: 3;
    }

    .topbar-actions {
      justify-content: flex-start;
    }

    .content {
      padding: 1rem;
    }
  }

  @media (min-width: 1181px) {
    .bottom-nav-layout .content {
      padding-bottom: 1.34rem;
    }
  }

  @media (min-width: 1181px) {
    .nav-toggle {
      display: none;
    }
  }

  @media (max-width: 900px) {
    .drawer-grid {
      grid-template-columns: 1fr;
    }
  }

  @media (max-width: 720px) {
    .topbar,
    .topbar-actions {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
    }

    .topbar-start {
      width: 100%;
    }

    .topbar-copy {
      min-width: 0;
    }

    .topbar-copy h1 {
      font-size: 1.34rem;
    }

    .topbar-middle {
      width: 100%;
    }

    .bottom-nav {
      left: max(0.55rem, env(safe-area-inset-left));
      right: max(0.55rem, env(safe-area-inset-right));
      bottom: max(0.55rem, env(safe-area-inset-bottom));
      gap: 0.32rem;
      padding: 0.45rem;
    }

    .bottom-nav a,
    .bottom-nav button {
      padding: 0.55rem 0.2rem;
      font-size: 0.68rem;
    }

    .bottom-nav-icon {
      width: 1.9rem;
      height: 1.9rem;
      font-size: 0.6rem;
    }

    .bottom-drawer {
      padding: 0.75rem 0.8rem calc(6.3rem + env(safe-area-inset-bottom));
    }

    .topbar-actions,
    .workspace-chip,
    .menu-wrap,
    .action-button,
    .user-trigger {
      width: 100%;
    }

    .action-button {
      justify-content: space-between;
    }

    .menu-panel {
      left: 0;
      right: 0;
      min-width: 0;
    }

    .drawer-footer {
      grid-template-columns: 1fr;
    }

    .content {
      padding: 0.92rem;
    }
  }
</style>
