<script>
  let { currentView = 'dashboard', onNavigate = () => {}, isOpen = false, onToggle = null } = $props();
  let collapsed = $state(false);

  // Auto-collapse on desktop if screen is small but not mobile
  $effect(() => {
    if (typeof window !== 'undefined' && window.innerWidth <= 1024 && window.innerWidth > 768) {
      collapsed = true;
    }
  });

  const navItems = [
    { id: 'dashboard', label: 'Dashboard', icon: '🏠' },
    { id: 'tasks', label: 'Tasks', icon: '📋' },
    { id: 'users', label: 'Users', icon: '👥' },
  ];
</script>

<aside class="sidebar {collapsed ? 'collapsed' : ''} {isOpen ? 'mobile-open' : ''}">
  <div class="sidebar-header">
    {#if !collapsed}
      <span class="sidebar-title">Menu</span>
    {/if}
    <button
      class="collapse-btn {onToggle ? 'mobile-only' : ''}"
      onclick={() => onToggle ? onToggle() : (collapsed = !collapsed)}
      title={collapsed ? 'Expand' : 'Collapse'}
    >
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round">
        <line x1="3" y1="6" x2="21" y2="6"></line>
        <line x1="3" y1="12" x2="21" y2="12"></line>
        <line x1="3" y1="18" x2="21" y2="18"></line>
      </svg>
    </button>
  </div>

  <nav class="sidebar-nav">
    {#each navItems as item}
      <button
        class="nav-item {currentView === item.id ? 'active' : ''}"
        onclick={() => onNavigate(item.id)}
        title={item.label}
      >
        <span class="nav-icon">{item.icon}</span>
        {#if !collapsed}
          <span class="nav-label">{item.label}</span>
        {/if}
      </button>
    {/each}
  </nav>
</aside>

<style>
  .sidebar {
    width: 200px;
    height: 100vh;
    background: var(--color-bg-card);
    border-right: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    flex-shrink: 0;
    position: sticky;
    top: 0;
  }

  .sidebar.collapsed {
    width: 64px;
  }

  .sidebar.collapsed .sidebar-header {
    justify-content: center;
  }

  .sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 12px;
    border-bottom: 1px solid var(--color-border);
    min-height: 56px;
  }

  .sidebar-title {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-secondary);
    white-space: nowrap;
    overflow: hidden;
  }

  .collapse-btn {
    background: none;
    border: none;
    color: var(--color-text-secondary);
    cursor: pointer;
    padding: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    transition: all 0.2s;
    flex-shrink: 0;
  }

  .collapse-btn svg {
    width: 18px;
    height: 18px;
  }

  .collapse-btn:hover {
    color: var(--color-text-primary);
    background: var(--color-bg-card-hover);
  }

  .sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 8px;
  }

  .nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--color-text-secondary);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.15s;
    white-space: nowrap;
    text-align: left;
  }

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

  .nav-item.active {
    background: color-mix(in srgb, var(--color-accent) 15%, transparent);
    color: var(--color-accent);
  }

  .nav-icon {
    font-size: 16px;
    flex-shrink: 0;
  }

  .nav-label {
    overflow: hidden;
  }

  @media (max-width: 768px) {
    .sidebar {
      display: none; /* Hide by default on mobile */
      position: fixed;
      left: 0;
      top: 0;
      z-index: 1000;
      width: 280px !important;
      height: 100vh;
      transform: translateX(-100%);
      box-shadow: 20px 0 50px rgba(0,0,0,0.3);
      transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

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

    .sidebar-header {
      padding: 20px 16px;
    }

    .nav-item {
      padding: 14px 16px;
      font-size: 15px;
    }

    .mobile-only {
      display: flex !important;
    }
  }
</style>
