<template>
  <div class="page-layout">
    <aside class="sidebar">
      <div class="sidebar-header">
        <div class="logo-container">
          <img
            :src="logoSrc"
            alt="Logo Malio"
            class="logo"
          />
        </div>
        <div class="sidebar-divider" />
      </div>
      <div class="sidebar-content">
        <slot name="sidebar" />
      </div>
      <div class="sidebar-footer">
        <div class="sidebar-divider" />
        <div class="footer-row">
          <p class="font-mono text-[10px] tracking-widest uppercase text-white/40">
            Supervisor v1.0
          </p>
        </div>
      </div>
    </aside>

    <button class="mobile-menu-button" type="button" @click="isMenuOpen = true">
      <IconifyIcon icon="mdi:menu" class="text-2xl" />
    </button>

    <div v-if="isMenuOpen" class="mobile-menu-backdrop" @click="isMenuOpen = false" />

    <aside v-if="isMenuOpen" class="mobile-sidebar">
      <div class="sidebar-header">
        <div class="logo-container">
          <img
            :src="logoSrc"
            alt="Logo Malio"
            class="logo"
          />
        </div>
        <div class="sidebar-divider" />
      </div>
      <div class="sidebar-content">
        <slot name="sidebar" />
      </div>
      <div class="sidebar-footer">
        <div class="sidebar-divider" />
        <div class="footer-row">
          <p class="font-mono text-[10px] tracking-widest uppercase text-white/40">
            Supervisor v1.0
          </p>
          <button class="close-button" type="button" @click="isMenuOpen = false">
            <IconifyIcon icon="mdi:close" class="text-xl" />
          </button>
        </div>
      </div>
    </aside>

    <main class="content bg-noise">
      <slot />
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue"
import { Icon as IconifyIcon } from "@iconify/vue"
import logoSrc from '~/assets/LOGO_CARRE_BLANC.png'

const isMenuOpen = ref(false)
</script>

<style scoped>
.page-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

.sidebar,
.mobile-sidebar {
  background: linear-gradient(
    180deg,
    rgb(var(--m-sidebar-from)) 0%,
    rgb(var(--m-sidebar-to)) 100%
  );
  color: white;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--m-sidebar-border);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  transition: background 0.4s ease, border-color 0.4s ease;
}

.sidebar-header {
  padding: 1.5rem 2rem 0;
  flex-shrink: 0;
}

.logo-container {
  display: flex;
  justify-content: center;
  padding: 0.5rem 0;
}

.logo {
  height: 100px;
  width: 100px;
  object-fit: contain;
  filter: drop-shadow(0 0 20px rgba(80, 140, 255, 0.15));
  transition: filter 0.3s ease;
}

.logo:hover {
  filter: drop-shadow(0 0 28px rgba(80, 140, 255, 0.3));
}

.sidebar-content {
  flex: 1;
  padding: 0.5rem 0;
}

.sidebar-footer {
  flex-shrink: 0;
}

.sidebar-divider {
  height: 1px;
  margin: 0.75rem 1.5rem;
  background: linear-gradient(
    90deg,
    transparent,
    var(--m-sidebar-divider),
    transparent
  );
}

.footer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1.5rem 0.75rem;
}

.content {
  background: rgb(var(--m-bg));
  overflow-y: auto;
  min-height: 100vh;
  transition: background-color 0.4s ease;
}

.mobile-menu-button,
.mobile-menu-backdrop,
.mobile-sidebar {
  display: none;
}

@media (max-width: 820px) {
  .page-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: none;
  }

  .mobile-menu-button {
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 40;
    display: flex;
    height: 44px;
    width: 44px;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    border: 1px solid rgb(var(--m-accent) / 0.18);
    background: rgb(var(--m-secondary));
    color: rgb(var(--m-text));
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.24);
  }

  .mobile-menu-backdrop {
    position: fixed;
    inset: 0;
    z-index: 45;
    display: block;
    background: rgba(3, 8, 20, 0.55);
    backdrop-filter: blur(3px);
  }

  .mobile-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 50;
    display: flex;
    height: 100vh;
    width: min(84vw, 320px);
    box-shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
  }

  .close-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 32px;
    width: 32px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.06);
    color: white;
  }
}
</style>
