import SwiftUI

struct BrandMark: View {
    let phase: Double

    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 13, style: .continuous)
                .fill(
                    LinearGradient(
                        colors: [
                            Color(red: 0.09, green: 0.30, blue: 0.82),
                            Color(red: 0.44, green: 0.20, blue: 0.94),
                            Color(red: 0.11, green: 0.71, blue: 0.94)
                        ],
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                )
                .overlay {
                    RoundedRectangle(cornerRadius: 13, style: .continuous)
                        .stroke(.white.opacity(0.28), lineWidth: 1)
                }
                .shadow(color: .blue.opacity(0.28), radius: 14, y: 8)

            RoundedRectangle(cornerRadius: 12, style: .continuous)
                .fill(.white.opacity(0.18))
                .frame(width: 34, height: 16)
                .blur(radius: 10)
                .offset(x: -8, y: -12)

            HStack(alignment: .bottom, spacing: 4) {
                Capsule()
                    .frame(width: 5, height: barHeight(base: 18, amplitude: 5, offset: 0))
                Capsule()
                    .frame(width: 5, height: barHeight(base: 27, amplitude: 7, offset: 0.8))
                Capsule()
                    .frame(width: 5, height: barHeight(base: 14, amplitude: 4, offset: 1.6))
            }
            .foregroundStyle(.white.opacity(0.94))
            .offset(x: -6, y: 4)

            Circle()
                .stroke(.white.opacity(0.92), lineWidth: 3.2)
                .frame(width: 17, height: 17)
                .offset(x: 12, y: 6)
            Capsule()
                .fill(.white.opacity(0.92))
                .frame(width: 13, height: 3.2)
                .rotationEffect(.degrees(-45))
                .offset(x: 21, y: 15)
        }
        .rotation3DEffect(.degrees(sin(phase) * 2.4), axis: (x: 0, y: 1, z: 0))
        .scaleEffect(1 + sin(phase * 0.7) * 0.018)
    }

    private func barHeight(base: Double, amplitude: Double, offset: Double) -> CGFloat {
        CGFloat(base + sin(phase + offset) * amplitude)
    }
}

struct AnimatedAppearance: ViewModifier {
    let appeared: Bool
    let delay: Double

    func body(content: Content) -> some View {
        content
            .opacity(appeared ? 1 : 0)
            .offset(y: appeared ? 0 : 18)
            .scaleEffect(appeared ? 1 : 0.985)
            .animation(.smooth(duration: 0.52).delay(delay), value: appeared)
    }
}

extension View {
    func animatedAppearance(_ appeared: Bool, delay: Double) -> some View {
        modifier(AnimatedAppearance(appeared: appeared, delay: delay))
    }
}
